我目前正在测试一个网站。当我点击签入字段时,日历会打开,当我点击下一个箭头时,它会缩小,这是一个错误。我编写了脚本来测试这个问题,但我的问题是我在错误后有一些脚本并且它不会执行。我在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
我不想找到那个元素。我希望我的脚本继续运行并执行其余的代码,即使在出现错误消息
之后答案 0 :(得分:1)
以下是您的问题的答案:
正如你提到的那样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)
< / p>
您的最终代码块如下所示:
@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");
}
请告诉我这是否是您的问题。
答案 1 :(得分:0)
正如你所提到的我不想找到那个元素。我希望我的脚本继续运行并执行其余的代码,即使在错误消息之后,只需将代码行放入try catch块
假设您希望在以下行中获得例外:
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");
然后将其包含在像
这样的try catch中try
{
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");
}
catch(Exception e)
{
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");
}
修改强>
我不确定什么应该是最好的答案但是如果你有类似的用例,即使ElementNotVisibleException
你的进一步代码应该执行而且测试方法应该算作失败,那么可以尝试以下方法:
if(driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div/a[2]")).isDisplayed())
{
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");
}
else
{
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");
org.testng.Assert.fail("Test method Failed due to ElementNotVisibleException");
}
代码说明:
如果条件将检查天气你的元素是否可见,如果它是可见的,那么如果元素不可见则它将继续执行然后它移动到else条件并执行你想要执行的其他代码
在最后完成执行
org.testng.Assert.fail("Test method Failed due to ElementNotVisibleException");
用于标记测试方法在其他条件下移动时失败。
答案 2 :(得分:0)
如何做这样的事情,实施和维护会更加清晰:
public boolean isElementFound(WebElement webs, String stringToFind) {
try {
webs.findElement(By.className(stringToFind));
return true;
}
catch (NoSuchContextException e) {
return false;
}
catch (Exception e)
{
return false;
}
}
您可以根据需要对xpath,ids等进行类似的实现。您可以像
BaseDriver baseDriver = new BaseDriver();
if (!(baseDriver.isElementFound(webs, "tagClass"))
答案 3 :(得分:0)
您可以实现一种方法,告诉您元素是否存在,并根据您可以失败或传递测试用例。 实现这一点有两种方法。
public boolean isElementExist(By identifer, int timeOut){
WebDriverWait wait = new WebDriverWait(driver, timeOut);
try{
wait.until(ExpectedConditions.presenceOfElementLocated(identifer));
return true;
}catch (TimeoutException te){
return false;
}
}
以下是无需等待的。
public boolean isElementExit(By identifer){
return driver.findElements(identifer).size() > 0;
}