Identifying Web Objects

Applies to TestLeft 15.40, last modified on March 17, 2022
Search Patterns

To search for objects in web applications, you use various search patterns.

.NET: The patterns belong to the SmartBear.TestLeft.TestObjects.Web namespace.

Java: The patterns belong to the com.smartbear.testleft.testobjects package.

Available patterns and their properties are described below.

Notes:

WebBrowserPattern

Generally, you should use the RunBrowser method to start the web browser and obtain the object that corresponds to the launched browser instance. However, if you want to use an already running browser, you can use the find<T> method and specify the desired browser using the WebBrowserPattern pattern.

Property Type Description
ObjectIdentifier String The browser’s process name without the .exe extension: chrome, firefox, iexplore, or edge.
BrowserIndex Integer When several instances of the same browser are running, this is the index of the desired instance starting from 1.
Example

Search for the running instance of Chrome and navigate to the specified URL:

C#

IWebBrowser browser = Driver.Find<IWebBrowser>(new WebBrowserPattern()
{
   ObjectIdentifier = "chrome"
});
browser.ToUrl("https://smartbear.com");

Visual Basic .NET

Dim browser As IWebBrowser = Driver.Find(Of IWebBrowser)(New WebBrowserPattern() With {
   .ObjectIdentifier = "chrome"
})
browser.ToUrl("https://smartbear.com")

Java

WebBrowser browser = driver.find(WebBrowser.class, new WebBrowserPattern() {{
   ObjectIdentifier = "chrome";
}});
browser.toUrl("https://smartbear.com");

WebPagePattern

Use this pattern to search for web pages opened in a browser.

Property Type Description
URL String The web page URL, including the protocol (such as http:// or https://), and, possibly, a trailing slash. The URL may differ from what you see in the browser’s address bar, since some browsers hide the http:// part and the trailing slash by default. Use TestLeft UI Spy to find the correct value, or copy the URL from the browser’s address bar (it copies a complete URL).
ObjectGroupIndex Integer The page’s index (0-based) among other instances of the same page opened on other tabs of the browser.
Visible Boolean True if the page is in an active browser tab, False if it is in an inactive tab.
Example

Open https://smartbear.com in Chrome and get the page object:

C#

IWebBrowser browser = Driver.Applications.RunBrowser(BrowserType.Chrome, "https://smartbear.com");
IWebPage page = browser.Find<IWebPage>(new WebPagePattern()
{
   URL = "*smartbear.com*" // wildcards to account for possible redirections from the initial URL
});

Visual Basic .NET

Dim browser As IWebBrowser = Driver.Applications.RunBrowser(BrowserType.Chrome, "https://smartbear.com")
Dim page As IWebPage = browser.Find(Of IWebPage)(New WebPagePattern() With {
   .URL = "*smartbear.com*" ' wildcards to account for possible redirections from the initial URL
})

Java

WebBrowser browser = driver.getApplications().runBrowser(BrowserType.Chrome, "https://smartbear.com");
WebPage page = browser.find(WebPage.class, new WebPagePattern() {{
   URL = "*smartbear.com*"; // wildcards to account for possible redirections from the initial URL
}});

WebElementPattern

Use this pattern to search for individual elements on web pages.

Property Type Description
contentText String The combined text contents (not HTML markup) of the object and its child objects. Use this property instead of browser-specific innerText and textContent properties.
Enabled Boolean Specifies whether the object is enabled or disabled. Disabled objects are usually shown grayed out.
idStr String The value of the object’s id attribute with special characters (hyphens "-", colons ":" and dots ".") replaced by underscores ("_").
ObjectIdentifier String or
Integer

The identifier of web objects is generated as follows:

  • For Button (<button> and <input type="button">), ResetButton (<input type="reset">) and SubmitButton (<input type="submit">) – the value attribute.

  • For Image objects (<img>) – the last component of the src attribute value (usually the file name).

  • For Area objects (<area>) – the title attribute value.

For other web elements, ObjectIdentifier is derived from the object’s id or name attribute (in that order), excluding the dynamic parts of the value. For example, ctl00_ctl32_g_119bf8b1_toolBarTbl becomes toolBarTbl, and autogenerated IDs like ui-id-23 are ignored.

If several objects have the same type and identifier, they have indexes appended to their ID, for example, my_link, my_link_2, my_link_3.

If the above procedure results in an empty ID, ObjectIdentifier is the object’s 0-based index among sibling objects of the same type.

ObjectLabel String

The object’s caption or label:

  • For buttons (<button> and <input type="button/reset/submit">) – the button text.
  • For Fieldset objects – The text content of the child Legend object.
  • For DevExpress ASP.NET, jQuery UI, Infragistics, Sencha Ext JS, Telerik ASP.NET and YUI 2 objects – the value of the native property that specifies the object’s label or placeholder text.
  • For text boxes, check boxes, radio buttons, combo boxes, and date pickers – the text content of the associated label element.

Not all objects have this property.

ObjectType String The element’s role on the web page: Link, Form, CheckBox, and so on.
readOnly Boolean true if the element is read-only, false otherwise. Read-only elements are not editable, though they can receive the focus (unlike disabled elements). This value corresponds to the readonly HTML attribute.
Visible Boolean Specifies whether the object is visible in the application. Objects that are out of screen bounds or overlapped by other objects are still considered visible.
alt, href, role, title, and other properties   The value of the corresponding HTML attribute.

To search for custom web elements implemented with the Web Components specifications, you can add the CustomObjectType property to WebElementPattern (see Using Custom Properties in Search Patterns). The property returns a string that is based on the name under which a custom element is registered in your web application, with dashes removed and parts of the names capitalized (for example, x-element becomes XElement). This name is used to name custom objects in TestLeft UI Spy.

Example

Get the search box on Google:

C#

ITextEdit searchBox = pageGoogle.Find<ITextEdit>(new WebElementPattern()
{
   ObjectType = "Textbox",
   idStr = "lst-ib"
}, 15);
searchBox.Keys("TestLeft[Enter]");

Visual Basic .NET

Dim searchBox As ITextEdit = pageGoogle.Find(Of ITextEdit)(New WebElementPattern() With {
   .ObjectType = "Textbox",
   .idStr = "lst-ib"
}, 15)
searchBox.Keys("TestLeft[Enter]")

Java

TextEdit searchBox = pageGoogle.find(TextEdit.class, new WebElementPattern() {{
   ObjectType = "Textbox";
   idStr = "lst-ib";
}}, 15);
searchBox.keys("TestLeft[Enter]");

Note: In addition to WebElementPattern (property-value pairs), you can search for web objects by XPath and CSS selectors. To do this, use FindChildByXPath<T> and QuerySelector<T> instead of Find<T>.

WebCellElementPattern

Use this pattern to search for web table cells (TR and TH elements). It allows you to identify cells by the row and column indexes among other properties.

Property Type Description
ColumnIndex Integer The cell’s column index, 0-based.
contentText String The combined text contents (not HTML markup) of the cell and its child objects. Use this property instead of browser-specific innerText and textContent properties.
Enabled Boolean Specifies whether the object is enabled or disabled. Disabled objects are usually shown grayed out.
idStr String The value of the object’s id attribute, with special characters (hyphens "-", colons ":" and dots ".") replaced by underscores ("_").
RowIndex Integer The cell’s row index, 0-based.
Visible Boolean Specifies whether the object is visible in the application. Objects that are out of screen bounds or overlapped by other objects are still considered visible.
Example

Get cell in row 2, column 0:

C#

IWebCell cell = table.Find<IWebCell>(new WebCellElementPattern()
{
   RowIndex = 2,
   ColumnIndex = 0
});
Assert.AreEqual(cell.contentText, "Oranges");

Visual Basic .NET

Dim cell As IWebCell = table.Find(Of IWebCell)(New WebCellElementPattern() With {
   .RowIndex = 2,
   .ColumnIndex = 0
})
Assert.AreEqual(cell.contentText, "Oranges")

Java

WebCell cell = table.find(WebCell.class, new WebCellElementPattern() {{
   RowIndex = 2;
   ColumnIndex = 0;
}});
Assert.assertEquals(cell.contentText, "Oranges");

WebBrowserUIPattern

This pattern is used to identify some browser UI elements in Internet Explorer and Firefox, such as tab buttons, and custom toolbars and panels added by Firefox addons.

Property Type Description
Enabled Boolean Specifies whether the object is enabled or disabled. Disabled objects are usually shown grayed out.
ObjectIdentifier String or
Integer
The object identifier.
ObjectType String The object type.
Visible Boolean Specifies whether the object is visible in the application. Objects that are out of screen bounds or overlapped by other objects are still considered visible.
Example

Click the Bookmark button (star) on the Firefox toolbar:

C#

IControl bookmarkButton = Driver.Find<IWebBrowser>(new WebBrowserPattern()
{
   ObjectIdentifier = "firefox"
}).Find<IWebPage>(new WebPagePattern()
{
   URL = "chrome://browser/content/browser.xul"
}).Find<IControl>(new WebBrowserUIPattern()
{
   ObjectType = "toolbar",
   ObjectIdentifier = "nav_bar"
}).Find<IControl>(new WebBrowserUIPattern()
{
   ObjectType = "toolbarbutton",
   ObjectIdentifier = "bookmarks_menu_button"
}, 2);

bookmarkButton.Click();

Visual Basic .NET

Dim bookmarkButton As IControl = Driver.Find(Of IWebBrowser)(New WebBrowserPattern() With {
   .ObjectIdentifier = "firefox"
}).Find(Of IWebPage)(New WebPagePattern() With {
   .URL = "chrome://browser/content/browser.xul"
}).Find(Of IControl)(New WebBrowserUIPattern() With {
   .ObjectType = "toolbar",
   .ObjectIdentifier = "nav_bar"
}).Find(Of IControl)(New WebBrowserUIPattern() With {
   .ObjectType = "toolbarbutton",
   .ObjectIdentifier = "bookmarks_menu_button"
}, 2)

bookmarkButton.Click()

Java

Control bookmarkButton = driver.find(WebBrowser.class, new WebBrowserPattern() {{
   ObjectIdentifier = "firefox";
}}).find(WebPage.class, new WebPagePattern() {{
   URL = "chrome://browser/content/browser.xul";
}}).find(Control.class, new WebBrowserUIPattern() {{
   ObjectType = "toolbar";
   ObjectIdentifier = "nav_bar";
}}).find(Control.class, new WebBrowserUIPattern() {{
   ObjectType = "toolbarbutton";
   ObjectIdentifier = "bookmarks_menu_button";
}}, 2);

bookmarkButton.click();

See Also

Understanding Object Identification
Preparing Web Applications for Testing

Highlight search results