Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@
<dependency>
<groupId>com.github.aquality-automation</groupId>
<artifactId>aquality-selenium-core</artifactId>
<version>4.10.0</version>
<version>4.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.18.0</version>
<version>3.20.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.devtools.v142.performance.Performance;
import org.openqa.selenium.devtools.v142.performance.model.Metric;
import org.openqa.selenium.devtools.v143.performance.Performance;
import org.openqa.selenium.devtools.v143.performance.model.Metric;

import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.v142.dom.model.RGBA;
import org.openqa.selenium.devtools.v142.emulation.Emulation;
import org.openqa.selenium.devtools.v142.emulation.model.MediaFeature;
import org.openqa.selenium.devtools.v142.emulation.model.ScreenOrientation;
import org.openqa.selenium.devtools.v143.dom.model.RGBA;
import org.openqa.selenium.devtools.v143.emulation.Emulation;
import org.openqa.selenium.devtools.v143.emulation.model.MediaFeature;
import org.openqa.selenium.devtools.v143.emulation.model.ScreenOrientation;

import java.util.Collections;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import org.openqa.selenium.devtools.idealized.Javascript;
import org.openqa.selenium.devtools.idealized.ScriptId;
import org.openqa.selenium.devtools.idealized.target.model.SessionID;
import org.openqa.selenium.devtools.v142.page.Page;
import org.openqa.selenium.devtools.v142.page.model.ScriptIdentifier;
import org.openqa.selenium.devtools.v142.runtime.Runtime;
import org.openqa.selenium.devtools.v143.page.Page;
import org.openqa.selenium.devtools.v143.page.model.ScriptIdentifier;
import org.openqa.selenium.devtools.v143.runtime.Runtime;
import org.openqa.selenium.logging.EventType;
import org.openqa.selenium.logging.HasLogEvents;
import org.openqa.selenium.remote.Augmenter;
Expand Down
100 changes: 82 additions & 18 deletions src/main/java/aquality/selenium/browser/devtools/NetworkHandling.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.devtools.NetworkInterceptor;
import org.openqa.selenium.devtools.idealized.Network;
import org.openqa.selenium.devtools.v142.network.model.*;
import org.openqa.selenium.devtools.v143.network.model.*;
import org.openqa.selenium.remote.http.*;

import java.net.URI;
Expand All @@ -22,11 +22,11 @@

import static aquality.selenium.browser.AqualityServices.getBrowser;
import static aquality.selenium.logging.LocalizedLoggerUtility.logByLevel;
import static org.openqa.selenium.devtools.v142.network.Network.*;
import static org.openqa.selenium.devtools.v143.network.Network.*;

/**
* DevTools commands for version-independent network interception.
* For more information, see {@link org.openqa.selenium.devtools.v142.network.Network} and {@link Network}.
* For more information, see {@link org.openqa.selenium.devtools.v143.network.Network} and {@link Network}.
*/
public class NetworkHandling {
public static final String LOC_NETWORK_INTERCEPTOR_START = "loc.browser.network.interceptor.start";
Expand Down Expand Up @@ -62,6 +62,7 @@ public void stopMonitoring() {

/**
* Overrides the values of user agent.
*
* @param userAgent User agent to use.
*/
public void setUserAgent(String userAgent) {
Expand All @@ -71,6 +72,7 @@ public void setUserAgent(String userAgent) {

/**
* Overrides the values of user agent.
*
* @param userAgent User agent to use.
*/
public void setUserAgent(Network.UserAgent userAgent) {
Expand All @@ -80,7 +82,8 @@ public void setUserAgent(Network.UserAgent userAgent) {

/**
* Add basic authentication handler.
* @param whenThisMatches URI matcher.
*
* @param whenThisMatches URI matcher.
* @param useTheseCredentials parameters, such as URI matcher and credentials.
*/
public void addAuthHandler(Predicate<URI> whenThisMatches, Supplier<Credentials> useTheseCredentials) {
Expand All @@ -90,6 +93,7 @@ public void addAuthHandler(Predicate<URI> whenThisMatches, Supplier<Credentials>

/**
* Add basic authentication handler.
*
* @param hostPart part of the host name for URI matcher.
* @param username authentication username.
* @param password authentication password.
Expand Down Expand Up @@ -117,6 +121,7 @@ public void resetNetworkFilter() {

/**
* Starts traffic interception with specified filter.
*
* @param filter HTTP filter.
*/
public void interceptTrafficWith(Filter filter) {
Expand All @@ -126,6 +131,7 @@ public void interceptTrafficWith(Filter filter) {

/**
* Adds listener to network request sent event.
*
* @param listener a listener to add.
*/
public void addRequestListener(Consumer<RequestWillBeSent> listener) {
Expand All @@ -136,6 +142,7 @@ public void addRequestListener(Consumer<RequestWillBeSent> listener) {

/**
* Adds listener to network response received event.
*
* @param listener a listener to add.
*/
public void addResponseListener(Consumer<ResponseReceived> listener) {
Expand Down Expand Up @@ -200,7 +207,7 @@ private Consumer<ResponseReceived> getResponseLogger(HttpExchangeLoggingOptions
formatHeaders(response.getHeaders()));
}
if (loggingOptions.getResponseBody().isEnabled()) {
String responseBody = tools.sendCommand(org.openqa.selenium.devtools.v142.network.Network.getResponseBody(requestId)).getBody();
String responseBody = tools.sendCommand(getResponseBody(requestId)).getBody();
if (StringUtils.isNotEmpty(responseBody)) {
logByLevel(loggingOptions.getResponseBody().getLogLevel(),
"loc.browser.network.event.responsereceived.log.body",
Expand All @@ -212,6 +219,7 @@ private Consumer<ResponseReceived> getResponseLogger(HttpExchangeLoggingOptions

/**
* Enables HTTP Request/Response logging.
*
* @param loggingOptions logging parameters {@link HttpExchangeLoggingOptions}.
*/
public void enableHttpExchangeLogging(HttpExchangeLoggingOptions loggingOptions) {
Expand All @@ -221,6 +229,7 @@ public void enableHttpExchangeLogging(HttpExchangeLoggingOptions loggingOptions)

/**
* Starts network interceptor.
*
* @param httpHandler HTTP handler.
* @return an instance of {@link NetworkInterceptor}.
*/
Expand All @@ -231,6 +240,7 @@ public NetworkInterceptor startNetworkInterceptor(HttpHandler httpHandler) {

/**
* Starts network interceptor.
*
* @param filter network filter.
* @return an instance of {@link NetworkInterceptor}.
*/
Expand All @@ -241,6 +251,7 @@ public NetworkInterceptor startNetworkInterceptor(Filter filter) {

/**
* Starts network interceptor.
*
* @param routable a filter with matcher.
* @return an instance of {@link NetworkInterceptor}.
*/
Expand All @@ -251,8 +262,9 @@ public NetworkInterceptor startNetworkInterceptor(Routable routable) {

/**
* Starts network interceptor.
*
* @param requestMatcher predicate to match the request.
* @param handler handler for matched requests.
* @param handler handler for matched requests.
* @return an instance of {@link NetworkInterceptor}.
*/
public NetworkInterceptor startNetworkInterceptor(Predicate<HttpRequest> requestMatcher, Supplier<HttpHandler> handler) {
Expand All @@ -261,6 +273,7 @@ public NetworkInterceptor startNetworkInterceptor(Predicate<HttpRequest> request

/**
* Intercepts any request with predefined response.
*
* @param response HTTP response.
* @return an instance of {@link NetworkInterceptor}.
*/
Expand All @@ -270,7 +283,8 @@ public NetworkInterceptor interceptAllRequests(HttpResponse response) {

/**
* Adds request transformer.
* @param requestMatcher predicate to match the request.
*
* @param requestMatcher predicate to match the request.
* @param requestTransformer function to transform the request.
* @return an instance of {@link NetworkInterceptor}.
*/
Expand All @@ -281,6 +295,7 @@ public NetworkInterceptor addRequestTransformer(Predicate<HttpRequest> requestMa

/**
* Adds request handler.
*
* @param requestMatcher predicate to match the request.
* @param requestHandler handler for matched requests.
* @return an instance of {@link NetworkInterceptor}.
Expand All @@ -292,7 +307,8 @@ public NetworkInterceptor addRequestHandler(Predicate<HttpRequest> requestMatche

/**
* Adds response handler.
* @param responseMatcher predicate to match the response.
*
* @param responseMatcher predicate to match the response.
* @param responseTransformer function to transform the response.
* @return an instance of {@link NetworkInterceptor}.
*/
Expand All @@ -309,35 +325,83 @@ public NetworkInterceptor addResponseHandler(Predicate<HttpResponse> responseMat
* Currently, Selenium supports only a single network interceptor. Any new Network interceptor will override the previous one.
* And on {@link NetworkInterceptor#close()} the NetworkInterceptor class just calls the {@link Network#resetNetworkFilter()}, so it's enough to call it.
* If multiple network interceptors at the same time would be allowed, we may want to store all
* {@link NetworkInterceptor} classes to call {@link NetworkInterceptor#close()} for each.
* {@link NetworkInterceptor} classes to call {@link NetworkInterceptor#close()} for each.
*/
public void clearNetworkInterceptor() {
resetNetworkFilter();
}

/**
* Activates emulation of network conditions.
* @param offline True to emulate internet disconnection.
* @param latency Minimum latency from request sent to response headers received (ms).
*
* @param offline True to emulate internet disconnection.
* @param latency Minimum latency from request sent to response headers received (ms).
* @param downloadThroughput Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
* @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
* @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
* @deprecated This command is deprecated in favor of the {@link #emulateConditionsByRule(Boolean, List)}
* and {@link #overrideState} commands, which can be used together to the same effect.
*/
@Deprecated
public void emulateConditions(Boolean offline, Number latency, Number downloadThroughput, Number uploadThroughput) {
tools.sendCommand(enable(Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()));
tools.sendCommand(emulateNetworkConditions(offline, latency, downloadThroughput, uploadThroughput, Optional.empty(),
Optional.empty(), Optional.empty(), Optional.empty()));
}

/**
* Activates emulation of network conditions for individual requests using URL match patterns.
* Unlike the deprecated Network.emulateNetworkConditions this method does not affect `navigator` state.
* Use Network.overrideNetworkState to explicitly modify `navigator` behavior.
*
* @param offline True to emulate internet disconnection.
* @param matchedNetworkConditions List of network conditions to apply.
*/
public void emulateConditionsByRule(Boolean offline, List<NetworkConditions> matchedNetworkConditions) {
tools.sendCommand(enable(Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()));
tools.sendCommand(emulateNetworkConditionsByRule(offline, matchedNetworkConditions));
}

/**
* Override the state of navigator.onLine and navigator.connection.
*
* @param offline True to emulate internet disconnection.
* @param latency Minimum latency from request sent to response headers received (ms).
* @param downloadThroughput Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
* @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
*/
public void overrideState(Boolean offline, Number latency, Number downloadThroughput, Number uploadThroughput) {
tools.sendCommand(enable(Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()));
tools.sendCommand(overrideNetworkState(offline, latency, downloadThroughput, uploadThroughput, Optional.empty()));
}

/**
* Override the state of navigator.onLine and navigator.connection.
*
* @param offline True to emulate internet disconnection.
* @param latency Minimum latency from request sent to response headers received (ms).
* @param downloadThroughput Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
* @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
* @param connectionType Connection type if known.
*/
public void overrideState(Boolean offline, Number latency, Number downloadThroughput, Number uploadThroughput, ConnectionType connectionType) {
tools.sendCommand(enable(Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()));
tools.sendCommand(overrideNetworkState(offline, latency, downloadThroughput, uploadThroughput, Optional.of(connectionType)));
}

/**
* Activates emulation of network conditions.
* @param offline True to emulate internet disconnection.
* @param latency Minimum latency from request sent to response headers received (ms).
*
* @param offline True to emulate internet disconnection.
* @param latency Minimum latency from request sent to response headers received (ms).
* @param downloadThroughput Maximal aggregated download throughput (bytes/sec). -1 disables download throttling.
* @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
* @param connectionType Connection type if known.
* Possible values: "none", "cellular2g", "cellular3g", "cellular4g", "bluetooth", "ethernet",
* "wifi", "wimax", "other".
* @param uploadThroughput Maximal aggregated upload throughput (bytes/sec). -1 disables upload throttling.
* @param connectionType Connection type if known.
* Possible values: "none", "cellular2g", "cellular3g", "cellular4g", "bluetooth", "ethernet",
* "wifi", "wimax", "other".
* @deprecated This command is deprecated in favor of the {@link #emulateConditionsByRule(Boolean, List)}
* and {@link #overrideState} commands, which can be used together to the same effect.
*/
@Deprecated
public void emulateConditions(Boolean offline, Number latency, Number downloadThroughput, Number uploadThroughput, String connectionType) {
tools.sendCommand(enable(Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()));
tools.sendCommand(emulateNetworkConditions(offline, latency, downloadThroughput, uploadThroughput,
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/automationpractice/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ private Constants(){
}

public static final String URL_AUTOMATIONPRACTICE = "http://automationpractice.com/index.php";
public static final String URL_MYLOCATIONORG = "https://my-location.org";
public static final String URL_MYLOCATIONORG = "https://whereamirightnow.org";
}
8 changes: 4 additions & 4 deletions src/test/java/forms/MyLocationForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

public class MyLocationForm extends Form {

private final ILabel lblLatitude = getElementFactory().getLabel(By.id("latitude"), "Latitude");
private final ILabel lblLongitude = getElementFactory().getLabel(By.id("longitude"), "Longitude");
private final IButton btnConsent = getElementFactory().getButton(By.xpath("//button[@aria-label='Consent']"), "Consent");
private final ILabel lblLatitude = getElementFactory().getLabel(By.xpath("//*[@id='latitude'] | (//td[contains(text(), 'Latitude')]/following-sibling::td)[1]"), "Latitude");
private final ILabel lblLongitude = getElementFactory().getLabel(By.xpath("//*[@id='longitude'] | (//td[contains(text(), 'Longitude')]/following-sibling::td)[1]"), "Longitude");
private final IButton btnConsent = getElementFactory().getButton(By.xpath("//button[@aria-label='Consent' or contains(@class,'fc-cta-consent')]"), "Consent");

public MyLocationForm() {
super(By.xpath("//h1[contains(text(),'My Location')]"), "My Location");
super(By.xpath("//*[contains(text(),'Location')]"), "My Location");
}

public double getLatitude() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import aquality.selenium.browser.AqualityServices;
import aquality.selenium.browser.devtools.EmulationHandling;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.devtools.v141.emulation.Emulation;
import org.openqa.selenium.devtools.v141.emulation.model.DisplayFeature;
import org.openqa.selenium.devtools.v142.emulation.Emulation;
import org.openqa.selenium.devtools.v142.emulation.model.DisplayFeature;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down
Loading
Loading