-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackLocalSteps.java
More file actions
89 lines (76 loc) · 3.22 KB
/
StackLocalSteps.java
File metadata and controls
89 lines (76 loc) · 3.22 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
package com.browserstack.stepdefs.local;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import org.json.JSONObject;
import org.testng.Assert;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
public class StackLocalSteps {
private Playwright playwright;
private Browser browser;
private BrowserContext context;
private Page page;
@Before
public void setUp(Scenario scenario) throws Exception {
playwright = Playwright.create();
BrowserType browserType = playwright.chromium();
Map<String, Object> config = loadBrowserStackYaml();
String userName = envOrYaml("BROWSERSTACK_USERNAME", config, "userName");
String accessKey = envOrYaml("BROWSERSTACK_ACCESS_KEY", config, "accessKey");
HashMap<String, String> caps = new HashMap<>();
caps.put("browserstack.user", userName);
caps.put("browserstack.key", accessKey);
caps.put("browserstack.source", "cucumber-java-playwright:sample-master:v1.0");
caps.put("browser", "chrome");
caps.put("browserstack.local", "true");
caps.put("sessionName", scenario.getName());
String encoded = URLEncoder.encode(new JSONObject(caps).toString(), "UTF-8");
String wsEndpoint = "wss://cdp.browserstack.com/playwright?caps=" + encoded;
browser = browserType.connect(wsEndpoint);
context = browser.newContext(new Browser.NewContextOptions().setViewportSize(1920, 1080));
page = context.newPage();
}
@Given("I am on {string}")
public void i_am_on(String url) {
page.navigate(url);
}
@Then("the page title should contain {string}")
public void the_page_title_should_contain(String expected) {
Assert.assertTrue(page.title().contains(expected),
"expected title to contain '" + expected + "' but was '" + page.title() + "'");
}
@After
public void tearDown() {
if (context != null) context.close();
if (browser != null) browser.close();
if (playwright != null) playwright.close();
}
private Map<String, Object> loadBrowserStackYaml() {
File file = new File(System.getProperty("user.dir") + "/browserstack.yml");
try (InputStream in = Files.newInputStream(file.toPath())) {
return new Yaml().load(in);
} catch (Exception e) {
throw new RuntimeException("Could not read browserstack.yml: " + e.getMessage(), e);
}
}
private String envOrYaml(String envKey, Map<String, Object> yaml, String yamlKey) {
String fromEnv = System.getenv(envKey);
if (fromEnv != null && !fromEnv.isEmpty()) return fromEnv;
Object fromYaml = yaml.get(yamlKey);
return fromYaml == null ? null : fromYaml.toString();
}
}