-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathBrowserStackTest.java
More file actions
65 lines (54 loc) · 2.46 KB
/
BrowserStackTest.java
File metadata and controls
65 lines (54 loc) · 2.46 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
package com.browserstack;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import com.codeborne.selenide.WebDriverRunner;
import org.yaml.snakeyaml.Yaml;
public class BrowserStackTest {
public RemoteWebDriver driver;
public static String userName, accessKey;
public static Map<String, Object> browserStackYamlMap;
public static final String USER_DIR = "user.dir";
public BrowserStackTest() {
File file = new File(getUserDir() + "/browserstack.yml");
this.browserStackYamlMap = convertYamlFileToMap(file, new HashMap<>());
userName = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : (String) browserStackYamlMap.get("userName");
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : (String) browserStackYamlMap.get("accessKey");
}
@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {
MutableCapabilities capabilities = new MutableCapabilities();
HashMap<String, String> bstackOptions = new HashMap<>();
bstackOptions.put("source", "selenide:sample-sdk:v1.1");
capabilities.setCapability("bstack:options", bstackOptions);
driver = new RemoteWebDriver(new URL(String.format("https://%s:%s@hub-cloud.browserstack.com/wd/hub", userName, accessKey)), capabilities);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
WebDriverRunner.setWebDriver(driver);
}
@AfterMethod(alwaysRun = true)
public void tearDown() {
WebDriverRunner.getWebDriver().quit();
}
private String getUserDir() {
return System.getProperty(USER_DIR);
}
private Map<String, Object> convertYamlFileToMap(File yamlFile, Map<String, Object> map) {
try {
InputStream inputStream = Files.newInputStream(yamlFile.toPath());
Yaml yaml = new Yaml();
Map<String, Object> config = yaml.load(inputStream);
map.putAll(config);
} catch (Exception e) {
throw new RuntimeException(String.format("Malformed browserstack.yml file - %s.", e));
}
return map;
}
}