forked from saucelabs-training/demo-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeleniumTest.java
72 lines (61 loc) · 2.35 KB
/
SeleniumTest.java
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
package com.saucedemo.selenium.demo;
import org.junit.jupiter.api.Assertions;
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.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* Demo tests with Selenium.
*/
public class SeleniumTest {
public RemoteWebDriver driver;
/**
* 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 {
ChromeOptions options = new ChromeOptions();
options.setPlatformName("Windows 10");
options.setBrowserVersion("latest");
Map<String, Object> sauceOptions = new HashMap<>();
sauceOptions.put("username", System.getenv("SAUCE_USERNAME"));
sauceOptions.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
sauceOptions.put("name", testInfo.getDisplayName());
options.setCapability("sauce:options", sauceOptions);
URL url = new URL("https://ondemand.us-west-1.saucelabs.com/wd/hub");
driver = new RemoteWebDriver(url, options);
}
@DisplayName("Selenium Navigation Test")
@Test
public void navigateAndClose() {
driver.navigate().to("https://www.saucedemo.com");
Assertions.assertEquals("Swag Labs", driver.getTitle());
}
/**
* 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();
}
}
}