gpt4 book ai didi

java - 在 Selenium Webdriver 出现 Element Not Visible Exception 后继续运行后续步骤

转载 作者:行者123 更新时间:2023-11-30 10:27:35 28 4
gpt4 key购买 nike

我目前正在测试一个网站。当我单击 checkin 字段时,日历会打开,当我单击下一个箭头时,它会最小化,这是一个错误。我写了脚本来测试这个问题,但我的问题是错误后我有一些脚本,它不会执行。我在 chrome 上使用 Selenium Webdriver 并使用 Extent Reports v3.0.6 生成报告。我也在使用 TestNG

我希望该特定步骤显示为失败,并且脚本继续执行后续步骤。这可能吗?

这是我的代码:

@Test
public void test6()
{
test = extent.createTest("Test Case 6 : Selecting different months from secondary Calendar", "Browsing through and testing the calendar in check in/out field");

driver.navigate().to("https://www.hoteltreeoflife.com/reservation/");
test.info("Navigated to first page");

driver.findElement(By.id("checkIn")).click(); // clicking on checkin field
test.info("Clicked on Check In field");

driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div/a[2]")).click(); // The test fails on this line since its not visible
test.info("Clicked on foward arrow to change check in month");

driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/table/tbody/tr[3]/td[4]")).click();//choosing date AUG 6th
test.info("Check In Date selected");

driver.findElement(By.id("checkOut")).click(); //clicking on checkout field

driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div/a[2]")).click(); //clicking next arrow
test.info("Clicked on foward arrow to change check out month");

driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/table/tbody/tr[5]/td[4]")).click();//choosing date AUG 16th
test.info("Check Out Date selected");

test.info("Finding Check Availability");
driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div/form/div[4]/button")).click();
test.info("Clicks on Check Availability");
}

@AfterMethod
public void check(ITestResult result) throws IOException
{
if(result.getStatus() == ITestResult.FAILURE)
{
String screenshotPath = GetScreenshot.capture(driver);
test.fail("Test Case Failed");
test.info(result.getThrowable());
test.info("Screenshot Below : " + test.addScreenCaptureFromPath(screenshotPath));

}
else if(result.getStatus() == ITestResult.SKIP)
{
test.fail("Test Case Has Been Skipped");
}
else
{
test.pass("Test Case Passed");
}
}

这是我遇到的错误:

org.openqa.selenium.ElementNotVisibleException: element not visible

我不想找到那个元素。我希望我的脚本即使在出现错误消息后也能继续运行并执行其余代码

最佳答案

这是您问题的答案:

正如您提到的,您编写了脚本来测试这个问题,所以这个步骤本身有一个单独的验证点,您必须断言它是一个完整的测试用例,并且应该与一个单独的 @Test 注解。这符合@Grasshopper 的想法。

现在还提到,您有错误后的一些脚本,它不会执行。这是正确的行为。因此,为了让剩余的 block 得到执行,您可以考虑将剩余的 block 移动到单独的 @Test 注释 block 。

所以,现在 if driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div/a[2]")).click() ; 抛出异常,您将得到 Fail 的结果,如果通过,您将得到 Pass 的结果。

Now as test7 will depend on test6 so for test7 add the clause dependsOnMethods = "test6" in the declaration as @Test (dependsOnMethods = {"test6"}, alwaysRun = true)

您的最终代码块将如下所示:

@Test
public void test6()
{
test = extent.createTest("Test Case 6 : Selecting different months from secondary Calendar", "Browsing through and testing the calendar in check in/out field");

driver.navigate().to("https://www.hoteltreeoflife.com/reservation/");
test.info("Navigated to first page");

driver.findElement(By.id("checkIn")).click(); // clicking on checkin field
test.info("Clicked on Check In field");

driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div/a[2]")).click(); // If the test pass/fail result is updated.
}

@Test (dependsOnMethods = {"test6"}, alwaysRun = true)
public void test7 ()
{
driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/table/tbody/tr[3]/td[4]")).click();//choosing date AUG 6th
test.info("Check In Date selected");

driver.findElement(By.id("checkOut")).click(); //clicking on checkout field

driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div/a[2]")).click(); //clicking next arrow
test.info("Clicked on foward arrow to change check out month");

driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/table/tbody/tr[5]/td[4]")).click();//choosing date AUG 16th
test.info("Check Out Date selected");

test.info("Finding Check Availability");
driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div/form/div[4]/button")).click();
test.info("Clicks on Check Availability");
}

如果这回答了您的问题,请告诉我。

关于java - 在 Selenium Webdriver 出现 Element Not Visible Exception 后继续运行后续步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45273747/

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