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();

}

}