From a3a5bc5d2c6cb8f1c44a0622e3b8a58b43b499d6 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Thu, 20 Mar 2025 12:07:28 +0530 Subject: [PATCH 1/4] [java] Add code examples to show how to use user context for single browser instance --- .../MultipleInstanceParallelTest.java | 92 ++++++++++++ .../SingleInstanceCookieParallelGridTest.java | 139 ++++++++++++++++++ .../SingleInstanceCookieParallelTest.java | 124 ++++++++++++++++ 3 files changed, 355 insertions(+) create mode 100644 examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/MultipleInstanceParallelTest.java create mode 100644 examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelGridTest.java create mode 100644 examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java diff --git a/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/MultipleInstanceParallelTest.java b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/MultipleInstanceParallelTest.java new file mode 100644 index 000000000000..7f5b5a6ccc1d --- /dev/null +++ b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/MultipleInstanceParallelTest.java @@ -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(); + } +} diff --git a/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelGridTest.java b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelGridTest.java new file mode 100644 index 000000000000..845eca0391d1 --- /dev/null +++ b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelGridTest.java @@ -0,0 +1,139 @@ +package dev.selenium.bidirectional.webdriver_bidi.user_context; + +import org.junit.Ignore; +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.FirefoxOptions; +import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.RemoteWebDriver; +import org.openqa.selenium.remote.RemoteWebElement; + +import java.net.MalformedURLException; +import java.net.URL; + +class SingleInstanceCookieParallelGridTest { + + private static WebDriver driver; + BrowsingContext context; + + @BeforeAll + public static void beforeAll() throws MalformedURLException { + FirefoxOptions options = new FirefoxOptions(); + options.enableBiDi(); + + 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); + } + + @Ignore + @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"); + } + + @Ignore + @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"); + } + + @Ignore + @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(); + } +} diff --git a/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java new file mode 100644 index 000000000000..c3ddd3a25351 --- /dev/null +++ b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java @@ -0,0 +1,124 @@ +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); + } + + @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(); + } +} From 83110cdd1affd51fc85d6eff869e7a20d11e36a3 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Thu, 20 Mar 2025 12:49:28 +0530 Subject: [PATCH 2/4] Add dependency --- examples/java/pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/java/pom.xml b/examples/java/pom.xml index ca1ac5f7358c..96f7d176f49e 100644 --- a/examples/java/pom.xml +++ b/examples/java/pom.xml @@ -48,6 +48,12 @@ selenium-logger 2.4.0 + + junit + junit + 4.13.2 + test + From 60d6f362e9620404f4d4661533e5858d9c27eec8 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Thu, 20 Mar 2025 14:22:13 +0530 Subject: [PATCH 3/4] Delete the Grid test --- .../SingleInstanceCookieParallelGridTest.java | 139 ------------------ .../SingleInstanceCookieParallelTest.java | 13 ++ 2 files changed, 13 insertions(+), 139 deletions(-) delete mode 100644 examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelGridTest.java diff --git a/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelGridTest.java b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelGridTest.java deleted file mode 100644 index 845eca0391d1..000000000000 --- a/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelGridTest.java +++ /dev/null @@ -1,139 +0,0 @@ -package dev.selenium.bidirectional.webdriver_bidi.user_context; - -import org.junit.Ignore; -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.FirefoxOptions; -import org.openqa.selenium.interactions.Actions; -import org.openqa.selenium.remote.Augmenter; -import org.openqa.selenium.remote.RemoteWebDriver; -import org.openqa.selenium.remote.RemoteWebElement; - -import java.net.MalformedURLException; -import java.net.URL; - -class SingleInstanceCookieParallelGridTest { - - private static WebDriver driver; - BrowsingContext context; - - @BeforeAll - public static void beforeAll() throws MalformedURLException { - FirefoxOptions options = new FirefoxOptions(); - options.enableBiDi(); - - 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); - } - - @Ignore - @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"); - } - - @Ignore - @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"); - } - - @Ignore - @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(); - } -} diff --git a/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java index c3ddd3a25351..e28f19f3fcc5 100644 --- a/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java +++ b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java @@ -18,8 +18,12 @@ import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.interactions.Actions; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.RemoteWebElement; +import java.net.URL; + class SingleInstanceCookieParallelTest { private static WebDriver driver; @@ -30,6 +34,15 @@ 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 From f946df9df5cc9086e7189a4b8e8fcf0d0f38f36e Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Thu, 20 Mar 2025 14:23:08 +0530 Subject: [PATCH 4/4] Remove extra dependency --- examples/java/pom.xml | 6 ------ .../user_context/SingleInstanceCookieParallelTest.java | 4 ---- 2 files changed, 10 deletions(-) diff --git a/examples/java/pom.xml b/examples/java/pom.xml index 96f7d176f49e..ca1ac5f7358c 100644 --- a/examples/java/pom.xml +++ b/examples/java/pom.xml @@ -48,12 +48,6 @@ selenium-logger 2.4.0 - - junit - junit - 4.13.2 - test - diff --git a/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java index e28f19f3fcc5..a40a6da5a85c 100644 --- a/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java +++ b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java @@ -18,12 +18,8 @@ import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.interactions.Actions; -import org.openqa.selenium.remote.Augmenter; -import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.RemoteWebElement; -import java.net.URL; - class SingleInstanceCookieParallelTest { private static WebDriver driver;