Wednesday, July 25, 2012

Getting started with WebDriver

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WebDriverExample  {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        
        //Close the browser
        driver.quit();
    }
}

WebDriver and Selenium Server

You may, or may not, need the Selenium Server, depending on how you intend to use Selenium-WebDriver. If you will be only using the WebDriver API you do not need the Selenium-Server. If your browser and tests will all run on the same machine, and your tests only use the WebDriver API, then you do not need to run the Selenium-Server; WebDriver will run the browser directly.
There are some reasons though to use the Selenium-Server with Selenium-WebDriver.
  • You are using Selenium-Grid to distribute your tests over multiple machines or virtual machines (VMs).
  • You want to connect to a remote machine that has a particular browser version that is not on your current machine.
  • You are not using the Java bindings (i.e. Python, C#, or Ruby) and would like to use HtmlUnit Driver

WebDriver

WebDriver:

WebDriver is a selenium 2.0 and it has all cool features like

1. Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation.
2. Selenium-WebDriver, drives the browser directly using the browser’s built in support for automation.
3. No need to start the server.
4. Works with higher versions of firefox.
5. Android and iPhone based testing.
6. We can do object extraction in Bulk.

WebDriver and Selenium Server:

You may, or may not, need the Selenium Server, depending on how you intend to use Selenium-WebDriver. If you will be only using the WebDriver API you do not need the Selenium-Server.. more

Tuesday, July 24, 2012

How to run Firefox 4.0 and higher versions using Selenium RC?

So many of us are facing this problem for a quite long time and unable to run test using selenium RC 1.0.x. The problem behind this is: in the selenium server the max version of firefox to be used is defined as 3.6. To overcome this problem, we need to override the value to the version number you want to use (in my case 4.0). Below are the step on how to do it:
  1. Change the file extension of the selenium-server.jar file to .zip.
  2. Now, extract the file content to any particular folder let’s say ‘selserver’.
  3. Now search for all available install.rdf files. You may get 5-6 such file depending on the version.
  4. Open those files with note pad. In this file you will see a tag like “3.6.*”. As it’s defined as 3.6, hence we need to change it to 4.0 and save the file.
  5. Compress all the files again as you had decompressed them to selenium-server.zip. Change the file extension to .jar.
We are ready with our server with support for Firefox 4.0. Now happy testing using Firefox 4.0 and higher veriosns...  :) .

Tuesday, August 2, 2011

Selenium IDE

            The Selenium-IDE (Integrated Development Environment) is the tool you use to develop your Selenium test cases. It’s an easy-to-use Firefox plug-in and is generally the most efficient way to develop test cases.
             It also contains a context menu that allows you to first select a UI element from the browser’s currently displayed page and then select from a list of Selenium commands with parameters pre-defined according to the context of the selected UI element. This is not only a time-saver, but also an excellent way of learning Selenium script syntax.

Merits:
a. Easy to use.
b. Easy to identify element id's and xpath of the id's

De-Merits:
a. It works only with Firefox. So thers is no possibility of browser compatibility testing.
b. IDE script is not customizable.

Selenium

Selenium Information:

                         
                          The Selenium-IDE (Integrated Development Environment) is the tool you use to develop your Selenium test cases. It’s an easy-to-use Firefox plug-in and is generally the most efficient way to develop test cases.
                         It also contains a context menu that allows you to first select a UI element from the browser’s currently displayed page and then select from a list of Selenium commands with parameters pre-defined according to the context of the selected UI element. This is not only a time-saver, but also an excellent way of learning Selenium script syntax.


TestNG



TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionality that make it more powerful and easier to use, such as:

* JDK 5 Annotations (JDK 1.4 is also supported with JavaDoc annotations).
* Flexible test configuration.
* Support for data-driven testing (with @DataProvider).
* Support for parameters.
* Allows distribution of tests on slave machines.
* Powerful execution model (no more TestSuite).
* Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
* Embeds BeanShell for further flexibility.
* Default JDK functions for runtime and logging (no dependencies).
* Dependent methods for application server testing.

TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc...