0

I have an Excel file where all my links are stored in one single column. One Link on each row. For E.g:

www.example.com
www.test.com
www.demo.com

and so on. What I want to do is visit each link and open it in Firefox. Get the link in address bar and compare it with link in excel cell. If both are same them set the string "Pass" in next cell otherwise set string "Fail". How do I do This. Can you please give me a sample code? I am using selenium web driver with java.

Here is what I tried:

try {
    FileInputStream file=new FileInputStream(new File(path));
    FileOutputStream outFile=new FileOutputStream(new File(path));
    HSSFWorkbook workbook=new HSSFWorkbook(file);
    HSSFSheet sheet=workbook.getSheetAt(0);
    HSSFCell cell=null;     
    int s=sheet.getLastRowNum()+1;
    for(int i=0; i<s; i++){
        cell=sheet.getRow(i).getCell(0);
        String url=cell.toString();
        driver.get(url);
        Thread.sleep(10000);
        String urlnew=driver.getCurrentUrl().toString();
        HSSFRow row=sheet.getRow(i);
        HSSFCell cellresult=row.createCell(1);
        if(url==urlnew){
            cellresult.setCellValue("Pass");
        }else{
            cellresult.setCellValue("fail");
        }
        workbook.write(outFile);
    }    
    file.close();  
    outFile.close();
} 
2
  • I can write and give you c# code for this will it help you?
    – CheryJose
    Mar 24, 2013 at 2:45
  • Are you using junit framework. Let me know. I can give you the code. Mar 24, 2013 at 3:42

2 Answers 2

0
  1. driver = webdriver.Firefox();
  2. Load the Excel file (you can use this)
  3. In the rows loop, add this code: driver.get(< cell URL>);
1
  • I am not able to compare the url with urlnew. Neither I can write Pass or Fail in my Excel sheet. Mar 24, 2013 at 11:24
0

In your code you have used the following statement to compare two strings.

if(url==urlnew)

Use the below code. it will work.

if(url.equals(urlnew))

For more information on String comparision, check this link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.