【问题标题】:Data driven testing in Selenium using POI and TestNG ..code getting error使用 POI 和 TestNG 在 Selenium 中进行数据驱动测试 ..code 出错
【发布时间】:2015-02-08 20:12:19
【问题描述】:

使用 Selenium,我编写了以下代码来从 Excel 工作表中读取数据,并将电子邮件和密码键入 Facebook 的登录页面。错误是使用 try/catch 的未处理表达式。我该如何解决这个问题?

package Excelpack;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

 public class ClassTwo {

    WebDriver driver;

     String username;
     String password;

     @Test(dataProvider="testdata")
     public void testFireFox(String uname, String password1){

      driver.findElement(By.id("email")).clear();
      driver.findElement(By.id("email")).sendKeys(uname);
      driver.findElement(By.id("pass")).clear();
      driver.findElement(By.id("pass")).sendKeys(password1); 

     }   

    @DataProvider(name="testdata")
    public  Object[][] TestDataFeed1()  
    {

          FileInputStream fis=new FileInputStream("D:\\book3.xlsx");
          XSSFWorkbook wb=new XSSFWorkbook(fis);
          XSSFSheet sh1= wb.getSheetAt(0);
         int  numrow = sh1.getLastRowNum()-sh1.getFirstRowNum();  
           int  colnum =sh1.getRow(1).getLastCellNum()+1; 
             Object [][] facebookdata=new Object[numrow][colnum];
      for(int i=0;i<numrow;i++)
      {

         facebookdata[i][0]=sh1.getRow(0).getCell(i).getStringCellValue();
         facebookdata[i][1]=sh1.getRow(1).getCell(i).getStringCellValue();   

      }
        return facebookdata;
    }



  @BeforeTest
  public void Setup(){
  driver=new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get("https://www.facebook.com/");
  }
}

【问题讨论】:

  • 你没有捕获哪个异常,在哪里?

标签: java selenium-webdriver apache-poi testng data-driven


【解决方案1】:

如果您使用的是 Eclipse 或 intellijI 等任何 IDE,IDE 会提示您添加“引发必要的异常”或在 try/catch 中编写代码块的解决方案。从代码的外观看来,您似乎需要在数据提供者 TestDataFeed1 上声明 throws 异常。

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2021-10-22
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多