简体   繁体   中英

Selenium : Check an Element present at any point of time

I'm looking for some cucumber tag like @During where it checks for an element at any point of time.For example,this code will run at all line of my test and incase the alert is present i want to do some action on that point.

public boolean isAlertPresent() {

    boolean presentFlag = false;

    try {

        // Check the presence of alert
        Alert alert = this.getDriver().switchTo().alert();
        // Alert present; set the flag
        presentFlag = true;
        // if present consume the alert
        alert.accept();
        //( Now, click on ok or cancel button )

    } catch (NoAlertPresentException ex) {
        // Alert not present
        ex.printStackTrace();
    }

    return presentFlag;
}

You could create a class that implements the interface WebDriverEventListener . This interface lets you create before and after hooks for all kinds of events such as afterClickOn , afterFindBy , afterAlertAccept , etc. This would let you call your method isAlertPresent() in one of these event hooks and deal with the popup anytime your script encounters it. You can alternatively have your class extend AbstractWebDriverEventListener abstract class if you don't need to implement all the hooks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM