Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom condition and collection condition for validation #2300

Closed
amuthansakthivel opened this issue Dec 1, 2022 · 4 comments
Closed
Assignees
Milestone

Comments

@amuthansakthivel
Copy link
Contributor

amuthansakthivel commented Dec 1, 2022

The problem

In case of appium, some of the CollectionCondition we have might not work.

For example, shouldHave(exactTexts(String... strings)

In case of android, the attribute can be text or content-desc but in case of ios it can be label or name attribute. So the test will fail as it cannot fetch text attribute.

In my work, I have created my own custom condition. But I think if we add this in SelenideAppium it might help everyone.

exactAttribute(String androidAttribute, String iosAttribute, String toVerify);

exactAttribute(Function<Element, String> androidFunction, 
               Function<Element, String> iosFunction,  
               String toVerify);

Usage:

AppiumCollectionCondition.exactAttribute("content-desc", "label", "value123");

or using lambdas:

AppiumCollectionCondition.exactAttribute(element->element.getText() , element->element.getAttribute("name"), "value123");

This is my custom implementation to give some idea

public class ContainsTextsInAnyOrder extends ExactTexts {

  ContainsTextsInAnyOrder(String... expectedTexts) {
    super(expectedTexts);
  }

  ContainsTextsInAnyOrder(List<String> expectedTexts) {
    super(expectedTexts);
  }

  @Override
  public boolean test(List<WebElement> elements) {

    List<String> elementsTexts = getActualStringsToCompare(elements);
    Iterator<String> expectedTexts = this.expectedTexts.iterator();

    boolean found;
    do {
      if (!expectedTexts.hasNext()) {
        return true;
      }

      String expectedText = expectedTexts.next();
      found = false;

      for (String elementText : elementsTexts) {
        if (Html.text.contains(elementText, expectedText)) {
          found = true;
        }
      }
    } while (found);

    return false;
  }

  private List<String> getActualStringsToCompare(List<WebElement> elements) {
    return elements.stream().map(getFunction()).collect(Collectors.toList());
  }

  private Function<WebElement, String> getFunction() {
    Map<Boolean, Function<WebElement, String>> map = new HashMap<>();
    map.put(true, WebElement::getText);
    map.put(false, element -> element.getAttribute("name"));
    return map.get(AppiumUtils.isAndroid());
  }
}
@amuthansakthivel amuthansakthivel changed the title Add custom collection condition for validation Add custom condition and collection condition for validation Dec 31, 2022
@asolntsev
Copy link
Member

@amuthansakthivel I started thinking on your suggestion. In the essence, it's very useful, we definitely need to make collection conditions work with Appium.

But I think we don't really need to re-implement conditions. Instead, we should only make function "get element text" overridable (injected via plugin system). Then we could override this function in selenide-appium, and all Selenide built-in conditions can still be reused.

What do you think?

@amuthansakthivel
Copy link
Contributor Author

@asolntsev - Seems like a more reliable solution with fewer efforts. Could you please provide few pointers where I could get some example to implement this

@asolntsev
Copy link
Member

Let me review Selenide code to understand how many different places we would need to extract to a plugin...

@asolntsev asolntsev transferred this issue from selenide/selenide-appium May 23, 2023
@asolntsev asolntsev self-assigned this May 23, 2023
@asolntsev
Copy link
Member

Implemented in selenide/selenide-appium#135 and #2315

@asolntsev asolntsev added this to the 6.15.0 milestone May 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants