"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/dotnet/test/common/PageLoadingTest.cs" (17 Feb 2023, 19231 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 using System;
    2 using NUnit.Framework;
    3 using OpenQA.Selenium.Environment;
    4 
    5 namespace OpenQA.Selenium
    6 {
    7     [TestFixture]
    8     public class PageLoadingTest : DriverTestFixture
    9     {
   10         private IWebDriver localDriver;
   11 
   12         [SetUp]
   13         public void RestartOriginalDriver()
   14         {
   15             driver = EnvironmentManager.Instance.GetCurrentDriver();
   16         }
   17 
   18         [TearDown]
   19         public void QuitAdditionalDriver()
   20         {
   21             if (localDriver != null)
   22             {
   23                 localDriver.Quit();
   24                 localDriver = null;
   25             }
   26         }
   27 
   28         [Test]
   29         public void NoneStrategyShouldNotWaitForPageToLoad()
   30         {
   31             InitLocalDriver(PageLoadStrategy.None);
   32 
   33             string slowPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("sleep?time=5");
   34 
   35             DateTime start = DateTime.Now;
   36             localDriver.Url = slowPage;
   37             DateTime end = DateTime.Now;
   38 
   39             TimeSpan duration = end - start;
   40             // The slow loading resource on that page takes 6 seconds to return,
   41             // but with 'none' page loading strategy 'get' operation should not wait.
   42             Assert.That(duration.TotalMilliseconds, Is.LessThan(1000), "Took too long to load page: " + duration.TotalMilliseconds);
   43         }
   44 
   45 
   46         [Test]
   47         public void NoneStrategyShouldNotWaitForPageToRefresh()
   48         {
   49             InitLocalDriver(PageLoadStrategy.None);
   50 
   51             string slowPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("sleep?time=5");
   52 
   53             // We discard the element, but want a check to make sure the page is loaded
   54             WaitFor(() => localDriver.FindElement(By.TagName("body")), TimeSpan.FromSeconds(10), "did not find body");
   55 
   56             DateTime start = DateTime.Now;
   57             localDriver.Navigate().Refresh();
   58             DateTime end = DateTime.Now;
   59 
   60             TimeSpan duration = end - start;
   61             // The slow loading resource on that page takes 6 seconds to return,
   62             // but with 'none' page loading strategy 'refresh' operation should not wait.
   63             Assert.That(duration.TotalMilliseconds, Is.LessThan(1000), "Took too long to load page: " + duration.TotalMilliseconds);
   64         }
   65 
   66         [Test]
   67         [IgnoreBrowser(Browser.Edge, "Edge driver does not support eager page load strategy")]
   68         public void EagerStrategyShouldNotWaitForResources()
   69         {
   70             InitLocalDriver(PageLoadStrategy.Eager);
   71 
   72             string slowPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("slowLoadingResourcePage.html");
   73 
   74             DateTime start = DateTime.Now;
   75             localDriver.Url = slowPage;
   76             // We discard the element, but want a check to make sure the GET actually
   77             // completed.
   78             WaitFor(() => localDriver.FindElement(By.Id("peas")), TimeSpan.FromSeconds(10), "did not find element");
   79             DateTime end = DateTime.Now;
   80 
   81             // The slow loading resource on that page takes 6 seconds to return. If we
   82             // waited for it, our load time should be over 6 seconds.
   83             TimeSpan duration = end - start;
   84             Assert.That(duration.TotalMilliseconds, Is.LessThan(5 * 1000), "Took too long to load page: " + duration.TotalMilliseconds);
   85         }
   86 
   87         [Test]
   88         [IgnoreBrowser(Browser.Edge, "Edge driver does not support eager page load strategy")]
   89         public void EagerStrategyShouldNotWaitForResourcesOnRefresh()
   90         {
   91             InitLocalDriver(PageLoadStrategy.Eager);
   92 
   93             string slowPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("slowLoadingResourcePage.html");
   94             localDriver.Url = slowPage;
   95 
   96             // We discard the element, but want a check to make sure the GET actually
   97             // completed.
   98             WaitFor(() => localDriver.FindElement(By.Id("peas")), TimeSpan.FromSeconds(10), "did not find element");
   99 
  100             DateTime start = DateTime.Now;
  101             localDriver.Navigate().Refresh();
  102             // We discard the element, but want a check to make sure the GET actually
  103             // completed.
  104             WaitFor(() => localDriver.FindElement(By.Id("peas")), TimeSpan.FromSeconds(10), "did not find element");
  105             DateTime end = DateTime.Now;
  106 
  107             // The slow loading resource on that page takes 6 seconds to return. If we
  108             // waited for it, our load time should be over 6 seconds.
  109             TimeSpan duration = end - start;
  110             Assert.That(duration.TotalMilliseconds, Is.LessThan(5 * 1000), "Took too long to load page: " + duration.TotalMilliseconds);
  111         }
  112 
  113         [Test]
  114         [IgnoreBrowser(Browser.Edge, "Edge driver does not support eager page load strategy")]
  115         public void EagerStrategyShouldWaitForDocumentToBeLoaded()
  116         {
  117             InitLocalDriver(PageLoadStrategy.Eager);
  118 
  119             string slowPage = EnvironmentManager.Instance.UrlBuilder.WhereIs("sleep?time=3");
  120 
  121             localDriver.Url = slowPage;
  122 
  123             // We discard the element, but want a check to make sure the GET actually completed.
  124             WaitFor(() => localDriver.FindElement(By.TagName("body")), TimeSpan.FromSeconds(10), "did not find body");
  125         }
  126 
  127         [Test]
  128         public void NormalStrategyShouldWaitForDocumentToBeLoaded()
  129         {
  130             driver.Url = simpleTestPage;
  131 
  132             Assert.AreEqual(driver.Title, "Hello WebDriver");
  133         }
  134 
  135         [Test]
  136         [IgnoreBrowser(Browser.All, "Server not properly redirecting")]
  137         public void ShouldFollowRedirectsSentInTheHttpResponseHeaders()
  138         {
  139             driver.Url = redirectPage;
  140             Assert.AreEqual(driver.Title, "We Arrive Here");
  141         }
  142 
  143         [Test]
  144         public void ShouldFollowMetaRedirects()
  145         {
  146             driver.Url = metaRedirectPage;
  147             WaitFor(() => { return driver.Title == "We Arrive Here"; }, "Browser title was not 'We Arrive Here'");
  148             Assert.AreEqual(driver.Title, "We Arrive Here");
  149         }
  150 
  151         [Test]
  152         [IgnoreBrowser(Browser.Firefox, "Browser doesn't see subsequent navigation to a fragment as a new navigation.")]
  153         public void ShouldBeAbleToGetAFragmentOnTheCurrentPage()
  154         {
  155             driver.Url = xhtmlTestPage;
  156             driver.Url = xhtmlTestPage + "#text";
  157             driver.FindElement(By.Id("id1"));
  158         }
  159 
  160         [Test]
  161         [NeedsFreshDriver(IsCreatedBeforeTest = true)]
  162         public void ShouldThrowIfUrlIsMalformed()
  163         {
  164             Assert.That(() => driver.Url = "www.test.com", Throws.InstanceOf<WebDriverException>());
  165         }
  166 
  167         [Test]
  168         [NeedsFreshDriver(IsCreatedBeforeTest = true)]
  169         public void ShouldThrowIfUrlIsMalformedInPortPart()
  170         {
  171             Assert.That(() => driver.Url = "http://localhost:30001bla", Throws.InstanceOf<WebDriverException>());
  172         }
  173 
  174         [Test]
  175         public void ShouldReturnUrlOnNotExistedPage()
  176         {
  177             string url = EnvironmentManager.Instance.UrlBuilder.WhereIs("not_existed_page.html");
  178             driver.Url = url;
  179             Assert.AreEqual(url, driver.Url);
  180         }
  181 
  182         [Test]
  183         public void ShouldBeAbleToLoadAPageWithFramesetsAndWaitUntilAllFramesAreLoaded()
  184         {
  185             driver.Url = framesetPage;
  186 
  187             driver.SwitchTo().Frame(0);
  188             IWebElement pageNumber = driver.FindElement(By.XPath("//span[@id='pageNumber']"));
  189             Assert.AreEqual(pageNumber.Text.Trim(), "1");
  190 
  191             driver.SwitchTo().DefaultContent().SwitchTo().Frame(1);
  192             pageNumber = driver.FindElement(By.XPath("//span[@id='pageNumber']"));
  193             Assert.AreEqual(pageNumber.Text.Trim(), "2");
  194         }
  195 
  196         [Test]
  197         [NeedsFreshDriver(IsCreatedBeforeTest = true)]
  198         public void ShouldDoNothingIfThereIsNothingToGoBackTo()
  199         {
  200             string originalTitle = driver.Title;
  201             driver.Url = formsPage;
  202 
  203             driver.Navigate().Back();
  204             // We may have returned to the browser's home page
  205             string currentTitle = driver.Title;
  206             Assert.That(currentTitle, Is.EqualTo(originalTitle).Or.EqualTo("We Leave From Here"));
  207             if (driver.Title == originalTitle)
  208             {
  209                 driver.Navigate().Back();
  210                 Assert.AreEqual(originalTitle, driver.Title);
  211             }
  212         }
  213 
  214         [Test]
  215         public void ShouldBeAbleToNavigateBackInTheBrowserHistory()
  216         {
  217             driver.Url = formsPage;
  218 
  219             driver.FindElement(By.Id("imageButton")).Submit();
  220             WaitFor(TitleToBeEqualTo("We Arrive Here"), "Browser title was not 'We Arrive Here'");
  221             Assert.AreEqual(driver.Title, "We Arrive Here");
  222 
  223             driver.Navigate().Back();
  224             WaitFor(TitleToBeEqualTo("We Leave From Here"), "Browser title was not 'We Leave From Here'");
  225             Assert.AreEqual(driver.Title, "We Leave From Here");
  226         }
  227 
  228         [Test]
  229         public void ShouldBeAbleToNavigateBackInTheBrowserHistoryInPresenceOfIframes()
  230         {
  231             driver.Url = xhtmlTestPage;
  232 
  233             driver.FindElement(By.Name("sameWindow")).Click();
  234             WaitFor(TitleToBeEqualTo("This page has iframes"), "Browser title was not 'This page has iframes'");
  235             Assert.AreEqual(driver.Title, "This page has iframes");
  236 
  237             driver.Navigate().Back();
  238             WaitFor(TitleToBeEqualTo("XHTML Test Page"), "Browser title was not 'XHTML Test Page'");
  239             Assert.AreEqual(driver.Title, "XHTML Test Page");
  240         }
  241 
  242         [Test]
  243         public void ShouldBeAbleToNavigateForwardsInTheBrowserHistory()
  244         {
  245             driver.Url = formsPage;
  246 
  247             driver.FindElement(By.Id("imageButton")).Submit();
  248             WaitFor(TitleToBeEqualTo("We Arrive Here"), "Browser title was not 'We Arrive Here'");
  249             Assert.AreEqual(driver.Title, "We Arrive Here");
  250 
  251             driver.Navigate().Back();
  252             WaitFor(TitleToBeEqualTo("We Leave From Here"), "Browser title was not 'We Leave From Here'");
  253             Assert.AreEqual(driver.Title, "We Leave From Here");
  254 
  255             driver.Navigate().Forward();
  256             WaitFor(TitleToBeEqualTo("We Arrive Here"), "Browser title was not 'We Arrive Here'");
  257             Assert.AreEqual(driver.Title, "We Arrive Here");
  258         }
  259 
  260         [Test]
  261         [Ignore("Unable to open secure url")]
  262         [IgnoreBrowser(Browser.IE, "Browser does not support using insecure SSL certs")]
  263         [IgnoreBrowser(Browser.Safari, "Browser does not support using insecure SSL certs")]
  264         public void ShouldBeAbleToAccessPagesWithAnInsecureSslCertificate()
  265         {
  266             String url = EnvironmentManager.Instance.UrlBuilder.WhereIsSecure("simpleTest.html");
  267             driver.Url = url;
  268 
  269             // This should work
  270             Assert.AreEqual(driver.Title, "Hello WebDriver");
  271         }
  272 
  273         [Test]
  274         public void ShouldBeAbleToRefreshAPage()
  275         {
  276             driver.Url = xhtmlTestPage;
  277 
  278             driver.Navigate().Refresh();
  279 
  280             Assert.AreEqual(driver.Title, "XHTML Test Page");
  281         }
  282 
  283         /// <summary>
  284         /// see <a href="http://code.google.com/p/selenium/issues/detail?id=208">Issue 208</a>
  285         /// </summary>
  286         [Test]
  287         [IgnoreBrowser(Browser.IE, "Browser does, in fact, hang in this case.")]
  288         [IgnoreBrowser(Browser.Firefox, "Browser does, in fact, hang in this case.")]
  289         public void ShouldNotHangIfDocumentOpenCallIsNeverFollowedByDocumentCloseCall()
  290         {
  291             driver.Url = documentWrite;
  292 
  293             // If this command succeeds, then all is well.
  294             driver.FindElement(By.XPath("//body"));
  295         }
  296 
  297         [Test]
  298         [NeedsFreshDriver(IsCreatedAfterTest = true)]
  299         public void PageLoadTimeoutCanBeChanged()
  300         {
  301             TestPageLoadTimeoutIsEnforced(2);
  302             TestPageLoadTimeoutIsEnforced(3);
  303         }
  304 
  305         [Test]
  306         [NeedsFreshDriver(IsCreatedAfterTest = true)]
  307         public void CanHandleSequentialPageLoadTimeouts()
  308         {
  309             long pageLoadTimeout = 2;
  310             long pageLoadTimeBuffer = 10;
  311             string slowLoadingPageUrl = EnvironmentManager.Instance.UrlBuilder.WhereIs("sleep?time=" + (pageLoadTimeout + pageLoadTimeBuffer));
  312             driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(2);
  313             AssertPageLoadTimeoutIsEnforced(() => driver.Url = slowLoadingPageUrl, pageLoadTimeout, pageLoadTimeBuffer);
  314             AssertPageLoadTimeoutIsEnforced(() => driver.Url = slowLoadingPageUrl, pageLoadTimeout, pageLoadTimeBuffer);
  315         }
  316 
  317         [Test]
  318         [NeedsFreshDriver(IsCreatedAfterTest = true)]
  319         public void ShouldTimeoutIfAPageTakesTooLongToLoad()
  320         {
  321             try
  322             {
  323                 TestPageLoadTimeoutIsEnforced(2);
  324             }
  325             finally
  326             {
  327                 driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(300);
  328             }
  329 
  330             // Load another page after get() timed out but before test HTTP server served previous page.
  331             driver.Url = xhtmlTestPage;
  332             WaitFor(TitleToBeEqualTo("XHTML Test Page"), "Title was not expected value");
  333         }
  334 
  335         [Test]
  336         [NeedsFreshDriver(IsCreatedAfterTest = true)]
  337         public void ShouldTimeoutIfAPageTakesTooLongToLoadAfterClick()
  338         {
  339             driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(2);
  340 
  341             driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("page_with_link_to_slow_loading_page.html");
  342             IWebElement link = WaitFor(() => driver.FindElement(By.Id("link-to-slow-loading-page")), "Could not find link");
  343 
  344             try
  345             {
  346                 AssertPageLoadTimeoutIsEnforced(() => link.Click(), 2, 3);
  347             }
  348             finally
  349             {
  350                 driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(300);
  351             }
  352 
  353             // Load another page after get() timed out but before test HTTP server served previous page.
  354             driver.Url = xhtmlTestPage;
  355             WaitFor(TitleToBeEqualTo("XHTML Test Page"), "Title was not expected value");
  356         }
  357 
  358         [Test]
  359         [NeedsFreshDriver(IsCreatedAfterTest = true)]
  360         public void ShouldTimeoutIfAPageTakesTooLongToRefresh()
  361         {
  362             // Get the sleeping servlet with a pause of 5 seconds
  363             long pageLoadTimeout = 2;
  364             long pageLoadTimeBuffer = 0;
  365             string slowLoadingPageUrl = EnvironmentManager.Instance.UrlBuilder.WhereIs("sleep?time=" + (pageLoadTimeout + pageLoadTimeBuffer));
  366             driver.Url = slowLoadingPageUrl;
  367 
  368             driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(2);
  369 
  370             try
  371             {
  372                 AssertPageLoadTimeoutIsEnforced(() => driver.Navigate().Refresh(), 2, 4);
  373             }
  374             finally
  375             {
  376                 driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(300);
  377             }
  378 
  379             // Load another page after get() timed out but before test HTTP server served previous page.
  380             driver.Url = xhtmlTestPage;
  381             WaitFor(TitleToBeEqualTo("XHTML Test Page"), "Title was not expected value");
  382         }
  383 
  384         [Test]
  385         [IgnoreBrowser(Browser.Chrome, "Chrome driver does, in fact, stop loading page after a timeout.")]
  386         [IgnoreBrowser(Browser.Edge, "Edge driver does, in fact, stop loading page after a timeout.")]
  387         [NeedsFreshDriver(IsCreatedAfterTest = true)]
  388         public void ShouldNotStopLoadingPageAfterTimeout()
  389         {
  390             try
  391             {
  392                 TestPageLoadTimeoutIsEnforced(1);
  393             }
  394             finally
  395             {
  396                 driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(300);
  397             }
  398 
  399             WaitFor(() =>
  400             {
  401                 try
  402                 {
  403                     string text = driver.FindElement(By.TagName("body")).Text;
  404                     return text.Contains("Slept for 11s");
  405                 }
  406                 catch (NoSuchElementException)
  407                 {
  408                 }
  409                 catch (StaleElementReferenceException)
  410                 {
  411                 }
  412                 return false;
  413             }, TimeSpan.FromSeconds(30), "Did not find expected text");
  414         }
  415 
  416         private Func<bool> TitleToBeEqualTo(string expectedTitle)
  417         {
  418             return () => { return driver.Title == expectedTitle; };
  419         }
  420 
  421         /**
  422          * Sets given pageLoadTimeout to the {@link #driver} and asserts that attempt to navigate to a
  423          * page that takes much longer (10 seconds longer) to load results in a TimeoutException.
  424          * <p>
  425          * Side effects: 1) {@link #driver} is configured to use given pageLoadTimeout,
  426          * 2) test HTTP server still didn't serve the page to browser (some browsers may still
  427          * be waiting for the page to load despite the fact that driver responded with the timeout).
  428          */
  429         private void TestPageLoadTimeoutIsEnforced(long webDriverPageLoadTimeoutInSeconds)
  430         {
  431             // Test page will load this many seconds longer than WD pageLoadTimeout.
  432             long pageLoadTimeBufferInSeconds = 10;
  433             string slowLoadingPageUrl = EnvironmentManager.Instance.UrlBuilder.WhereIs("sleep?time=" + (webDriverPageLoadTimeoutInSeconds + pageLoadTimeBufferInSeconds));
  434             driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(webDriverPageLoadTimeoutInSeconds);
  435             AssertPageLoadTimeoutIsEnforced(() => driver.Url = slowLoadingPageUrl, webDriverPageLoadTimeoutInSeconds, pageLoadTimeBufferInSeconds);
  436         }
  437 
  438         private void AssertPageLoadTimeoutIsEnforced(TestDelegate delegateToTest, long webDriverPageLoadTimeoutInSeconds, long pageLoadTimeBufferInSeconds)
  439         {
  440             DateTime start = DateTime.Now;
  441             Assert.That(delegateToTest, Throws.InstanceOf<WebDriverTimeoutException>(), "I should have timed out after " + webDriverPageLoadTimeoutInSeconds + " seconds");
  442             DateTime end = DateTime.Now;
  443             TimeSpan duration = end - start;
  444             Assert.That(duration.TotalSeconds, Is.GreaterThan(webDriverPageLoadTimeoutInSeconds));
  445             Assert.That(duration.TotalSeconds, Is.LessThan(webDriverPageLoadTimeoutInSeconds + pageLoadTimeBufferInSeconds));
  446         }
  447 
  448         private void InitLocalDriver(PageLoadStrategy strategy)
  449         {
  450             EnvironmentManager.Instance.CloseCurrentDriver();
  451             if (localDriver != null)
  452             {
  453                 localDriver.Quit();
  454             }
  455 
  456             PageLoadStrategyOptions options = new PageLoadStrategyOptions();
  457             options.PageLoadStrategy = strategy;
  458             localDriver = EnvironmentManager.Instance.CreateDriverInstance(options);
  459         }
  460 
  461         private class PageLoadStrategyOptions : DriverOptions
  462         {
  463             public override void AddAdditionalOption(string capabilityName, object capabilityValue)
  464             {
  465             }
  466 
  467             public override ICapabilities ToCapabilities()
  468             {
  469                 return null;
  470             }
  471         }
  472     }
  473 }