Functional Testing with Selenium WebDriver and Selector in Python
Posted by fRui Apps | Filed under test-driven-development
In this post, we shall be exploring functional/integration/system testing via selenium in Python. The post assumes, that you have a basic understanding of the terminologies of test driven development, you know basic python. The post does not assume any basic knowledge of selenium and is aimed to get you started with selenium with python. A brief overview followed by a simple demo.
To install selenium all you need to do is
pip install selenium
To carry out functional testing, we need three things:
Browser
Automated Custom Key Actions
Use 1,2 to assert some predefined behavior
1. Getting hold of a browser
from selenium import webdriver provides us a WebDriver implementation, currently supported ones are Firefox and Chrome and from selenium.webdriver.common.keys import Keys provides with common keys. (On mac brew install chromedriver fixes the Chrome driver issue)
We want a driver instance and we get it via driver = webdriver.Firefox().
2. Simulating KeyPress
To simulate a key press at a location we need to do 2 things:
2.1 get the element
Element can be a form field where one would want to type some text, or probably a submit button. There are various methods:
find_element_by_id
find_element_by_xpath
find_element_by_link_text
find_element_by_name
find_element_by_tag_name
find_element_by_css_selector
Given an html structure as <input type="text" name="get_text" id="text-id">
To get the above form element all we would want to do is
elem = driver.find_element_by_name("get_text")
elem = driver.find_element_by_id("text-id")
elem = driver.find_element_by_xpath("//input[@id='text-id']")
.....
There are no hard and fast rules as to which kind of selector you want to choose. However the following table simplifies locating html elements.
| Method | Usecase | Returns |
|---|---|---|
| locating by Id | when id attribute of element is known | returns the first element with id attribute matching |
| locating by Name | when name attribute of element is known | returns the first element with name attribute matching |
| locating by hyperlink text | when link text used within an anchor tag is known | returns the first element with the link text value matching the location is returned |
| locating by XPath | when there is confusion in chosing name/id | returns the absolute element |
Few things to keep in mind for using XPaths is that, they should be used when the name or id for an element is not available. XPaths should be relative rather than absolute, this helps making the test more robust, sice a minor change before the element in the DOM structure, might cause the XPath selector to fail. The recommended method is to find the id/name of the nearest element and then calculate the relative XPath from the id.
2.2 trigger the key press
To enter a value in the input form now, that we already have the element, we use elem.send_keys("Some random text inside the form field")
When everything is done we can quit the browser by driver.close
3. Asserting some values
Having performed the above steps, we would want to test features. Probably clicking a button and verifying some data on the new page, or some ajax response. The basic lies in asserting, eg. assert "Page Title". Functional testing revolves all around the above three steps. Now that we have basics in place in the second part of the post, we will play around with a simple app and write functional tests.
Simple Demo
If you move over to http://www.fruiapps.com/contact/ we have a simple form, lets move over to it, and automate the form submission using selenium. The basic steps would be:
- Basic Imports in place
- Open the url, in a web driver
- Fetch the form elements and trigger key press
- Press the submit button
- Assert the changes in the dom.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#initializing driver, and opening the webpage
driver = webdriver.Firefox()
url = "http://www.fruiapps.com/contact/"
driver.get(url)
# defining selectors
name_element = driver.find_element_by_name("name")
email_element = driver.find_element_by_name("email")
subject_element = driver.find_element_by_name("subject")
message_element = driver.find_element_by_name("message")
send_btn_element = driver.find_element_by_id("submit")
# triggering key press
name_element.send_keys("John Doe")
email_element.send_keys("[email protected]")
subject_element.send_keys("Went through Selenium Article")
message_element.send_keys("Nice tut bro!")
send_btn_element.click()
submit_success_text_elem = driver.find_element_by_tag_name('h4')
success_msg = "Thank you, your message has been sent."
assert submit_success_text_elem.text == success_msg
Bingo, you have done some good stuff!
Previous Post Next Post