Mehavarnan Murugan
Mehavarnan Murugan

Reputation: 107

Selenium: How to stop execuing subsequest steps in @Test method after catching an exception using TestNG Listener?

I am working on PageFactory in Page Object Model and TestNG framework for automation. I am using beforeFindBy method in WebDriverEventListener to listen for the presence of an element. When an element is not present, NoSuchElementException is thrown and I catch the exception using try-catch block. How to stop executing remaining steps in @Test method. I do not want to use try-catch in my @Test method. Any suggestion on changes in my design is most appreciated. Below is my design.

LoginPage

    public class LoginPage {

        @FindBy(xpath = "//img[@src='/webres_5786257bd7c8a5.72130757/themes/default/images/login/logo.png']")
        WebElement loginLogo;

        public LoginPage(WebDriver driver, ExtentTest test) {
            this.driver = driver;
            PageFactory.initElements(driver, this);
            this.test = test;
        }

        public void verifyLoginPageLogo() throws IOException {
            Assert.assertTrue(loginLogo.isDisplayed());
            test.log(LogStatus.PASS, "Logo is displayed", takeScreenshot(driver, test));
        }
}

Listenter

public class CustomListeners extends TestListenerAdapter implements WebDriverEventListener {

    //All Implemented Methods

    public void beforeFindBy(By by, WebElement element, WebDriver driver) {
        try {
            driver.findElement(by);
        } catch (NoSuchElementException e) {
            System.out.println("Element " + by + "not found");
        }
    }
}

Test class

@Listeners(CustomListeners.class)
public class Test1 extends BaseTestSuite {

    LoginPage lp;
    TabMenu tm;

    @Test(priority = 0, testName = "Verify Login")
    public void login() throws Exception {
        lp = new LoginPage(driver, test);
        tm = new TabMenu(driver, test);
        driver.get(Constants.url);
        lp.verifyLoginPageLogo();  //Element not present here
        lp.setUserName("dmin");
        lp.setPassword("admin");
        lp.clickLoginBtn();
        tm.verifyTabMenu();
        tm.isCurrentTab("Dashboard");
    }
}

Upvotes: 0

Views: 765

Answers (1)

Grasshopper
Grasshopper

Reputation: 9058

The Login PageObject ideally should not contain assertion code. That code should be in your test class. Also the Extent functionality should also be in the test class and not the PageObject.

There is no need for a testng listener or a webdrivereventlistener. In the PageObject if restructure the verifyLoginPageLogo() method to below -

public boolean isLoginPageLogoDisplayed() throws IOException {
            //Write logic to check if element is displayed.    
            //Return false in case of exception etc
        }

In the test class do this -

Assert.assertTrue(lp.isLoginPageLogoDisplayed());

If an ArrertionError is thrown the remaining steps are skipped automatically.

ALso look at creating a BasePageObject which has utility methods for checking presence, displayed, finding elements etc etc. Just extend this.

Upvotes: 1

Related Questions