Skip to content

Utility classes

Bernd Weigel edited this page Jun 10, 2024 · 26 revisions

We added several utility classes in order to address problems that most test automation project face. We won't cover every function at this place but we want to bring the classes to your awareness in order to have a look if the needed functionality is already implemented. If you encounter other things that should be supported by Neodymium don't hesitate to get in contact with us.

SelenideAddons

The SelenideAddons class contains several methods that we think are missing in the Selenide API. We provide new Conditions or shortcuts to functions that are hard to use if you don't have deeper knowledge of Selenide.

Example 1

  // validate that the value attribute contains non digit characters using a regex
  $("#search-container .search-field").should(SelenideAddons.matchValue("placeholder", "\\D+"));

Example 2

  // use the following function to execute a whole code block that results in a StaleElementReferenceException from time to time 
  // it will result in an automatic retry in case of error
  SelenideAddons.$safe(() -> {
      $("selectorOne").find("selectorTwo").shouldBe(visible);
  });
  // or also with returning value
  SelenideElement someElement = SelenideAddons.$safe(() -> {
     return $("selectorOne").find("selectorTwo").shouldBe(visible);
  });

Example 3

Drag and drop webpage sliders until a given condition. Use the following function to execute linear 2d movements. The code example shows the basic function to execute a sider movement. The amount of retries and the time to wait between the movements can be set by the user.

  SelenideAddons.dragAndDropUntilCondition((SelenideElement) elementToMove, (SelenideElement) elementToCheck, (int) horizontalMovement, (int) verticalMovement, (int) pauseBetweenMovements, (int) retryMovements, (Condition) condition));

If needed the user can add more special functions based on the basic function. The code example shows a horizontal movement until a given text.

  private void leftHorizontalDragAndDropUntilText((SelenideElement) elementToMove, (SelenideElement) elementToCheck, (int) horizontalMovement, (String) sliderValueAttributeName, (String) moveUntil) 
  {
    SelenideAddons.dragAndDropUntilCondition(elementToMove, elementToCheck, horizontalMovement, 0, 3000, 10, Condition.attribute(sliderValueAttributeName, moveUntil));
  }

If there is no possible condition to validate the interaction or it is no needed for some reasons you can use the following function. E.g. unlocking something with a simple slide interaction.

  SelenideAddons.dragAndDrop((SelenideElement) elementToMove, (int) horizontalMovement, (int) verticalMovement);

Example 4

Sometimes it comes handy to open a HTML snippet within the current browser and perform interactions or validations upon it e.g. if you validate emails.

  String htmlSnippet = "<div dir=\"auto\">Hi<div dir=\"auto\"><br></div><div dir=\"auto\">How are you?)</div><div dir=\"auto\"><br></div><div dir=\"auto\">Bye</div></div>";
  SelenideAddons.openHtmlContentWithCurrentWebDriver(htmlSnippet);

Example 5 - optionalWaitConditions

Sometimes it is necessary to wait for an optional element matching a condition without throwing an exception if the condition didn't apply during the expected time. These functions will return false if the element does not match the given condition or can not be found in the given time.

optionalWaitUntilCondition

Waits until an optional element matches a condition without throwing an exception if the element does not exist.

java
  // wait until the optional condition matches
            boolean var = SelenideAddons.optionalWaitUntilCondition($(<selector>), <condition>, CUSTOM_MAX_WAITING_TIME);

optionalWaitWhileCondition

Waits while an optional element matches a condition without throwing an exception if the element does not exist.

java
 // wait until the optional condition does not match anymore
            boolean var = SelenideAddons.optionalWaitWhileCondition($(<selector>), <condition>);

AllureAddons

The AllureAddons class contains methods that help you to pass information of your choice to the report.

Example

  // wrap an action otherwise you would need to create an extra function and annotate it using @Step 
  AllureAddons.step("Need to see this description in the report", () -> {
    // Some actions
    $("#masthead .search-toggle").click();
  });

JavaScriptUtils

The JavaScriptUtils class contains methods that help you to validate the state/readiness of a webpage via JavaScript.

Example

  // This methods waits for three events when configured (jQuery is active, DOM is loaded, the loading animation is gone)
  // Especially waiting that the loading animation is gone prevent you from using timeout mechanisms and slowing down your test automation
  JavaScriptUtils.waitForReady();
  
  // Some actions that require a fully loaded website
  $("#masthead .search-toggle").click();

DataUtils

The DataUtils class contains methods to handle your test data easily.

We used the basic access method Neodymium.dataValue(key) to provide conversions for some basic types (boolean, double, float, int, long, String). We provide two methods for each type. The first one is asTYPE(String key) which raises an IllegalArgumentException if the data field can't be found. The second method is asTYPE(String key, TYPE default) and it returns the given default value if the data field can't be found. If you need to check if a certain key exists you can use exists(String key).

Furthermore, we provide a function that can instantiate POJO models via reflection. Please see the following example to understand how to use it. Please visit also our Test data provider wiki page for mor examples on this.

Example

Create a plain old java object (Pojo) that contains all fields you need.

// the date pojo you want to use
public class TestPojo
{
    private String field;

    private String otherField;

    public String getField()
    {
        return field;
    }

    public void setField(String field)
    {
        this.field = field;
    }

    public String getOtherField()
    {
        return otherField;
    }

    public void setOtherField(String otherField)
    {
        this.otherField = otherField;
    }
}

Setup your test data so that you have all values that you gonna need. BTW: If you don't need a field for a certain test case leave it out it will result in a null for this field.

[
  {
     "field":"value1", 
     "otherField":"otherValue2",
     "willIgnoreThis":"whenInstantiatingThePojo"
  }
]

Use the DataUtils.get method to instantiate the Pojo (Plain Old Java Object). This can also be done in a @Before annotated method to achieve a better code separation.

// the test class instantiating the pojo
@RunWith(NeodymiumRunner.class)
public class SomeTest
{
    @Test
    public void test()
    {
        //instantiate the pojo
        TestPojo pojo = DataUtils.get(TestPojo.class);
        //use the pojo
        doSomething(pojo);
    }
}

WebDriverUtils

The WebDriverUtils class contains functions that need to/can be referenced within project if you want to use Cucumber with Neodymium. Please find more on this topic and how to use it in our Cucumber documentation.

Clone this wiki locally