|
| 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.bidi.emulation; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.Optional; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.openqa.selenium.WindowType; |
| 26 | +import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; |
| 27 | +import org.openqa.selenium.bidi.browsingcontext.CreateContextParameters; |
| 28 | +import org.openqa.selenium.bidi.browsingcontext.ReadinessState; |
| 29 | +import org.openqa.selenium.bidi.module.Browser; |
| 30 | +import org.openqa.selenium.bidi.module.Script; |
| 31 | +import org.openqa.selenium.bidi.script.EvaluateResult; |
| 32 | +import org.openqa.selenium.bidi.script.EvaluateResultSuccess; |
| 33 | +import org.openqa.selenium.testing.JupiterTestBase; |
| 34 | +import org.openqa.selenium.testing.NeedsFreshDriver; |
| 35 | + |
| 36 | +public class SetUserAgentOverrideTest extends JupiterTestBase { |
| 37 | + |
| 38 | + private String getBrowserUserAgent(String contextId, Script script) { |
| 39 | + EvaluateResult result = |
| 40 | + script.evaluateFunctionInBrowsingContext( |
| 41 | + contextId, "navigator.userAgent", false, Optional.empty()); |
| 42 | + return ((EvaluateResultSuccess) result).getResult().getValue().get().toString(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @NeedsFreshDriver |
| 47 | + void canSetUserAgentOverrideWithContexts() { |
| 48 | + BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); |
| 49 | + String contextId = context.getId(); |
| 50 | + |
| 51 | + String url = appServer.whereIs("formPage.html"); |
| 52 | + context.navigate(url, ReadinessState.COMPLETE); |
| 53 | + |
| 54 | + Script script = new Script(driver); |
| 55 | + String initialUserAgent = getBrowserUserAgent(contextId, script); |
| 56 | + |
| 57 | + Emulation emulation = new Emulation(driver); |
| 58 | + String customUserAgent = "Mozilla/5.0 (Custom Test Agent)"; |
| 59 | + emulation.setUserAgentOverride( |
| 60 | + new SetUserAgentOverrideParameters(customUserAgent).contexts(List.of(contextId))); |
| 61 | + |
| 62 | + String overriddenUserAgent = getBrowserUserAgent(contextId, script); |
| 63 | + assertThat(overriddenUserAgent).isEqualTo(customUserAgent); |
| 64 | + |
| 65 | + // Clear the override |
| 66 | + emulation.setUserAgentOverride( |
| 67 | + new SetUserAgentOverrideParameters(null).contexts(List.of(contextId))); |
| 68 | + |
| 69 | + String restoredUserAgent = getBrowserUserAgent(contextId, script); |
| 70 | + assertThat(restoredUserAgent).isEqualTo(initialUserAgent); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @NeedsFreshDriver |
| 75 | + void canSetUserAgentOverrideWithUserContexts() { |
| 76 | + Browser browser = new Browser(driver); |
| 77 | + String userContext = browser.createUserContext(); |
| 78 | + |
| 79 | + try { |
| 80 | + BrowsingContext context = |
| 81 | + new BrowsingContext( |
| 82 | + driver, new CreateContextParameters(WindowType.TAB).userContext(userContext)); |
| 83 | + String contextId = context.getId(); |
| 84 | + |
| 85 | + try { |
| 86 | + driver.switchTo().window(contextId); |
| 87 | + String url = appServer.whereIs("formPage.html"); |
| 88 | + context.navigate(url, ReadinessState.COMPLETE); |
| 89 | + |
| 90 | + Script script = new Script(driver); |
| 91 | + String initialUserAgent = getBrowserUserAgent(contextId, script); |
| 92 | + |
| 93 | + Emulation emulation = new Emulation(driver); |
| 94 | + String customUserAgent = "Mozilla/5.0 (Custom User Context Agent)"; |
| 95 | + emulation.setUserAgentOverride( |
| 96 | + new SetUserAgentOverrideParameters(customUserAgent).userContexts(List.of(userContext))); |
| 97 | + |
| 98 | + String overriddenUserAgent = getBrowserUserAgent(contextId, script); |
| 99 | + assertThat(overriddenUserAgent).isEqualTo(customUserAgent); |
| 100 | + |
| 101 | + // Clear the override |
| 102 | + emulation.setUserAgentOverride( |
| 103 | + new SetUserAgentOverrideParameters(null).userContexts(List.of(userContext))); |
| 104 | + |
| 105 | + String restoredUserAgent = getBrowserUserAgent(contextId, script); |
| 106 | + assertThat(restoredUserAgent).isEqualTo(initialUserAgent); |
| 107 | + } finally { |
| 108 | + context.close(); |
| 109 | + } |
| 110 | + } finally { |
| 111 | + browser.removeUserContext(userContext); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments