Continue running the next steps after the Selenium Webdriver has an Element Not Visible Exception
I am currently testing a website. When I click on the checkin field, the calendar opens, and when I click on the next arrow, it is minimized, which is an error. I wrote a script to test this issue, but my problem is that after the error I have some scripts and it won’t execute. I use Selenium Webdriver on chrome and generate reports using Extent Reports v3.0.6. I’m also using TestNG
I want that particular step to show as a failure and the script to continue with the next steps. Is this possible?
Here is my code :
@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");
}
}
Here’s the error I’m getting :
org.openqa.selenium.ElementNotVisibleException: element not visible
I don’t want to find that element. I want my script to continue to run and execute the rest of the code even after an error message
Solution
Here is the answer to your question:
As you mentioned, you wrote a script to
test this issue, so this step itself has a separate verification point that you must assert that it is a complete test case and should be annotated with a separate @Test
. This is in line with @Grasshopper idea.
Now also mention that you have errors after some scripts and it will not execute
. This is the right behavior. Therefore, in order for the remaining blocks to be executed, you can consider moving the remaining blocks to a separate @Test-annotated block.
So, now if driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div/a[2]")).click() ;
Throw an exception and you will get the result of Fail
, and if you pass, you will get the result of Pass
.
Now as
test7
will depend ontest6
so fortest7
add the clausedependsOnMethods = "test6"
in the declaration as@Test (dependsOnMethods = {"test6"}, alwaysRun = true)
Your final code block will look like this:
@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");
}
If this answers your question, please let me know.