Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Your Python Selenium tests should be written in a manner to conform to pytest standards

The initial code you were given was a sample piece of the code showing you the Selenium capabilities. But in order to use Selenium in a testing environment, you will need to structure your code as follows

  1. Each file containing the tests should follow one of the these formats

    1. test_<file_name>.py

    2. <file_name>_test.py

  2. The file should comprise of tests. Tests follow the standard as defined by pytest, see this link https://docs.pytest.org/en/reorganize-docs/new-docs/user/assert_statements.html

    1. A test method should begin test_<method_name>()

We originally supplied you with the following piece of code

import time
from selenium import webdriver
from selenium.webdriver.common.alert import Alert

driver = webdriver.Chrome('c:\\pf\\bin\\chromedriver.exe')  
# Optional argument, if not specified will search path.
#driver = webdriver.Chrome()
driver.get('file://C:/work/git/nodejs-sky/selenium-setup/simple_page.html')
input_field = driver.find_element_by_id("message")
input_field.send_keys("help me!")
submitBtn = driver.find_element_by_id("submit-button")
time.sleep(2) # Let the user actually see something!

submitBtn.click()
time.sleep(3)

alert = Alert(driver)
alert.accept()
time.sleep(3)

driver.get('http://www.google.com/')
time.sleep(5) # Let the user actually see something!

search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!

driver.quit()

Let’s clean it up

import time
from selenium import webdriver
from selenium.webdriver.common.alert import Alert

driver = object

def setup():
    global driver

    driver = webdriver.Chrome('c:\\pf\\bin\\chromedriver.exe')  
    # Optional argument, if not specified will search path.
    #driver = webdriver.Chrome()

def test_some_functionality():
    setup()

    driver.get('file://C:/work/git/nodejs-sky/selenium-setup/simple_page.html')
    input_field = driver.find_element_by_id("message")
    input_field.send_keys("help me!")
    submitBtn = driver.find_element_by_id("submit-button")
    time.sleep(2) # Let the user actually see something!

    submitBtn.click()
    time.sleep(3)

    alert = Alert(driver)
    alert.accept()
    time.sleep(3)

    driver.get('http://www.google.com/')
    time.sleep(5) # Let the user actually see something!

    search_box = driver.find_element_by_name('q')
    search_box.send_keys('ChromeDriver')
    search_box.submit()
    time.sleep(5) # Let the user actually see something!

    driver.quit()

    assert True
    
test_some_functionality()

Notice we put any intialisation code in something called setup(). This is so we can reuse this code across multiple tests.

To run the above code use the following command

pytest <file_name>.py

We then place the rest of the code in a method called test_some)functionality(). we could have split this up further into two separate tests as shown here

import time
from selenium import webdriver
from selenium.webdriver.common.alert import Alert

driver = object


def setup_module():     # this method is called once before any tests run
    global driver

    driver = webdriver.Chrome('c:\\pf\\bin\\chromedriver.exe')  
    # Optional argument, if not specified will search path.
    #driver = webdriver.Chrome()

def teardown_module():  # this method is called once after all tests have run
    driver.close()
    driver.quit()

def test_fields_on_form():
    driver.get('file://C:/work/git/nodejs-sky/selenium-setup/simple_page.html')
    input_field = driver.find_element_by_id("message")
    input_field.send_keys("help me!")
    submitBtn = driver.find_element_by_id("submit-button")
    time.sleep(2) # Let the user actually see something!

    submitBtn.click()
    time.sleep(3)

    alert = Alert(driver)
    alert.accept()
    time.sleep(3)

    assert True

def test_navigation():
    driver.get('http://www.google.com/')
    time.sleep(5) # Let the user actually see something!

    search_box = driver.find_element_by_name('q')
    search_box.send_keys('ChromeDriver')
    search_box.submit()
    time.sleep(5) # Let the user actually see something!

    assert True

Notice how we have cleaned up the code.

If we put this code in a file called test_demo_selenium.py. It could be executed by simply typing pytest

Breakdown of Selenium API

Selenium Python Basics

Locating Elements

Action Chains

Waiting for HTTP Requests

  • No labels