...
Code Block | ||
---|---|---|
| ||
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.
...
Waiting for HTTP Requests
...
Selenium so quick, things fail lol
You may be getting an interesting error when you try to use an element on a page, such as reading a value from it or clicking on it. This is because when you try to invoke the action, the page hasn’t fully loaded the elements into the DOM. Use the following code courtesy of Gregor to get around the problem
Code Block | ||
---|---|---|
| ||
from selenium.webdriver.support.ui import WebDriverWait
element_id = object
def find_the_element_by_id(driver):
element = driver.find_element_by_id(element_id)
if element:
return element
else:
return False
def function_where_test_written():
global element_id
element_id = "qa"
qa = WebDriverWait(driver, 1).until(find_the_element_by_id)
qa.click() |
Code explained
Line 3 : set up a variable that we can pass the id or whatever the search criteria is into
Line 5-10 : create a function that does the query on the DOM using the webdriver that is passed in
Line 15 : specify the thing that you want search for
Line 16 : use a special API call WebDriverWait() that tells the webdriver (the first parameter being passed in) to wait for a certain period (the second parameter, in this example up to 1 second) until calling your query function “find_the_element_by_id” - this is passed into the until() method. Essentially it causes the WebDriver to slow down until the DOM is stable (in other words the page has been properly loaded).
Line 17 : perform your action on the element as normal