protected string GetDialogText(IntPtr dialogHandle, IBrowser browser) { DateTime timeout = DateTime.Now.AddSeconds(1); dialogText = ""; string lastDialogText = dialogText; while (lastDialogText == dialogText && DateTime.Now < timeout) { KeyboardInput input = new KeyboardInput(browser); input.Copy(dialogHandle); Thread t = new Thread(new ThreadStart(GetClipboardText)); t.SetApartmentState(ApartmentState.STA); t.Start(); t.Join(); } return dialogText; }
private void PressKeys(IdentifierType identType, string identifier, string word, string tagName, bool focusOnSpecificElement, bool checkAttached, string windowTitle) { if (checkAttached) _browser.AssertBrowserIsAttached(); _browser.Sleep(200); if (focusOnSpecificElement) { //Let each browser find the element in its own way, then stimulate it by selecting the element. _browser.ElementFireEvent(identType, identifier, tagName, "onfocus"); } else if (checkAttached) { _browser.WaitForBrowserReadyState(); } if (_browserType == BrowserType.Safari) { _browser.PressKeys(identType, identifier, word, tagName); return; } bool isUnlockedDesktop = NativeMethods.IsDesktopUnlocked(); KeyboardInput keyboard = new KeyboardInput(_browser); Regex begPattern = new Regex(begKeyPattern); if (begPattern.IsMatch(word)) { Regex endPattern = new Regex(endKeyPattern); if (!endPattern.IsMatch(word)) throw new ArgumentException("The key code sequence has not been completed. Please add a \\} at the end of the keyword."); if (!isUnlockedDesktop || _forceBrowserPressKeys) throw new LockedDesktopEnvironmentException(); string keyValue = word.Substring(2, word.Length - 4); if (keyValue.ToUpper().StartsWith("ALT")) { keyboard.ProcessAltKeyCombination(keyValue); } else { if (keyValue.ToUpper().Equals("SHIFT+TAB")) { keyboard.ProcessShiftMappedKey(GetMappedKey("TAB")); } else { keyboard.ProcessKey(GetMappedKey(keyValue)); } } keyboard.SendInputString(windowTitle == "" ? _browser.GetCurrentWindowTitle() : windowTitle); } else if (((_browserType == BrowserType.FireFox) || !isUnlockedDesktop || _forceBrowserPressKeys) && focusOnSpecificElement) { _browser.PressKeys(identType, identifier, word, tagName); } else if (!focusOnSpecificElement && (!isUnlockedDesktop || _forceBrowserPressKeys)) { throw new LockedDesktopEnvironmentException(); } else { foreach (char t in word) { string asciiValue = _browser.getAsciiValue(t); if (!asciiValue.Equals("")) keyboard.ProcessInternationalKey(asciiValue); else keyboard.ProcessKey(NativeMethods.VkKeyScan(t)); } keyboard.SendInputString(windowTitle == "" ? _browser.GetCurrentWindowTitle() : windowTitle); _browser.Sleep((13 * word.Length) + 500); // Put in for longer strings to finish firing events in the browser } }
private void SetFileInputPath(IdentifierType identType, string identifier, string filePath, string tagName) { if (!File.Exists(filePath)) { throw new FileNotFoundException(string.Format("Could not find file {0}", filePath)); } ElementFireEvent(identType, identifier, tagName, "onfocus"); KeyboardInput keyboard = new KeyboardInput(this); keyboard.ProcessKey(this.getKeyValue("ENTER")); keyboard.SendInputString(this.GetCurrentWindowTitle()); StringBuilder className = new StringBuilder(255); StringBuilder btnName = new StringBuilder(255); Sleep(2000); IntPtr dialogHwnd = IntPtr.Zero; for (int i = 0; i < 30; i++) { NativeMethods.GetWindowWithSubstring("Open", 0, 0, ref dialogHwnd); if (dialogHwnd != IntPtr.Zero) { //Now find the combobox fill in box List<IntPtr> windowChildren = NativeMethods.GetWindowChildren(dialogHwnd); foreach (IntPtr dlgComboBox in windowChildren) { NativeMethods.GetClassName(dlgComboBox, className, className.MaxCapacity); //get the childs's type if (string.Equals(className.ToString(), "ComboBoxEx32")) { IntPtr comboBoxTxt = NativeMethods.GetChildWindowHwnd(dlgComboBox, "Edit"); //find the combobox AutomationElement editBox = AutomationElement.FromHandle(comboBoxTxt); ValuePattern valuePattern = (ValuePattern)editBox.GetCurrentPattern(ValuePattern.Pattern); valuePattern.SetValue(filePath); break; } } //Click on the OK Button foreach (IntPtr okBtn in windowChildren) { NativeMethods.GetWindowText(okBtn, btnName, btnName.Capacity); if (string.Equals(btnName.ToString(), "&Open")) // we found the OK Button { NativeMethods.RECT placement; NativeMethods.GetClientRect(okBtn, out placement); uint lParam = (uint)((placement.Left + 1 * 0x010000) + placement.Top + 1); //Find it's coordinates and generate the parameter //click on it NativeMethods.SendMessage(okBtn, 0x201, 0x000, lParam); //WM_LBUTTONDOWN NativeMethods.SendMessage(okBtn, 0x202, 0x000, lParam); //WM_LBUTTONUP break; } } break; } else { Sleep(100); } } if (dialogHwnd == IntPtr.Zero) { throw new Exception(string.Format("Could not find file input dialog handle")); } Sleep(100); //Time for filepath to be displayed }