Continue to run next steps after Element Not Visible Exception in Selenium Webdriver

Viewed 4594

I am currently testing a website. When I click on the check in field a calendar opens and when I click on the next arrow it mininmizes, which is an error. I wrote the script to test this issue, but my problem is I have some script after the error and It won't execute. I am using Selenium Webdriver on chrome and using Extent Reports v3.0.6 to generate the report. I'm using TestNG aswell

I want that particular step to be shown as failed and also the script to continue with the next steps. Will that be possible?

This 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");            
    }
}

This is the error that I'm getting:

org.openqa.selenium.ElementNotVisibleException: element not visible

I dont want to find that element. I want my script to keep running and execute the rest of the code even after the error message

4 Answers
Related