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