Test object description examples

The examples below show some simple test code with programmatic descriptions for Web and Windows objects.

C#

This section includes C# examples for a Web application and a Windows application.

Web Link Object

This example initializes testBrowser as a Chrome browser and navigates to an imaginary Sample web site that sells computer equipment.

It then creates a keyboardLink test object that will be recognized within the launched testBrowser.

IBrowser testBrowser = BrowserFactory.Launch(BrowserType.Chrome);
testBrowser.Navigate("http://www.sample.com");
var keyboardLink = testBrowser.Describe<ILink>(new LinkDescription { TagName = "A", InnerText = "Computer Keyboards" });

Standard Windows Calculator Objects

This example creates the calcApp Window test object.

Inside the calcApp hierarchy, it creates a Button test object to represent the number 1 button in the calculator, and a Static test object to represent the main calculator display box.

var calcApp = Desktop.Describe<IWindow>(new WindowDescription { WindowClassRegExp = "CalcFrame", WindowTitleRegExp = "Calculator" });
var calcButton1 = calcApp.Describe<IButton>(new ButtonDescription { WindowId = 131, NativeClass = "Button" });
var calcDisplay = calcApp.Describe<IStatic>(new StaticDescription { WindowId = 150, NativeClass = "Static" });

Back to top

Java

This section includes Java examples for a Web application and a Windows application.

Web Link Object

This example initializes Browser as a Chrome browser and navigates to the AdvantageDEMO web site.

It then creates a link test object to represent the Speakers link on the web page.

Browser browser = BrowserFactory.launch(BrowserType.CHROME);
browser.navigate("http://advantageonlineshopping.com/#/");
browser.describe(Link.class, new LinkDescription.Builder()
.innerText("SPEAKERS Shop Now ").tagName("DIV").build());

Standard Windows Notepad Objects

This example creates the notepadWindow test object.

Inside the notepadWindow hierarchy, it creates a Menu test object to represent the main NotePad menu.

Inside the notepadMenu hierarchy, it uses the buildMenuPath method to create a test object for the Format > Font sub-menu command and to perform the select operation on that object.

Finally, it creates a notepadFontDialog object as a child of the main notepadWindow and the fontsComboBox object as a child of the dialog box.

 
Window notepadWindow = Desktop.describe(Window.class, new  WindowDescription.Builder().windowClassRegExp("Notepad").windowTitleRegExp(" Notepad").build());        
Menu notepadMenu = notepadWindow.describe(Menu.class, new  MenuDescription(MenuType.MENU));

// Build the "Format"-->"Font" menu item
String path = notepadMenu.buildMenuPath("Format", 2);

// Open the "Font" dialog by selecting "Format"-->"Font" in the menu 
MenuItem menuItem = notepadMenu.getItem(path);
notepadMenu.select(menuItem); 

Dialog notepadFontDialog = notepadWindow.describe(Dialog.class, new  DialogDescription.Builder().windowTitleRegExp("Font").build());
ComboBox fontsComboBox = notepadFontDialog.describe(ComboBox.class, new  ComboBoxDescription.Builder().attachedText("&Font:").nativeClass("ComboBox").build());

Back to top

JavaScript

This section includes a JavaScript examples for a Web application.

Web List Object

This example navigates to a Web site in an already initialized and launched browser.

It creates the list object as a Web list box child object.  After selecting an additional item in the list, it verifies information about the state of the list.  Then it deselects the item and again verifies the new state of the list.

	    browser.navigate("http://mySite.com");

        var list = browser.$(new Web.ListBox({
            name: "what-to-wear"
        }));

        list.select("Cape", 4);

        list.selectedItems()
            .then(function (items) {
                expect.equal(items.length, 2);
                expect.strictEqual(items[0], "Socks");
                expect.strictEqual(items[1], "Cape");
            });
 
        list.deselect(4, "Cape");
 
        list.selectedItems()
            .then(function (items) {
                expect.equal(items.length, 1);
            })

Back to top

See also: