2017-03-08 65 views
0

我试图使用Eclipse中的Selenium提交按钮自动化工作流。检查字符串的外观(并提取)数字值

我使用的是自定义函数waitForVisible检查,如果WebElement ID为“naviInfo”显示,如果它拥有具有或者“没有行被发现”或消息“未找到{number}行”的消息。

问题是我无法排序和检查文本的数字部分。下面给出了示例代码。

String message = waitForVisible(By.id("naviInfo")).getText(); 

if ("No rows were found".equals(message)) { 
     log.info("No rows were found after submit"); 
} 
else if ("**1804** rows were found".equals(message)) { 
     log.info("**1804** rows found after submit"); 
} 
else { 
     (other error checks) 
} 

我该如何检查在找到普通文本行之前是否有数字值?另外还将这个数字保存到一个变量?

回答

1

如果我找到你了,你只是问如何验证消息匹配预期的模式,以及如何从字符串中提取数字?在这种情况下,这与Selenium无关,但是是一个简单的正则表达式问题。

Pattern p = Pattern.compile("^\\*{2}(\\d+)\\*{2} rows were found$"); //pattern that says: start of string, followed by two *s, then some digits, then two *s again, then the string " rows were found", and finally the end of string, capturing the digits only 
Matcher m = p.matcher("**1804** rows were found");  
boolean found = m.find(); //find and capture the pattern of interest 
if (found) 
    int count = Integer.parseInt(m.group(1)); //get the first (and only) captured group, and parse the integer from it 

阅读关于Java的正则表达式here

+0

谢谢kaqqao。你的评论确实帮助我弄清楚我需要什么。 – Nitya

0

所以这就是我让自己的病情起作用的原因。

if (" no rows were found".equals(waitForVisible(By.id("naviInfo")).getText())) 
    waitForVisible(By.xpath("//td[contains(text(),'Nothing found to display.')]")); 
else if (Pattern.matches("^ \\d+ rows were found$", waitForVisible(By.id("naviInfo")).getText())) 
    waitForVisible(By.xpath("//tbody//tr//td/a")); 
else 
    other error checks