Tuesday 30 August 2011

Selenium API using Python


Selenium API using Python

The Selenium API defines dozens of commands that can be categorized into the following:
  • Actions
    • Actions are commands that change the state of the application like clicking links or buttons, select an option in a <select> or type a character sequence in a given textbox. Ex - click(locator)
  • Accessors
    • Accessors inspect the state of the application and store values in variables
  • Assertions
    • Assertions are also able to inspect the current page. It comes into 3 flavors
    • assert : if assertion fails, test is aborted and marked as failed.
    • verify : if a verification fails, the test doesn’t stop but a trace will be printed in the log.
    • waitFor : these commands pause the test until a condition is satisfied or a timeout is reached
Selenium uses what is called locators to find and match the elements of your page that it needs to interact with. There are 8 locators strategies included in Selenium:
  • Identifier
  • Id
  • Name
  • Link
  • DOM
  • XPath
  • CSS
  • UI-element
1)  Browser Initialization

from selenium import selenium
selenium=selenium("localhost",4444,"*firefox","<URL>")
                   or
selenium=selenium("localhost",4444,”*iexplore","<URL>")

host - Specifies the IP address of the computer where the server is located.
Port - Specifies the TCP/IP socket where the server is listening waiting for the client to establish a connection.
Browser - The browser in which you want to run the tests. This is a required parameter.
url - The base url of the application under test.

2)  Navigating Application URL

selenium.start()
selenium.open("/")

3)  Wait
selenium.wait_for_page_to_load('30000')
selenium.set_timeout('30000')
time.sleep(5)

4)  Buttons
-- Use click method.

HTML –
<input type="submit" name="" value="Log In" class="submitbutton" id="loginbtn" />

Example –
selenium.click("id= loginbtn ")
selenium.click("//input[@id=' loginbtn ']")
selenium.click("css=# loginbtn")

5)  Textboxes
-- Use type method.

HTML –
<input type="text" name="username" id="username" class="textfield" value="">

Example –
selenium.type("id=username","test123")
selenium.type("name=username","test123")
selenium.type("//input[@id='username']","test123")
selenium.type("css=#username","test123")

6)  Link
-- Use click method

HTML –
<li id="nav-login"><a href="https://www.example.com/login.do" title="My account">My Login</a></li>

Example –
selenium.click ("link=My Login")
selenium.click ("//a[contains(text(),'My Login')]")
selenium.click ("//li[@id='nav-login']/a")

7)  Selection
-- Use select method

HTML –
<select name="country">
<option value="01">India</option>
<option value="02">Malaysia</option>

Example –
selenium.select("name=country", "India")
selenium.select("//select[@name='country']", "India")
selenium.select("css=select[name=country]", "India")

8)  Checkbox
-- Use click method

HTML –
<em class="checker"><input type="checkbox" name="rememberMe" value="1" checked="checked" >Remember me</em>

Example –
selenium.click("name=rememberMe")
selenium.click("//input[@name='rememberMe']")

9)  Image
-- Use click method

HTML –
<li id="nav-sign"><a href="/logout.do" title="Sign out"><img src="https://www.example.com/logoutbutton.xxx?image=logout&amp;time=1324324324" alt="Sign Out" /></a></li>

Example –
selenium.click("//img[@alt='Sign Out']")
selenium.click("css=img[alt='Sign Out']")

10)         Verify text on page
-- Use is_text_present method

Example –
selenium.is_text_present("Secret Question")

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 (When you know the window name)–

selenium.click("send")
     selenium.wait_for_pop_up("_blank", "30000")
     selenium.select_window("send email page")
     selenium.type("toAddress1", "abs@gmail.com")
     selenium.type("shortNote", "test")
     selenium.click("link=Send Email")
     selenium.wait_for_page_to_load("30000")
selenium.select_window("null")

Example (When you don’t know the window name)–

win_title=[]
selenium.click("print")
selenium.wait_for_pop_up("_blank", "30000")
win_title=selenium.get_all_window_titles()
selenium.select_window(win_title[1])
selenium.close() #Close the popup window
time.sleep(2)
selenium.select_window("null") #select the main window
time.sleep(5)

12)         Set focus on element
-- Use focus() method
-- Move the focus to the specified element; for example, if the element is an input field, move the cursor to that field

Example –
focus("//input[@id='username']")

13)         Stop the selenium execution
-- Use stop() method

Example –
selenium.stop()

14)         Few more commonly used APIs –
·         get_location(self) - Gets the absolute URL of the current page.
·         get_title(self) - Gets the title of the current page.
·         get_body_text(self) - Gets the entire text of the page.
·         get_value(self, locator) - Gets the value of an input field (or anything else with a value parameter).
·         get_text(self, locator) - Gets the text of an element.

1 comment: