Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[java] Implementing a convenient devtools method Network.setUserAgent
- Loading branch information
Showing
8 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
java/client/test/org/openqa/selenium/devtools/ChangeUserAgentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.devtools; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.Test; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.JavascriptExecutor; | ||
import org.openqa.selenium.devtools.idealized.Network; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.stream.Collectors; | ||
|
||
public class ChangeUserAgentTest extends DevToolsTestBase { | ||
|
||
@Test | ||
public void canChangeUserAgent() throws InterruptedException, ExecutionException, TimeoutException { | ||
devTools.getDomains().network().setUserAgent( | ||
new Network.UserAgent("Camembert 1.0") | ||
.platform("FreeBSD").acceptLanguage("da, en-gb, *")); | ||
driver.get(appServer.whereIs("/echo")); | ||
|
||
Map<String, String> headers = driver.findElements(By.cssSelector("#headers tr")).stream() | ||
.map(row -> row.findElements(By.tagName("td"))) | ||
.collect(Collectors.toMap(cells -> cells.get(0).getText(), cells -> cells.get(1).getText())); | ||
String userAgent = driver.findElements(By.cssSelector("#headers tr")).stream() | ||
.map(row -> row.findElements(By.tagName("td"))) | ||
.filter(cells -> cells.get(0).getText().equals("User-Agent")) | ||
.map(cells -> cells.get(1).getText()) | ||
.findFirst().orElseThrow(); | ||
assertThat(headers).containsEntry("User-Agent", "Camembert 1.0"); | ||
assertThat(headers).containsEntry("Accept-Language", "da, en-gb;q=0.9, *;q=0.8"); | ||
Object platform = ((JavascriptExecutor) driver).executeScript("return window.navigator.platform"); | ||
assertThat(platform).isEqualTo("FreeBSD"); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
java/client/test/org/openqa/selenium/environment/webserver/EchoHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.environment.webserver; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME; | ||
|
||
import com.google.common.base.Splitter; | ||
|
||
import org.openqa.selenium.Cookie; | ||
import org.openqa.selenium.remote.http.Contents; | ||
import org.openqa.selenium.remote.http.HttpHandler; | ||
import org.openqa.selenium.remote.http.HttpRequest; | ||
import org.openqa.selenium.remote.http.HttpResponse; | ||
|
||
import java.io.UncheckedIOException; | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Collection; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class EchoHandler implements HttpHandler { | ||
|
||
private static final String EPOCH_START = RFC_1123_DATE_TIME.format( | ||
ZonedDateTime.ofInstant(Instant.ofEpochMilli(0), ZoneId.of("UTC"))); | ||
private static final String RESPONSE_STRING = | ||
"<html><head><title>Done</title></head><body>" | ||
+ "<h1>Method: <span id='method'>%s</span></h1>" | ||
+ "<h1>Headers</h1><table id='headers'><tbody>%s</tbody></table>" | ||
+ "<h1>Body:</h1><pre>%s</pre>" | ||
+ "</body></html>"; | ||
|
||
@Override | ||
public HttpResponse execute(HttpRequest request) throws UncheckedIOException { | ||
HttpResponse response = new HttpResponse(); | ||
response.setHeader("Content-Type", "text/html"); | ||
//Dont Cache Anything at the browser | ||
response.setHeader("Cache-Control","no-cache"); | ||
response.setHeader("Pragma","no-cache"); | ||
response.setHeader ("Expires", EPOCH_START); | ||
|
||
String method = request.getMethod().toString(); | ||
String headers = StreamSupport.stream(request.getHeaderNames().spliterator(), false) | ||
.flatMap(name -> StreamSupport.stream(request.getHeaders(name).spliterator(), false) | ||
.map(value -> String.format("<tr><td>%s</td><td>%s</td></tr>", name, value))) | ||
.collect(Collectors.joining("")); | ||
String body = Contents.utf8String(request.getContent()); | ||
|
||
response.setContent(Contents.string(String.format(RESPONSE_STRING, method, headers, body), UTF_8)); | ||
|
||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters