|
| 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 | +// To use Grid uncomment the lines below |
| 35 | + |
| 36 | +// driver = new RemoteWebDriver( |
| 37 | +// new URL("http://localhost:4444"), |
| 38 | +// options, false); |
| 39 | +// |
| 40 | +// Augmenter augmenter = new Augmenter(); |
| 41 | +// driver = augmenter.augment(driver); |
| 42 | + } |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + public void setup() { |
| 46 | + Browser browser = new Browser(driver); |
| 47 | + String userContext = browser.createUserContext(); |
| 48 | + |
| 49 | + CreateContextParameters parameters = new CreateContextParameters(WindowType.TAB); |
| 50 | + parameters.userContext(userContext); |
| 51 | + |
| 52 | + context = new BrowsingContext(driver, parameters); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void canSwitchToBlue() { |
| 57 | + context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE); |
| 58 | + |
| 59 | + RemoteValue value = context.locateNode(Locator.xpath("/html/body/button[1]")); |
| 60 | + |
| 61 | + Input inputModule = new Input(driver); |
| 62 | + Actions actions = new Actions(driver); |
| 63 | + |
| 64 | + RemoteWebElement element = new RemoteWebElement(); |
| 65 | + element.setId(value.getSharedId().get()); |
| 66 | + actions.moveToElement(element).click(); |
| 67 | + |
| 68 | + inputModule.perform(context.getId(), actions.getSequences()); |
| 69 | + |
| 70 | + value = context.locateNode(Locator.xpath("/html/body")); |
| 71 | + |
| 72 | + NodeProperties properties = (NodeProperties) value.getValue().get(); |
| 73 | + String bgColor = properties.getAttributes().get().get("style"); |
| 74 | + |
| 75 | + Assertions.assertEquals(bgColor, "background-color: lightblue;"); |
| 76 | + System.out.println( |
| 77 | + Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1] |
| 78 | + .getMethodName() + " => executed successfully"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void canSwitchToGreen() { |
| 83 | + context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE); |
| 84 | + |
| 85 | + RemoteValue value = context.locateNode(Locator.xpath("/html/body")); |
| 86 | + |
| 87 | + NodeProperties properties = (NodeProperties) value.getValue().get(); |
| 88 | + String bgColor = properties.getAttributes().get().get("style"); |
| 89 | + |
| 90 | + Assertions.assertEquals(bgColor, "background-color: white;"); |
| 91 | + |
| 92 | + value = context.locateNode(Locator.xpath("/html/body/button[2]")); |
| 93 | + |
| 94 | + Input inputModule = new Input(driver); |
| 95 | + Actions actions = new Actions(driver); |
| 96 | + |
| 97 | + RemoteWebElement element = new RemoteWebElement(); |
| 98 | + element.setId(value.getSharedId().get()); |
| 99 | + actions.moveToElement(element).click(); |
| 100 | + |
| 101 | + inputModule.perform(context.getId(), actions.getSequences()); |
| 102 | + |
| 103 | + value = context.locateNode(Locator.xpath("/html/body")); |
| 104 | + |
| 105 | + properties = (NodeProperties) value.getValue().get(); |
| 106 | + bgColor = properties.getAttributes().get().get("style"); |
| 107 | + |
| 108 | + Assertions.assertEquals(bgColor, "background-color: lightgreen;"); |
| 109 | + System.out.println( |
| 110 | + Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1] |
| 111 | + .getMethodName() + " => executed successfully"); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void canHaveTheDefaultBackgroundColor() { |
| 116 | + context.navigate("https://www.selenium.dev/selenium/web/cookie-background.html", ReadinessState.COMPLETE); |
| 117 | + |
| 118 | + RemoteValue value = context.locateNode(Locator.xpath("/html/body")); |
| 119 | + |
| 120 | + NodeProperties properties = (NodeProperties) value.getValue().get(); |
| 121 | + String bgColor = properties.getAttributes().get().get("style"); |
| 122 | + |
| 123 | + Assertions.assertEquals(bgColor, "background-color: white;"); |
| 124 | + System.out.println( |
| 125 | + Thread.currentThread().getName() + " " + Thread.currentThread().getStackTrace()[1] |
| 126 | + .getMethodName() + " => executed successfully"); |
| 127 | + } |
| 128 | + |
| 129 | + @AfterAll |
| 130 | + public static void cleanup() { |
| 131 | + driver.quit(); |
| 132 | + } |
| 133 | +} |
0 commit comments