/// <summary> /// Select a specified value from a specified drop down /// There is redundant logic in this method so that if the specified text is not found in its entirety, /// it will search for an element that contains the specified text. /// If now option in the drop down is found, an error will be logged and an exception thrown to stop the test. /// </summary> /// <param name="element">The drop down to use</param> /// <param name="expectedValue">The value to select</param> public static void SelectFromDropDown(IWebElement element, string expectedValue) { if (expectedValue == null) { return; } var recievedValue = DoSelect(element, expectedValue); if (recievedValue != expectedValue) { if (recievedValue.Contains(expectedValue)) { return; } LogFunctions.WriteWarning("Selected element not as expected. Recieved: " + recievedValue + " should have been: " + expectedValue); LogFunctions.WriteWarning("Attempting select one more time."); recievedValue = DoSelect(element, expectedValue); if (recievedValue != expectedValue) { LogFunctions.WriteError("Selected element not as expected. Recieved: " + recievedValue + " should have been: " + expectedValue); throw new Exception("Selected element not as expected"); } LogFunctions.WriteInfo(expectedValue + " has been selected."); } else { LogFunctions.WriteInfo(expectedValue + " has been selected."); } }
/// <summary> /// The Checkbox method provides the ability to check or uncheck a checkbox and validate that it is set correctly /// based on the expected value that is passed in /// </summary> /// <param name="element">The checkbox element</param> /// <param name="turnOn">True if the checkbox should be checked, false if the checkbox should be unchecked</param> /// <returns>True if the checkbox is set as expected, false if it is not set as expected</returns> public static bool Checkbox(IWebElement element, bool turnOn) { if (!element.Displayed) { return(false); } if (!element.Enabled) { return(false); } if (element.Selected && turnOn) { LogFunctions.WriteWarning("Checkbox already selected. No changes made"); return(element.Selected); } if (element.Selected && !turnOn) { LogFunctions.WriteInfo("Checkbox has been un-selected."); element.Click(); return(!element.Selected); } if (!element.Selected && turnOn) { LogFunctions.WriteInfo("Checkbox has been selected."); element.Click(); return(element.Selected); } if (!element.Selected && !turnOn) { LogFunctions.WriteWarning("Checkbox already not selected. No changes made"); return(!element.Selected); } LogFunctions.WriteError("Checkbox error"); Browser.TakeScreenshot("CheckboxStatusError"); return(false); }
/// <summary> /// This method will type a specified value into a specified text box element /// Error checking and validation are done to ensure that the expected value has been typed correctly into the text box /// If the text is unable to be entered into the text box, errors are written and an exception is thrown to stop the test /// </summary> /// <param name="element">The text box to type into</param> /// <param name="expectedValue">The text that should be entered</param> public static void Type(IWebElement element, string expectedValue) { if (expectedValue == null) { LogFunctions.WriteError("Did not get an expected value!"); return; } if (expectedValue == "blank") { expectedValue = ""; } Browser.WaitForElementDisplayed(element); Browser.WaitForElementEnabled(element); element.Click(); element.Clear(); element.SendKeys(expectedValue.Replace("%", "").Trim()); element.SendKeys(Keys.Tab); string recievedValue = element.GetAttribute("value"); if (recievedValue != expectedValue) { LogFunctions.WriteWarning("Element value not as expected. Recieved: " + recievedValue + " should have been: " + expectedValue); throw new Exception("Typed element not as expected"); } LogFunctions.WriteInfo(expectedValue + " has been typed into the element."); }
public static void Quit() { _webDriver.Close(); _webDriver.Quit(); foreach (var pid in _pid) { try { Process.GetProcessById(pid); } catch (ArgumentException) { LogFunctions.WriteInfo("Process " + pid + " completed sucessfully."); continue; } try { var handle = Process.GetProcessById(pid).MainWindowHandle; if (handle.ToInt32() == 0) { LogFunctions.WriteWarning("Waiting for Process " + pid + " to complete on its own." + DateTime.Now); Process.GetProcessById(pid).WaitForExit(300000); LogFunctions.WriteWarning("Wait completed: " + DateTime.Now); try { Process.GetProcessById(pid); } catch (ArgumentException) { LogFunctions.WriteInfo("Process " + pid + " completed sucessfully."); continue; } Process.GetProcessById(pid).Kill(); throw new ApplicationException("Manually killed this process"); } } catch (InvalidOperationException) { try { Process.GetProcessById(pid).Kill(); throw new ApplicationException("Invalid Operation - Manually killed this process"); } catch (ArgumentException) { LogFunctions.WriteInfo("Process " + pid + " completed sucessfully on second try."); } } } }
/// <summary> /// This method is called by the SelectFromDropDown method and does the actual selection /// If no selection can be made, errors are logged and an exception is thrown to stop the test /// </summary> /// <param name="element">The drop down to use</param> /// <param name="expectedValue">The value to select</param> /// <returns>The text that was actually selected in the drop down</returns> private static string DoSelect(IWebElement element, string expectedValue) { Browser.WaitForElementDisplayed(element); Browser.WaitForElementEnabled(element); var selectedElement = new SelectElement(element); try { selectedElement.SelectByText(expectedValue); } catch (NoSuchElementException) { var allItems = selectedElement.Options; if (allItems.Count == 0) { throw new Exception("There are no options available to select in the drop down"); } foreach (var item in allItems.Where(item => item.Text.Contains(expectedValue))) { try { selectedElement.SelectByText(item.Text); } catch (NoSuchElementException) { LogFunctions.WriteError("Can not find element with text: " + expectedValue); LogFunctions.WriteError("As alternative, also attempted to select item with text: " + item.Text); LogFunctions.WriteError("Select was unsuccessful"); Browser.TakeScreenshot("selectFromDropDown"); throw; } LogFunctions.WriteInfo("Item Selected was: " + item.Text); return(item.Text); } } return(selectedElement.SelectedOption.Text); }
/// <summary> /// The MultiSelectRemove method handles multi select controls, allowing the removal of one or more /// elements from the right side of the select, which will then be put back into the available items on the left side of the list /// The XPath within the item list may need to be changed based on the application under test /// </summary> /// <param name="availableItemId">The left side of the multi select control, listing the available itmes</param> /// <param name="selectedItemId">The right side of the multi select control, listing the selected items</param> /// <param name="itemToSelect">The text of the item to be selected</param> /// <param name="removeButtonId">The button that performs the removal of the selected element</param> public static void MultiSelectRemove(string availableItemId, string selectedItemId, string itemToSelect, string removeButtonId) { LogFunctions.WriteInfo("Trying selected items list find element"); var selectedItemList = Browser.FindElement_byId(selectedItemId); LogFunctions.WriteInfo("Trying remove button find element"); var removeButton = Browser.FindElement_byCss(removeButtonId); LogFunctions.WriteInfo("Trying selected items list wait for element displayed"); Browser.WaitForElementDisplayed(selectedItemList); LogFunctions.WriteInfo("Trying remove button wait for element displayed"); Browser.WaitForElementDisplayed(removeButton); LogFunctions.WriteInfo("Trying to get selected items list elements"); var selectedItems = selectedItemList.FindElements(By.XPath("child::option")); foreach (var selectedItem in selectedItems.Where(selectedItem => selectedItem.Text == itemToSelect)) { LogFunctions.WriteInfo("Waiting for a selected item"); Browser.WaitForElementDisplayed(selectedItem); try { LogFunctions.WriteInfo("clicking a selected item"); selectedItem.Click(); } catch (StaleElementReferenceException) { LogFunctions.WriteInfo("stale element after trying to click"); return; } LogFunctions.WriteInfo("Waiting to click on the remove button"); Browser.WaitForElementDisplayed(removeButton); LogFunctions.WriteInfo("Clicking on the remove button"); removeButton.Click(); break; } LogFunctions.WriteInfo("Trying available items list find element second time"); var availableItemList = Browser.FindElement_byId(availableItemId); LogFunctions.WriteInfo("Trying selected items list find element second time"); selectedItemList = Browser.FindElement_byId(selectedItemId); LogFunctions.WriteInfo("Trying remove button find element second time"); removeButton = Browser.FindElement_byCss(removeButtonId); LogFunctions.WriteInfo("Trying available items list wait for element displayed second time"); Browser.WaitForElementDisplayed(availableItemList); LogFunctions.WriteInfo("Trying selected items list wait for element displayed second time"); Browser.WaitForElementDisplayed(selectedItemList); LogFunctions.WriteInfo("Trying remove button wait for element displayed second time"); Browser.WaitForElementDisplayed(removeButton); LogFunctions.WriteInfo("Trying to get available items list elements"); var availableItems = availableItemList.FindElements(By.XPath("child::option")); foreach (var availableItem in availableItems) { LogFunctions.WriteInfo("Waiting for an available item"); Browser.WaitForElementDisplayed(availableItem); if (availableItem.Text != itemToSelect) { continue; } LogFunctions.WriteInfo("Returning available item is the same as the item to select"); return; } LogFunctions.WriteError("Selected item not found: " + itemToSelect); }