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.
/*
* The MIT License
*
* Copyright 2018 Intuit Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.intuit.karate.driver;
import com.intuit.karate.Constants;
import com.intuit.karate.FileUtils;
import com.intuit.karate.Json;
import com.intuit.karate.JsonUtils;
import com.intuit.karate.Logger;
import com.intuit.karate.StringUtils;
import com.intuit.karate.core.Feature;
import com.intuit.karate.core.MockHandler;
import com.intuit.karate.core.ScenarioEngine;
import com.intuit.karate.core.Variable;
import com.intuit.karate.graal.JsValue;
import com.intuit.karate.http.HttpRequest;
import com.intuit.karate.http.ResourceType;
import com.intuit.karate.http.Response;
import com.intuit.karate.http.WebSocketClient;
import com.intuit.karate.http.WebSocketOptions;
import com.intuit.karate.shell.Command;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import org.graalvm.polyglot.Value;
/**
*
* @author pthomas3
*/
public abstract class DevToolsDriver implements Driver {
protected final DriverOptions options;
protected final Command command;
protected final WebSocketClient client;
private boolean terminated;
private final DevToolsWait wait;
protected final String rootFrameId;
private Integer windowId;
private String windowState;
private Integer executionContextId;
protected String sessionId;
protected boolean domContentEventFired;
protected final Set framesStillLoading = new HashSet();
private final Map frameSessions = new HashMap();
private boolean submit;
protected String currentDialogText;
private int nextId;
public int nextId() {
return ++nextId;
}
private MockHandler mockHandler;
protected final Logger logger;
protected DevToolsDriver(DriverOptions options, Command command, String webSocketUrl) {
logger = options.driverLogger;
this.options = options;
this.command = command;
this.wait = new DevToolsWait(this, options);
int pos = webSocketUrl.lastIndexOf('/');
rootFrameId = webSocketUrl.substring(pos + 1);
logger.debug("root frame id: {}", rootFrameId);
WebSocketOptions wsOptions = new WebSocketOptions(webSocketUrl);
wsOptions.setMaxPayloadSize(options.maxPayloadSize);
wsOptions.setTextConsumer(text -> {
if (logger.isTraceEnabled()) {
logger.trace("<< {}", text);
} else {
// to avoid swamping the console when large base64 encoded binary responses happen
logger.debug("<< {}", StringUtils.truncate(text, 1024, true));
}
Map map = Json.of(text).value();
DevToolsMessage dtm = new DevToolsMessage(this, map);
receive(dtm);
});
client = new WebSocketClient(wsOptions, logger);
}
@Override
public Driver timeout(Integer millis) {
options.setTimeout(millis);
return this;
}
@Override
public Driver timeout() {
return timeout(null);
}
public DevToolsMessage method(String method) {
return new DevToolsMessage(this, method);
}
// this can be used for exploring / sending any raw message !
public Map send(Map map) {
DevToolsMessage dtm = new DevToolsMessage(this, map);
dtm.setId(nextId());
return sendAndWait(dtm, null).toMap();
}
public void send(DevToolsMessage dtm) {
String json = JsonUtils.toJson(dtm.toMap());
logger.debug(">> {}", json);
client.send(json);
}
public DevToolsMessage sendAndWait(DevToolsMessage dtm, Predicate condition) {
boolean wasSubmit = submit;
if (condition == null && submit) {
submit = false;
condition = DevToolsWait.ALL_FRAMES_LOADED;
}
// do stuff inside wait to avoid missing messages
DevToolsMessage result = wait.send(dtm, condition);
if (result == null && !wasSubmit) {
throw new RuntimeException("failed to get reply for: " + dtm);
}
return result;
}
public void receive(DevToolsMessage dtm) {
if (dtm.methodIs("Page.domContentEventFired")) {
domContentEventFired = true;
logger.trace("** set dom ready flag to true");
}
if (dtm.methodIs("Page.javascriptDialogOpening")) {
currentDialogText = dtm.getParam("message");
// this will stop waiting NOW
wait.setCondition(DevToolsWait.DIALOG_OPENING);
}
if (dtm.methodIs("Page.frameStartedLoading")) {
String frameLoadingId = dtm.getParam("frameId");
if (rootFrameId.equals(frameLoadingId)) { // root page is loading
domContentEventFired = false;
framesStillLoading.clear();
frameSessions.clear();
logger.trace("** root frame started loading, cleared all page state: {}", frameLoadingId);
} else {
framesStillLoading.add(frameLoadingId);
logger.trace("** frame started loading, added to in-progress list: {}", framesStillLoading);
}
}
if (dtm.methodIs("Page.frameStoppedLoading")) {
String frameLoadedId = dtm.getParam("frameId");
framesStillLoading.remove(frameLoadedId);
logger.trace("** frame stopped loading: {}, remaining in-progress: {}", frameLoadedId, framesStillLoading);
}
if (dtm.methodIs("Target.attachedToTarget")) {
frameSessions.put(dtm.getParam("targetInfo.targetId"), dtm.getParam("sessionId"));
logger.trace("** added frame session: {}", frameSessions);
}
if (dtm.methodIs("Fetch.requestPaused")) {
handleInterceptedRequest(dtm);
}
// all needed state is set above before we get into conditional checks
wait.receive(dtm);
}
private void handleInterceptedRequest(DevToolsMessage dtm) {
String requestId = dtm.getParam("requestId");
String requestUrl = dtm.getParam("request.url");
if (mockHandler != null) {
String method = dtm.getParam("request.method");
Map headers = dtm.getParam("request.headers");
String postData = dtm.getParam("request.postData");
logger.debug("intercepting request for url: {}", requestUrl);
HttpRequest request = new HttpRequest();
request.setUrl(requestUrl);
request.setMethod(method);
headers.forEach((k, v) -> request.putHeader(k, v));
if (postData != null) {
request.setBody(FileUtils.toBytes(postData));
} else {
request.setBody(Constants.ZERO_BYTES);
}
Response response = mockHandler.handle(request.toRequest());
String responseBody = response.getBody() == null ? "" : Base64.getEncoder().encodeToString(response.getBody());
List