-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeleniumTestScheduling.java
More file actions
166 lines (140 loc) · 7.52 KB
/
SeleniumTestScheduling.java
File metadata and controls
166 lines (140 loc) · 7.52 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package examples;
import com.google.common.collect.ImmutableMap;
import com.testfabrik.webmate.javasdk.*;
import com.testfabrik.webmate.javasdk.browsersession.BrowserSessionRef;
import com.testfabrik.webmate.javasdk.devices.*;
import com.testfabrik.webmate.javasdk.selenium.WebmateSeleniumSession;
import com.testfabrik.webmate.javasdk.testmgmt.TestRunEvaluationStatus;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static examples.MyCredentials.*;
import static examples.helpers.Helpers.waitForElement;
import static org.junit.Assert.assertEquals;
/**
* Simple test that shows how to schedule Selenium tests with the webmate Java sdk.
*
* __Disclaimer__: This approach will only work if everyone who tests in the corresponding project adheres to the following structure.
* 1. use scheduleDevice to request and wait for a fresh device that fulfills the given requirements (Browser, BrowserVersion and Platform)
* 2. use the slotId returned by scheduleDevice in your Selenium test capabilities to ensure that you run your test on the device
* 3. run your test.
* 4. make sure you release the device when your test is finished, otherwise it will crash.
*/
@RunWith(JUnit4.class)
public class SeleniumTestScheduling {
private WebmateAPISession webmateSession;
@Before
public void setup() throws URISyntaxException {
WebmateAuthInfo authInfo = new WebmateAuthInfo(MyCredentials.WEBMATE_APIKEY);
webmateSession = new WebmateAPISession(
authInfo,
WebmateEnvironment.create(new URI(WEBMATE_API_URI)),
WEBMATE_PROJECTID);
}
@Test
public void performTest() throws MalformedURLException, InterruptedException {
Platform platform = new Platform(PlatformType.WINDOWS, "11","64");
Browser browser = new Browser(BrowserType.CHROME, "121", platform);
executeTest(browser);
}
// Helper functions
private DesiredCapabilities getCapabilities(Browser browser) {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browserName", browser.getBrowserType().getValue());
caps.setCapability("browserVersion", browser.getVersion());
caps.setCapability("platformName", browser.getPlatform().toString());
caps.setCapability("wm:apikey", WEBMATE_APIKEY);
caps.setCapability("wm:project", WEBMATE_PROJECTID.toString());
return caps;
}
public DeviceDTO scheduleDevice(String deviceName, Browser browser, int maxRetries) throws InterruptedException{
DeviceDTO deviceDTO = null;
String deviceState = "";
String platform = browser.getPlatform().getPlatformType() + "_" + browser.getPlatform().getPlatformVersion() + "_" + browser.getPlatform().getPlatformArchitecture();
Map requirements = ImmutableMap.of(
DevicePropertyName.Platform, platform,
DevicePropertyName.AutomationAvailable, true,
DevicePropertyName.Browsers, ImmutableMap.of(
"version", browser.getVersion(), "browserType", browser.getBrowserType()
));
DeviceRequest request = new DeviceRequest(deviceName, new DeviceRequirements(requirements));
int retries = 0;
while ((deviceDTO == null || !deviceState.equals("running")) && retries < maxRetries) {
try {
System.out.println("Sending Request for Device");
deviceDTO = webmateSession.device.requestDeviceByRequirements(request);
while (deviceDTO == null || !deviceState.equals("running")) {
TimeUnit.MINUTES.sleep(1); // Wait for a minute before retrying
if (deviceDTO != null) {
deviceState = webmateSession.device.getDeviceInfo(deviceDTO.getId()).getState();
}
}
} catch (Exception e) {
System.out.println("Could not get device, will retry ...");
TimeUnit.MINUTES.sleep(1); // Wait for a minute before retrying after an exception
retries++;
}
}
return deviceDTO;
}
public void executeTest(Browser browser) throws MalformedURLException, InterruptedException {
// 1. Schedule device for test
System.out.println("Scheduling test and wait for free device slot");
DeviceDTO device = scheduleDevice("TestDevice", browser,5);
if (device != null) {
// 2. adding slotId to capabilities ensures that the freshly deployed device is used by the test
DesiredCapabilities caps = getCapabilities(browser);
caps.setCapability("wm:slot", device.getSlot().getValueAsString());
// 3. run test as usual
RemoteWebDriver driver = new RemoteWebDriver(new URL(WEBMATE_SELENIUM_URL), caps);
WebmateSeleniumSession seleniumSession = webmateSession.addSeleniumSession(driver.getSessionId().toString());
BrowserSessionRef browserSession = webmateSession.browserSession
.getBrowserSessionForSeleniumSession(driver.getSessionId().toString());
try {
driver.get("http://www.examplepage.org/form_interaction");
System.out.println("Click on link");
waitForElement(driver, "lk").click();
assertEquals("Link Clicked!", waitForElement(driver, ".success").getText());
browserSession.startAction("Click on button");
System.out.println("Clicking on Button");
waitForElement(driver, "bn").click();
browserSession.finishAction();
browserSession.startAction("Click on Checkbox");
System.out.println("Clicking on Checkbox");
waitForElement(driver, "ck").click();
browserSession.finishAction();
browserSession.startAction("Click on Radiobutton");
System.out.println("Clicking on RadioButton");
waitForElement(driver, "rd").click();
browserSession.createState("after radio button");
browserSession.finishAction("was successful");
System.out.println("Clicking on Element with a Hover Event");
waitForElement(driver, "mover").click();
System.out.println("Entering some Text...");
waitForElement(driver, "text-input").click();
waitForElement(driver, "text-input").sendKeys("Test test");
System.out.println("Entering more text...");
waitForElement(driver, "area").click();
waitForElement(driver, "area").sendKeys("Here some more test");
seleniumSession.finishTestRun(TestRunEvaluationStatus.PASSED, "TestRun completed successfully");
System.out.println("Selenium expedition completed");
} catch (Throwable e) {
seleniumSession.finishTestRun(TestRunEvaluationStatus.FAILED, "TestRun has failed");
e.printStackTrace();
} finally {
driver.quit();
// 4. important -> release the device when the test is done or fails
webmateSession.device.releaseDevice(device.getId());
}
}
}
}