Skip to content

[java] Add code examples to show how to use user context for single browser instance #2228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package dev.selenium.bidirectional.webdriver_bidi.user_context;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

class MultipleInstanceParallelTest {

private WebDriver driver;

@BeforeEach
public void setup() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
options.addArguments("-private");
driver = new FirefoxDriver(options);
}

@Test
void canSwitchToBlue() {
driver.get("https://www.selenium.dev/selenium/web/cookie-background.html");

WebElement body = driver.findElement(By.tagName("body"));
String bgColor = body.getCssValue("background-color");

String expectedColor = "rgb(255, 255, 255)";
// Background color is white
Assertions.assertEquals(bgColor, expectedColor);

driver.get("https://www.selenium.dev/selenium/web/cookie-background.html");

driver.findElement(By.id("blue-btn")).click();
body = driver.findElement(By.tagName("body"));
bgColor = body.getCssValue("background-color");

expectedColor = "rgb(173, 216, 230)";
// Background color is blue
Assertions.assertEquals(bgColor, expectedColor);

System.out.println(
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
.getMethodName() + " => executed successfully");
}

@Test
void canSwitchToGreen() {
driver.get("https://www.selenium.dev/selenium/web/cookie-background.html");

WebElement body = driver.findElement(By.tagName("body"));
String bgColor = body.getCssValue("background-color");

String expectedColor = "rgb(255, 255, 255)";
Assertions.assertEquals(bgColor, expectedColor);

driver.findElement(By.id("green-btn")).click();
body = driver.findElement(By.tagName("body"));
bgColor = body.getCssValue("background-color");

expectedColor = "rgb(144, 238, 144)";
Assertions.assertEquals(bgColor, expectedColor);

System.out.println(
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
.getMethodName() + " => executed successfully");
}

@Test
void canHaveTheDefaultBackgroundColor() {
driver.get("https://www.selenium.dev/selenium/web/cookie-background.html");

WebElement body = driver.findElement(By.tagName("body"));
String bgColor = body.getCssValue("background-color");

String expectedColor = "rgb(255, 255, 255)";
Assertions.assertEquals(bgColor, expectedColor);

System.out.println(
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
.getMethodName() + " => executed successfully");
}

@AfterEach
public void cleanup() {
driver.quit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package dev.selenium.bidirectional.webdriver_bidi.user_context;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
import org.openqa.selenium.bidi.browsingcontext.CreateContextParameters;
import org.openqa.selenium.bidi.browsingcontext.Locator;
import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
import org.openqa.selenium.bidi.module.Browser;
import org.openqa.selenium.bidi.module.Input;
import org.openqa.selenium.bidi.script.NodeProperties;
import org.openqa.selenium.bidi.script.RemoteValue;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebElement;

class SingleInstanceCookieParallelTest {

private static WebDriver driver;
BrowsingContext context;

@BeforeAll
public static void beforeAll() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true);
driver = new FirefoxDriver(options);

// To use Grid uncomment the lines below

// driver = new RemoteWebDriver(
// new URL("http://localhost:4444"),
// options, false);
//
// Augmenter augmenter = new Augmenter();
// driver = augmenter.augment(driver);
}

@BeforeEach
public void setup() {
Browser browser = new Browser(driver);
String userContext = browser.createUserContext();

CreateContextParameters parameters = new CreateContextParameters(WindowType.TAB);
parameters.userContext(userContext);

context = new BrowsingContext(driver, parameters);
}

@Test
void canSwitchToBlue() {
context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE);

RemoteValue value = context.locateNode(Locator.xpath("/html/body/button[1]"));

Input inputModule = new Input(driver);
Actions actions = new Actions(driver);

RemoteWebElement element = new RemoteWebElement();
element.setId(value.getSharedId().get());
actions.moveToElement(element).click();

inputModule.perform(context.getId(), actions.getSequences());

value = context.locateNode(Locator.xpath("/html/body"));

NodeProperties properties = (NodeProperties) value.getValue().get();
String bgColor = properties.getAttributes().get().get("style");

Assertions.assertEquals(bgColor, "background-color: lightblue;");
System.out.println(
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
.getMethodName() + " => executed successfully");
}

@Test
void canSwitchToGreen() {
context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE);

RemoteValue value = context.locateNode(Locator.xpath("/html/body"));

NodeProperties properties = (NodeProperties) value.getValue().get();
String bgColor = properties.getAttributes().get().get("style");

Assertions.assertEquals(bgColor, "background-color: white;");

value = context.locateNode(Locator.xpath("/html/body/button[2]"));

Input inputModule = new Input(driver);
Actions actions = new Actions(driver);

RemoteWebElement element = new RemoteWebElement();
element.setId(value.getSharedId().get());
actions.moveToElement(element).click();

inputModule.perform(context.getId(), actions.getSequences());

value = context.locateNode(Locator.xpath("/html/body"));

properties = (NodeProperties) value.getValue().get();
bgColor = properties.getAttributes().get().get("style");

Assertions.assertEquals(bgColor, "background-color: lightgreen;");
System.out.println(
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
.getMethodName() + " => executed successfully");
}

@Test
void canHaveTheDefaultBackgroundColor() {
context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE);

RemoteValue value = context.locateNode(Locator.xpath("/html/body"));

NodeProperties properties = (NodeProperties) value.getValue().get();
String bgColor = properties.getAttributes().get().get("style");

Assertions.assertEquals(bgColor, "background-color: white;");
System.out.println(
Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1]
.getMethodName() + " => executed successfully");
}

@AfterAll
public static void cleanup() {
driver.quit();
}
}
Loading