Last active
March 20, 2021 09:00
-
-
Save carlosmmelo/0ca23e212dcd98028baa to your computer and use it in GitHub Desktop.
Python Behave example using selenium webdriver (with page object model)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from features.apps.portal import Portal | |
class Browser(object): | |
driver = webdriver.Chrome() | |
driver.implicitly_wait(15) | |
def close(context): | |
context.driver.close() | |
def visit(context, location=''): | |
context.driver.get(Portal.SITE + location) | |
def query_selector_css(context, selector): | |
element = context.driver.find_element_by_css_selector(selector) | |
return element |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: Creating a Company | |
As a provider admin | |
I want to be able to create a company | |
So that brands and users can be properly categorized within the Private Marketing Network | |
Scenario: Provider users are able to access the "Create Company" page | |
Given a "provider admin" user | |
When the user visit "/app/admin/#/create/company" page | |
Then the "Add a Company" screen is displayed | |
Scenario Outline: User is creating a company and wants to immediately manipulate the company after creation | |
Given a "provider admin" user | |
When the user visit "/app/admin/#/create/company" page | |
And the user enters company name "<company_name>" | |
And selects a company type "<company_type>" | |
And adds email slugs "<email_slugs>" | |
And submits the form | |
Then the user is redirected to the company detail page for the record just created | |
Examples: Successfully create a company | |
| company_name | company_type | email_slugs | | |
| Test Company 1 | Agency | @gmail.com | | |
| Test Company 2 | Client | @xbox | | |
| Test Company 3 | Provider | @ie.com | | |
| Test Company 4 | Publisher | @surface.com | | |
| Test Company 5 | Vendor | @windows-support.com | | |
Scenario: User abandons company creation prior to submission | |
Given the user visit "/app/admin/#/create/company" page | |
When the user clicks "Cancel" | |
Then the user should be redirected to "/app/admin/#/list/company" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from features.apps.browser import Browser | |
from features.apps.login import LoginPage | |
from features.apps.admin.admin_create_company import AddCompany | |
def before_all(context): | |
context.browser = Browser() | |
context.add_company = AddCompany() | |
context.signin = LoginPage() | |
def after_all(context): | |
context.signin.log_out() | |
#context.browser.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from features.apps.browser import Browser | |
class LoginPage(Browser): | |
LOGIN = '/login' | |
LOGOUT = '/logout' | |
def log_in(self, username, password): | |
""" | |
Locate and input values to the user and password fields and submit the form | |
:param username: | |
:param password: | |
""" | |
username_field = self.query_selector_css('.form-control[name="username"]') | |
username_field.send_keys(username) | |
password_field = self.query_selector_css('.form-control[name="password"]') | |
password_field.send_keys(password) | |
login_button = self.query_selector_css('.btn-success') | |
login_button.click() | |
def log_out(self): | |
""" | |
Visit the logout page | |
""" | |
self.visit(LoginPage.LOGOUT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
class Portal(object): | |
SITE = os.getenv('BASE_URL', '<default_url>') |
Can you provide an implementation of steps too? This would be a huge help.
Hi,
Can you please share the project structure, As my script is not identifying page objects
Feature file is calling -> Step definition file and then it is not able to call -> Page objects where I have stored all selenium code
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you provide implementation of steps too?