Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
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 after_tests():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!

    driver.quit()

    assert True

setup()

Notice how we have cleaned up the code. The function call setup() at line 47. because the code above is all function definitions, they don’t get called until setup() has been called because the code is executed sequentially.

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

...