|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 21 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.openqa.selenium.environment.webserver.AppServer; |
| 27 | +import org.openqa.selenium.environment.webserver.NettyAppServer; |
| 28 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 29 | +import org.openqa.selenium.testing.JupiterTestBase; |
| 30 | +import org.openqa.selenium.testing.NotYetImplemented; |
| 31 | +import org.openqa.selenium.testing.drivers.Browser; |
| 32 | + |
| 33 | +class WebNetworkTest extends JupiterTestBase { |
| 34 | + |
| 35 | + private String page; |
| 36 | + private AppServer server; |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + public void setUp() { |
| 40 | + server = new NettyAppServer(); |
| 41 | + server.start(); |
| 42 | + } |
| 43 | + |
| 44 | + @AfterEach |
| 45 | + public void cleanUp() { |
| 46 | + driver.quit(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @NotYetImplemented(Browser.CHROME) |
| 51 | + @NotYetImplemented(Browser.EDGE) |
| 52 | + void canAddAuthenticationHandler() { |
| 53 | + ((RemoteWebDriver) driver) |
| 54 | + .network() |
| 55 | + .addAuthenticationHandler(new UsernameAndPassword("test", "test")); |
| 56 | + |
| 57 | + page = server.whereIs("basicAuth"); |
| 58 | + driver.get(page); |
| 59 | + |
| 60 | + assertThat(driver.findElement(By.tagName("h1")).getText()).isEqualTo("authorized"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @NotYetImplemented(Browser.CHROME) |
| 65 | + @NotYetImplemented(Browser.EDGE) |
| 66 | + void canRemoveAuthenticationHandler() { |
| 67 | + long id = |
| 68 | + ((RemoteWebDriver) driver) |
| 69 | + .network() |
| 70 | + .addAuthenticationHandler(new UsernameAndPassword("test", "test")); |
| 71 | + |
| 72 | + ((RemoteWebDriver) driver).network().removeAuthenticationHandler(id); |
| 73 | + page = server.whereIs("basicAuth"); |
| 74 | + driver.get(page); |
| 75 | + |
| 76 | + assertThatExceptionOfType(UnhandledAlertException.class) |
| 77 | + .isThrownBy(() -> driver.findElement(By.tagName("h1"))); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + @NotYetImplemented(Browser.CHROME) |
| 82 | + @NotYetImplemented(Browser.EDGE) |
| 83 | + void canClearAuthenticationHandlers() { |
| 84 | + ((RemoteWebDriver) driver) |
| 85 | + .network() |
| 86 | + .addAuthenticationHandler(new UsernameAndPassword("test", "test")); |
| 87 | + |
| 88 | + ((RemoteWebDriver) driver) |
| 89 | + .network() |
| 90 | + .addAuthenticationHandler(new UsernameAndPassword("test1", "test1")); |
| 91 | + |
| 92 | + ((RemoteWebDriver) driver).network().clearAuthenticationHandlers(); |
| 93 | + page = server.whereIs("basicAuth"); |
| 94 | + driver.get(page); |
| 95 | + |
| 96 | + assertThatExceptionOfType(UnhandledAlertException.class) |
| 97 | + .isThrownBy(() -> driver.findElement(By.tagName("h1"))); |
| 98 | + } |
| 99 | +} |
0 commit comments