Skip to content

Commit 0e7df55

Browse files
authored
[java] Add code examples to show how to use user context for single browser instance (#2228)
1 parent b17c472 commit 0e7df55

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package dev.selenium.bidirectional.webdriver_bidi.user_context;
2+
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.openqa.selenium.By;
8+
import org.openqa.selenium.WebDriver;
9+
import org.openqa.selenium.WebElement;
10+
import org.openqa.selenium.firefox.FirefoxDriver;
11+
import org.openqa.selenium.firefox.FirefoxOptions;
12+
13+
class MultipleInstanceParallelTest {
14+
15+
private WebDriver driver;
16+
17+
@BeforeEach
18+
public void setup() {
19+
FirefoxOptions options = new FirefoxOptions();
20+
options.setCapability("webSocketUrl", true);
21+
options.addArguments("-private");
22+
driver = new FirefoxDriver(options);
23+
}
24+
25+
@Test
26+
void canSwitchToBlue() {
27+
driver.get("https://www.selenium.dev/selenium/web/cookie-background.html");
28+
29+
WebElement body = driver.findElement(By.tagName("body"));
30+
String bgColor = body.getCssValue("background-color");
31+
32+
String expectedColor = "rgb(255, 255, 255)";
33+
// Background color is white
34+
Assertions.assertEquals(bgColor, expectedColor);
35+
36+
driver.get("https://www.selenium.dev/selenium/web/cookie-background.html");
37+
38+
driver.findElement(By.id("blue-btn")).click();
39+
body = driver.findElement(By.tagName("body"));
40+
bgColor = body.getCssValue("background-color");
41+
42+
expectedColor = "rgb(173, 216, 230)";
43+
// Background color is blue
44+
Assertions.assertEquals(bgColor, expectedColor);
45+
46+
System.out.println(
47+
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
48+
.getMethodName() + " => executed successfully");
49+
}
50+
51+
@Test
52+
void canSwitchToGreen() {
53+
driver.get("https://www.selenium.dev/selenium/web/cookie-background.html");
54+
55+
WebElement body = driver.findElement(By.tagName("body"));
56+
String bgColor = body.getCssValue("background-color");
57+
58+
String expectedColor = "rgb(255, 255, 255)";
59+
Assertions.assertEquals(bgColor, expectedColor);
60+
61+
driver.findElement(By.id("green-btn")).click();
62+
body = driver.findElement(By.tagName("body"));
63+
bgColor = body.getCssValue("background-color");
64+
65+
expectedColor = "rgb(144, 238, 144)";
66+
Assertions.assertEquals(bgColor, expectedColor);
67+
68+
System.out.println(
69+
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
70+
.getMethodName() + " => executed successfully");
71+
}
72+
73+
@Test
74+
void canHaveTheDefaultBackgroundColor() {
75+
driver.get("https://www.selenium.dev/selenium/web/cookie-background.html");
76+
77+
WebElement body = driver.findElement(By.tagName("body"));
78+
String bgColor = body.getCssValue("background-color");
79+
80+
String expectedColor = "rgb(255, 255, 255)";
81+
Assertions.assertEquals(bgColor, expectedColor);
82+
83+
System.out.println(
84+
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
85+
.getMethodName() + " => executed successfully");
86+
}
87+
88+
@AfterEach
89+
public void cleanup() {
90+
driver.quit();
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package dev.selenium.bidirectional.webdriver_bidi.user_context;
2+
3+
import org.junit.jupiter.api.AfterAll;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.openqa.selenium.WebDriver;
9+
import org.openqa.selenium.WindowType;
10+
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
11+
import org.openqa.selenium.bidi.browsingcontext.CreateContextParameters;
12+
import org.openqa.selenium.bidi.browsingcontext.Locator;
13+
import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
14+
import org.openqa.selenium.bidi.module.Browser;
15+
import org.openqa.selenium.bidi.module.Input;
16+
import org.openqa.selenium.bidi.script.NodeProperties;
17+
import org.openqa.selenium.bidi.script.RemoteValue;
18+
import org.openqa.selenium.firefox.FirefoxDriver;
19+
import org.openqa.selenium.firefox.FirefoxOptions;
20+
import org.openqa.selenium.interactions.Actions;
21+
import org.openqa.selenium.remote.RemoteWebElement;
22+
23+
class SingleInstanceCookieParallelTest {
24+
25+
private static WebDriver driver;
26+
BrowsingContext context;
27+
28+
@BeforeAll
29+
public static void beforeAll() {
30+
FirefoxOptions options = new FirefoxOptions();
31+
options.setCapability("webSocketUrl", true);
32+
driver = new FirefoxDriver(options);
33+
34+
// To use Grid uncomment the lines below
35+
36+
// driver = new RemoteWebDriver(
37+
// new URL("http://localhost:4444"),
38+
// options, false);
39+
//
40+
// Augmenter augmenter = new Augmenter();
41+
// driver = augmenter.augment(driver);
42+
}
43+
44+
@BeforeEach
45+
public void setup() {
46+
Browser browser = new Browser(driver);
47+
String userContext = browser.createUserContext();
48+
49+
CreateContextParameters parameters = new CreateContextParameters(WindowType.TAB);
50+
parameters.userContext(userContext);
51+
52+
context = new BrowsingContext(driver, parameters);
53+
}
54+
55+
@Test
56+
void canSwitchToBlue() {
57+
context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE);
58+
59+
RemoteValue value = context.locateNode(Locator.xpath("/html/body/button[1]"));
60+
61+
Input inputModule = new Input(driver);
62+
Actions actions = new Actions(driver);
63+
64+
RemoteWebElement element = new RemoteWebElement();
65+
element.setId(value.getSharedId().get());
66+
actions.moveToElement(element).click();
67+
68+
inputModule.perform(context.getId(), actions.getSequences());
69+
70+
value = context.locateNode(Locator.xpath("/html/body"));
71+
72+
NodeProperties properties = (NodeProperties) value.getValue().get();
73+
String bgColor = properties.getAttributes().get().get("style");
74+
75+
Assertions.assertEquals(bgColor, "background-color: lightblue;");
76+
System.out.println(
77+
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
78+
.getMethodName() + " => executed successfully");
79+
}
80+
81+
@Test
82+
void canSwitchToGreen() {
83+
context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE);
84+
85+
RemoteValue value = context.locateNode(Locator.xpath("/html/body"));
86+
87+
NodeProperties properties = (NodeProperties) value.getValue().get();
88+
String bgColor = properties.getAttributes().get().get("style");
89+
90+
Assertions.assertEquals(bgColor, "background-color: white;");
91+
92+
value = context.locateNode(Locator.xpath("/html/body/button[2]"));
93+
94+
Input inputModule = new Input(driver);
95+
Actions actions = new Actions(driver);
96+
97+
RemoteWebElement element = new RemoteWebElement();
98+
element.setId(value.getSharedId().get());
99+
actions.moveToElement(element).click();
100+
101+
inputModule.perform(context.getId(), actions.getSequences());
102+
103+
value = context.locateNode(Locator.xpath("/html/body"));
104+
105+
properties = (NodeProperties) value.getValue().get();
106+
bgColor = properties.getAttributes().get().get("style");
107+
108+
Assertions.assertEquals(bgColor, "background-color: lightgreen;");
109+
System.out.println(
110+
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
111+
.getMethodName() + " => executed successfully");
112+
}
113+
114+
@Test
115+
void canHaveTheDefaultBackgroundColor() {
116+
context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE);
117+
118+
RemoteValue value = context.locateNode(Locator.xpath("/html/body"));
119+
120+
NodeProperties properties = (NodeProperties) value.getValue().get();
121+
String bgColor = properties.getAttributes().get().get("style");
122+
123+
Assertions.assertEquals(bgColor, "background-color: white;");
124+
System.out.println(
125+
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
126+
.getMethodName() + " => executed successfully");
127+
}
128+
129+
@AfterAll
130+
public static void cleanup() {
131+
driver.quit();
132+
}
133+
}

0 commit comments

Comments
 (0)