0

JAVA といくつかの Selenium スクリプトを使用して、Eclipse IDE でテスト ケースを作成します。

私の問題は、テストケースの継続的な実行が、selenium.waitForPageToLoad("30000") メソッドでエラー/失敗したテストを生成することがあることです。特定の条件が満たされるまでメソッドがループするという解決策を作成したので、このコードにたどり着きました。しかし、これは機能しません。

Junit Test を実行 > @Test1,2,3..n > ページがロードされない @ Test n > 次の行のコードを実行 > @Test n でテストに失敗 > ページがロードされないため、次のスクリプトを実行できないため(ページが読み込まれないため、ページに必要な要素がありません)。

これは想定されていることです: Junit テストを実行 > @Test1,2,3..n > ページが読み込まれない @ テスト n > 特定の条件が満たされるまでページが読み込まれるのを待つ (例: 要素 X がページに既に存在する) > 次の行のコードを実行 > @Test n でテストに合格

スクリプトの次の行に必要な要素が存在するまで、ページの読み込みを待機するソリューションが必要です。

このコードは機能しません。あなたの助けが必要です。ありがとう

//Wait for Page to Load until Expected Element is not Present   
public void waitForPageToLoadElement(final String isElementPresent){

boolean elementBoolean;
do{
selenium.waitForPageToLoad("30000");
elementBoolean = selenium.isElementPresent(isElementPresent);
if (elementBoolean==false){
try{Thread.sleep(3000);}
catch (Exception e) {
//catch
}}
}
while(elementBoolean==false);
}
//Wait for Page to Load until Expected Text is not Present
public void waitForPageToLoadText(String isTextPresent){

boolean elementBoolean;
do{
selenium.waitForPageToLoad("30000");
elementBoolean = selenium.isTextPresent(isTextPresent);
if (elementBoolean==false){
try{Thread.sleep(3000);}
catch (Exception e) {
//catch
}}
}
while(elementBoolean==false);

}

//Opens url until Expected Element is not Present
public void openUrl(String url){

boolean userNameBoolean, passwordBoolean;
do {
selenium.open(url);
userNameBoolean = selenium.isElementPresent("id=loginForm:username");
passwordBoolean = selenium.isElementPresent("id=loginForm:password");
if (userNameBoolean==false && passwordBoolean==false){
try{Thread.sleep(3000);}
catch (Exception e) {
//catch
}}
}while (userNameBoolean==false && passwordBoolean==false);

}
4

1 に答える 1

0

このタイプのロジックは役に立つかもしれません

public static void waitforElement(Selenium selenium,String element)
{

        try
        {
            int second;
            for (second = 0; ; second++) 
            {

                if (selenium.isElementPresent(element))
                {   
                    break;
                } 
                if (second >= 20)
                { 
                    break;
                }
                Thread.sleep(1000);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
  }

要素で何らかの操作を行う前に、その要素に対してこのメ​​ソッドを呼び出すだけで、その要素が見つからない場合、タイムアウトに達するまで (つまり、20 秒) その要素を待機します。

waitforElement(selenium,"id=loginForm:username");
selenium.type("id=loginForm:username","username");
waitforElement(selenium,"id=loginForm:password");
selenium.type("id=loginForm:password","password");
selenium.click("submit");
于 2013-02-07T13:35:47.090 に答える