How to put things together in Python-Behave and Selenium Page Object Model

1.4k views Asked by At

I found a boilerplate on github and wanted to integrate a POM design to my BDD framework with Python-Behave and Selenium. My Python is not good, and i get this error: AttributeError: 'Context' object has no attribute 'get'

class Page(object):
    def __init__(self, browser, base_url = 'https://someUrl.com'):
        self.base_url = base_url
        self.browser = browser
        self.timeout = 30

    def open(self,url):
        self.browser.get(url)

class LoginPage(Page):
    def visit_login_page(self, url):
        self.open(url)        
---
from pages import *

@when('a valid user clicking on the login button after typing in user name and password')
def step_impl(context):
    page = LoginPage(context)
    page.visit_login_page("https://someUrl.com")
#AttributeError: 'Context' object has no attribute 'get'

--

environment.py

from selenium import webdriver

def before_all (context):

#define a Chrome browser instance
    context.browser = webdriver.Chrome()
    context.browser.maximize_window()
    context.browser.implicitly_wait(2)

def after_all(context):
    context.browser.quit()

UPDATE

if i do:

page = LoginPage(context.browser) 

it erros:

'LoginPage' object has no attribute 'visit_login_page'
1

There are 1 answers

0
Łukasz Kosiorowski On

Try this:

My base page (your page):

class BasePage(object):
    def __init__(self, browser):
        self.browser = browser

and one of my pages (login page):

class LoginPage(BasePage):

    def __init__(self, context):
        BasePage.__init__(
            self,
            context.browser)
        self.context = context