Skip to content

Commit a3a5bc5

Browse files
committed
[java] Add code examples to show how to use user context for single browser instance
1 parent 559f4be commit a3a5bc5

File tree

3 files changed

+355
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)