0

I'm using PageFactory in my selenium tests. And I've faced a problem in waiting for loading page. I'm not talking about an element on a page I'm talking about timeout of page loading. So, I have a method like the following:

 public MyProjectsPage ClickSaveAndCloseButton()
        {
            //Do something and click a button
            SaveAndCloseButton.Click();

            return new MyProjectsPage(Driver); //return new page
        }

And when I'm waiting for returning new PageObject (in my case it is "MyProjectsPage") I got a timeout exception. So, where can I set a page loading timeout?

Actual mistake looks like this:

AutomatedTests.FrontEnd.SouvenirProduct.SouvenirTestExecution.OrderSouvenirWithAuthorization(ByCash,Pickup,True,Cup):
OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:7585/session/b68c04d1ead1fc78fe083e06cbece38f/element/0.46564483968541026-14/click timed out after 60 seconds.
  ----> System.Net.WebException : The operation has timed out

I have: The latest version of WebDriver And the latest version of ChromeDriver and the latest version of Chrome Browser The mistake that is above apears int the next line:

return new MyProjectsPage(Driver); //return new page

I create my ChromeDriver the next way:

 public DriverCover(IWebDriver driver)
        {
            _driver = driver;
            _driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
        }

        private readonly IWebDriver _driver;
Denis Koreyba
  • 3,144
  • 1
  • 31
  • 49
  • What line is that thrown on? What version of Selenium? What browser? What version of that browser? How are you creating the driver? More code please! – Arran Feb 06 '15 at 14:16
  • I've added more info in my post. But version of browser and other stuff don't matter. I'm asking how to set up a time to wait while a page is loading. Now it's 60 seconds (as my mistake tells me) and I want to increase it. How?) – Denis Koreyba Feb 06 '15 at 14:47
  • What does the constructor of `MyProjectsPage` look like? – Arran Feb 06 '15 at 15:06
  • as this one: public MyProjectsPage (IWebDriver webDriver) { WebDriver = webDriver; PageFactory.InitElements(WebDriver, this); } – Denis Koreyba Feb 06 '15 at 17:30

3 Answers3

1

1 note considering wait mechanisms on the page: Take a couple of webElements and apply for them fluentWait() . That'll be explicit wait webdriver approach.

Another approach is to try out implicit wait like:

int timeToWait=10;
driver.manage().timeouts().implicitlyWait(timeToWait, TimeUnit.SECONDS);

Considering you pageObject code: I would recommed you the following:

MyPage myPageInstance= PageFactory.initElements(driver, MyPage.class);

then you write the following method :

public MyPage clickSaveAndOtherActions(MyPage testPageToClick)
        {
            testPageToClick.clickFirstButton();
            testPageToClick.clickSecondButton();
            testPageToClick.closePoPup();


            return testPageToClick; //return page in a new state
    }

and if you wanna continue working (I mean update your page state) you do the following:

  myPageInstance = clickSaveAndOtherActions(myPageInstance ); 

Hope this helps you. Thanks.

UPD : as I see from the log something looks wrong with remoteWebdriver server:

OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:7585/session/b68c04d1ead1fc78fe083e06cbece38f/element/0.46564483968541026-14/click timed out after 60 seconds. ----> System.Net.WebException : The operation has timed out

Also, I'd recommend you to double check you driver method init. I'm using the following piece of java code for driver init (UI , chrome instance, selenium grid+ hub nodes test architecture):

public static WebDriver driverSetUp(WebDriver driver) throws MalformedURLException {
        DesiredCapabilities capability = DesiredCapabilities.chrome();
        log.info("Google chrome is selected");
        //System.setProperty("webdriver.chrome.driver", System.getProperty("user.home")+"/Documents/Tanjarine/chromedriver");
        System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        capability.setBrowserName("chrome");
        capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);


        String webDriverURL = "http://" + environmentData.getHubIP() + ":" + environmentData.getHubPort() + "/wd/hub";
        driver = new RemoteWebDriver(new URL(webDriverURL), capability);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
        driver.manage().window().setSize(new Dimension(1920, 1080));

        return driver;

    }
Community
  • 1
  • 1
eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
1

What you should really be doing when using the PageFactory pattern is when initialising your Page you should be using a constructor to initialise the elements.

public MyProjectsPage ClickSaveAndCloseButton()
{
        //Do something and click a button
        //I am guessing this is taking you to the MyProjectsPage
        SaveAndCloseButton.Click();

        return new MyProjectsPage(Driver); //return new page
}


public class MyProjectsPage
{

    [FindsBy(How = How.Id, Using = "yourId")]
    public IWebElement AWebElement { get; set; }

    private IWebDriver WebDriver;

    public MyProjectsPage (IWebDriver webDriver)
    {
        WebDriver = webDriver;
        PageFactory.InitElements(WebDriver, this);
    }   

}

When you return the page, all elements using the FindsBy attribute will be initialised.

Update:

set this property on the driver when you initialise it:

WebDriver.Manage().Timeouts().SetPageLoadTimeout(timespan)
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • I don't have problems with elements. They cannot be loaded as the whole page is loading too long. I do initialize my elements as you suggest but it's not the issue. – Denis Koreyba Feb 06 '15 at 13:55
  • How long are the pages taking to load, are you running the tests locally or using the GRID server? – Jamie Rees Feb 06 '15 at 13:56
  • I run them locally. How long pages take to load I don't know but it's more then 60 seconds as my driver waits – Denis Koreyba Feb 06 '15 at 14:40
  • Please check my updated answer. But i suspect that there is something wrong with your PageObject or how you are accessing it since it shouldn't take that long. – Jamie Rees Feb 06 '15 at 15:09
  • Problem is not with my PageObject, the problem is an actual page of web site. – Denis Koreyba Feb 07 '15 at 10:57
0
// Wait Until Object is Clickable
    public static void WaitUntilClickable(IWebElement elementLocator, int timeout)
    {
        try
        {
            WebDriverWait waitForElement = new WebDriverWait(DriverUtil.driver, TimeSpan.FromSeconds(10));
            waitForElement.Until(ExpectedConditions.ElementToBeClickable(elementLocator));
        }
        catch (NoSuchElementException)
        {
            Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
            throw;
        }
    }

    // Wait For Page To Load
    public static void WaitForPage()
    {
        new WebDriverWait(DriverUtil.driver, MyDefaultTimeout).Until(
            d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
    }
LoflinA
  • 350
  • 4
  • 19