public void Execute(Arguments arguments) { var wrapper = MsCrmManager.AttachToExistingCRM(arguments.Phrase.Value, arguments.By.Value); if (wrapper != null && wrapper.Ie != null) { OnScriptEnd = () => { MsCrmManager.Detach(wrapper); }; Scripter.Variables.Scripter.Variables.SetVariableValue(arguments.Result.Value, new BooleanStructure(true)); wrapper.ActivateTab(arguments.Phrase.Value, arguments.By.Value); ActivateBrowserWindow(wrapper); } else { throw new ApplicationException("Cannot attach to MS Dynamics CRM, please check is IE with " + arguments.Phrase.Value + " opened."); } }
private void StartRecording() { bool justStarted = true; while (!stopRecording) { try { var wrapper = MsCrmManager.AttachToExistingCRM(MsCrmUrlPhrase, "url", true); if (wrapper.Ie == null && justStarted == true) { MessageBox.Show($"Dynamics CRM Instance with url containing phrase '{MsCrmUrlPhrase}' could not be found.", "Dynamics CRM Instance not found", MessageBoxButtons.OK, MessageBoxIcon.Information); isRecording = false; StartRecordingButton.Invoke((MethodInvoker)(() => { StartRecordingButton.Text = "Start recording"; })); return; } else { justStarted = false; } wrapper.Ie.ShowWindow(WatiN.Core.Native.Windows.NativeMethods.WindowShowStyle.Minimize); wrapper.Ie.ShowWindow(WatiN.Core.Native.Windows.NativeMethods.WindowShowStyle.Restore); wrapper.Ie.ShowWindow(WatiN.Core.Native.Windows.NativeMethods.WindowShowStyle.Restore); wrapper.Ie.RunScript("$('.G1antMsCrmWizardClass').remove()", "javascript"); while (!stopRecording) { var div = MsCrmManager.CurrentCRM.Ie.Div("MsCrmWizard"); while (!stopRecording && !div.Exists) { try { div.WaitUntilExists(1); } catch { } Thread.Sleep(100); } if (!stopRecording) { var action = div.GetAttributeValue("data-element-type"); var iframeId = div.GetAttributeValue("target_iframe_id"); iframeId = !string.IsNullOrEmpty(iframeId) ? iframeId : null; var iframeTitle = div.GetAttributeValue("target_iframe_title"); iframeTitle = !string.IsNullOrEmpty(iframeTitle) ? iframeTitle : null; var iframeName = div.GetAttributeValue("target_iframe_name"); iframeName = !string.IsNullOrEmpty(iframeName) ? iframeName : null; string iFrame = string.Empty; if (iframeTitle != null && (iframeTitle.ToLower().Contains("content area") || iframeTitle.ToLower().Contains("contentiframe"))) { iFrame = string.Empty; } else if (iframeId != null) { iFrame = iframeId; } else { iFrame = "none"; } string finalIframeName = iFrame; iFrame = string.IsNullOrEmpty(iFrame) ? string.Empty : $" iframe ‴{iFrame}‴"; var id = div.GetAttributeValue("target_id"); var className = div.GetAttributeValue("target_class"); string searchBy = !string.IsNullOrEmpty(id) ? "id" : "class"; string searchPhrase = !string.IsNullOrEmpty(id) ? id : className; string command = string.Empty; bool addCommand = true; switch (action) { case "click": command = searchBy == "id" ? $"mscrm.click ‴{searchPhrase}‴" + iFrame : $"mscrm.click by ‴{searchBy}‴ search ‴{searchPhrase}‴" + iFrame; break; case "setvalue": Command.ExecutionTime = Environment.TickCount; Command.Timeout = 10000; var currentIframe = wrapper.GetCurrentIframe(finalIframeName); var element = wrapper.GetInputElement(searchPhrase, searchBy, finalIframeName); List <string> availableOptions = null; var fieldType = wrapper.RecognizeFieldType(element, currentIframe, ref availableOptions); MsCrmSetValueForm setValueForm = new MsCrmSetValueForm(searchPhrase, availableOptions); wrapper.TypeText("⋘esc⋙"); setValueForm.StartPosition = FormStartPosition.Manual; setValueForm.Locate(xPosition, yPosition); setValueForm.ShowDialog(); if (setValueForm.DialogResult == DialogResult.OK) { switch (setValueForm.ResultType) { case ResultType.SetValue: command = searchBy == "id" ? $"mscrm.setvalue ‴{searchPhrase}‴ value ‴{setValueForm.ResultValue}‴" + iFrame : $"mscrm.setvalue by ‴{searchBy}‴ search ‴{searchPhrase}‴ value ‴{setValueForm.ResultValue}‴" + iFrame; if (setValueForm.ValidationErrorMessage != null) { command += $@"{System.Environment.NewLine}dialog ‴{setValueForm.ValidationErrorMessage}‴ if {SpecialChars.Variable}validationresult==false{System.Environment.NewLine}stop if {SpecialChars.Variable}validationresult==false"; } Command.ExecutionTime = Environment.TickCount; Command.Timeout = 10000; try { wrapper.SetVal(searchPhrase, setValueForm.ResultValue, searchBy, finalIframeName ?? string.Empty); } catch { } break; case ResultType.Click: command = searchBy == "id" ? $"mscrm.click ‴{searchPhrase}‴" + iFrame : $"mscrm.click by ‴{searchBy}‴ search ‴{searchPhrase}‴" + iFrame; break; } } else { addCommand = false; } break; } if (addCommand) { AppendRecordingRichTextBox(command); } wrapper.Ie.RunScript("$('.G1antMsCrmWizardClass').remove()", "javascript"); } } try { wrapper.MsCrmRecordingSwitch = false; wrapper.Dettach(true); } catch { } } catch { } } stopRecording = false; isRecording = false; }