Skip to content

Commit 0dcdcc0

Browse files
committed
appium example added
1 parent b8639ed commit 0dcdcc0

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed

appium-maven/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
.DS_Store

appium-maven/pom.xml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>appium-maven</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
17+
<repositories>
18+
<repository>
19+
<id>jitpack.io</id>
20+
<url>https://jitpack.io</url>
21+
</repository>
22+
</repositories>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>com.github.Visual-Regression-Tracker</groupId>
27+
<artifactId>sdk-java</artifactId>
28+
<version>5.1.0</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>io.appium</groupId>
33+
<artifactId>java-client</artifactId>
34+
<version>7.5.1</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.testng</groupId>
39+
<artifactId>testng</artifactId>
40+
<version>6.14.3</version>
41+
<scope>test</scope>
42+
</dependency>
43+
44+
</dependencies>
45+
</project>
7.85 MB
Binary file not shown.
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import io.appium.java_client.service.local.AppiumDriverLocalService;
2+
import org.testng.annotations.AfterSuite;
3+
import org.testng.annotations.BeforeSuite;
4+
5+
import java.net.URL;
6+
7+
public abstract class BaseTest {
8+
9+
private static AppiumDriverLocalService service;
10+
11+
@BeforeSuite
12+
public void globalSetup () {
13+
service = AppiumDriverLocalService.buildDefaultService();
14+
service.start();
15+
}
16+
17+
@AfterSuite
18+
public void globalTearDown () {
19+
if (service != null) {
20+
service.stop();
21+
}
22+
}
23+
24+
public URL getServiceUrl () {
25+
return service.getUrl();
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)