测试后复选框保持选中状态并影响其他测试

软件测试 网络驱动程序
2022-01-23 21:54:04

我创建了一个测试用例,我将在其中输入要在未来日期和时间安排的交易的详细信息。我有一个弹出窗口,其中列出了所有现有交易,其中包含用于独立选择其中一个的复选框。我只能成功创建和运行测试用例一次,但在进一步执行时,第一个复选框总是被选中。

//steps to click on a check box 

new WebDriverWait(driver, 100).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//span[contains(.,'CAR')]")));

String s=driver.findElement(By.xpath("//span[contains(.,'CAR')]")).getAttribute("id");

int number=Integer.parseInt(s.split("_")[3]);

String chkboxid="AdminMainContent_grdDealDetails_chkSelect_"+number;

new WebDriverWait(driver, 100).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id(chkboxid)));

driver.findElement(By.id(chkboxid)).click();


//click on ok button
new WebDriverWait(driver,100).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("AdminMainContent_lbtnOkGrid")));
driver.findElement(By.id("AdminMainContent_lbtnOkGrid")).click();

谢谢

2个回答

我使用下面的代码解决了问题。问题在于我用来遍历所有复选框 id 的 for 循环,如果页面没有,它将单击下一步按钮。实际上,我放错了单击下一步按钮的位置。

for (int page = 1; page <= Integer.parseInt(totaPage); page++) {
    String dealTitle;

    String dealtitleCheck = s.getCell(2, 4).getContents().trim();

// {

    System.out.println("current page number is)" + page);
    for (int i = 0; i < 10; i++) {
        //System.out.println("I=" + i);
        Thread.sleep(500);
        // get the list of all available names

        dealTitle = driver.findElement(
        By.id("AdminMainContent_grdDealDetails_lblTitle_" + i)).getAttribute("innerHTML").trim();

        System.out.println("dealTitle"+dealTitle);

        // getting the id of car from excel
        // String dealtitleCheck = s.getCell(2, 4).getContents();

        System.out.println("dealtitleCheck="+dealtitleCheck+"dealTitle="+dealTitle);

        boolean stat = dealTitle.equals(dealtitleCheck);

        System.out.println("equal status="+stat);

        if (dealTitle.equals(dealtitleCheck)) {

            // System.out.println("you got it in"+i);
            // getting id of check box to select
            String chkboxid = "AdminMainContent_grdDealDetails_chkSelect_" + i;

            // waiting
            new WebDriveWait(driver, 100)
                .until(ExpectedConditions
                .presenceOfAllElementsLocatedBy(By
                .id(chkboxid)));

            // click on the check box for specific deal title
            // obtained from excel
            driver.findElement(By.id(chkboxid)).click();

            //click on ok button
            new WebDriverWait(driver, 100).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("AdminMainContent_lbtnOkGrid")));
            driver.findElement(By.id("AdminMainContent_lbtnOkGrid")).click();

            break;
        }//end of if condition

    }//end of for inner loop
    new WebDriverWait(driver, 10)
        .until(ExpectedConditions.presenceOfAllElementsLocatedBy(By
        .id("AdminMainContent_PagingCntrlDealSchedule_lbtnNext")));

    driver.findElement(
        By.id("AdminMainContent_PagingCntrlDealSchedule_lbtnNext"))
    .click();

}//end of for outer loop

测试如何进一步执行?在一个循环?每次执行都会重新生成 number 和 chkboxid 吗?

检查每个复选框的 id 是否是您对每次执行的期望。