-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathAppiumTest.java
More file actions
100 lines (88 loc) · 3.63 KB
/
AppiumTest.java
File metadata and controls
100 lines (88 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.saucedemo.selenium;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.TestWatcher;
import org.openqa.selenium.MutableCapabilities;
/** Demo tests with Appium. */
public class AppiumTest {
public AppiumDriver driver;
public MutableCapabilities options;
/**
* A Test Watcher is needed to be able to get the results of a Test so that it can be sent to
* Sauce Labs. Note that the name is never actually used
*/
@RegisterExtension public SauceTestWatcher watcher = new SauceTestWatcher();
@BeforeEach
public void setup(TestInfo testInfo) throws MalformedURLException {
String platformName = System.getenv().getOrDefault("PLATFORM_NAME", "Android");
options = new MutableCapabilities();
String appName = System.getenv().getOrDefault("APP_NAME", null);
String defaultBrowser = appName != null ? null : "Chrome";
options.setCapability(
"browserName", System.getenv().getOrDefault("BROWSER_NAME", defaultBrowser));
options.setCapability("platformName", platformName);
if (appName != null) {
options.setCapability("appium:app", "storage:filename=" + appName);
}
options.setCapability("appium:platformVersion", System.getenv("PLATFORM_VERSION"));
options.setCapability(
"appium:deviceName", System.getenv().getOrDefault("DEVICE_NAME", "Google.*"));
options.setCapability(
"appium:automationName", System.getenv().getOrDefault("AUTOMATION_NAME", "UiAutomator2"));
ArrayList<String> tags = new ArrayList<>();
if (System.getenv("GITPOD_WORKSPACE_ID") != null) {
tags.add("gitpod");
}
Map<String, Object> sauceOptions = new HashMap<>();
sauceOptions.put("appiumVersion", "stable");
sauceOptions.put("username", System.getenv("SAUCE_USERNAME"));
sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
sauceOptions.put("build", System.getenv("BUILD"));
sauceOptions.put("name", testInfo.getDisplayName());
sauceOptions.put("tags", tags);
options.setCapability("sauce:options", sauceOptions);
String region = System.getenv().getOrDefault("REGION", "us-west-1");
String ondemandUrl = "https://ondemand." + region + ".saucelabs.com:443/wd/hub";
URL url = new URL(ondemandUrl);
if ("iOS".equalsIgnoreCase(platformName)) {
driver = new IOSDriver(url, options);
} else {
driver = new AndroidDriver(url, options);
}
}
@DisplayName("Appium Test from Gitpod")
@Test
public void navigateAndClose() throws InterruptedException {
// Add tests and assertions here
if (options.getCapability("browserName") != null) {
driver.get("https://saucedemo.com");
}
// Replace this with commands and assertions
Thread.sleep(5000);
}
/** Custom TestWatcher for Sauce Labs projects. */
public class SauceTestWatcher implements TestWatcher {
@Override
public void testSuccessful(ExtensionContext context) {
driver.executeScript("sauce:job-result=passed");
driver.quit();
}
@Override
public void testFailed(ExtensionContext context, Throwable cause) {
driver.executeScript("sauce:job-result=failed");
driver.quit();
}
}
}