Automate web UI testing with Python and Selenium
Pycon India 2018
5th Oct 2018
Mike Cohn's test automation pyramid
So, where does Python fit in?
Used to scale tests to multiple browsers and environments using the selenium grid
#Python selenium bindings installation
pip install selenium
#Test script
from selenium import webdriver
import time
driver= webdriver.Firefox()
driver.get('http://seleniumhq.org')
time.sleep(2)
driver.close()
The HTML ‘id’ attribute specifies a unique id for a HTML element
The rules for the ‘id’ attribute are-
1. At least one character
2. No space characters
3. Case sensitive
4. Unique
#Find element by ID
login_form = driver.find_element_by_id('loginForm')
Locating by id
Usually used in forms to reference the element when data is submitted.
# Find username and password by name
username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')
#Returns login
continue = driver.find_element_by_name('continue')
Locating by name
XPath uses path expressions to identify and navigate nodes in an XML document
#locate the login form element
login_form_absolute = driver.find_element_by_xpath("/html/body/form[1]")
login_form_relative = driver.find_element_by_xpath("//form[1]")
login_form_id = driver.find_element_by_xpath("//form[@id='loginForm']")
#locate the username element
username = driver.find_element_by_xpath("//form/input[@name='username']")
username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
username = driver.find_element_by_xpath("//input[@name='username']")
Locating by xpath
These classnames are used to point to a class in a style sheet
#Find by class name
content = driver.find_element_by_class_name('content')
Locating by class
#Finding an element
search = driver.find_element_by_name('q');
#Interacting with the element
search.clear();
search.send_keys("pycon");
search.send_keys(Keys.RETURN);
from selenium.webdriver.support.ui import Select
#Selecting options
select= Select(driver.find_element_by_name('numReturnSelect'))
select.select_by_index(4)
select.select_by_visible_text("200")
select.select_by_value("250")
#Deselecting options
select.deselect_all()
#All options
options = select.options
#Submitting
submit_button = driver.find_element_by_name('continue')
submit_button.submit();
from selenium.webdriver import ActionChains
action_chains= ActionChains(driver)
#Locate source element
source= driver.find_element_by_id('draggable')
#Locate target element
target = driver.find_element_by_id('droppable')
#Drag and drop by offset
action_chains.drag_and_drop_by_offset(source, 100, 100).perform()
#Drag and drop from source to target
action_chains.drag_and_drop(source, target).perform()
While locating elements on a page, the locate function will raise a ‘ElementNotVisibleException’ if the element has not been loaded into the DOM at that point of time.
Waiting adds a time interval between actions performed by the WebDriver
2 types- explicit and implicit
The waiting time is specified in seconds
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")
Use a combination of the classes WebDriverWait and Expected Condition to add explicit waits.
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
Questions?
bhoomika10@gmail.com
abhi12ravi@gmail.com