2017-03-08 5 views
0

EclipseでSeleniumを使用して[送信]ボタンを使用してワークフローを自動化しようとしています。文字列の数値の出現を確認します(そして抽出する)

私はID「naviInfo」とWebElementが表示されているかどうかを確認するために、カスタム関数waitForVisibleを使用していますし、それが行が見つかりませんでした」またはいずれかを有するメッセージを保持している場合、メッセージを「{数値}行が発見されました」。

問題は、テキストの数値部分をソートしてチェックすることができないことです。以下のコード例。

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

ありがとうございます。あなたのご意見は本当に私が必要なものを見つけ出すのを助けました。 – 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 
関連する問題