Saturday, February 26, 2011

Selenium 1.0 vs Selenium 2.0 (Selenium Web-driver)

Selenium 1.0 vs Selenium 2.0 (Selenium Web-driver)

Selenium is browser automation tool, for more information select this link.
We already have Selenium1.0, why Selenium2.0?

Selenium1.0 can't tackle following items.
1. Native keyboard and mouse events.
2. Same origin policy XSS/HTPP(S)
3. Pop-ups, dialogs (Basic authentication, Self signed certificates and File upload/download)

Selenium2.0 has cleaner API, webdriver and webelements object, better abstraction.
//Web driver object
browser = webdriver.firefox()
//Web element object, to refer any object on the browser
search_box_google = browser.find_element_name('q')
Support of mobile devices- Android, iOS (Open web testing)
node.JS is server side JavaScript new programming language supported.
I have not seen Java Docs for Opera and Safari browsers.




Support for different types of mobile testing
1. Emulator.
2. Device connected to workstation.
3. Real device on real location on real network

Improved architecture.
Removing road blocks, hacks and workarounds
Scales up/down.
Selenium2 = Webdriver + Selenium1.0 (merging two different projects)


Lets understand the reason behind merging these two projects.

Selenium 1.0 - You can program in any language, but the prerequisite is that it should support HTTP library.  It initially started as bucket of JavaScript, later Selenium RC(Remote Control) was introduced. RC is a headless Java serer that acts as a proxy server to send commands to to the Selenium Core (JavaScript program that is running in the browser, set of functions). RC receives commands from the test program, interprets them, report back the results of those tests to the test program.  RC consist of Selenium core, that is inject into the browser using client library API when the test program opens the browser. Selenium core interprets the commands coming from the test program and execute selenese commands using browsers built-in JavaScript engine.

Selenium is the first open source browser based testing framework that quickly added support for new browsers as it is written in JavaScript.

Like any large project, Selenium is not perfect. As it is entirely written in JavaScript, which causes significant weakness. Every browser impose very strict security rules on the JavaScript being executed to protect the users from malicious scripts. This make testing harder for some scenarios. For example IE JavaScript security model don't allow to change the value of the INPUT file element for uploading the file and navigating between different domains (same origin policy) .
As it is a mature product, Selenium APIs has grown over time and it becomes harder to understand how best to use it.

Webdriver project was created by Simon Stewart, it is a clean and fast framework for browser testing automation. Webdriver take different approach for the problems faced by selenium (discussed above). Rather than being a JavaScript application used in the browser, it uses whichever mechanism is most appropriate to control the browser. IE - C++ mainly using automation APIs, Firefox - JavaScript in a XPCOM component (Add-in) ,Chrome - ???.
By changing the mechanism used to control the browser we can overcome the JavaScript security model. When these techniques are not sufficient, Webdiver can use facilities provided by operating System, especially when user want to simulate inputs from the Keyboard and mouse. By using these techniques we are trying to simulate how a real user interact with the browser.
Webdriver had Object Base API when compared with the Selenium which as Directory based approach.
Webdriver JAVA API looks like this.
// Create an instance of WebDriver backed by Firefox
WebDriver driver = new FirefoxDriver();

// Now go to the Google home page
driver.get("http://www.google.com");

// Find the search box, search for something
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("selenium");

// And now display the title of the page
System.out.println("Title: " + driver.getTitle());
When these two frameworks are compared side by side, weakness of one framework is addressed by other.
Webdriver support for multiple browsers require lot of effort from the framework developers, where as selenium can be easily extended. Selenium always require real browser, but it can make use of Webdriver HTML unit driver which is very fast and light weight browser that execute in the system memory. Selenium solve most of the common situations in an automation testing, but Webdriver ability to support out side the JavaScript sandbox provides more interesting possibilities. Webdriver don't support parallel testing, where as Selenium has answer by using Selenium GRID (new GRID 2.0 require selenium server). Although this would not solve the limitations of existing Selenium JavaScript, but it would become easier to test broad range of browsers.

Webdriver APIs are used for driving the browsers as per the user requirement, every browser has most natural language "Best Fit" for driving it, so that each developer can develop the driver independently.
But there is a problem, a fix made to one driver don't guarantee that the same fix will resolve the issue in other drivers. Every programmer is good in his own language, Java programmer many not be efficient in writing C++ programs, this create lot of redundant work across the drivers and huge testing activity.
Now the team came up with the concept of Atoms, they have decided to merge the common code across the drivers so that maintenance and development is easy. What will be the common code across all the drivers? Querying the state of browser to find an element and getting attribute values from the page DOM is the major effort across the drivers, the best common language across all the browsers for accessing DOM is by using JavaScript and this is what Selenium Core consist of. Instead of loading the single large JavaScript file into the browser and creating burden on the browser engine. They have decided to break the code into chunks(i.e Atoms), compress the JavaScript with other tools and load it on demand. These atoms are common across all the drivers.
IE browser use C++ as best fit language, how do they pass atoms(JavaScript) to it? I think by placing it in header files.

Following are the drivers available in Selenium2.0 currently
AndroidDriverChromeDriverEventFiringWebDriverFirefoxDriverHtmlUnitDriverInternetExplorerDriverIPhoneDriverIPhoneSimulatorDriverRemoteWebDriver

Name of driverAvailable on which OS?Class to instantiate
HtmlUnit DriverAllorg.openqa.selenium.htmlunit.HtmlUnitDriver
Firefox DriverAllorg.openqa.selenium.firefox.FirefoxDriver
Internet Explorer DriverWindowsorg.openqa.selenium.ie.InternetExplorerDriver
Chrome DriverAllorg.openqa.selenium.chrome.ChromeDriver
HTMLUnit driver is a pure Java driver that run in memory without displaying the browser on the screen. It use Rihno as JavaScript engine.

Selenium2 five minutes starting guide
Selenium2 documentation
Selenium2 webdriver new kid

Enjoy using Selenium 2.0 clean API.

---

Friday, February 25, 2011

Selenium - File Upload/Download using Autoit

Selenium - File Upload/Download using Autoit


I was working on selenium scripts and came across file upload functionality. Selenium core JavaScript engine don't have capability to handle the windows pop-up due to same origin policy, so we need to relay on other tools. One of the best open source tool for handling windows objects is Autoit. Lets understand how to use this tool for the functionality that can't be handled by the selenium.


I would like to explain the Autoit tool on the file upload functionality. Download the tool form the above mentioned site, create the appropriate Autoit script, compile into exe file, test the exe file to verify whether the functionality is working as expected and integrate with selenium. Read the help file for better understanding.


I have web button on the page, when selected, browser opens the following window(attaching screen shot below). Selenium works fine till this button selection, now it is the responsibility of the Autoit script to select the file so that selenium can continue with the remaining GUI activity.




What are the different steps that need to be performed by the Autoit to select the file from the above window.
1. Select the window and get the focus on it.
2. Type the file name with path in the text box.
3. Select open button, so that all the selection details are transfered into web page control and selenium can take control of the next activity.


Following is the Autoit script for the above steps, compile and convert into exe file and call from selenium. How to call the exe file from JAVA?
Java Code
Runtime.getRuntime().exec("C:\\NEW.EXE " + FileName); //call Autoit script with one parameter FileName
AutoIt Script
;MsgBox(0,"Parameters",$CmdLine[1]) ;Sending parameters through command line
FileCopy("C:\W3C.png", "C:\"&$CmdLine[1]&".png") ;Make a duplicate copy of the file, as same file name is not accepted
sleep(3000);delay for 3 seconds for the file to create
WinWaitActive("#32770", "Choose File to Upload",10)
WinFlash("Choose File to Upload","", 4, 500) ; Just to Flash the window
ControlSetText("Choose File to Upload", "", "[CLASS:Edit; INSTANCE:1]", "C:\"&$CmdLine[1]&".png")
ControlClick("Choose File to Upload", "","[CLASS:Button; INSTANCE:2]");

How to capture the Windows object properties, by using Autoit Windows Info tool, following screen shot demonstrates how to open and capture the windows controls.


Drag and drop the finder tool icon on the windows objects to get its corresponding properties.
Verify help to understand the syntax of Autoit commands. 



Enjoy scripting Autoit...cool tool.

--- 

Friday, February 18, 2011

Adobe’s Flash and Apple’s support for HTML5

Adobe’s Flash and Apple’s support for HTML5

Interesting post by Jason Huggins at Saucelabs

---

The Future of Testing

The Future of Testing

Interesting post by Jeff Goldsmith on QA, Agile development and Automation,  select this link for reading entire post.

---