我目前正在测试一个网站。当我单击“签入”字段时,将打开一个日历,而当我单击下一个箭头时,它将最小化,这是一个错误。我编写了脚本来测试此问题,但是我的问题是错误后我有一些脚本并且无法执行。我在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
我不想找到那个元素。我希望我的脚本继续运行并执行其余代码,即使出现错误消息之后
最佳答案
这是您的问题的答案:
正如您所提到的,您wrote the script to test this issue
因此,此步骤本身具有一个单独的验证点,您必须声明该验证点,这将是一个完整的Testcase,并应带有一个单独的@Test
批注。这与@Grasshopper的想法一致。
现在还提到了,您有some script after the error and It won't execute
。那是正确的行为。因此,要执行剩余的块,可以考虑将剩余的块移动到单独的@Test
注释块。
因此,现在,如果driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div/a[2]")).click();
引发异常,您将得到的结果为Fail
,如果通过则将得到结果为Pass
。
现在,由于test7
将依赖于test6
,因此对于test7
,在声明中将子句dependsOnMethods = "test6"
添加为@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");
}
让我知道这是否回答您的问题。