/// <summary> /// It Takes an instance of IWebPlatform(ehich extends IPlatformService ) and Action payload and call the required functions for execution. /// Supported actions are Browser Action and Ui Element action with Page object Model Support /// /// </summary> /// <param name="service"></param> /// <param name="platformAction"></param> /// <returns></returns> public void HandleRunAction(IPlatformService service, ref NodePlatformAction platformAction) { // add try catch !!!!!!!!!! IWebPlatform webPlatformService = (IWebPlatform)service; switch (platformAction.ActionType) { case "BrowserAction": //TODO: cache BrowserActionhandler Handler = new BrowserActionhandler(webPlatformService); Handler.ExecuteAction(ref platformAction); break; case "UIElementAction": UIELementActionHandler Handler2 = new UIELementActionHandler(webPlatformService); Handler2.ExecuteAction(ref platformAction); break; case "SmartSyncAction": ExecuteSmartSyncAction(webPlatformService, ref platformAction); break; default: platformAction.error += "HandleRunAction: handler not found: "; break; } }
public void HandleRunAction(IPlatformService service, ref NodePlatformAction platformAction) { Platformservice = (IWebServicePlatform)service; RestClient = Platformservice.RestClient; try { GingerHttpRequestMessage Request = GetRequest(platformAction); GingerHttpResponseMessage Response = RestClient.PerformHttpOperation(Request); platformAction.Output.Add("Header: Status Code ", Response.StatusCode.ToString()); foreach (var RespHeader in Response.Headers) { platformAction.Output.Add("Header: " + RespHeader.Key, RespHeader.Value); } platformAction.Output.Add("Request:", Response.RequestBodyString); platformAction.Output.Add("Response:", Response.Resposne); } catch (Exception ex) { platformAction.addError(ex.Message); } }
public void PostWithTextBodyAndHeadersTest() { string filepath = TestResources.GetTestResourcesFile(@"PostWithTextBodyAndHeaders.json"); string filecontent = System.IO.File.ReadAllText(filepath); NodePlatformAction nodePlatformAction = JsonConvert.DeserializeObject <NodePlatformAction>(filecontent); nodePlatformAction.Output = new NodeActionOutput(); Tests.Service.PlatformActionHandler.HandleRunAction(Tests.Service, ref nodePlatformAction); Assert.AreEqual("OK", nodePlatformAction.Output.OutputValues.Where(x => x.Param == "Header: Status Code ").FirstOrDefault().Value); }
public void ExecuteAction(ref NodePlatformAction platformAction) { try { mPlatformAction = platformAction; InputParams = platformAction.InputParams; // convert the JArray to list of locators JObject Locators = (JObject)InputParams["Locators"]; eElementType ElementType = (eElementType)Enum.Parse(typeof(eElementType), (string)InputParams["ElementType"]); IGingerWebElement uiElement = null; foreach (JProperty locator in Locators.Children()) { uiElement = LocateElement(ref ElementType, locator.Name, locator.Value.ToString()); if (uiElement != null) { platformAction.exInfo += "UI Element Located using: " + locator.Name + "=" + locator.Value; break; } } if (uiElement == null) { platformAction.error += "Element not found"; return; } ElementAction = (eElementAction)Enum.Parse(typeof(eElementAction), (string)platformAction.InputParams["ElementAction"]); RunActionOnUIElement(uiElement, ElementType); //TODO: remove // update output values - TODO: do it directlyon platformAction !!! foreach (NodeActionOutputValue nodeActionOutputValue in AOVs) { platformAction.Output.Add(nodeActionOutputValue.Param, (string)nodeActionOutputValue.Value); // TODO: add path !!! } } catch (Exception Ex) { Error = Ex.Message; ExecutionInfo = Ex.StackTrace; // DO not put stacktrace !!!! } }
private NewPayLoad RunPlatformAction(NewPayLoad payload) { // GingerNode needs to remain generic so we have one entry point and delagate the work to the platform handler if (mService is IPlatformService platformService) { NodePlatformAction nodePlatformAction = payload.GetJSONValue <NodePlatformAction>(); nodePlatformAction.Output = new NodeActionOutput(); // Verify platformActionData is valid !!! platformService.PlatformActionHandler.HandleRunAction(platformService, ref nodePlatformAction); NewPayLoad actionResult = CreateActionResult(nodePlatformAction.exInfo, nodePlatformAction.error, nodePlatformAction.Output.OutputValues); return(actionResult); } NewPayLoad err2 = NewPayLoad.Error("RunPlatformAction: service is not supporting IPlatformService cannot delegate to run action "); return(err2); }
private void ExecuteSmartSyncAction(IWebPlatform webPlatformService, ref NodePlatformAction platformAction) { Dictionary <string, object> InputParams = platformAction.InputParams; int MaxTimeout = Int32.Parse(InputParams.ContainsKey("WaitTime") ? InputParams["WaitTime"].ToString() : (string.IsNullOrEmpty(InputParams["Value"].ToString()) ? "5" : InputParams["Value"].ToString())); string SmartSyncAction = InputParams["SmartSyncAction"] as string; string LocateValue = InputParams["LocateValue"] as string; string LocateBy = InputParams["LocateBy"] as string; eElementType ElementType = eElementType.WebElement; IGingerWebElement WebElement = null; Stopwatch st = new Stopwatch(); switch (SmartSyncAction) { case "WaitUntilDisplay": st.Reset(); st.Start(); WebElement = LocateElement(ref ElementType, LocateBy, LocateValue, webPlatformService); while (!(WebElement != null && (WebElement.IsVisible() || WebElement.IsEnabled()))) { Task.Delay(100); WebElement = LocateElement(ref ElementType, LocateBy, LocateValue, webPlatformService); if (st.ElapsedMilliseconds > MaxTimeout * 1000) { platformAction.addError("Smart Sync of WaitUntilDisplay is timeout"); break; } } break; case "WaitUntilDisapear": st.Reset(); st.Start(); WebElement = LocateElement(ref ElementType, LocateBy, LocateValue, webPlatformService); if (WebElement == null) { return; } else { st.Start(); while (WebElement != null && WebElement.IsVisible()) { Task.Delay(100); WebElement = LocateElement(ref ElementType, LocateBy, LocateValue, webPlatformService); if (st.ElapsedMilliseconds > MaxTimeout * 1000) { platformAction.addError("Smart Sync of WaitUntilDisapear is timeout"); break; } } } break; default: platformAction.error = "Smart Sync " + SmartSyncAction + "Action Not found"; break; } }
private NewPayLoad CreateActionResult(NodePlatformAction platformAction) { return(GingerNode.CreateActionResult(platformAction.exInfo, platformAction.error, null)); // platformAction.outputValues }
public void ExecuteAction(ref NodePlatformAction platformAction) { try { mPlatformAction = platformAction; InputParams = platformAction.InputParams; // convert the JArray to list of locators JObject Locators = (JObject)InputParams["Locators"]; eElementType ElementType = (eElementType)Enum.Parse(typeof(eElementType), (string)InputParams["ElementType"]); IGingerWebElement uiElement = null; JArray Frames = null; if (InputParams.ContainsKey("Frames")) { Frames = (JArray)InputParams["Frames"]; if (Frames != null && Frames.Children().Count() > 0) { mPlatformService.BrowserActions.SwitchToDefaultContent(); foreach (JToken jf in Frames.Children()) { IGingerWebElement GWA = mPlatformService.LocateWebElement.LocateElementByXPath(eElementType.WebElement, jf.ToString()); mPlatformService.BrowserActions.SwitchToFrame(GWA); } } } foreach (JProperty locator in Locators.Children()) { uiElement = WebPlatformActionHandler.LocateElement(ref ElementType, locator.Name, locator.Value.ToString(), mPlatformService); if (uiElement != null) { platformAction.exInfo += "UI Element Located using: " + locator.Name + "=" + locator.Value; break; } } if (uiElement == null) { platformAction.error += "Element not found"; return; } ElementAction = (eElementAction)Enum.Parse(typeof(eElementAction), (string)platformAction.InputParams["ElementAction"]); RunActionOnUIElement(uiElement, ElementType); //TODO: remove // update output values - TODO: do it directlyon platformAction !!! foreach (NodeActionOutputValue nodeActionOutputValue in AOVs) { platformAction.Output.Add(nodeActionOutputValue.Param, (string)nodeActionOutputValue.Value); // TODO: add path !!! } } catch (Exception Ex) { Error = Ex.Message; ExecutionInfo = Ex.StackTrace; // DO not put stacktrace !!!! } }
private GingerHttpRequestMessage GetRequest(NodePlatformAction platformAction) { GingerHttpRequestMessage Request = new GingerHttpRequestMessage(); Request.URL = new Uri(platformAction.InputParams["EndPointURL"].ToString()); if (platformAction.ActionType == "ActWebAPISoap") { Request.Method = "POST"; Request.ContentType = "XML"; } else { Request.Method = platformAction.InputParams["RequestType"].ToString(); Request.ContentType = platformAction.InputParams.ContainsKey("ContentType") ? platformAction.InputParams["ContentType"].ToString() : ""; if (platformAction.InputParams["RequestKeyValues"] is Newtonsoft.Json.Linq.JArray RObj) { Request.RequestKeyValues = new List <RestAPIKeyBodyValues>(); foreach (Newtonsoft.Json.Linq.JToken Jt in RObj.Children()) { RestAPIKeyBodyValues RKV = new RestAPIKeyBodyValues(); if (Jt["ValueType"].ToString() == "1") { RKV.ValueType = RestAPIKeyBodyValues.eValueType.File; Byte[] FileBytes = (Byte[])Jt["FileBytes"]; string Path = Jt["Value"].ToString(); RKV.Filename = System.IO.Path.GetFileName(Path); ByteArrayContent fileContent = new ByteArrayContent(FileBytes); fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data"); RKV.Content = fileContent; } RKV.Value = Jt["Value"].ToString(); RKV.Param = Jt["Param"].ToString(); Request.RequestKeyValues.Add(RKV); } } } Request.BodyString = platformAction.InputParams.ContainsKey("RequestBody") ? platformAction.InputParams["RequestBody"].ToString() : ""; if (platformAction.InputParams["Headers"] is Newtonsoft.Json.Linq.JObject JsonObj) { foreach (Newtonsoft.Json.Linq.JProperty Jt in JsonObj.Children()) { Request.Headers.Add(new KeyValuePair <string, string>(Jt.Name, Jt.Value.ToString())); } } return(Request); }
public void ExecuteAction(ref NodePlatformAction platformAction) { InputParams = platformAction.InputParams; string Value = (string)InputParams["Value"]; List <NodeActionOutputValue> AOVs = new List <NodeActionOutputValue>(); try { // use enum or string/const ??? eControlAction ElementAction = (eControlAction)Enum.Parse(typeof(eControlAction), InputParams["ControlAction"].ToString()); switch (ElementAction) { case eControlAction.GotoURL: string GotoURLType = String.Empty; if (InputParams.ContainsKey("GotoURLType")) { GotoURLType = (string)InputParams["GotoURLType"]; } if (string.IsNullOrEmpty(GotoURLType)) { GotoURLType = "Current"; } BrowserService.Navigate(Value, GotoURLType); platformAction.exInfo += "Navigated to: " + Value; break; case eControlAction.GetPageURL: platformAction.Output.Add("PageUrl", BrowserService.GetCurrentUrl()); break; case eControlAction.Maximize: BrowserService.Maximize(); break; case eControlAction.Close: BrowserService.CloseCurrentTab(); break; case eControlAction.CloseAll: BrowserService.CloseWindow(); break; case eControlAction.Refresh: BrowserService.Refresh(); break; case eControlAction.NavigateBack: BrowserService.NavigateBack(); break; case eControlAction.DismissMessageBox: BrowserService.DismissAlert(); break; case eControlAction.DeleteAllCookies: BrowserService.DeleteAllCookies(); break; case eControlAction.AcceptMessageBox: BrowserService.AcceptAlert(); break; case eControlAction.GetWindowTitle: string Title = BrowserService.GetTitle(); AOVs.Add(new NodeActionOutputValue() { Param = "Actual", Value = Title }); break; case eControlAction.GetMessageBoxText: string AlertText = BrowserService.GetAlertText(); AOVs.Add(new NodeActionOutputValue() { Param = "Actual", Value = AlertText }); break; case eControlAction.SetAlertBoxText: string value = (string)InputParams["Value"]; BrowserService.SendAlertText(value); break; case eControlAction.SwitchFrame: string ElementLocateBy; string Locatevalue; string mElementType = ""; Locatevalue = (string)InputParams["LocateValue"]; object elb; InputParams.TryGetValue("ElementLocateBy", out elb); ElementLocateBy = elb != null?elb.ToString() : ""; if (string.IsNullOrEmpty(ElementLocateBy)) { ElementLocateBy = (string)InputParams["LocateBy"]; } if (string.IsNullOrEmpty(Locatevalue)) { Locatevalue = (string)InputParams["Value"]; } eElementType ElementType = eElementType.WebElement; _ = Enum.TryParse <eElementType>(mElementType, out ElementType); IGingerWebElement Element = LocateElement(ElementType, ElementLocateBy, Locatevalue); BrowserService.SwitchToFrame(Element); break; case eControlAction.RunJavaScript: string javascript = (string)InputParams["javascript"]; object Output = BrowserService.ExecuteScript(javascript); if (Output != null) { platformAction.Output.Add("Actual", Output.ToString()); } break; case eControlAction.GetPageSource: string PageSource = BrowserService.GetPageSource(); AOVs.Add(new NodeActionOutputValue() { Param = "PageSource", Value = PageSource }); break; case eControlAction.InjectJS: break; } } catch (Exception ex) { platformAction.addError(ex.Message); } finally { platformAction.Output.OutputValues.AddRange(AOVs); } }