简体   繁体   English

Selenium Webdriver Java代码,如果在设置的时间段后找不到页面上的文本,则显示消息

[英]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. 我正在尝试编写Java Webdriver方法,如果该方法成功在屏幕上找到一些文本,它将记录一条消息。 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. 例如,我想利用if-else语句和system.out来执行“文本在页面上”和“文本不在页面上”的操作。 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. 您可以尝试尝试使用Logger之类的东西(Simple Java Logger或Apache Log4j可以使用)。

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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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