简体   繁体   中英

Selenium - Java Wait for page to load fails in test scripts

I create test cases in Eclipse IDE using JAVA with some Selenium scripts.

My Problem is that sometimes, Continuous Run of Test Cases produce error/failed test in the selenium.waitForPageToLoad("30000") method. I made a solution that the method will loop until a specific condition is met so I came to this code. BUT THIS DOESN'T WORK.

This what happens: Run Junit Test > @Test1,2,3..n > page does not load @ Test n > execute next line code > failed test in @Test n > because Page does not load so the next scripts cannot be done (missing required elements in the page because it doesn't load).

This what SUPPOSED to happen:Run Junit Test > @Test1,2,3..n > page does not load @ Test n > wait for page to load until Specific condition is met (ex. Element X is already present in the page)> execute next line code > passed test in @Test n

I need a Solution that wait the Page to load until the element required for the next lines of scripts is present.

THIS CODE DOESN'T WORK. I badly need your help. Thanks

//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);

}

This type of logic might be helpful for you

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();
        }
  }

Before doing any operation with any element just call this method for that element so that it will wait for that element until timeout reached (ie, 20sec) if that element is not found.

Example

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");

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