Tuesday 30 August 2011

Handling Browser Security


Handling Browser Security –
1)  Handle FireFox Security
Method-1:
·         Run the selenium test
·         Select "Accept this certificate permanently" when prompted by popup
·         Click on the OK button
·         Open Windows Explorer and navigate to => "C:\Users\xxxxxxxx\AppData\Local\Temp\customProfileDirxxxx"
·         This is a temparary profile created by Firefox which contains a file called "cert_override.txt"
·         Copy "cert_override.txt" to your temp directory
·         Stop your selenium server.
·         Open your "selenium-server.jar" file from "c:\selenium-remote-control-xxx\selenium-server-xxx" using WinRar
·         Drag "cert_override.txt" file into the "selenium-server.jar\customProfileDirCUSTFFCHROME" folder in WinRar (do not delete or edit anything in the .jar file)
·         Close WinRar, start selenium and try it again :)
Method-2:
·         Navigate your application in firefox.
·         Select "Accept this certificate permanently" when prompted by popup.
·         Copy "cert_override.txt" and "cert8.db" to your temp directory.
·         Create a new directory in any drive. Ex – C:\FFProfile. Paste the above two files under this.
·         Now run the selenium server with below option – java -jar selenium-server-standalone-2.3.0.jar –firefoxprofile C:\FFProfile
·         Now it will not show the certificate screen.

2)  Handle IE7 Security
-- Use the below code in your script
if (brw.is_text_present("Continue to this website (not recommended).")):
        brw.click("link=Continue to this website (not recommended).")
        time.sleep(10)

HTML Report using HTMLTestRunner


Example –
from selenium import selenium
import HTMLTestRunner
import unittest, time

class main_script(unittest.TestCase):
    selenium = selenium("localhost", 4444, "*firefox","http://google.co.in")
    selenium.start()
    selenium.open("/")
    def test01_script(self):
        browser=self.selenium
        print "Open the google page"
        browser.type("q", "selenium RC")
        print "Enter the search test Selenium RC"
        browser.click("btnG")
        print "Click on Google Search button"
        browser.stop()
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 – 

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.

Configuration - Selenium and Python


Configure Selenium Server
1) Download selenium server from the http://seleniumhq.org/download/  site.
2) The latest version is selenium-server-standalone-2.3.0.jar
3) Create a folder/Directory in any drive (ex – D:\SeleniumServer).
4) Copy the downloaded jar file in above created directory.


Python Client Libraries Installation

Python Installation
1) Download the latest version of python “python-2.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.win32-py2.7.exe” from the site http://pypi.python.org/pypi/setuptools.
2) Run the installer and complete the installation.

Path Setting
1) Right click on the My Computer and select Properties.
2) Click on the Advanced tab.
3) Click on the Environment Variables button.
4) 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).