Validate Assertions

Learn how to use BELLATRIX Validate assertion methods.

Example

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

    button.Click();

    button.ValidateIsNotDisabled();

    var answerLabel = App.Components.CreateByName<Label>("Answer");

    answerLabel.ValidateIsVisible();

    var password = App.Components.CreateById<Password>("IntegerB");

    password.SetPassword("9");

    var textField = App.Components.CreateById<TextField>("IntegerA");

    textField.SetText("1");

    textField.ValidateTextIs("1");

    var checkBox = 
	App.Components.CreateByIOSNsPredicate<CheckBox>(
		"type == \"XCUIElementTypeSwitch\" AND name == \"All-day\"");

    checkBox.Check();

    checkBox.ValidateIsChecked();

    var radioButton = 
	App.Components.CreateByIOSNsPredicate<RadioButton>(
		"type == \"XCUIElementTypeSwitch\" AND name == \"All-day\"");

    radioButton.Click();

    radioButton.ValidateIsChecked(timeout: 30, sleepInterval: 2);

	Bellatrix.Assertions.Assert.Multiple(
               () => radioButton.ValidateIsChecked(timeout: 30, sleepInterval: 2),
               () => radioButton.ValidateIsChecked());
}

Explanations

button.ValidateIsNotDisabled();
//Assert.AreEqual(false, button.IsDisabled);

We can assert whether the control is disabled. The different BELLATRIX iOS elements classes contain lots of these properties which are a representation of the most important app element attributes. The biggest drawback of using vanilla assertions is that the messages displayed on failure are not meaningful at all. If the bellow assertion fails the following message is displayed: “*Message: Assert.AreEqual failed. Expected:. Actual:.*" You can guess what happened, but you do not have information which element failed and on which page. If we use the Validate extension methods, BELLATRIX waits some time for the condition to pass. After this period if it is not successful, a beatified meaningful exception message is displayed: "***The control should be disabled but it was NOT.***"

var checkBox = App.Components.CreateByIOSNsPredicate<CheckBox>(
					"type == \"XCUIElementTypeSwitch\" AND name == \"All-day\"");
checkBox.ValidateIsChecked();

Here we assert that the checkbox is checked. On fail the following message is displayed: “Message: Assert.IsTrue failed.” Cannot learn much about what happened. Now if we use the ValidateIsChecked method and the assertion does not succeed the following error message is displayed: “The control should be checked but was NOT.”

var answerLabel = App.Components.CreateByName<Label>("Answer");
answerLabel.ValidateIsVisible();

See if the element is present or not using the IsPresent property.

var textField = App.Components.CreateById<TextField>("IntegerA");
textField.ValidateTextIs("1");

Assert that the proper text is set.

var radioButton = App.Components.CreateByIOSNsPredicate<RadioButton>(
						"type == \"XCUIElementTypeSwitch\" AND name == \"All-day\"");
radioButton.ValidateIsChecked(timeout: 30, sleepInterval: 2);

By default, all Validate methods have 5 seconds timeout. However, you can specify a custom timeout and sleep interval (period for checking again).

Bellatrix.Assertions.Assert.Multiple(
               () => radioButton.ValidateIsChecked(timeout: 30, sleepInterval: 2),
               () => radioButton.ValidateIsChecked());

You can execute multiple validate assertions failing only once viewing all results.

Note: BELLATRIX provides you with a full BDD logging support for Validate assertions and gives you a way to hook your logic in multiple places.