Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright Werner Punz
*
* Licensed 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.
*/
// Original code stemming 100% from me, hence relicense from EPL
package org.jboss.cdi.tck.selenium;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.output.NullOutputStream;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverLogLevel;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chromium.ChromiumNetworkConditions;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v108.network.Network;
import org.openqa.selenium.devtools.v108.network.model.Request;
import org.openqa.selenium.devtools.v108.network.model.RequestId;
import org.openqa.selenium.devtools.v108.network.model.ResponseReceived;
import org.openqa.selenium.devtools.v108.network.model.TimeSinceEpoch;
import org.openqa.selenium.html5.LocalStorage;
import org.openqa.selenium.html5.Location;
import org.openqa.selenium.html5.SessionStorage;
import org.openqa.selenium.interactions.Sequence;
import org.openqa.selenium.logging.EventType;
import org.openqa.selenium.mobile.NetworkConnection;
import org.openqa.selenium.print.PrintOptions;
import org.openqa.selenium.remote.*;
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticator;
import org.openqa.selenium.virtualauthenticator.VirtualAuthenticatorOptions;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
/**
* Extended driver which we need for getting
* the http response code
* and the http response
* without having to revert to proxy solutions
*
* We need access top the response body and response
* code from always the last access
*
* We use the chrome dev tools to access the data but we isolate
* the new functionality in an interface, so other drivers must apply something different
* to get the results
*
* @see also https://medium.com/codex/selenium4-a-peek-into-chrome-devtools-92bca6de55e0
*/
class HttpCycleData {
public RequestId requestId;
public Request request;
public ResponseReceived responseReceived;
}
@SuppressWarnings("unused")
public class ChromeDevtoolsDriver implements ExtendedWebDriver {
ChromeDriver delegate;
List cycleData = new CopyOnWriteArrayList<>();
String lastGet;
public ChromeDevtoolsDriver(ChromeOptions options) {
ChromeDriverService chromeDriverService = new ChromeDriverService.Builder().build();
chromeDriverService.sendOutputTo(NullOutputStream.NULL_OUTPUT_STREAM);
delegate = new ChromeDriver(chromeDriverService, options);
}
/**
* initializes the extended functionality
*/
public void postInit() {
DevTools devTools = getDevTools();
//we store always the last request for further reference
initNetworkListeners(devTools);
initDevTools(devTools);
disableCache(devTools);
}
private static void disableCache(DevTools devTools) {
devTools.send(Network.setCacheDisabled(true));
}
private static void initDevTools(DevTools devTools) {
try {
devTools.createSession();
devTools.send(Network.clearBrowserCache());
} catch (TimeoutException ex) {
Logger log = Logger.getLogger(ChromeDevtoolsDriver.class.getName());
log.warning("Init timeout error, can happen, " +
"if the driver already has been used, can be safely ignore");
}
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
}
private void initNetworkListeners(DevTools devTools) {
devTools.addListener(Network.requestWillBeSent(),
entry -> {
// the jsf ajax only targets itself
// that way we can filter out resource requests early
if (!entry.getRequest().getUrl().contains(lastGet)) {
return;
}
HttpCycleData data = new HttpCycleData();
data.requestId = entry.getRequestId();
data.request = entry.getRequest();
cycleData.add(data);
});
devTools.addListener(Network.responseReceived(), entry -> {
RequestId requestId = entry.getRequestId();
//only in case of a match we add response to request
//so we only cover our ajax cycle and the original get
Optional found = cycleData.stream().filter(item -> item.requestId.toJson().equals(requestId.toJson())).findFirst();
found.ifPresent(httpCycleData -> httpCycleData.responseReceived = entry);
});
}
public Capabilities getCapabilities() {
return delegate.getCapabilities();
}
public void setFileDetector(FileDetector detector) {
delegate.setFileDetector(detector);
}
public void onLogEvent(EventType kind) {
delegate.onLogEvent(kind);
}
public void register(Predicate whenThisMatches, Supplier useTheseCredentials) {
delegate.register(whenThisMatches, useTheseCredentials);
}
public LocalStorage getLocalStorage() {
return delegate.getLocalStorage();
}
public SessionStorage getSessionStorage() {
return delegate.getSessionStorage();
}
public Location location() {
return delegate.location();
}
public void setLocation(Location location) {
delegate.setLocation(location);
}
public NetworkConnection.ConnectionType getNetworkConnection() {
return delegate.getNetworkConnection();
}
public NetworkConnection.ConnectionType setNetworkConnection(NetworkConnection.ConnectionType type) {
return delegate.setNetworkConnection(type);
}
public void launchApp(String id) {
delegate.launchApp(id);
}
public Map executeCdpCommand(String commandName, Map parameters) {
return delegate.executeCdpCommand(commandName, parameters);
}
public Optional maybeGetDevTools() {
return delegate.maybeGetDevTools();
}
public List