|  | 
|  | 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 | +} | 
0 commit comments