예제 #1
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);
        }
예제 #2
0
        /// <summary>
        /// Executes an action using its proxy.
        /// </summary>
        /// <param name="actionProxy">The action proxy to execute.</param>
        /// <param name="by">A locator for the <see cref="IWebElement"/> to execute the action on.</param>
        /// <param name="timeoutInMilliSeconds">Timeout for the action execution duration.</param>
        /// <returns>A potentially updated <see cref="ActionProxy"/> with updated output fields (if any).</returns>
        public ActionProxy Execute(ActionProxy actionProxy, By by = null, int timeoutInMilliSeconds = -1)
        {
            if (by != null)
            {
                // Because serializing does not work 'out of the box' for By objects,
                // we need to handle that with a custom method.
                actionProxy.ProxyDescriptor.By = by.ConvertToSerializable();
            }

            // The payload to be sent to the Agent needs to be formatted first.
            actionProxy.ProxyDescriptor.Parameters = this.FormatParameters(actionProxy);

            ActionExecutionResponse response = AgentClient.GetInstance().ExecuteProxy(actionProxy, timeoutInMilliSeconds);

            if (!response.ResultType.Equals(ActionExecutionResponse.ExecutionResultType.Passed))
            {
                throw new SdkException($"Error occurred during addon action execution: {response.Message}");
            }

            foreach (ActionExecutionResponse.ResultField field in response.Fields)
            {
                if (!field.Output)
                {
                    // Skip input fields
                    continue;
                }

                var proxyField = actionProxy.GetType().GetProperty(field.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

                // Check if action has an attribute with the name of the field
                if (proxyField == null)
                {
                    Logger.Warn($"Action {actionProxy.ProxyDescriptor.Guid} does not have a field named '{field.Name}'");
                    continue;
                }

                proxyField.SetValue(actionProxy, field.Value);

                Logger.Trace($"Value of output field '{proxyField.Name}' has been set to '{proxyField.GetValue(actionProxy)}'");
            }

            return(actionProxy);
        }