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/SingleInstanceCookieParallelTest.java b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java new file mode 100644 index 000000000000..a40a6da5a85c --- /dev/null +++ b/examples/java/src/test/java/dev/selenium/bidirectional/webdriver_bidi/user_context/SingleInstanceCookieParallelTest.java @@ -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(); + } +}