|
4 | 4 | import java.util.List;
|
5 | 5 | import org.junit.jupiter.api.Assertions;
|
6 | 6 | import org.junit.jupiter.api.BeforeEach;
|
| 7 | +import org.junit.jupiter.api.Disabled; |
7 | 8 | import org.junit.jupiter.api.Test;
|
| 9 | +import org.openqa.selenium.By; |
| 10 | +import org.openqa.selenium.JavascriptExecutor; |
| 11 | +import org.openqa.selenium.Rectangle; |
| 12 | +import org.openqa.selenium.WebElement; |
8 | 13 | import org.openqa.selenium.WindowType;
|
9 | 14 | import org.openqa.selenium.bidi.BiDiException;
|
10 | 15 | import org.openqa.selenium.bidi.browsingcontext.BrowsingContext;
|
|
13 | 18 | import org.openqa.selenium.bidi.browsingcontext.ReadinessState;
|
14 | 19 | import org.openqa.selenium.firefox.FirefoxDriver;
|
15 | 20 | import org.openqa.selenium.firefox.FirefoxOptions;
|
| 21 | +import org.openqa.selenium.print.PrintOptions; |
| 22 | +import org.openqa.selenium.remote.RemoteWebElement; |
| 23 | + |
| 24 | +import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs; |
| 25 | +import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated; |
16 | 26 |
|
17 | 27 | class BrowsingContextTest extends BaseTest {
|
18 | 28 |
|
@@ -141,4 +151,189 @@ void testCloseATab() {
|
141 | 151 |
|
142 | 152 | Assertions.assertThrows(BiDiException.class, tab2::getTree);
|
143 | 153 | }
|
| 154 | + |
| 155 | + @Test |
| 156 | + void testActivateABrowsingContext() { |
| 157 | + BrowsingContext window1 = new BrowsingContext(driver, driver.getWindowHandle()); |
| 158 | + BrowsingContext window2 = new BrowsingContext(driver, WindowType.WINDOW); |
| 159 | + |
| 160 | + window1.activate(); |
| 161 | + |
| 162 | + boolean isFocused = (boolean) ((JavascriptExecutor) driver).executeScript("return document.hasFocus();"); |
| 163 | + |
| 164 | + Assertions.assertTrue(isFocused); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + void testReloadABrowsingContext() { |
| 169 | + BrowsingContext browsingContext = new BrowsingContext(driver, WindowType.TAB); |
| 170 | + |
| 171 | + browsingContext.navigate("https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html", ReadinessState.COMPLETE); |
| 172 | + |
| 173 | + NavigationResult reloadInfo = browsingContext.reload(ReadinessState.INTERACTIVE); |
| 174 | + |
| 175 | + Assertions.assertNotNull(reloadInfo.getNavigationId()); |
| 176 | + Assertions.assertTrue(reloadInfo.getUrl().contains("/bidi/logEntryAdded.html")); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + void testHandleUserPrompt() { |
| 181 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 182 | + |
| 183 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html"); |
| 184 | + |
| 185 | + driver.findElement(By.id("alert")).click(); |
| 186 | + |
| 187 | + browsingContext.handleUserPrompt(); |
| 188 | + |
| 189 | + Assertions.assertTrue(driver.getPageSource().contains("Testing Alerts and Stuff")); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + void testAcceptUserPrompt() { |
| 194 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 195 | + |
| 196 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html"); |
| 197 | + |
| 198 | + driver.findElement(By.id("alert")).click(); |
| 199 | + |
| 200 | + browsingContext.handleUserPrompt("true"); |
| 201 | + |
| 202 | + Assertions.assertTrue(driver.getPageSource().contains("Testing Alerts and Stuff")); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + void testDismissUserPrompt() { |
| 207 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 208 | + |
| 209 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html"); |
| 210 | + |
| 211 | + driver.findElement(By.id("alert")).click(); |
| 212 | + |
| 213 | + browsingContext.handleUserPrompt("true"); |
| 214 | + |
| 215 | + Assertions.assertTrue(driver.getPageSource().contains("Testing Alerts and Stuff")); |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + void testPassUserTextToUserPrompt() { |
| 220 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 221 | + |
| 222 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html"); |
| 223 | + |
| 224 | + driver.findElement(By.id("prompt-with-default")).click(); |
| 225 | + |
| 226 | + String userText = "Selenium automates browsers"; |
| 227 | + browsingContext.handleUserPrompt(true, userText); |
| 228 | + |
| 229 | + Assertions.assertTrue(driver.getPageSource().contains(userText)); |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + void testDismissUserPromptWithText() { |
| 234 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 235 | + |
| 236 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html"); |
| 237 | + |
| 238 | + driver.findElement(By.id("prompt-with-default")).click(); |
| 239 | + |
| 240 | + String userText = "Selenium automates browsers"; |
| 241 | + browsingContext.handleUserPrompt(false, userText); |
| 242 | + |
| 243 | + Assertions.assertFalse(driver.getPageSource().contains(userText)); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + void textCaptureScreenshot() { |
| 248 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 249 | + |
| 250 | + driver.get("https://www.selenium.dev/selenium/web/alerts.html"); |
| 251 | + |
| 252 | + String screenshot = browsingContext.captureScreenshot(); |
| 253 | + |
| 254 | + Assertions.assertTrue(screenshot.length() > 0); |
| 255 | + } |
| 256 | + |
| 257 | + @Test |
| 258 | + void textCaptureViewportScreenshot() { |
| 259 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 260 | + |
| 261 | + driver.get("https://www.selenium.dev/selenium/web/coordinates_tests/simple_page.html"); |
| 262 | + |
| 263 | + WebElement element = driver.findElement(By.id("box")); |
| 264 | + Rectangle elementRectangle = element.getRect(); |
| 265 | + |
| 266 | + String screenshot = |
| 267 | + browsingContext.captureBoxScreenshot( |
| 268 | + elementRectangle.getX(), elementRectangle.getY(), 5, 5); |
| 269 | + |
| 270 | + Assertions.assertTrue(screenshot.length() > 0); |
| 271 | + } |
| 272 | + |
| 273 | + @Test |
| 274 | + void textCaptureElementScreenshot() { |
| 275 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 276 | + |
| 277 | + driver.get("https://www.selenium.dev/selenium/web/formPage.html"); |
| 278 | + WebElement element = driver.findElement(By.id("checky")); |
| 279 | + |
| 280 | + String screenshot = browsingContext.captureElementScreenshot(((RemoteWebElement) element).getId()); |
| 281 | + |
| 282 | + Assertions.assertTrue(screenshot.length() > 0); |
| 283 | + } |
| 284 | + |
| 285 | + @Test |
| 286 | + void textSetViewport() { |
| 287 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 288 | + driver.get("https://www.selenium.dev/selenium/web/formPage.html"); |
| 289 | + |
| 290 | + browsingContext.setViewport(250, 300); |
| 291 | + |
| 292 | + List<Long> newViewportSize = |
| 293 | + (List<Long>) |
| 294 | + ((JavascriptExecutor) driver) |
| 295 | + .executeScript("return [window.innerWidth, window.innerHeight];"); |
| 296 | + |
| 297 | + Assertions.assertEquals(250, newViewportSize.get(0)); |
| 298 | + Assertions.assertEquals(300, newViewportSize.get(1)); |
| 299 | + } |
| 300 | + |
| 301 | + @Test |
| 302 | + @Disabled("Supported by Firefox Nightly 124") |
| 303 | + void textSetViewportWithDevicePixelRatio() { |
| 304 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 305 | + driver.get("https://www.selenium.dev/selenium/web/formPage.html"); |
| 306 | + |
| 307 | + browsingContext.setViewport(250, 300, 5); |
| 308 | + |
| 309 | + Long newDevicePixelRatio = |
| 310 | + (Long) ((JavascriptExecutor) driver).executeScript("return window.devicePixelRatio"); |
| 311 | + |
| 312 | + Assertions.assertEquals(5, newDevicePixelRatio); |
| 313 | + } |
| 314 | + |
| 315 | + @Test |
| 316 | + void canPrintPage() { |
| 317 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 318 | + |
| 319 | + driver.get("https://www.selenium.dev/selenium/web/formPage.html"); |
| 320 | + PrintOptions printOptions = new PrintOptions(); |
| 321 | + |
| 322 | + String printPage = browsingContext.print(printOptions); |
| 323 | + |
| 324 | + Assertions.assertTrue(printPage.length() > 0); |
| 325 | + } |
| 326 | + |
| 327 | + @Test |
| 328 | + @Disabled("Supported by Firefox Nightly 124") |
| 329 | + void testNavigateBackInTheBrowserHistory() { |
| 330 | + BrowsingContext browsingContext = new BrowsingContext(driver, driver.getWindowHandle()); |
| 331 | + browsingContext.navigate("https://www.selenium.dev/selenium/web/formPage.html", ReadinessState.COMPLETE); |
| 332 | + |
| 333 | + wait.until(visibilityOfElementLocated(By.id("imageButton"))).submit(); |
| 334 | + wait.until(titleIs("We Arrive Here")); |
| 335 | + |
| 336 | + browsingContext.back(); |
| 337 | + Assertions.assertTrue(driver.getPageSource().contains("We Leave From Here")); |
| 338 | + } |
144 | 339 | }
|
0 commit comments