|
| 1 | +import io.appium.java_client.MobileBy; |
| 2 | +import io.appium.java_client.ios.IOSDriver; |
| 3 | +import io.appium.java_client.ios.IOSElement; |
| 4 | +import io.visual_regression_tracker.sdk_java.*; |
| 5 | +import org.openqa.selenium.OutputType; |
| 6 | +import org.openqa.selenium.TakesScreenshot; |
| 7 | +import org.openqa.selenium.WebElement; |
| 8 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 9 | +import org.openqa.selenium.support.ui.ExpectedConditions; |
| 10 | +import org.openqa.selenium.support.ui.WebDriverWait; |
| 11 | +import org.testng.annotations.AfterTest; |
| 12 | +import org.testng.annotations.BeforeTest; |
| 13 | +import org.testng.annotations.DataProvider; |
| 14 | +import org.testng.annotations.Test; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.Objects; |
| 18 | + |
| 19 | +public class IOSBasicInteractionsTest extends BaseTest { |
| 20 | + String platformName = "iOS"; |
| 21 | + String deviceName = "iPhone 12"; |
| 22 | + String platformVersion = "14.5"; |
| 23 | + private IOSDriver<WebElement> driver; |
| 24 | + private VisualRegressionTracker vrt; |
| 25 | + |
| 26 | + @BeforeTest |
| 27 | + public void setUp() throws IOException, InterruptedException { |
| 28 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 29 | + capabilities.setCapability("deviceName", deviceName); |
| 30 | + capabilities.setCapability("platformName", platformName); |
| 31 | + capabilities.setCapability("platformVersion", platformVersion); |
| 32 | + String appPath = Objects.requireNonNull(getClass().getClassLoader().getResource("App.zip")).getPath(); |
| 33 | + capabilities.setCapability("app", appPath); |
| 34 | + capabilities.setCapability("automationName", "XCUITest"); |
| 35 | + |
| 36 | + driver = new IOSDriver<>(getServiceUrl(), capabilities); |
| 37 | + |
| 38 | + VisualRegressionTrackerConfig vrtConfig = VisualRegressionTrackerConfig.builder() |
| 39 | + .apiUrl("http://localhost:4200") |
| 40 | + .apiKey("11Q288KSQKMDJGMH3RVY9N7Y0FJ3") |
| 41 | + .project("9cb1ff4d-5675-4c85-a41b-8e08dbf6d5f6") |
| 42 | + .branchName("master") |
| 43 | + .enableSoftAssert(true) |
| 44 | + .build(); |
| 45 | + |
| 46 | + vrt = new VisualRegressionTracker(vrtConfig); |
| 47 | + vrt.start(); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterTest |
| 51 | + public void tearDown() throws IOException, InterruptedException { |
| 52 | + if (driver != null) { |
| 53 | + driver.quit(); |
| 54 | + } |
| 55 | + vrt.stop(); |
| 56 | + } |
| 57 | + |
| 58 | + @DataProvider(name = "menu-buttons") |
| 59 | + public Object[][] getMenuButtons() { |
| 60 | + return new Object[][]{ |
| 61 | + {"Home page", MobileBy.AccessibilityId("Home")}, |
| 62 | + {"Web page", MobileBy.AccessibilityId("Webview")}, |
| 63 | + {"Login page", MobileBy.iOSClassChain("**/XCUIElementTypeButton[`label == \"Login\"`]")}, |
| 64 | + {"Forms page", MobileBy.AccessibilityId("Forms")}, |
| 65 | + {"Swipe page", MobileBy.AccessibilityId("Swipe")}, |
| 66 | + {"Drag page", MobileBy.AccessibilityId("Drag")}, |
| 67 | + |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + @Test(dataProvider = "menu-buttons") |
| 72 | + public void shouldMatchScreenshot(String pageName, MobileBy locator) throws Exception { |
| 73 | + |
| 74 | + IOSElement button = (IOSElement) new WebDriverWait(driver, 30) |
| 75 | + .until(ExpectedConditions.visibilityOfElementLocated(locator)); |
| 76 | + |
| 77 | + button.click(); |
| 78 | + |
| 79 | + vrtTrackWithRetry( |
| 80 | + pageName, |
| 81 | + TestRunOptions.builder() |
| 82 | + .device(deviceName) |
| 83 | + .os(platformName) |
| 84 | + .build(), |
| 85 | + 2 |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + void vrtTrackWithRetry(String pageName, TestRunOptions testRunOptions, int maxRetries) throws Exception { |
| 90 | + TestRunResult result = null; |
| 91 | + Exception exception = null; |
| 92 | + |
| 93 | + try { |
| 94 | + result = vrt.track(pageName, |
| 95 | + ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64), |
| 96 | + testRunOptions); |
| 97 | + } catch (Exception ex) { |
| 98 | + exception = ex; |
| 99 | + } |
| 100 | + if (maxRetries <= 0 || (Objects.nonNull(result) && !result.getTestRunResponse().getStatus().equals(TestRunStatus.UNRESOLVED)) |
| 101 | + ) { |
| 102 | + if (Objects.nonNull(exception)) { |
| 103 | + throw exception; |
| 104 | + } else { |
| 105 | + return; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + vrtTrackWithRetry(pageName, testRunOptions, maxRetries - 1); |
| 110 | + } |
| 111 | +} |
0 commit comments