Tuesday, May 29, 2012

How to capture the full page screenshot in webdriver

In order to get the full page screenshot using web driver,

//Include these packages in the script.
import java.io.File;
import org.apache.commons.io.FileUtils;

//Add these snippet to your code.
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\path\\filename.jpg"),true);

Tuesday, May 22, 2012

Getting Browser details using Selenium web driver 2.0


Browser capabilities:
Getting Browser details in selenium is quite easy. Using this we can run our scripts across various machines, various OS, and various browsers (It will be explained in Selenium Grid concept).

Include these two packages into your script
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.Capabilities;


Add the snippet in your script.
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
System.out.println(browserName);
String os = cap.getPlatform().toString();
System.out.println(os);
String v = cap.getVersion().toString();
System.out.println(v);

Saturday, May 19, 2012

Maximizing windows in webdriver

Maximizing windows in webdriver


//Include this package in your script.
import java.awt.Toolkit;


//Add this snippet to your code.
 Toolkit toolkit = Toolkit.getDefaultToolkit();
 Dimension screenResolution = new Dimension((int)toolkit.getScreenSize().getWidth(),(int)toolkit.getScreenSize().getHeight());
driver.manage().window().setSize(screenResolution);

Thursday, May 17, 2012

Setting up Selenium Web driver

Selenium is a popular open source automation tool. Working with selenium is quite easy. The advantage in selenium is you can use various programming languages like Java, C#, Ruby, Python etc.


You can use Java because it has many features and it is secured. I'm using Eclipse for developing the scripts.


Steps for setting up selenium web driver 2.0


Prerequisites:

  • Download latest version of eclipse.
  • Ensure your system has jdk version 1.6 and above.
  • Download Selenium Standalone server (latest version 2.21) Download here


Creating a new project:
  • Open Eclipse.
  • Go to File -> New -> Java project.
  • Create a package and class under the project.
  • Right click your project. Go to Build Path -> Configure Build Path.
  • Add the selenium standalone server under libraries.
Now write the scripts and run the project as Junit test(Alt+Shift+X,T).

Click here for a simple example.





Wednesday, May 16, 2012

Reading and writing data in excel sheet using Selenium web driver 2.0

Reading and writing data in excel sheet is quite easy. Here is the simple example to read and write data using selenium web driver 2.0. Import jxl jar file and then run the script

Reading data from excel:


 public String[][] getXLData(String location, String sheetname)
       {
               Workbook w = null;
               try {
                       w = Workbook.getWorkbook(new File(location));
               } catch (BiffException e) {
                       e.printStackTrace();
               } catch (IOException e) {
                       e.printStackTrace();
               }
               Sheet s = w.getSheet(sheetname);
               String a[][] = new String[10][10];
               try
               {
               for (int j=0;j<s.getColumns();j++)
               {
                       for (int i=0;i<s.getRows();i++)
                       {
                               a[j][i] = s.getCell(j, i).getContents();
                               System.out.println(j+" and "+i+" "+a[j][i]);
                       }
               }

               }
               catch(Exception e)
               {
                       e.printStackTrace();
               }
               return a;
       }

Writing data to excel:

A simple example to write the title of the page in the excel sheet


public class google {
private WebDriver driver;

@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void test() throws Exception {
driver.get("http://www.google.co.in/");
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys("Testing");
driver.findElement(By.id("gbqfq")).sendKeys(Keys.ENTER);
driver.findElement(By.linkText("Software testing - Wikipedia, the free encyclopedia")).click();
String s = driver.getTitle();
writereport(s);

}

@After
public void tearDown() throws Exception {
driver.quit();
}



public void writereport(String text) 
       { 
        try
        {
       FileOutputStream f = new FileOutputStream("c:\\Test\\output.xls",true);
       WritableWorkbook book = Workbook.createWorkbook(f); 
       WritableSheet sheet = book.createSheet("output", 0);
       Label l = new Label(0, 0, text);
       sheet.addCell(l);
       book.write(); 
       book.close(); 
        }
        catch (Exception e)
        {
        e.printStackTrace();
        }
        }



Tuesday, May 15, 2012

Retrieving the data using selenium



Retrieving data:

driver.findElement(By.id("id")).getAttribute("value");
Store the value in the string and retrieve it

You can also use 'getText()'

Retrieving Check box value:

driver.findElement(By.id("id1")).getAttribute("checked");
The outcome will be a Boolean value

Page title:

driver.getTitle();
Store the value in the string and retrieve it

Page source and URL:

driver.getPageSource();
driver.getCurrentUrl();







Introduction to Web Driver 2.0 with a simple example

A simple way to begin web driver 2.0. Install Selenium IDE in your firefox browser and create the script by record and playback method. For instance, try google or irctc site.


Convert your code into Webdriver by Export test case option available in IDE.


A sample script is given below,



public class google {
private WebDriver driver;


@Before
public void setUp() throws Exception {
driver = new FirefoxDriver(); //Initializing firefox driver
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
}


@Test
public void test() throws Exception {
driver.get("http://www.google.co.in/");
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys("Testing");
driver.findElement(By.id("gbqfq")).sendKeys(Keys.ENTER); //Hitting Enter for search
driver.findElement(By.linkText("Software testing - Wikipedia, the free encyclopedia")).click();
}


@After
public void tearDown() throws Exception {
driver.quit();

}

}



Monday, May 14, 2012

Getting started with Selenium Web driver 2.0

Selenium is a very popular open source test automation tool. The tool is named as 'Selenium' based on a comment posted by Huggins in one of his mail to his peers. He said a comment by mocking a competitor named Mercury, saying that you can cure mercury poisoning by taking Selenium supplements (hope you guys remember that a element named selenium in the periodic tables). Later the name itself used for this tool. 


Selenium has the following components
  • Selenium IDE
  • Selenium Remote Control
  • Selenium Web Driver
  • Selenium Grid

Selenium IDE is an integrated development environment for Selenium scripts. It is implemented as a Firefox browser plug-in which allows you to record, play, edit, and debug tests. It is good for beginners. As a automation tester you can use this tool as a reference tool to find your web elements. Even though it has some features, IDE has more drawbacks. 

It does not give your test suite reports
It does not take snapshots (Now, it has option to take snapshot on failure)
It does not handle browsers in multiple window
Apart from that, the main thing is it only runs in Firefox browser :)

Selenium Remote Control (RC) is a component that allows you to write automated web application UI tests in any programming language using any mainstream JavaScript-enabled browser.
Selenium RC comes in two parts. 
  • A server which automatically launches and kills browsers, and acts as a HTTP proxy for web requests from them.
  • Client libraries for your favorite computer language.
Even though, RC has overcome the drawbacks of IDE, there are some advantages to migrate to Web driver 2.0

  • Multi-browser testing including improved functionality for browsers not well-supported by Selenium-1.0.
  • Handling multiple frames, multiple browser windows, popups, and alerts.
  • Support for iPhone and Android testing
  • Better features for Ajax testing.
  • Unlike RC you dont have to start a server in webdriver.
  • You can simulate movement of a mouse using selenium.
  • You can find coordinates of any object using Webdriver.
  • You have classes in Webdriver which help you to simulate key press events of keyboard.
Selenium Web driver 2.0

Using Web driver API, you can Drive a browser natively as a user would either locally or on a remote machine using the Selenium Server.

Why Selenium Web driver?

  • Support for mobile phone testing
  • Ajax testing
  • Native browser
  • Explicit and implicit waits
  • Browser manipulation
  • Remote web driver etc
Selenium Grid allows you to run Selenium tests in parallel, cutting down the time required for running acceptance tests to a fraction of the total time it currently takes. Run them all on a single machine or on a server farm.

It allows you to, 
  • manage multiple environments from a central point, making it easy to run the tests against a vast combination of browsers / OS.
  • minimize the maintenance time for the grid by allowing you to implement custom hooks to leverage virtual infrastructure for instance.