Wednesday, April 29, 2015

Sample python program using selenium web driver

In my previous post, we have seen how to install Python and Selenium. Now, let us write a sample program using Selenium web driver

I just want to automate the following manual test steps:
  1. Open FireFox web browser
  2. Load 'google.com' website
  3. Asset if Google page is loaded
  4. Close the browser
Code:

#!/bin/python
# Import Selenium webdriver module
from selenium import webdriver


#Initiate FireFox webdriver
browser = webdriver.Firefox()
 
#Load the webpage in FireFox browser
browser.get('https://www.google.com')
 
#debug statement
print "Page Title is: %s" %browser.title
 
#Assert by Page title
assert 'Google' in browser.title
 
#Close the browser
browser.quit()



 

Code Explanation: 
"from selenium import webdriver" statement imports webdriver form Selenium module. 

'webdriver.Firefox()' load FireFox driver. 

'browser.get()' loads the specified url in the browser. Note that 'browser' is the object name that we have given.

'assert <A> in <B>" is python statement which checks if the A is listed in B. Here we check if page title contains the text 'Google'. 


Comparison is case sensitive.  e.g: Assume that the Page title is "Google Search Engine". when we call the assert statement, python check if page title contains the text 'Google'. 

The following statement will fail as the comparison is case sensitive
assert 'google' in browser.title


No comments:

Post a Comment