Locate Elements

Learn how to locate desktop elements with BELLATRIX desktop module.

Example

[Test]
public void MessageChanged_When_ButtonHovered_Wpf()
{
    var button = App.Components.CreateByName<Button>("E Button");

    button.Hover();

    Console.WriteLine(button.By.Value);

    Console.WriteLine(button.WrappedComponent.Coordinates);
}

Explanations

var button = App.Components.CreateByName<Button>("E Button");

There are different ways to locate elements in the app. To do it you use the element create service. You need to know that BELLATRIX has a built-in complex mechanism for waiting for elements, so you do not need to worry about this anymore. Keep in mind that when you use the Create methods, the element is not searched. All elements use lazy loading.Which means that they are searched once you perform an action or assertion on them. By default on each new action, the element is searched again and be refreshed.

Console.WriteLine(button.By.Value);

Because of the proxy element mechanism (we have a separate type of element instead of single WebDriver IWebElement interface) we have several benefits. Each control (element type- ComboBox, TextField and so on) contains only the actions you can do with it, and the methods are named properly. In vanilla WebDriver to type the text you call SendKeys method. Also, we have some additional properties in the proxy web control such as- By. Now you can get the locator with which you element was found.

Console.WriteLine(button.WrappedComponent.Coordinates);

You can access the WebDriver wrapped element through WrappedElement and the current WebDriver instance through- WrappedDriver

Available Create Methods

BELLATRIX extends the vanilla WebDriver selectors and give you additional ones.

CreateByTag

App.Components.CreateByTag<Button>("button");

Searches the element by its tag.

CreateById

App.Components.CreateById<Button>("myId");

Searches the element by its ID.

CreateByXpath

App.Components.CreateByXpath<Button>("//*[@title='Add to cart']");

Searches the element by XPath locator.

CreateByClass

App.Components.CreateByClassContaining<Button>("ul.products");

Searches the element by its CSS classes.

CreateByName

App.Components.CreateByName<Button>("products");

Searches the element by its name.

CreateByAccessibilityId

App.Components.CreateByAccessibilityId<Button>("myCustomButton");

Searches the element by its accessibility ID.

CreateByAutomationId

App.Components.CreateByAutomationId<Search>("search");

Searches the element by its automation ID.

Find Multiple Elements

Sometimes we need to find more than one component. For example, in this test we want to locate all Add to Cart buttons. To do it you can use the element create service CreateAll method.

[Test]
public void CheckAllAddToCartButtons()
{
    App.Navigation.Navigate("http://demos.bellatrix.solutions/");

    var blogLink = App.Components.CreateAllByXpath<Anchor>("//*[@title='Add to cart']");
}

Available CreateAll Methods

CreateAllByTag

App.Components.CreateAllByTag<Button>("button");

Searches the elements by its tag.

CreateAllById

App.Components.CreateAllById<Button>("myId");

Searches the elements by its ID.

CreateAllByXpath

App.Components.CreateAllByXpath<Button>("//*[@title='Add to cart']");

Searches the elements by XPath locator.

CreateAllByClass

App.Components.CreateAllByClassContaining<Button>("ul.products");

Searches the elements by its CSS classes.

CreateAllByName

App.Components.CreateAllByName<Button>("products");

Searches the elements by its name.

CreateAllByAccessibilityId

App.Components.CreateAllByAccessibilityId<Button>("myCustomButton");

Searches the elements by its accessibility ID.

CreateAllByAutomationId

App.Components.CreateAllByAutomationId<Search>("search");

Searches the elements by its automation ID.

Find Nested Elements

Sometimes it is easier to locate one element and then find the next one that you need, inside it. For example in this test the list box is located and then the button inside it.

public void ReturnNestedElement_When_ElementContainsOneChildElement_Wpf()
{
    var comboBox = App.Components.CreateByAutomationId<ComboBox>("listBoxEnabled");
    var comboBoxItem = comboBox.CreateByAutomationId<Button>("lb2");

    comboBoxItem.Hover();
}

Note: It is entirely legal to create a Button instead of TextField. BELLATRIX library does not care about the real type of the elements. The proxy types are convenience wrappers so to say. Meaning they give you a better interface of predefined properties and methods to make your tests more readable.

Available Create Methods for Finding Nested Elements

CreateByTag

component.CreateByTag<Button>("button");

Searches the element by its tag.

CreateById

component.CreateById<Button>("myId");

Searches the element by its ID.

CreateByXpath

component.CreateByXpath<Button>("//*[@title='Add to cart']");

Searches the element by XPath locator.

CreateByClass

component.CreateByClassContaining<Button>("ul.products");

Searches the element by its CSS classes.

CreateByName

component.CreateByName<Button>("products");

Searches the element by its name.

CreateByAccessibilityId

component.CreateByAccessibilityId<Button>("myCustomButton");

Searches the element by its accessibility ID.

CreateByAutomationId

component.CreateByAutomationId<Search>("search");

Searches the element by its automation ID.

CreateAllByTag

component.CreateAllByTag<Button>("button");

Searches the elements by its tag.

CreateAllById

component.CreateAllById<Button>("myId");

Searches the elements by its ID.

CreateAllByXpath

component.CreateAllByXpath<Button>("//*[@title='Add to cart']");

Searches the elements by XPath locator.

CreateAllByClass

component.CreateAllByClassContaining<Button>("ul.products");

Searches the elements by their classes.

CreateAllByName

component.CreateAllByName<Button>("products");

Searches the elements by its name.

CreateAllByAccessibilityId

component.CreateAllByAccessibilityId<Button>("myCustomButton");

Searches the elements by its accessibility ID.

CreateAllByAutomationId

component.CreateAllByAutomationId<Search>("search");

Searches the elements by its automation ID.