'Appium Explicit Wait

I have been using Thread.sleep in my Appium script and it is messing up my script iOS Appium Script with Java.

Is there a way to implement a wait in my BaseFile and extends to all classes so that I won't have to be writing explicit wait on every line of my script. I tried to implement wait.until(ExpectedConditions.elementToBeClickable(By.(". "))); but it does not work with AccessibilityId for iOS.

Also, if I have to use xpath, I will have to implement it at almost every line of code just like I have been using Thread.sleep and this does not make for a clean code.

Is there a block of code that I can add to my BaseFile and extend to all my test scripts that I wouldn't have to add wait in every line of code?

Attached is a section of my script and you will see how messed up it is with Thread.sleep:

@Test(description =" This test checks if Items are successfully added to Cart")
public void AddItemToCart() throws MalformedURLException, InterruptedException {
    
    service = startServer();

    IOSDriver<IOSElement> driver = DesiredCapabilities();

    OnboardingPage OnboardingPage = new OnboardingPage(driver);

    OnboardingPage.startNow.click();
    Thread.sleep(2000);

    OnboardingPage.allow.click();

    OnboardingPage.zipCodeField.sendKeys("00000");
    Thread.sleep(2000);

    OnboardingPage.confirmZipCod.click();
    Thread.sleep(2000);

    HomePage HomePage = new HomePage(driver);

    HomePage.medicationsCategory.click();
    Thread.sleep(2000);
}


Solution 1:[1]

Thread.sleep() is a static wait, it is not recommended in the appium scripting you can use Implicit wait, It can be applied to all steps where you are locating the elements. It can be used immediately after initializing the driver object.

Below is the code syntax.

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

Wait of minimum 60 seconds in recommended appium script.

If you like my answer please upvote and marked it as answered

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jayshrikant Shrivastava