/// <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> /// Retrieves the version of the Agent currently in use. /// </summary> /// <returns>An instance of the <see cref="Version"/> class containing the Agent version.</returns> private Version GetAgentVersion() { RestRequest getAgentStatusRequest = new RestRequest(Endpoints.STATUS, Method.GET); IRestResponse getAgentStatusResponse = this.client.Execute(getAgentStatusRequest); if (getAgentStatusResponse.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)getAgentStatusResponse.StatusCode >= 400) { throw new AgentConnectException($"Failed to get Agent status: {getAgentStatusResponse.ErrorMessage}"); } AgentStatusResponse agentStatusResponse = CustomJsonSerializer.FromJson <AgentStatusResponse>(getAgentStatusResponse.Content, this.serializerSettings); Logger.Info($"Current Agent version is {agentStatusResponse.Tag}"); return(new Version(agentStatusResponse.Tag)); }
/// <summary> /// Retrieves the version of the Agent currently in use. /// </summary> /// <returns>An instance of the <see cref="Version"/> class containing the Agent version.</returns> private Version GetAgentVersion() { RestRequest getAgentStatusRequest = new RestRequest(Endpoints.STATUS, Method.GET); IRestResponse getAgentStatusResponse = this.client.Execute(getAgentStatusRequest); if ((int)getAgentStatusResponse.StatusCode >= 400) { throw new AgentConnectException($"Failed to get Agent status: {getAgentStatusResponse.ErrorMessage}"); } AgentStatusResponse agentStatusResponse = CustomJsonSerializer.FromJson <AgentStatusResponse>(getAgentStatusResponse.Content, this.serializerSettings); Logger.Info($"Current Agent version is {agentStatusResponse.Tag}"); return(new Version(agentStatusResponse.Tag)); }
/// <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(); } }
private void StartSdkSession(IRestResponse startSessionResponse, DriverOptions capabilities) { if (capabilities is AppiumOptions) { // Appium capabilities cannot be deserialized directly into an AppiumOptions instance, // so we need to do this ourselves Logger.Info($"Creating Appium options from capabilities returned by Agent..."); AppiumSessionResponse sessionResponse = CustomJsonSerializer.FromJson <AppiumSessionResponse>(startSessionResponse.Content, this.serializerSettings); AppiumOptions appiumOptions = this.CreateAppiumOptions(sessionResponse.Capabilities); this.CreateAgentSession(sessionResponse, appiumOptions); this.OpenSocketConnectionUsing(sessionResponse); } else { // Capabilities for web browsers and the generic driver can be serialized directly. // Since there's a separate Options class for each supported browser, we'll have to // differentiate between them as well. if (capabilities is ChromeOptions) { ChromeSessionResponse sessionResponse = CustomJsonSerializer.FromJson <ChromeSessionResponse>(startSessionResponse.Content, this.serializerSettings); this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities); this.OpenSocketConnectionUsing(sessionResponse); } else if (capabilities is FirefoxOptions) { FirefoxSessionResponse sessionResponse = CustomJsonSerializer.FromJson <FirefoxSessionResponse>(startSessionResponse.Content, this.serializerSettings); this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities); this.OpenSocketConnectionUsing(sessionResponse); } else if (capabilities is EdgeOptions) { EdgeSessionResponse sessionResponse = CustomJsonSerializer.FromJson <EdgeSessionResponse>(startSessionResponse.Content, this.serializerSettings); this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities); this.OpenSocketConnectionUsing(sessionResponse); } else if (capabilities is SafariOptions) { SafariSessionResponse sessionResponse = CustomJsonSerializer.FromJson <SafariSessionResponse>(startSessionResponse.Content, this.serializerSettings); this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities); this.OpenSocketConnectionUsing(sessionResponse); } else if (capabilities is InternetExplorerOptions) { IESessionResponse sessionResponse = CustomJsonSerializer.FromJson <IESessionResponse>(startSessionResponse.Content, this.serializerSettings); this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities); this.OpenSocketConnectionUsing(sessionResponse); } else if (capabilities is GenericOptions) { GenericSessionResponse sessionResponse = CustomJsonSerializer.FromJson <GenericSessionResponse>(startSessionResponse.Content, this.serializerSettings); this.CreateAgentSession(sessionResponse, sessionResponse.Capabilities); this.OpenSocketConnectionUsing(sessionResponse); } else { throw new SdkException($"The options type '{capabilities.GetType().Name}' is not supported by the OpenSDK."); } } }