Advanced web testing | Ranorex
Help CenterUser GuideWeb and mobile testing

Advanced web testing

In this chapter, we’ll cover some advanced topics regarding web testing.

In this chapter

    Use the Wait for action to wait until an element has loaded

    Loading times that take longer than usual are a common issue in web tests. They can cause tests to fail and can get quite annoying, as they are often outside of the tester’s control. One way to work around these loading times is by instructing the test to wait until a certain element has loaded. In Ranorex Studio, you can easily do so with the Wait for action.

    In the action table, add a Wait for action after an action that loads a web page and before an action that manipulates an element on the loaded page.

    Wait for action with 10s waiting time. The action waits until the linked repository item exists. This should be the repository item that is manipulated in the following action.

    The repository item that the action waits for to exist.

    Note icon

    Note

    Your test can still fail if loading takes longer than the time set in the Wait for action and the repository item doesn’t exist. Choose a sensible time value that prevents failure because of insignificant loading delays and doesn’t keep your test searching for an element for too long.
    Note icon

    Note

    Instead of the Wait for action, you can also use the Delay action to pause your test run.

    However, the Delay action always pauses the test run for the specified time, regardless of an element existing or not. This is why you should use the Wait for action if there is a repository item you can sensibly link it to. This way, your test won’t idle unnecessarily.

    Use the Invoke action to wait until entire pages have loaded (Make this a h2 again if we reuse it)

    The action Invoke action has a property called WaitForDocumentLoaded that can be linked to repository items representing web pages. This will cause Ranorex Studio to wait until the entire web page has loaded with a timeout as specified in the action’s properties.

    First you need to…

    Use the Set value action to enter values more robustly

    Entering values like text strings in web forms is a common scenario in web tests. This can either be accomplished by simulating the keypresses on a keyboard (Key sequence action) or by directly setting the form to a specific value. The latter is usually more robust because no mouse clicks or similar are required, so there is less potential for failure. Conversely, this can cause the test to miss certain defects because you deviate farther from a true user experience.

    Test situation

    In our example, we’ll enter a name in the sample form on the Ranorex test website. The process is as follows:

    Enter the name in the text field of the form.

    Click Submit to submit the name.

    The page that appears as a result.

    What it looks like in the action table

    Set value action that enters the name in the text field.

    Repository item Testname that represents the text field.

    Result

    The value is set directly in the form without any mouse clicks or typing.

    Reference

    For more details on the Set value action, refer to

    Ranorex Studio fundamentals > Actions > ⇢ Action properties.

    Use the Get value action to read out values for use in the test

    It’s often useful or necessary to read out values on websites (numbers, strings, etc.) and use them further along in the test. The simplest way to do this is with the Get value action.

    Test situation

    In our example, we’ll read out a list value to reuse it in the sample form on the Ranorex test website.

    With the default values selected, clicking the Submit button produces the following result:

    Color/testcolor is set to green.

    Colors/testmultiple is set to green and yellow.

    We’ll now

    • get the value blue from the testmultiple parameter and
    • set the testcolor parameter to this read-out value.

    What it looks like in the action table

    The Get value action reads out the value blue from the Colors field and passes it to the variable $Color.

    The Set value action uses the value of the variable $Color and sets the field Color/testcolor to it.

    Result

    The resulting page will look like this:

    The field testcolor now has the value blue, as set by the Set value action.

    The testmultiple field is still set to the default values. The Get value action only reads out the value blue.

    Reference

    For more details on the Get value action, refer to

    Ranorex Studio fundamentals > Actions > ⇢ Action properties.

    WebDocument Adapter

    The WebDocument Adapter creates a representation of the complete website including all tags (e.g. the header tag, body tag, etc.). Furthermore it offers useful ways to make your test scripts more effective.

    The following sample shows how to use these features:

    // Identify a web document by its title WebDocument webDocument = "/dom[@caption='Ranorex Test Page']"; // Open a website webDocument.Navigate("http://www.ranorex.com"); // Wait until the document is loaded webDocument.WaitForDocumentLoaded(); // Execute a javascript code webDocument.ExecuteScript("history.back();");
    ' Identify a web document by its title Dim webDocument As WebDocument = "/dom[@caption='Ranorex Test Page']" ' Open a website webDocument.Navigate("http://www.ranorex.com") ' Wait until the document is loaded webDocument.WaitForDocumentLoaded() ' Execute a javascript code webDocument.ExecuteScript("history.back();")

    Find or filter web elements

    The Ranorex Framework offers a wide range of adapters for each HTML tag elements (e.g.: ATag adapter for <a> tags). Each adapter has specific methods and attributes; the link tag (<a>) for example has additional attributes like HREF, TARGET and REL.

    // Start IE with a specific website System.Diagnostics.Process.Start("iexplore.exe", "/web-testing-examples"); // Identify the webdocument by its title WebDocument webDocument = "/dom[@caption='Ranorex Test Page']"; // Find a link by its link text (innertext) ATag link = webDocument.FindSingle(".//a[@innertext='simple link']"); link.Click();
    ' Start IE with a specific website System.Diagnostics.Process.Start("iexplore.exe", "/web-testing-examples") ' Identify the webdocument by its title Dim webDocument As WebDocument = "/dom[@caption='Ranorex Test Page']" ' Find a link by its link text (innertext) Dim link As ATag = webDocument.FindSingle(".//a[@innertext='simple link']") link.Click()

    Repositories and the WebDocument

    The following example shows how to access the methods of the WebDocument using a repository:

    // Load repository ProjectRepository repo = ProjectRepository.Instance; // Open a website repo.WebPage.Self.Navigate("http://www.ranorex.com"); // Wait until the document is loaded repo.WebPage.Self.WaitForDocumentLoaded();
    ' Load repository Dim repo As ProjectRepository = ProjectRepository.Instance ' Open a website repo.WebPage.Self.Navigate("http://www.ranorex.com") ' Wait until the document is loaded repo.WebPage.Self.WaitForDocumentLoaded()