|
22 | 22 |
|
23 | 23 | import io.appium.java_client.MobileBy; |
24 | 24 | import org.apache.commons.lang3.StringUtils; |
| 25 | +import org.junit.After; |
25 | 26 | import org.junit.FixMethodOrder; |
26 | 27 | import org.junit.Test; |
27 | 28 | import org.junit.runners.MethodSorters; |
| 29 | +import org.openqa.selenium.TimeoutException; |
| 30 | +import org.openqa.selenium.WebDriverException; |
28 | 31 | import org.openqa.selenium.support.ui.WebDriverWait; |
29 | 32 |
|
30 | 33 | import java.util.function.Supplier; |
31 | 34 |
|
32 | 35 | @FixMethodOrder(MethodSorters.NAME_ASCENDING) |
33 | 36 | public class IOSAlertTest extends AppIOSTest { |
34 | 37 |
|
35 | | - private WebDriverWait waiting = new WebDriverWait(driver, 10000); |
| 38 | + private static final long ALERT_TIMEOUT_SECONDS = 5; |
| 39 | + private static final int CLICK_RETRIES = 2; |
| 40 | + |
| 41 | + private WebDriverWait waiting = new WebDriverWait(driver, ALERT_TIMEOUT_SECONDS); |
36 | 42 | private static final String iOSAutomationText = "show alert"; |
37 | 43 |
|
38 | | - @Test public void acceptAlertTest() { |
| 44 | + private void ensureAlertPresence() { |
| 45 | + int retry = 0; |
| 46 | + // CI might not be performant enough, so we need to retry |
| 47 | + while (true) { |
| 48 | + try { |
| 49 | + driver.findElement(MobileBy.AccessibilityId(iOSAutomationText)).click(); |
| 50 | + waiting.until(alertIsPresent()); |
| 51 | + return; |
| 52 | + } catch (TimeoutException e) { |
| 53 | + retry++; |
| 54 | + if (retry >= CLICK_RETRIES) { |
| 55 | + throw e; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @After |
| 62 | + public void afterEach() { |
| 63 | + try { |
| 64 | + driver.switchTo().alert().accept(); |
| 65 | + } catch (WebDriverException e) { |
| 66 | + // ignore |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void acceptAlertTest() { |
39 | 72 | Supplier<Boolean> acceptAlert = () -> { |
40 | | - driver.findElement(MobileBy.AccessibilityId(iOSAutomationText)).click(); |
41 | | - waiting.until(alertIsPresent()); |
| 73 | + ensureAlertPresence(); |
42 | 74 | driver.switchTo().alert().accept(); |
43 | 75 | return true; |
44 | 76 | }; |
45 | 77 | assertTrue(acceptAlert.get()); |
46 | 78 | } |
47 | 79 |
|
48 | | - @Test public void dismissAlertTest() { |
| 80 | + @Test |
| 81 | + public void dismissAlertTest() { |
49 | 82 | Supplier<Boolean> dismissAlert = () -> { |
50 | | - driver.findElement(MobileBy.AccessibilityId(iOSAutomationText)).click(); |
51 | | - waiting.until(alertIsPresent()); |
| 83 | + ensureAlertPresence(); |
52 | 84 | driver.switchTo().alert().dismiss(); |
53 | 85 | return true; |
54 | 86 | }; |
55 | 87 | assertTrue(dismissAlert.get()); |
56 | 88 | } |
57 | 89 |
|
58 | | - @Test public void getAlertTextTest() { |
59 | | - driver.findElement(MobileBy.AccessibilityId(iOSAutomationText)).click(); |
60 | | - waiting.until(alertIsPresent()); |
| 90 | + @Test |
| 91 | + public void getAlertTextTest() { |
| 92 | + ensureAlertPresence(); |
61 | 93 | assertFalse(StringUtils.isBlank(driver.switchTo().alert().getText())); |
62 | 94 | } |
63 | 95 | } |
0 commit comments