简体   繁体   中英

Selenium Webdriver Java code to display message if text on page cannot be found after a set time period

I'm trying to write a Java Webdriver method that will log a message if it successfully finds some text on screen. If it doesn't find it within a given time period I would also like to log an appropriate message. The code below works to search for the text. Please could some one give me an example of how I can make it log a message if successful or unsuccessful. eg "The text is on the page" and "The text is not on the page" I would like to utilize and if-else statement and system.out to do this. The following code waits for the text but does not log the result nicely.

public static void waitForText(WebDriver driver, String text) throws Exception {
    final String searchText = text;
    (new WebDriverWait(driver, 30)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            return d.getPageSource().contains(searchText);
        }
    });
}

You can use something like Logger (Simple Java Logger or Apache Log4j will work) with try and catch.

private static final Logger logger = Logger.getLogger(ClassName.class);
try{
  //Do Something
  logger.info("Message");
}
catch(Exception e){
  logger.error("Message " + e);
}

Hope this helps!

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