/// <summary>
        /// Initializes a new instance of the <see cref="AgentClient"/> class.
        /// </summary>
        /// <param name="remoteAddress">The remote address where the Agent is running.</param>
        /// <param name="token">The development token used to authenticate with the Agent.</param>
        /// <param name="capabilities">Requested driver options for the browser session.</param>
        /// <param name="reportSettings">Contains the project and job name to report to TestProject.</param>
        /// <param name="disableReports">Set to true to disable all reporting to TestProject, false otherwise.</param>
        /// <param name="compatibleVersion">Minimum Agent version that supports the requested feature. Can be used to check Agent compatibility.</param>
        private AgentClient(Uri remoteAddress, string token, DriverOptions capabilities, ReportSettings reportSettings, bool disableReports, Version compatibleVersion = null)
        {
            this.remoteAddress = this.InferRemoteAddress(remoteAddress);

            ReportSettings sessionReportSettings = disableReports ? null : this.InferReportSettings(reportSettings);

            this.reportSettings = sessionReportSettings;

            if (token != null)
            {
                this.token = token;
            }
            else if (Environment.GetEnvironmentVariable(this.tpDevToken) != null)
            {
                this.token = Environment.GetEnvironmentVariable(this.tpDevToken);
            }
            else
            {
                throw new InvalidTokenException("No token has been provided.");
            }

            this.client = new RestClient(this.remoteAddress);
            this.client.AddDefaultHeader("Authorization", this.token);

            this.serializerSettings = CustomJsonSerializer.Populate(new JsonSerializerSettings());

            // Check that Agent version supports the requested feature.
            if (compatibleVersion != null)
            {
                Logger.Trace($"Checking if the Agent version is {compatibleVersion} at minimum");

                agentVersion = this.GetAgentVersion();

                if (agentVersion.CompareTo(compatibleVersion) < 0)
                {
                    throw new AgentConnectException($"Current Agent version {agentVersion} does not support the requested feature," +
                                                    $" should be at least {compatibleVersion}");
                }
            }

            this.reportsQueue = new ReportsQueue(this.client);

            this.StartSession(sessionReportSettings, capabilities);

            // Verify the agent version supports local report generation
            this.VerifyIfLocalReportsIsSupported(reportSettings.ReportType);

            // Show local report path only when executing on local agents.
            if (this.client.BaseUrl.IsLocal())
            {
                Logger.Info($"Execution Report: {this.sessionResponse.LocalReport}");
            }

            if (!string.IsNullOrWhiteSpace(this.sessionResponse.LocalReportUrl))
            {
                Logger.Info($"Execution Report Link: {this.sessionResponse.LocalReportUrl}");
            }
        }
Example #2
0
        /// <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);
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ReportsQueueBatch"/> class.
        /// </summary>
        /// <param name="client">The <see cref="RestClient"/> HTTP client to send reports to the Agent.</param>
        public ReportsQueueBatch(RestClient client)
            : base(client)
        {
            this.serializerSettings = CustomJsonSerializer.Populate(new JsonSerializerSettings());

            // Override default converter of enum to string since agent is case sensitive.
            this.serializerSettings.Converters.Clear();
            this.serializerSettings.Converters.Add(new StringEnumConverter());

            // Try to get maximum report batch size from env variable.
            string tpMaxBatchSizeEnvVal = Environment.GetEnvironmentVariable(TpMaxBatchSizeVariableName);

            this.maxBatchSize = (tpMaxBatchSizeEnvVal != null) ? int.Parse(tpMaxBatchSizeEnvVal) : MaxReportsBatchSize;
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AgentClient"/> class.
        /// </summary>
        /// <param name="remoteAddress">The remote address where the Agent is running.</param>
        /// <param name="token">The development token used to authenticate with the Agent.</param>
        /// <param name="capabilities">Requested driver options for the browser session.</param>
        /// <param name="reportSettings">Contains the project and job name to report to TestProject.</param>
        /// <param name="disableReports">Set to true to disable all reporting to TestProject, false otherwise.</param>
        /// <param name="compatibleVersion">Minimum Agent version that supports the requested feature. Can be used to check Agent compatibility.</param>
        private AgentClient(Uri remoteAddress, string token, DriverOptions capabilities, ReportSettings reportSettings, bool disableReports, Version compatibleVersion)
        {
            this.remoteAddress = this.InferRemoteAddress(remoteAddress);

            ReportSettings sessionReportSettings = disableReports ? null : this.InferReportSettings(reportSettings);

            if (token != null)
            {
                this.token = token;
            }
            else if (Environment.GetEnvironmentVariable(this.tpDevToken) != null)
            {
                this.token = Environment.GetEnvironmentVariable(this.tpDevToken);
            }
            else
            {
                throw new InvalidTokenException("No token has been provided.");
            }

            this.client = new RestClient(this.remoteAddress);
            this.client.AddDefaultHeader("Authorization", this.token);

            this.serializerSettings = CustomJsonSerializer.Populate(new JsonSerializerSettings());

            // Check that Agent version supports the requested feature.
            if (compatibleVersion != null)
            {
                Logger.Trace($"Checking if the Agent version is {compatibleVersion} at minimum");

                this.agentVersion = this.GetAgentVersion();

                // a.CompareTo(b) returns:
                // * <0 if a is earlier than b
                // *  0 if a is the same version as b
                // * >0 is a is later than b
                if (this.agentVersion.CompareTo(compatibleVersion) < 0)
                {
                    throw new AgentConnectException($"Current Agent version {this.agentVersion} does not support the requested feature," +
                                                    $" should be at least {compatibleVersion}");
                }
            }

            this.reportsQueue = new ReportsQueue(this.client);

            this.StartSession(sessionReportSettings, capabilities);
        }