/// <summary> /// Executes a specified action proxy from an addon. /// </summary> /// <param name="actionProxy">The <see cref="ActionProxy"/> to execute.</param> /// <param name="timeout">The action execution timeout (in milliseconds).</param> /// <returns>The response from the Agent upon executing the action proxy.</returns> public ActionExecutionResponse ExecuteProxy(ActionProxy actionProxy, int timeout) { RestRequest sendActionProxyRequest = new RestRequest(Endpoints.EXECUTE_ACTION_PROXY, Method.POST); sendActionProxyRequest.RequestFormat = DataFormat.Json; string json = CustomJsonSerializer.ToJson(actionProxy.ProxyDescriptor, this.serializerSettings); sendActionProxyRequest.AddJsonBody(json); if (timeout > 0) { // Since action proxy execution can take a while, you can override the default timeout. this.client.Timeout = timeout; } IRestResponse sendActionProxyResponse = this.client.Execute(sendActionProxyRequest); if (sendActionProxyResponse.StatusCode.Equals(HttpStatusCode.NotFound)) { string errorMessage = $"Action {actionProxy.ProxyDescriptor.ClassName} in addon {actionProxy.ProxyDescriptor.Guid} is not installed for your account."; Logger.Error(errorMessage); throw new AddonNotInstalledException(errorMessage); } // Reset the client timeout to the RestSharp default. this.client.Timeout = this.defaultRestClientTimeoutInMilliseconds; return(CustomJsonSerializer.FromJson <ActionExecutionResponse>(sendActionProxyResponse.Content, this.serializerSettings)); }
/// <summary> /// Overriding the base method to handle reports at batches. /// While there are reports in the queue - collect up to 10 reports and send them at batch. /// </summary> /// <exception>FailedReportException if cannot send report to the agent more than <see cref="MaxReportFailureAttempts"/> attempts.</exception> protected override void HandleReport() { // LinkedList to store the reports batch before sending them. LinkedList <Report> batchReports = new LinkedList <Report>(); // Extract and remove up to 10 items or till queue is empty from queue - without blocking it. while (this.ReportItems.Count > 0 && batchReports.Count < this.maxBatchSize) { QueueItem item; // Get the first item in the queue without blocking it. bool taken = this.ReportItems.TryTake(out item); if (taken && item != null && item.Report != null) { batchReports.AddLast(item.Report); } } if (batchReports.Count == 0) { return; } // Build REST request. RestRequest sendReportsBatchRequest = new RestRequest(Endpoints.REPORT_BATCH, Method.POST); sendReportsBatchRequest.RequestFormat = DataFormat.Json; string json = CustomJsonSerializer.ToJson(batchReports, this.serializerSettings); sendReportsBatchRequest.AddJsonBody(json); this.SendReport(sendReportsBatchRequest); }
/// <summary> /// Starts a new session with the Agent. /// </summary> /// <param name="reportSettings">Settings (project name, job name) to be included in the report.</param> /// <param name="capabilities">Additional options to be applied to the driver instance.</param> private void StartSession(ReportSettings reportSettings, DriverOptions capabilities) { RestRequest startSessionRequest = new RestRequest(Endpoints.DEVELOPMENT_SESSION, Method.POST); startSessionRequest.RequestFormat = DataFormat.Json; string json = CustomJsonSerializer.ToJson(new SessionRequest(reportSettings, capabilities), this.serializerSettings); startSessionRequest.AddJsonBody(json); IRestResponse startSessionResponse = this.client.Execute(startSessionRequest); if (startSessionResponse.ErrorException != null) { string errorMessage = $"An error occurred connecting to the Agent. Is your Agent running at {this.remoteAddress}?"; Logger.Error(errorMessage); throw new AgentConnectException(errorMessage); } if ((int)startSessionResponse.StatusCode >= 400) { this.HandleSessionStartFailure(startSessionResponse); return; } this.StartSdkSession(startSessionResponse, capabilities); // Only retrieve the Agent version when it has not yet been set if (this.agentVersion == null) { this.agentVersion = this.GetAgentVersion(); } }
/// <summary> /// Converts action parameters to a format understood by the Agent. /// </summary> /// <param name="actionProxy">The <see cref="ActionProxy"/> for which the parameters should be formatted.</param> /// <returns>A formatted set of action parameters.</returns> private Dictionary <string, object> FormatParameters(ActionProxy actionProxy) { string actionProxyAsJson = CustomJsonSerializer.ToJson(actionProxy, CustomJsonSerializer.Populate(new JsonSerializerSettings())); Dictionary <string, object> actionParameters = JsonConvert.DeserializeObject <Dictionary <string, object> >(actionProxyAsJson); actionParameters.Remove("proxyDescriptor"); return(actionParameters); }
/// <summary> /// Sends a <see cref="StepReport"/> to the Agent. /// </summary> /// <param name="stepReport">The payload object containing the step details to be reported.</param> public void ReportStep(StepReport stepReport) { RestRequest sendStepReportRequest = new RestRequest(Endpoints.REPORT_STEP, Method.POST); sendStepReportRequest.RequestFormat = DataFormat.Json; string json = CustomJsonSerializer.ToJson(stepReport, this.serializerSettings); sendStepReportRequest.AddJsonBody(json); this.reportsQueue.Submit(sendStepReportRequest, stepReport); }
/// <summary> /// Reports a WebDriver command to TestProject. /// </summary> /// <param name="driverCommandReport">Payload object containing command information and execution result.</param> public void ReportDriverCommand(DriverCommandReport driverCommandReport) { RestRequest sendDriverCommandRequest = new RestRequest(Endpoints.REPORT_COMMAND, Method.POST); sendDriverCommandRequest.RequestFormat = DataFormat.Json; string json = CustomJsonSerializer.ToJson(driverCommandReport, this.serializerSettings); sendDriverCommandRequest.AddJsonBody(json); this.reportsQueue.Submit(sendDriverCommandRequest, driverCommandReport); }
/// <summary> /// Starts a new session with the Agent. /// </summary> /// <param name="reportSettings">Settings (project name, job name) to be included in the report.</param> /// <param name="capabilities">Additional options to be applied to the driver instance.</param> private void StartSession(ReportSettings reportSettings, DriverOptions capabilities) { RestRequest startSessionRequest = new RestRequest(Endpoints.DEVELOPMENT_SESSION, Method.POST); startSessionRequest.RequestFormat = DataFormat.Json; string json = CustomJsonSerializer.ToJson(new SessionRequest(reportSettings, capabilities), this.serializerSettings); startSessionRequest.AddJsonBody(json); IRestResponse startSessionResponse = this.client.Execute(startSessionRequest); if ((int)startSessionResponse.StatusCode >= 400) { this.HandleSessionStartFailure(startSessionResponse); return; } SessionResponse sessionResponse = CustomJsonSerializer.FromJson <SessionResponse>(startSessionResponse.Content, this.serializerSettings); // A session request for the generic driver returns a partial response, so we generate our own session ID. if (sessionResponse.SessionId == null) { sessionResponse.SessionId = Guid.NewGuid().ToString(); } Logger.Info($"Session [{sessionResponse.SessionId}] initialized"); this.AgentSession = new AgentSession(new Uri(sessionResponse.ServerAddress), sessionResponse.SessionId, sessionResponse.Dialect, sessionResponse.Capabilities); SocketManager.GetInstance().OpenSocket(this.remoteAddress.Host, sessionResponse.DevSocketPort); // Only retrieve the Agent version when it has not yet been set if (this.agentVersion == null) { this.agentVersion = this.GetAgentVersion(); } }