Monday, 24 June 2013

Handling switch over from one window to another using Selenium Webdriver

This script helps you to switch over from a Parent window to a Child window and back cntrl to your Parent Window

String parentWindow = driver.getWindowHandle();
Set<String> handles =  driver.getWindowHandles();
for(String windowHandle  : handles)
{
if(!windowHandle.equals(parentWindow))
   {
     driver.switchTo().window(windowHandle);
      <!--Perform your operation here for new window-->
driver.close(); //closing child window
    driver.switchTo().window(parentWindow); //cntrl to parent window
    }
}

Tuesday, 4 June 2013

Cross-browser testing on Cloud using Python + RC + Sauce Labs

Generally, cross browser tests are done manually on Browserstack;  We can do the same using Selenium with Python bindings.

Selenium RC: JSON configuration
Sauce-specific settings are given inside Selenium's "browser" parameter. This is generally a string in the form "*browser" (e.g. "*iexplore", "*firefox"), but will now need to be a full JSON object like this:

{
"username": "your username here",
 "access-key": "your access key here",
 "os": "Linux",
 "browser": "firefox",
 "browser-version": "3"
}

Python Code:

#! /usr/bin/env python
#-.- coding=utf8 -.-

from selenium import selenium
import unittest, time, re

class login(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("saucelabs.com",
                                 4444,
                                 """{
                                     "username": "your-sauce-username",
                                     "access-key": "your-access-key",
                                     "os": "Linux",
                                     "browser": "firefox",
                                     "browser-version": "20"
                                 }""",
                                 "http://salesforce.com/")
        self.selenium.start()
        self.selenium.set_timeout(90000)
    
    def test_empty_info(self):
        sel = self.selenium
        sel.open("/in/")
        sel.click("id=button-login")
        sel.wait_for_page_to_load("30000")
        sel.type("id=username", "james")
        sel.type("id=password", "connor")
        sel.click("id=Login")
        sel.wait_for_page_to_load("30000")
        try: self.assertEqual("Your login attempt has failed. The username or password may be incorrect, or your location or login time may be restricted. Please contact the administrator at your company for help.", sel.get_text("css=div.loginError"))
        except AssertionError, e: self.verificationErrors.append(str(e))

     
    def tearDown(self):
        self.selenium.stop()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

Note:- Reports are generated as log and video