This article covers common techniques to make web tests more robust: synchronizing with page load, setting values reliably, and reading values for reuse, and using the WebDocument adapter in code.
Synchronize with page loading (Wait for vs Delay)
Slow or variable loading is one of the most common causes of flaky web tests. Use Wait for to wait until a specific repository item exists before continuing.
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.
Enter values more robustly (Set value)
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.
Read values for reuse (Get value)
Use Get value to extract text/numbers from the page and store them in a variable, then reuse that variable later in the test.
Test situation
In this example, 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.
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.
WebDocument adapter (navigate, wait, run JavaScript)
The WebDocument adapter represents the website and provides useful methods such as:
Navigate to a URL
Wait until the document is loaded
Execute JavaScript
You can also access WebDocument methods via your repository (e.g., repo.WebPage.Self.Navigate(...) and WaitForDocumentLoaded()).
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("https://support.ranorex.com");
// Wait until the document is loaded
webDocument.WaitForDocumentLoaded();
// Execute a javascript code
webDocument.ExecuteScript("history.back();");
VB.NET
' Identify a web document by its title
Dim webDocument As WebDocument = "/dom[@caption='Ranorex Test Page']"
' Open a website
webDocument.Navigate("https://support.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 element (e.g.: ATag adapter for tags). Each adapter has specific methods and attributes; the link tag (), 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();
VB.NET
' 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()
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("https://support.ranorex.com");
// Wait until the document is loaded
repo.WebPage.Self.WaitForDocumentLoaded();
VB.NET
' Load repository
Dim repo As ProjectRepository = ProjectRepository.Instance
' Open a website
repo.WebPage.Self.Navigate("https://support.ranorex.com")
' Wait until the document is loaded
repo.WebPage.Self.WaitForDocumentLoaded()