Vous êtes sur la page 1sur 9

The below details describe about the use of WebDriver (Selenium 2.0) using the Python scripting language.

What is WebDriver? WebDriver is a clean, fast framework for automated testing of web Applications. Selenium WebDriver is the successor to Selenium RC. Selenium WebDriver accepts commands and sends them to a browser. This is implemented through a browserspecific browser driver, which sends commands to a browser, and retrieves results. WebDriver uses native automation from each language. While this means it takes longer to support new browsers/languages, it does offer a much closer feel to the browser. Selenium Benefits over WebDriver

Supports many browsers and many languages, WebDriver needs native implementations for each new language/browser combo. Very mature and complete API Currently (Sept 2010) supports JavaScript alerts and confirms better

WebDriver Benefits over Selenium


Native automation faster and a little less prone to error and browser configuration Does not Requires Selenium-RC Server to be running Access to headlessHTMLUnit can allow really fast tests Great API

Client libraries

Client libraries provide the API to program tests. Supported API implementation exists in: o Java o .Net o Python o Ruby

Pre-Requisites Unlike in Selenium 1, where the Selenium RC server was necessary to run tests, Selenium WebDriver does not need a

special server to execute tests. Instead, the WebDriver directly starts a browser instance and controls it. Python Client Libraries Python should be installed (Python 3.x is not yet supported) Python Window (PyWin32) Setup Tool Python-Selenium libraries Python Client Libraries Installation Python Installation 1) Download the latest version of python python2.7.2.msi from the site http://www.python.org/download/. 2) Run the installer and complete the installation. Python Window 1) Download the python win pywin32-214.win32-py2.7.exe from the site http://sourceforge.net/projects/pywin32/files/. 2) Run the installer and complete the installation. Setup Tool 1) Download the setup tool setuptools-0.6c11.win32py2.7.exe from the site http://pypi.python.org/pypi/setuptools. 2) Run the installer and complete the installation. Path 1) 2) 3) 4) Setting Right click on the My Computer and select Properties. Click on the Advanced tab. Click on the Environment Variables button. Set the below path in Path variable under System Variable.

5) Click on OK button

Selenium Installation 1) Open Command Prompt. 2) Enter the command easy_install.py selenium. 3) It will show the below screen.

4) Selenium installation is completed. For generating Reports 1) Download the HTMLTestRunner. 2) Copy the HTMLTestRunner.py file in Python installation directory (D:\Python27\Lib\site-packages). WebDriver API using Python from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() driver.get("http://www.python.org") assert "Python" in driver.title elem = driver.find_element_by_name("q") elem.send_keys("selenium") elem.send_keys(Keys.RETURN) assert "Google" in driver.title driver.close() 1) Browser Initialization from selenium import webdriver from selenium.webdriver.common.keys import Keys

The selenium.webdriver module provides all the WebDriver implementations. Currently supported WebDriver implementations are Firefox, Chrome, Ie and Remote. The Keys class provide keys in the keyboard like RETURN, F1, ALT etc. driver = webdriver.Firefox() The instance of Firefox WebDriver is created 2) Navigating Application URL driver.get("http://www.python.org") The driver.get method will navigate to a page given by the URL. WebDriver will wait until the page has fully loaded before returning control to test or script. If page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded. 3) Wait a) implicitly_wait(time_to_wait) Sets a sticky timeout to implicitly wait for an element to be found, or a command to complete. Example driver.implicitly_wait(30) b) set_script_timeout(time_to_wait) Set the amount of time that the script should wait before throwing an error. Example driver.set_script_timeout(30) c) time.sleep(5) 4) Locating Element Selenium provides the following methods to locate elements in a page find_element_by_id find_element_by_name find_element_by_xpath find_element_by_link_text find_element_by_partial_link_text find_element_by_tag_name

find_element_by_class_name find_element_by_css_selector 5) Buttons -- Use click method. HTML <input type="submit" name="" value="Log In" class="submitbutton" id="loginbtn" /> Example wd.find_element_by_id("loginbtn").click() wd.find_element_by_xpath("//input[@id='loginbtn']").cl ick() wd.find_element_by_css_selector("#loginbtn").click() 6) Textboxes -- Use send_keys method. HTML <input type="text" name="username" id="username" class="textfield" value=""> Example wd.find_element_by_id("username").send_keys("test") wd.find_element_by_name("username").send_keys("test") wd.find_element_by_xpath("//input[@id='username']") send_keys("test") wd.find_element_by_css_selector("#username") send_keys("test") Another Method elem = wd.find_element_by_name(("username") elem.send_keys("selenium") 7) Link, Images -- Use click method HTML <li id="nav-login"><a href="https://www.example.com/login.do" title="My account">My Login</a></li> Example Wd.find_element_by_link_text("My account").click() Wd.find_element_by_xpath("//a[contains(text(),'My Login')]").click()

8) Selection -- Use select method HTML <select name="country"> <option value="01">India</option> <option value="02">Malaysia</option> Example el = wd.find_element_by_css_selector('select[name=country]' ) for option in el.find_elements_by_tag_name('option'): if option.text == 'India': option.click() el = wd.find_element_by_xpath("//select[@name='country']") for option in el.find_elements_by_tag_name('option'): if option.text == 'India': option.click() Another Method def select_by_text(web_element, select_text): """given a web element representing a select object, click the option matching select_text """ option_is_found = False options = web_element.find_elements_by_tag_name('option') for option in options: if option.text.strip() == select_text: option.click() option_is_found = True break elements = wd.find_element_by_xpath("//select[@name='country']") select_by_text(elements, "India") 9) Checkbox -- Use click method HTML

<em class="checker"><input type="checkbox" name="rememberMe" value="1" checked="checked" >Remember me</em> Example wd.find_element_name("rememberMe").click() wd.find_element_by_xpath ("//input[@name='rememberMe']").click() 10)Verify text on page -- In comparison with Selenium 1.0, is_text_present method is not available in WebDriver. So we need to create the method using page_source method def is_text_present (brw, string): brw.switch_to_default_content() if str(string) in brw.page_source: return True else: return False Example if (is_text_present(wd,"Account Details")): print "Text Present" 11)New Window -- Use select_window(self, windowID) method -- Selects a popup window using a window locator; once a popup window has been selected, all commands go to that window. To select the main window again, use null as the target. Example handle=[] brw.switch_to_default_content() brw.find_element_by_xpath("//a[@id='print']").click() time.sleep(15) handle=brw.window_handles brw.switch_to_window(handle[1]) brw.close() time.sleep(2) brw.switch_to_window(handle[0]) 12) Stop the WebDriver execution -- Use close () or quit () method -- The quit will exit entire browser where as close will close one tab, but if it just one tab, by default most browser will exit entirely

Example wd.close() Generate HTML Report using HTMLTestRunner Example from selenium import webdriver from selenium.webdriver.common.keys import Keys import HTMLTestRunner import unittest, time class main_script(unittest.TestCase): driver = webdriver.Firefox() driver.get("http://www.google.co.in") def test01_script(self): browser=self.driver print "Open the google page" browser.find_element_by_name("q").send_keys("s elenium RC") print "Enter the search test Selenium RC" browser.find_element_by_name("q").send_keys(Ke ys.RETURN) print "Click on Google Search button" browser.close() if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(main_script)) dateTimeStamp = time.strftime('%Y%m%d_%H_%M_%S') buf = file("TestReport" + "_" + dateTimeStamp + ".html", 'wb') runner = HTMLTestRunner.HTMLTestRunner( stream=buf, title='Test the Report', description='Result of tests' ) runner.run(suite) Report

Please find the attached example script. I will upload more examples.

Vous aimerez peut-être aussi