Thursday 21 March 2013

Selenium: Webdriver Exception:-Cannot find firefox binary in PATH

Hi Folks,

Recently I stuck up with an issue called "Cannot find firefox binary in PATH" while trying to run my selenium webdriver script. Later I found that selenium doesn't get the source path where the firefox browser is installed. To overcome the following issue specify the browser path to open by the selenium as follows,

Code:


@Before
  public void setUp() throws Exception {
System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\Firefox.exe");
driver = new FirefoxDriver();
        baseUrl = "http://www.google.co.in";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\Firefox.exe"); 

The above code helps to set the path where the firefox is installed.

Happy testing.

Monday 18 March 2013

Difference between Selenium IDE, Selenium RC, Selenium Webdriver

Hi,

Selenium IDE
Selenium RC
Selenium Web-driver
Only works on Mozilla Firefox
Works on almost all browsers.Does not work on latest version of Firefox/IE
Works on latest versions of almost all browsers - Firefox, IE(6,7,8), Opera, Chrome
Record and play/run tool
No Record and run
No Record and run
No server required to start
Server is required to start
No server required to start
Core engine is JavaScript based
Core engine is JavaScript based
Interacts natively with browser application
Very simple to use. If using User extensions, you require knowledge on java script which makes the work a little bit tough.
Its a simple and small API
Complex and a bit large API as compared to RC
Not at all object oriented
Less Object oriented API
Purely Object oriented API
Cannot move mouse with it
Cannot move mouse with it
Can move mouse cursor
Full xpaths have to be appended with 'xapth=\\' syntax
Full xpaths have to be appended with 'xapth=\\' syntax
No need to append 'xpath=\\'
No Listeners
No Listeners
Implementation of Listeners is provided
Cannot test iPhone/Android applications
Cannot test iPhone/Android applications
Can test iPhone/Android applications


Happy Testing.

Friday 8 March 2013

How read the data from excel and write the pass/fail status to another excel sheet

Hi,

This post would help you all to understand in better manner how to read and write to an excel file using jxl. Please go through the following code,


package excel;

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import jxl.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class write2excel{
private Selenium selenium;

@Before
public void startSelenium() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in");
selenium.start();
selenium.windowMaximize();
}


@Test
public void testGoogle1() throws Exception{

String s;
selenium.open("/");
Thread.sleep(3000);
FileInputStream file = new FileInputStream("src\\Resources\\Data\\data1.xls");
Workbook wb = Workbook.getWorkbook(file);
Sheet st = wb.getSheet("DataPool");
String a[][] = new String[st.getRows()][st.getColumns()];
System.out.println("s.getRows() = " + st.getRows());
FileOutputStream op = new FileOutputStream("src\\Resources\\Data\\data2.xls");
WritableWorkbook wb1 = Workbook.createWorkbook(op);
WritableSheet st1 = wb1.createSheet("DataPool",0);
for(int i=0;i<st.getRows();i++)
{
for(int t=0;t<st.getColumns();t++)
{
a[i][t] = st.getCell(t, i).getContents();
Label l = new Label(t, i, a[i][t]);
Label l1 = new Label(2, 0, "Status");
st1.addCell(l);
st1.addCell(l1);
}
}
for(int i=1;i<st.getRows();i++)
{
System.out.println("Opened");
String k = st.getCell(0,i).getContents();
selenium.type("id=gbqfq", k);
Thread.sleep(3000);
System.out.println("Typed");
selenium.click("id=gbqfb");
Thread.sleep(3000);
System.out.println("Clicked");
String j = st.getCell(1,i).getContents();
if(selenium.isTextPresent(j))
{
s= "Pass";
}
else
{
s="Fail";
}

Label l2=new Label(2,i,s);
st1.addCell(l2);

}
wb1.write();
wb1.close();
}

@After
public void tearDown() throws Exception {
//selenium.stop();
}


}

Code explanation:

Don't try to get/create an excel before "selenium.open("/");" function, if you do the browser does not open the url which need to be opened by the browser.

Below code helps to set the source file and sheet from which it gonna get the input data,

FileInputStream file = new FileInputStream("src\\Resources\\Data\\data1.xls");
Workbook wb = Workbook.getWorkbook(file); //Setting the workbook which will be used to get/read the data
Sheet st = wb.getSheet("DataPool"); //Setting the worksheet where the data is stored

Below code helps to create new file and sheet,

FileOutputStream op = new FileOutputStream("src\\Resources\\Data\\data2.xls");
WritableWorkbook wb1 = Workbook.createWorkbook(op); //Creating workbook
WritableSheet st1 = wb1.createSheet("DataPool",0);//Creating worksheet

Below code helps to copy the data from one excel to another excel file,


for(int i=0;i<st.getRows();i++)//st.getRows()-->count the number of rows present in sheet
{
for(int t=0;t<st.getColumns();t++)//st.getColumns()-->count the number of columns present in sheet
{
a[i][t] = st.getCell(t, i).getContents();//Read the data from sheet and Storing it into the array variable
Label l = new Label(t, i, a[i][t]);//Helps to set to which cell the data need to be inserted and specify which data to be inserted
Label l1 = new Label(2, 0, "Status");
st1.addCell(l);//Insert the data to the specified cell
st1.addCell(l1);
}
}

Below code helps to check the pass/fail status of the scenario,


if(selenium.isTextPresent(j))//selenium.isTextPresent("Text")-->helps to check whether specified text present in the loaded page
{
s= "Pass";
}
else
{
s="Fail";
}



Below code helps to write the test case status to the excel


Label l2=new Label(2,i,s);
st1.addCell(l2);

}
wb1.write();

Happy Testing

Wednesday 27 February 2013

Write data to excel sheet using selenium

Dudes,

In my previous post i was explained about how to read the data from excel sheet and use those in our selenium script. In this post i will explain about how to write the data to a excel sheet through selenium script.

As known by everyone selenium does n't support read/write data from/to external resources like excel, etc. JXL jar file helps to overcome that issue. Jxl jar helps selenium to read/write the data from/to external sources like excel, etc. I would be explaining how could implement this in this post.

Example: (Google search and write the pass fail status to excel sheet)

package excel;

import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import java.io.FileOutputStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import jxl.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class write2excel{
private Selenium selenium;

@Before
public void startSelenium() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in");
selenium.start();
selenium.windowMaximize();
}


@Test
public void testGoogle1() throws Exception{
String s;
selenium.open("/");
Thread.sleep(3000);
System.out.println("Opened");
selenium.type("id=gbqfq", "Software Testing");
Thread.sleep(3000);
System.out.println("Typed");
selenium.click("id=gbqfb");
Thread.sleep(3000);
System.out.println("Clicked");
if(selenium.isTextPresent("en.wikipedia.org/wiki/Software_testing"))
{
s= "Pass";
}
else
{
s="Fail";
}
write(s);
}

@After
public void tearDown() throws Exception {
//selenium.stop();
}
public void write(String t) {
try
{
FileOutputStream f = new FileOutputStream("src\\Resources\\Data\\data2.xls",true);
      WritableWorkbook book = Workbook.createWorkbook(f);
      WritableSheet sheet = book.createSheet("output", 0);
      Label l = new Label(0, 0, t);
      sheet.addCell(l);
      book.write();
      book.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

}

In the above example following code used to create new excel file and write the data into excel sheet. And isText Present function helps to verify whether the mentioned text or word or sentence present in loaded page. Using that we can find whether the test case is pass or fail.


FileOutputStream f = new FileOutputStream("src\\Resources\\Data\\data2.xls",true);
       WritableWorkbook book = Workbook.createWorkbook(f);//Creating the excel sheet
       WritableSheet sheet = book.createSheet("output", 0);//Creating work sheet for excel
       Label l = new Label(0, 0, t);
       sheet.addCell(l);//Adding data to cell.
       book.write();
       book.close();

Happy Testing guys.


Tuesday 26 February 2013

How to avoid the browser closing after selenium script completed

Hi,

This post is to tell you all that how to avoid browser closing after the selenium script is completed. This is very simple to implement, just comment the selenium.stop(); function present inside @After.


            @After
    public void tearDown() throws Exception {
            //selenium.stop();
    }

Happy testing.

Data driven testing using TestNG

Data driven testing is executing the same script with set of multiple data from the external source file. Selenium does not have any inbuilt source for data driven testing to fetch the data like QTP has. To overcome this selenium uses external sources like excel, etc. To achieve data driven testing in selenium through many frameworks. In that TestNG also one of the frameworks which could help to pass the multiple data to a single script.

TestNG is a testing framework which helps for data driven testing with additional some more features that make it more powerful and easier to use. DataProvider is one of the feature which passes the set of data stored in the variables. 

Jxl is a java API that enables to read, write, and modify excel spreadsheets dynamically.

Prerequesties:



Steps:

  1. Open eclipse IDE
  2. Create new JAVA project
  3. Create new package under the project
  4. Create new class under the package
  5. Add the jxl,TextNG, selenium server, junit jar files in java build path
  6. Add new folder inside src folder
  7. Add another folder inside newly added folder
  8. Create excel file as defined below and save it in finally created folder.
  9. Type the following code inside the class editor
  10. Save the project
  11. Right click on project name and click on Build Project option
  12. Right click on project name and choose Run as-->click on TestNG test option

Please look at the following example for data driven testing using jxl and TestNG,

Example: (Searching data in Google)


package google4;

import com.thoughtworks.selenium.*;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.*;
import java.io.File;
import jxl.*;


public class google1{
private Selenium selenium;

@BeforeClass
public void startSelenium() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in");
selenium.start();
selenium.windowMaximize();
}

@DataProvider(name = "DP1")
public Object[][] createData1() throws Exception{
    Object[][] retObjArr=getTableArray("src\\Resources\\Data\\data1.xls",
            "DataPool", "imdbTestData1"); // /test1/src/Resources/Data/data1.xls
    return(retObjArr);
   
}

@Test (dataProvider = "DP1")
public void testGoogle1(String search) throws Exception{
selenium.open("http://www.google.co.in/");
Thread.sleep(3000);
System.out.println("Opened");
selenium.type("id=gbqfq", search);
Thread.sleep(3000);
System.out.println("Typed");
selenium.click("id=gbqfb");
Thread.sleep(3000);
System.out.println("Clicked");
}

@AfterClass
public void tearDown() throws Exception {
//selenium.stop();
}
public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
        String[][] tabArray=null;
        
            Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
            Sheet sheet = workbook.getSheet(sheetName); 
            int startRow,startCol, endRow, endCol,ci,cj;
            Cell tableStart=sheet.findCell(tableName);
            startRow=tableStart.getRow();
            startCol=tableStart.getColumn();

            Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000,  false);                

            endRow=tableEnd.getRow();
            endCol=tableEnd.getColumn();
            System.out.println("startRow="+startRow+", endRow="+endRow+", " +
                    "startCol="+startCol+", endCol="+endCol);
            tabArray=new String[endRow-startRow-1][endCol-startCol-1];
            ci=0;

            for (int i=startRow+1;i<endRow;i++,ci++){
                cj=0;
                for (int j=startCol+1;j<endCol;j++,cj++){
                    tabArray[ci][cj]=sheet.getCell(j,i).getContents();
                }
            }
       return(tabArray);
    }
    
}


Excel data:



In the above example following code helped to read the data from the excel sheet and store it in the variable.

@DataProvider(name = "DP1")
public Object[][] createData1() throws Exception{
    Object[][] retObjArr=getTableArray("src\\Resources\\Data\\data1.xls",
            "DataPool", "imdbTestData1"); // /test1/src/Resources/Data/data1.xls
    return(retObjArr);
   
}




public String[][] getTableArray(String xlFilePath, String sheetName, String tableName) throws Exception{
        String[][] tabArray=null;
        
            Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
            Sheet sheet = workbook.getSheet(sheetName); 
            int startRow,startCol, endRow, endCol,ci,cj;
            Cell tableStart=sheet.findCell(tableName);
            startRow=tableStart.getRow();
            startCol=tableStart.getColumn();

            Cell tableEnd= sheet.findCell(tableName, startCol+1,startRow+1, 100, 64000,  false);                

            endRow=tableEnd.getRow();
            endCol=tableEnd.getColumn();
            System.out.println("startRow="+startRow+", endRow="+endRow+", " +
                    "startCol="+startCol+", endCol="+endCol);
            tabArray=new String[endRow-startRow-1][endCol-startCol-1];
            ci=0;

            for (int i=startRow+1;i<endRow;i++,ci++){
                cj=0;
                for (int j=startCol+1;j<endCol;j++,cj++){
                    tabArray[ci][cj]=sheet.getCell(j,i).getContents();
                }
            }
       return(tabArray);
    }
    

Happy Testing.

Monday 18 February 2013

Solution for:- Error opening registry key 'Software\JavaSoft\Java Runtime Environment.3'

Hi,

This morning when i try to run the eclipse, i have been turned up into the following error "Error opening registry key 'Software\JavaSoft\Java Runtime Environment.3' ", but java is already installed in my system and what happened now suddenly. Through many sources i found that, it happened due to some unexpected things happened in java registry files. To resolve this issue you either take anyone of the following solution,

Solution 1:

Add the JRE registry entries manually.


[HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment]
"CurrentVersion"="1.7"

[HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.7]
"JavaHome"="C:\\Program Files\\Java\\jre7"
"RuntimeLib"="C:\\Program Files\\Java\\jre7\\bin\\client\\jvm.dll"

[HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\1.7.0_01]
"JavaHome"="C:\\Program Files\\Java\\jre7"
"RuntimeLib"="C:\\Program Files\\Java\\jre7\\bin\\client\\jvm.dll"
Solution 2:
  • Install the latest version of the java or update latest version. Since you would not able to fully uninstall the java, re installation would help to uninstall the old libraries and update with latest.
  • Once the installation is completed just restart your system which helps the application to perform effectively.
Thanks.
Enjoy Testing.