gpt4 book ai didi

java - 如何使用java调用selenium中不同页面中的类

转载 作者:行者123 更新时间:2023-12-02 02:46:45 25 4
gpt4 key购买 nike

我创建了两个类,它们从 selenium 中的不同 Excel 表读取数据,那么如何创建对象并使用 java 调用第二个类,而不从脚本中提供任何数据。

在第二个类中有参数。我想从 Excel 工作表中获取这些数据,而不从类文件中提供任何数据。当我创建如下代码时,它无法正确运行。它显示此错误:

"Cannot inject @Test annotated Method [successfullycreaterate] 
with [class java.lang.String, class java.lang.String, class java.lang.String,
class java.lang.String, class java.lang.String, class java.lang.String,
class java.lang.String, class java.lang.String, class java.lang.String,
class java.lang.String]."
  1. 头等舱

        @Test(dataProvider = "excelData")
    public void read(String username,String password) throws InterruptedException {

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);


    //handle popup window
    Set<String> windowId = driver.getWindowHandles(); // get window id of current window
    Iterator<String> itererator = windowId.iterator();

    String mainWinID = itererator.next();
    String newAdwinID = itererator.next();

    driver.switchTo().window(newAdwinID);
    System.out.println(driver.getTitle());
    Thread.sleep(3000);



    WebDriverWait wait=new WebDriverWait(driver,3);

    WebElement uname= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( "//input[@id='j_username']")));
    uname.sendKeys(username);

    WebElement pwd= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( "//*[@id=\"j_password\"]")));
    pwd.sendKeys(password);

    WebElement login= driver.findElement(By.xpath("//*[@id=\"btnLogin\"]"));
    login.click();


    }
    @Test
    public void successfullycreaterate(String rateplan, String date, String hotel, String datevalidfrm, String datevalidto, String ratecatagory, String setcurrency, String taxlevel, String channel, String childlevel) throws IOException, InterruptedException {
    CreateRate execute=new CreateRate();
    execute.successfullyCreate(rateplan, date, hotel, datevalidfrm, datevalidto, ratecatagory, setcurrency, taxlevel, channel, childlevel);

    }


    }

2.第二课

@Test(dataProvider = "readExcelFile")
public void successfullyCreate(String rateplan,String date,String hotel,String datevalidfrm,String datevalidto,String ratecatagory,String setcurrency,String taxlevel,String channel,String childlevel ) throws IOException, InterruptedException {

driver = DataProviderTest.setUp();
}

@DataProvider(name="readExcelFile")
public static Object[][] readExcelFile() throws InvalidFormatException, IOException {
FileInputStream fis = new FileInputStream(resultFile);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sh = wb.getSheet("Sheet1");

System.out.println(sh.getPhysicalNumberOfRows());
System.out.println(sh.getRow(0).getPhysicalNumberOfCells());
int RowNum = sh.getPhysicalNumberOfRows();
int ColNum = sh.getRow(0).getPhysicalNumberOfCells();

String[][] xlData = new String[RowNum-1][ColNum];

for (int i = 0; i < RowNum - 1; i++)
{
XSSFRow row = sh.getRow(i + 1);
for (int j = 0; j < ColNum; j++)
{
if (row == null)
xlData[i][j] = "";
else {
XSSFCell cell = row.getCell(j);
if (cell == null)
xlData[i][j] = "";
else {
String value = formatter.formatCellValue(cell);
xlData[i][j] = value.trim();
}
}
}
}
return xlData;
}

应该从excel表中读取数据

最佳答案

@Samudi,您提到的“无法注入(inject)@Test注释方法”的错误是因为我们无法向@Test方法提供参数,而只能使用dataProvider。

我认为您在firstClass文件的第二个@Test注释中缺少对dataProvider的调用,而您在第一个@Test注释上做得很好。

关于java - 如何使用java调用selenium中不同页面中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57138744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com