Samples

The following web testing examples show how to access typical web elements of a web page.

The Ranorex web site provides a small page especially for web testing purposes:

www.ranorex.com/web-testing-examples/

All examples are available in the Web Test Sample Ranorex Studio and Visual Studio project located within your installation folder (..\Samples\WebTestSample)

Handling of AJAX

C#

GlobalRepository repo = GlobalRepository.Instance;
WebDocument webDocument = repo.WebPage.Self;
// Fill out the AJAX form
InputTag input1 = webDocument.FindSingle(".//form[@id='ajax-form']/fieldset//input[@name='value1']");
input1.EnsureVisible();
input1.Value = "Size";
InputTag input2 = webDocument.FindSingle(".//form[@id='ajax-form']/fieldset//input[@name='value2']");
input2.Value = "Weight";
InputTag checkbox = webDocument.FindSingle(".//form[@id='ajax-form']/fieldset//input[@name='checkbox2']");
checkbox.Checked = "true";
SelectTag selectColor = webDocument.FindSingle(".//form[@id='ajax-form']/fieldset//select[@name='color2']");
selectColor.TagValue = "blue";
// Submit data
InputTag submit = webDocument.FindSingle(".//form[@id='ajax-form']//input[@id='submit-ajax']");
submit.Click();

// Wait for the ajax request for max 10 seconds (10000 milliseconds)
PreTag result = webDocument.FindSingle(".//div[@id='ajax_response']/div/pre", 10000);

VB.NET

Dim repo As GlobalRepository = GlobalRepository.Instance
Dim webDocument As WebDocument = repo.WebPage.Self
' Fill out the AJAX form
Dim input1 As InputTag = webDocument.FindSingle(".//form[@id='ajax-form']/fieldset//input[@name='value1']")
input1.EnsureVisible()
input1.Value = "Size"
Dim input2 As InputTag = webDocument.FindSingle(".//form[@id='ajax-form']/fieldset//input[@name='value2']")
input2.Value = "Weight"
Dim checkbox As InputTag = webDocument.FindSingle(".//form[@id='ajax-form']/fieldset//input[@name='checkbox2']")
checkbox.Checked = "true"
Dim selectColor As SelectTag = webDocument.FindSingle(".//form[@id='ajax-form']/fieldset//select[@name='color2']")
selectColor.TagValue = "blue"
' Submit data
Dim submit As InputTag = webDocument.FindSingle(".//form[@id='ajax-form']//input[@id='submit-ajax']")
submit.Click()

' Wait for the ajax request for max 10 seconds (10000 milliseconds)
Dim result As PreTag = webDocument.FindSingle(".//div[@id='ajax_response']/div/pre", 10000)

List elements of a table and set css style

C#

GlobalRepository repo = GlobalRepository.Instance;
WebDocument webDocument = repo.WebPage.Self;

// List all elements of a table
foreach (TrTag row in repo.WebPage.DivTagContent.TableTagSimpletable.Find("./tbody/tr"))
{
    string rowInfo = "";

    TdTag rowNameCell = row.FindSingle("./td[2]");
    rowInfo += "Row index: " + rowNameCell.PreviousSibling.InnerText + ", ";
    rowInfo += "Row name: " + rowNameCell.InnerText + ", ";
    rowInfo += "Row value: " + rowNameCell.NextSibling.InnerText + ", ";

    // Get all cells from the row
    rowInfo += "All Cells: ";
    foreach (TdTag cell in row.Find("./td"))
    {
        rowInfo += cell.InnerText + ", ";
        // Move the mouse to each cell element
        cell.MoveTo();
        // Set css style
        cell.SetStyle("background-color","#33ff00");
    }

    Report.Info(rowInfo);
}

VB.NET

Dim repo As GlobalRepository = GlobalRepository.Instance
Dim webDocument As WebDocument = repo.WebPage.Self

' List all elements of a table
For Each row As TrTag In repo.WebPage.DivTagContent.TableTagSimpletable.Find("./tbody/tr")
	Dim rowInfo As String = ""

	Dim rowNameCell As TdTag = row.FindSingle("./td[2]")
	rowInfo += "Row index: " & rowNameCell.PreviousSibling.InnerText & ", "
	rowInfo += "Row name: " & rowNameCell.InnerText & ", "
	rowInfo += "Row value: " & rowNameCell.NextSibling.InnerText & ", "

	' Get all cells from the row
	rowInfo += "All Cells: "
	For Each cell As TdTag In row.Find("./td")
		rowInfo += cell.InnerText & ", "
		' Move the mouse to each cell element
		cell.MoveTo()
		' Set css style
		cell.SetStyle("background-color", "#33ff00")
	Next

	Report.Info(rowInfo)
Next

Set inputs, tag attributes and perform a click without mouse

C#

// Use mouse and keyboard to set Name
repo.WebPage.TestForm.InputTagTestname.Click();
Keyboard.Press("Test Name");
// Set email address directly via 'Value'
repo.WebPage.TestForm.InputTagTestemail.Value = "test@ranorex.com";
// Open calendar form
repo.WebPage.TestForm.ButtonTagCalendar.Click();
// Select the 22th of the current month
repo.WebPage.TdTag_22nd.Click();

// Select each item of list box
foreach (OptionTag option in repo.WebPage.TestForm.SelectTagTestmultiple.Find(".//option"))
{
    option["selected"] = "selected";
}

// Perform a click without moving the mouse to the button
repo.WebPage.TestForm.InputTagSubmit.PerformClick();

VB.NET

' Use mouse and keyboard to set Name
repo.WebPage.TestForm.InputTagTestname.Click()
Keyboard.Press("Test Name")
' Set email address directly via 'Value'
repo.WebPage.TestForm.InputTagTestemail.Value = "test@ranorex.com"
' Open calendar form
repo.WebPage.TestForm.ButtonTagCalendar.Click()
' Select the 22th of the current month
repo.WebPage.TdTag_22nd.Click()

' Select each item of list box
For Each [option] As OptionTag In repo.WebPage.TestForm.SelectTagTestmultiple.Find(".//option")
	[option]("selected") = "selected"
Next

' Perform a click without moving the mouse to the button
repo.WebPage.TestForm.InputTagSubmit.PerformClick()

Execute javascript code

C#

webDocument.ExecuteScript("history.back();");

VB.NET

webDocument.ExecuteScript("history.back();")

Handling of layer menus

C#

WebDocument webDocument = "/dom[@caption='Ranorex Test Page']";

DivTag topMenuDiv = webDocument.FindSingle(".//div[@id='top-menu']");
// Bring the main menu to the front
topMenuDiv.EnsureVisible();
// Automating a dropdown menu
foreach (LiTag item in topMenuDiv.Find(".//li[@visible='true']"))
{
    Mouse.MoveTo(item);
    // Move the mouse to each submenu item
    foreach (LiTag subitem in item.Find(".//li[@visible='true']"))
        Mouse.MoveTo(subitem);
}

VB.NET

Dim webDocument As WebDocument = "/dom[@caption='Ranorex Test Page']"

Dim topMenuDiv As DivTag = webDocument.FindSingle(".//div[@id='top-menu']")
' Bring the main menu to the front
topMenuDiv.EnsureVisible()
' Automating a dropdown menu
For Each item As LiTag In topMenuDiv.Find(".//li[@visible='true']")
	Mouse.MoveTo(item)
	' Move the mouse to each submenu item
	For Each subitem As LiTag In item.Find(".//li[@visible='true']")
		Mouse.MoveTo(subitem)
	Next
Next