How to use ExpectedCondition class of io.appium.java_client.functions package

Best io.appium code snippet using io.appium.java_client.functions.ExpectedCondition

Run io.appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

WaitUtils.java

Source: WaitUtils.java Github

copy
1package tests.mobiletests.utils;
2
3import io.appium.java_client.AppiumDriver;
4import io.appium.java_client.MobileElement;
5import io.appium.java_client.functions.ExpectedCondition;
6import org.openqa.selenium.support.ui.ExpectedConditions;
7import org.openqa.selenium.support.ui.WebDriverWait;
8
9public class WaitUtils {
10  public WebDriverWait wait;
11  public AppiumDriver driver;
12  public IRunner runner;
13
14  public WaitUtils(AppiumDriver driver) {
15    this.runner = RunnerFactory.runner;
16    this.driver = driver;
17    wait = new WebDriverWait(driver, 30);
18  }
19
20  public MobileElement waitForElement(MobileElement id) {
21    wait.until(ExpectedConditions.elementToBeClickable(id));
22    return id;
23  }
24
25  public MobileElement waitForElementToBeVisible(MobileElement id) {
26    wait.until(ExpectedConditions.visibilityOf(id));
27    return id;
28  }
29
30  public MobileElement refreshAndwaitForElementToBeVisible(MobileElement id) {
31    wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOf(id)));
32    return id;
33  }
34
35  public MobileElement waitForTextToAppear(MobileElement id) {
36    wait.until((ExpectedCondition<Boolean>) d -> id.getText().length() != 0);
37    return id;
38  }
39
40  public MobileElement waitForSpecificTextToBeDisplayed(MobileElement id, String text) {
41    wait.until(
42        ExpectedConditions.refreshed(
43            (ExpectedCondition<Boolean>) d -> id.getText().contains(text)));
44    return id;
45  }
46
47  public MobileElement waitForElementToBeEnabled(MobileElement id) {
48    wait.until(ExpectedConditions.elementToBeClickable(id));
49    return id;
50  }
51
52  public boolean isElementPresent(MobileElement id) {
53    try {
54      wait.until(ExpectedConditions.visibilityOf(id));
55      return true;
56    } catch (Exception e) {
57      return false;
58    }
59  }
60}
61
Full Screen

BaseMobileElement.java

Source: BaseMobileElement.java Github

copy
1package com.testvagrant.ekam.atoms.mobile;
2
3import com.testvagrant.ekam.atoms.MultiPlatformFinder;
4import io.appium.java_client.AppiumDriver;
5import io.appium.java_client.MobileElement;
6import io.appium.java_client.TouchAction;
7import io.appium.java_client.ios.IOSDriver;
8import org.awaitility.Awaitility;
9import org.awaitility.core.ConditionFactory;
10import org.openqa.selenium.By;
11import org.openqa.selenium.support.ui.ExpectedCondition;
12
13import java.time.Duration;
14
15class BaseMobileElement {
16
17  protected final AppiumDriver<MobileElement> driver;
18  protected By locator;
19  protected ConditionFactory wait;
20  protected TouchAction<?> touchAction;
21  protected Duration timeout;
22  protected QueryFunctions queryFunctions;
23
24  BaseMobileElement(AppiumDriver<MobileElement> driver, MultiPlatformFinder multiPlatformFinder) {
25    this.driver = driver;
26    this.locator = buildLocator(multiPlatformFinder);
27    init();
28  }
29
30  BaseMobileElement(AppiumDriver<MobileElement> driver, By locator) {
31    this.driver = driver;
32    this.locator = locator;
33    init();
34  }
35
36  protected <T> void waitUntilCondition(ExpectedCondition<T> expectedCondition) {
37    waitUntilCondition(expectedCondition, timeout);
38  }
39
40  protected <T> void waitUntilCondition(ExpectedCondition<T> expectedCondition, Duration duration) {
41    wait.atMost(duration)
42        .until(
43            () -> {
44              Object result = expectedCondition.apply(driver);
45              return result != null
46                      && result.getClass().getTypeName().toLowerCase().contains("boolean")
47                  ? (boolean) result
48                  : result != null;
49            });
50  }
51
52  protected By buildLocator(MultiPlatformFinder multiPlatformFinder) {
53    return driver instanceof IOSDriver
54        ? multiPlatformFinder.getIosFindBy()
55        : multiPlatformFinder.getAndroidFindBy();
56  }
57
58  private void init() {
59    this.timeout = Duration.ofSeconds(30);
60    this.wait = buildFluentWait(timeout);
61    this.touchAction = new TouchAction<>(driver);
62    this.queryFunctions = new QueryFunctions();
63  }
64
65  private ConditionFactory buildFluentWait(Duration duration) {
66    return Awaitility.await().atMost(duration).ignoreExceptions();
67  }
68}
69
Full Screen

PageBase.java

Source: PageBase.java Github

copy
1package PageObjects;
2
3import io.appium.java_client.AppiumDriver;
4import io.appium.java_client.MobileElement;
5import io.appium.java_client.functions.ExpectedCondition;
6import io.appium.java_client.pagefactory.AppiumFieldDecorator;
7import org.openqa.selenium.support.PageFactory;
8import org.openqa.selenium.support.ui.ExpectedConditions;
9import org.openqa.selenium.support.ui.WebDriverWait;
10
11public class PageBase {
12
13    AppiumDriver driver;
14    public static final long Wait = 10;
15
16    public PageBase(AppiumDriver appiumDriver)
17    {
18        PageFactory.initElements(new AppiumFieldDecorator(appiumDriver),this);
19        driver = appiumDriver;
20    }
21
22    public void waitForVisibility(MobileElement element)
23    {
24        WebDriverWait wait =  new WebDriverWait(driver,Wait);
25        wait.until(ExpectedConditions.visibilityOf(element));
26    }
27
28    public void clear(MobileElement element)
29    {
30        waitForVisibility(element);
31        element.clear();
32    }
33
34    public void sendText(MobileElement element, String text)
35    {
36        waitForVisibility(element);
37        element.sendKeys(text);
38    }
39    public void click(MobileElement element)
40    {
41        waitForVisibility(element);
42        element.click();
43    }
44
45    public String getInnerText(MobileElement element)
46    {
47        waitForVisibility(element);
48        return  element.getText();
49    }
50}
51
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in ExpectedCondition

    No methods found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)