Selenium: How to make the web driver to wait for page to refresh before executing another test

后端 未结 3 647
孤独总比滥情好
孤独总比滥情好 2020-12-13 00:50

I am writing Test Cases using Selenium web Driver in TestNG and I have a scenario where I am running multiple tests sequentially (refer below)

@Test(priority         


        
3条回答
  •  执念已碎
    2020-12-13 01:04

    If you are waiting for element to present then

    In selenium rc we used to do this using selenium.WaitForCondition("selenium.isElementPresent(\"fContent\")", "desiredTimeoutInMilisec")

    In web driver U can achieve the same thing using this

    WebDriverWait myWait = new WebDriverWait(webDriver, 45);
    ExpectedCondition conditionToCheck = new ExpectedCondition()
    {
        @Override
        public Boolean apply(WebDriver input) {
            return (input.findElements(By.id("fContent")).size() > 0);
        }
    };
    myWait.until(conditionToCheck);
    

    This way you can wait for your element to be present before executing your test2.

    UPDATED :

    If you are waiting for page to load then in web driver you can use this code:

    public void waitForPageLoaded(WebDriver driver)
    {
        ExpectedCondition expectation = new
    ExpectedCondition() 
        {
            public Boolean apply(WebDriver driver)
            {
                return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
        };
        Wait wait = new WebDriverWait(driver,30);
        try
        {
            wait.until(expectation);
        }
        catch(Throwable error)
        {
            assertFalse("Timeout waiting for Page Load Request to complete.",true);
        }
    }
    

提交回复
热议问题