Let's cover some advanced topics regarding web testing.
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.
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.
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 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.
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:
C#
// 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.
C#
// 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()
Repositories and the WebDocument
The following example shows how to access the methods of the WebDocument using a repository:
C#
// 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()