Tuesday, March 29, 2011

Automate rich text editor

Using selenium 1.0 difficult to Automate rich text editor, i have wasted my whole day to automate rich text editor , finally i got success.

to automate following type of rich text editor :

As selenium IDE will not record any text entering in to text area. we need to add manually


        selenium.selectFrame("//table/tbody/tr[2]/td/iframe"); //frame location
        selenium.typeKeys("//html/body/p/br", comment_fromdrupal);
        selenium.selectWindow("null");
        assertTrue(selenium.isElementPresent("edit-submit"));
        selenium.click("edit-submit");


so, for frame position, search using xpather, and use typeKeys.

Friday, March 4, 2011

Data driven test using multiple excel sheets

use jxl.jar

public String[] getExcelDataByRow(String sheetName, int rowNum){
       //Create a dynamic array
       String[] retVal=null;
       try{
           Workbook w=Workbook.getWorkbook(new File("c:/data.xls));
           Sheet s=w.getSheet(sheetName);
           //get the number of columns in the excel sheet
           int colCount=s.getColumns();
           //fix the size of the array with the col count
           retVal=new String[colCount];
           //loop through all the columns and store the value into
the array
           for(int i=0;i<colCount;i++){
               retVal[i]=s.getCell(i, rowNum-1).getContents();
           }
       }
       catch(Exception e){
           e.printStackTrace();
       }
       return retVal;
   }

--------------------------------------------
call the above method
String[] dataArr=dl.getExcelDataByRow("Login", 2);

Login is sheet name

Monday, February 28, 2011

Write results to excel sheet

After successfully executing scripts, every one want to write results to excel sheet..here is the way to write results to excel sheet....

Download Java excel library from  http://sourceforge.net/projects/jexcelapi/


package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.thoughtworks.selenium.*;

import org.openqa.selenium.server.*;
import org.testng.annotations.*;

public class Importexport1 {
public Selenium selenium;
public SeleniumServer seleniumserver;

@BeforeClass
public void setUp() throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
seleniumserver = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
seleniumserver.start();
selenium.start();
}

@Test
public void testImportexport1() throws Exception {

// Read data from excel sheet
FileInputStream fi = new FileInputStream(
"F:\\Framework\\testdata\\Login1_Credentials.xls");
Workbook w = Workbook.getWorkbook(fi);
Sheet s = w.getSheet(0);
String a[][] = new String[s.getRows()][s.getColumns()];
// Write the input data into another excel file
FileOutputStream fo = new FileOutputStream(
"F:\\Framework\\Results\\LoginResult1.xls");
WritableWorkbook wwb = Workbook.createWorkbook(fo);
WritableSheet ws = wwb.createSheet("loginresult1", 0);

selenium.open("http://www.gmail.com");
selenium.windowMaximize();

System.out.println("s.getRows() = " + s.getRows());

for (int i = 0; i < s.getRows(); i++) {
System.out.println("s.getColumns = " + s.getColumns());
for (int j = 0; j < s.getColumns(); j++) {
a[i][j] = s.getCell(j, i).getContents();
Label l = new Label(j, i, a[i][j]);
Label l1 = new Label(2, 0, "Result");
ws.addCell(l);
ws.addCell(l1);
}
}

for (int i = 1; i < s.getRows(); i++) {
selenium.type("Email", s.getCell(0, i).getContents());
selenium.type("Passwd", s.getCell(1, i).getContents());
selenium.click("signIn");
selenium.waitForPageToLoad("30000");

boolean aa = selenium.isTextPresent("The username or password you entered is incorrect. [?]");
System.out.println("the value of aa is::" + aa);
if (aa)

{

Label l3 = new Label(2, i, "fail");

ws.addCell(l3);
System.out.println("Login Failure");
Thread.sleep(10000);

} else {

Label l2 = new Label(2, i, "pass");

ws.addCell(l2);
selenium.click("link=Sign out");
Thread.sleep(10000);
}
}
wwb.write();
wwb.close();
}

@AfterClass
public void tearDown() throws Exception {
selenium.stop();
seleniumserver.stop();

}
}




Your input data should be like this....
















Your out put excel should be like this











Hope this post is helpful....

Friday, February 25, 2011

Start selenium server through IntelliJ IDEA

Include following libraries :


import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;

Public class SetuptearDown extends SeleneseTestCase{

    Selenium selenium;
    public static final String MAX_WAIT = "60000";
    private SeleniumServer seleniumServer;
     private String testURL = "www.testURL.com/";             // Change URL here..............................
    private String testplatform = "*firefox";                               // change platform here................



    @BeforeClass
    public void setUp() throws Exception {
        RemoteControlConfiguration rc = new RemoteControlConfiguration();
        rc.setAvoidProxy(true);
        rc.setSingleWindow(false);

        rc.setReuseBrowserSessions(true);

        seleniumServer =  new SeleniumServer(rc);

        selenium = new DefaultSelenium("localhost", 4444, testplatform, testURL);
        seleniumServer.start();
        selenium.start();
    }


    @AfterClass
    public void tearDown() throws Exception {
        selenium.stop();
        seleniumServer.stop();
    }
    public void login(String username,String password)
    {
       

    }
}

Thursday, February 24, 2011

Generating Test Result Report using Ant from Java eclipse

Always Reporting is the most important thing in testing, here described is the steps to configure and generate the test result from java eclipse using Ant.
this post is for users who have already familiar with java and eclipse.

Generating the ANT build:
Right click on the project and select Export --> Ant Buildfiles.-->Next-->Select the project-->Finish.
Configuring jar:
Select Windows menu-->Preference -->Global Entries -->Add External JARs-->Select the folder where the eclipse is installed. eclipse -->Plugin -->Currently its located in "org.junit_4.8.1.v4_8_1_v20100427-1100"-->"junit.jar"-->OK.
Run the Ant Build:
right click on build.xml go to run as- external tools confirgurations ---targets tab and select your test suites and junitreport.   then right click on build.xml and run as antbuild file...



You will get the report as below