Making Your Appium Tests Fast and Reliable, Part 3: Waiting for App States
Using bad locator strategies is one cause of elements in your app not being found, and contributing to flakiness and unreliability. But still when you & # x27; re using the correct locator strategy and are undertake to find the like component every clip you appear, that doesn & # x27; t mean the element will be there rightwhenyou look! What happens when you try to happen an element which just isn & # x27; t there? This is a signally common issue, and it & # x27; s part of a more general phenomenon in functional trial: race weather due to poor assumptions about app state. A race condition is when two processes or procedures are operating simultaneously, and either one could finish before the other. This is a problem in scripts because our code usually handles solely one scenario. So, in the substitute world where the unintuitive subprogram finishes first, our trial will fail. To use our exemplar of chance ingredient: our code usually implicitly assumes that the element will be present before we try to find it. In reality, the petition to find an element and the app & # x27; s own process of working to display the element are in a race. As human users of the app, we cognize how to gracefully lose the race: we only await! If we want to tap a button and it & # x27; s not yet on the screen, we wait for it to show up (for a few seconds, anyways, then we get bored and unfastened Twitter instead). Appium is less refined and does exactly what the node tells it to do, even if that means trying to find an component before it has been properly render on the screen. This is just one example of many potential examples in the family of tryout code assuming the app is in a certain province but being proven wrong. It can be a specially vexing problem because it might only show up infrequently, or only in CI environments. When we develop test code locally, we can often be tricked into do all kinds of assumptions about how race will adjudicate. Just because the app constantly wins a race (like we expect) when testing locally does not entail the app will behave the same in early environments. The solvent is to teach our test code to gain a bit of the gracility of the human loser, and not blow up with exceptions just because the state wasn & # x27; t what it expected. There are three basic agency to wait in your Appium scripts. Waiting & quot; statically & quot; simply entail use a lot of good oldThread.sleepall over the place. This is the brute force solution to a race condition. Is your test script trying to find an element before it is present? Force your test to slow down by adding a static sleep! Taking the login test, which is very conversant to our readers, this is what it would seem like habituate unchanging waits: Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script. I chose 3 seconds as my static wait amount. Why did I take that value? I & # x27; m not sure. It worked for me locally and clear my race condition job. Good enough, flop? Not exactly. There are some major problems with this approach: So, what else can we do? Because the designers of Selenium were well mindful of the element finding race conditions, a long time ago they added the ability in the Selenium (and we copied it with the Appium) waiter for the client to set an & quot; implicit wait timeout & quot;. This timeout is remembered by the host and used in any instance of element determination. If an element can & # x27; t be found instantly, the waiter will keep examine to find it up to the specified timeout. The like test implemented with implicit waits would look like: Wow! That codification is a lot nicer, for one. We & # x27; ve besides completely solved the problem about the ever-increasing waste of time we ran into with electrostatic waits. Because the server-side element-finding retry is on a pretty taut loop, we & # x27; re guaranteed to detect the element within (say) a second of when it really shew up, intend we waste really small time while simultaneously make our tryout lots more robust. There & # x27; s however a trouble or two with this attack, nevertheless: Thankfully, there & # x27; s an even more general solution that gets us past these (avowedly more minor) issues as well. Explicit waits are just that: they make explicit what you are wait for and how long it will take. At the cost of a little more verbosity, we get much more fine-grained control, and are able to teach our exam playscript how to wait for just the right condition in our app before moving on. For model: Here we use the WebDriverWait constructor to initialize a wait object with a certain timeout. We can reuse this object anytime we require the same timeout. We can configuredifferentwait objects with different timeouts and use them for different sort of waiting or elements. Then, we use the until method on the wait object in conjunction with something called an & quot; expected condition & quot;. An expected condition is just a especial method which returns an anonymous inner class whose apply method will be call sporadically until it returns something. The ExpectedConditions class has a number of useful, pre-made condition methods. What & # x27; s great about explicit waits, though, is that we & # x27; re not limited to what arrive in the box. We can make our own! If the app state we want to wait for is particularly complex, we can always do our own expected condition. For example, let & # x27; s say that the detent () command is awful unreliable, and often it fails, still when our element is found. So, what we want is to maintain retrying both the findandclick actions until theybothsucceed one after the other. We could make a custom wait condition, like so: We simply regress a newExpectedConditionand override theapply method with our special logic. Then we can use this in our examination codification, for example as in this revise of the previous test: Granted, this is a bit of a useless model, but it demonstrates how easy it is to create utilitarian and reusable waits that your whole team can use. And this completes our tour of scheme for expect for app states with Appium. Don & # x27; t forget to direct a look at thefull codefor the examples shown hither, including all the setup and teardown boilerplate! Interested in the future episode in this series on speed and reliability? Head on over to. Lead, Content Marketing, HeadSpin Inc. Piali is a dynamic and results-driven Content Marketing Specialist with 8+ years of experience in craft prosecute narrative and market collateral across diverse industries. She excels in collaborating with cross-functional teams to acquire innovative content scheme and deliver compelling, authentic, and impactful substance that resonate with target audiences and enhances brand authenticity. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts..png)



Making Your Appium Tests Fast and Reliable, Part 3: Waiting for App States
AI-Powered Key Takeaways
This article is the tertiary in a multi-part series on test speed and reliableness, inspired by a webinar I gave recently on the like subject (you canwatch the webinar hither). You might too require to check out and.
Check out:
Race Conditions
Also tab:
Read:
Waiting for App States
Static Waits
@ Test world void testLogin_StaticWait () throws InterruptedException {Thread.sleep (3000); driver.findElement (loginScreen) .click (); Thread.sleep (3000); driver.findElement (username) .sendKeys (AUTH_USER); driver.findElement (password) .sendKeys (AUTH_PASS); driver.findElement (loginBtn) .click (); Thread.sleep (3000); driver.findElement (getLoggedInBy (AUTH_USER));}Also read:
Implicit Waits
@ Test public void testLogin_ImplicitWait () {driver.manage () .timeouts () .implicitlyWait (10, TimeUnit.SECONDS); driver.findElement (loginScreen) .click (); driver.findElement (username) .sendKeys (AUTH_USER); driver.findElement (countersign) .sendKeys (AUTH_PASS); driver.findElement (loginBtn) .click (); driver.findElement (getLoggedInBy (AUTH_USER));}Explicit Waits
@ Test public nothingness testLogin_ExplicitWait () {WebDriverWait wait = new WebDriverWait (driver, 10); wait.until (ExpectedConditions.presenceOfElementLocated (loginScreen)) .click (); wait.until (ExpectedConditions.presenceOfElementLocated (username)) .sendKeys (AUTH_USER); wait.until (ExpectedConditions.presenceOfElementLocated (watchword)) .sendKeys (AUTH_PASS); wait.until (ExpectedConditions.presenceOfElementLocated (loginBtn)) .click (); wait.until (ExpectedConditions.presenceOfElementLocated (getLoggedInBy (AUTH_USER)));}Custom Explicit Waits
private ExpectedCondition@ Test populace void testLogin_CustomWait () {WebDriverWait wait = new WebDriverWait (driver, 10); wait.until (elementFoundAndClicked (loginScreen)); wait.until (ExpectedConditions.presenceOfElementLocated (username)) .sendKeys (AUTH_USER); wait.until (ExpectedConditions.presenceOfElementLocated (countersign)) .sendKeys (AUTH_PASS); wait.until (elementFoundAndClicked (loginBtn)); wait.until (ExpectedConditions.presenceOfElementLocated (getLoggedInBy (AUTH_USER)));}Piali Mazumdar
Making Your Appium Tests Fast and Reliable, Part 3: Waiting for App States
4 Parts
-1280X720-Final-2.jpg)
Regression Intelligence practical guide for advanced users (Part 3)
-1280X720-Final-2.jpg)
Regression Intelligence virtual guide for innovative users (Part 4)
Discover how HeadSpin can empower your occupation with superior examine capabilities







Discover how HeadSpin can empower your occupation with superior examine potentiality
Discover how HeadSpin can empower your business with superior testing capabilities
Connet Now


Automate This With SUSA
Test Your App Autonomously







.png)












