public static void ScrollToAnElement(string pageName, string locatorname, params String[] values) { By element = ElementLocator.GetLocator(pageName, locatorname, values); if (element != null) { try { wait.Until(WaitHelper.ExpectedConditions.PresenceOfAllElementsLocatedBy(element)); IWebElement webelement = driver.FindElement(element); IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript("arguments[0].scrollIntoView(true)", webelement); log.Info("Scroll to " + locatorname); } catch (WebDriverException wd) { Report.Fail("Exception on the element " + locatorname + " " + wd.Message); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorname + " not found"); } } else { log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorname + " not found"); } }
public static void SetAttribute(String pageName, String locatorName, String attrname, String value) { By element = ElementLocator.GetLocator(pageName, locatorName); if (element != null) { try { wait.Until(WaitHelper.ExpectedConditions.PresenceOfAllElementsLocatedBy(element)); IWebElement webelement = driver.FindElement(element); IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript("arguments[0].setAttribute(arguments[1],arguments[2]);", webelement, attrname, value); //js.ExecuteScript("arguments[0].value='" + text + "'", webelement); Report.Pass("Entered : " + value + " in " + attrname + " of " + attrname); log.Info("Entered : " + value + " in " + attrname + " of " + attrname); } catch (WebDriverException wd) { Report.Fail("Exception on the element " + locatorName + " " + wd.Message); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } } else { Report.Fail(locatorName + " not found"); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } }
/// <summary> /// This method sets the text in a text box /// </summary> /// <param name="pageName"></param> /// <param name="locatorName"></param> /// <param name="text"></param> /// <param name="values"></param> public static void SetText(string pageName, string locatorName, string text, params String[] values) { By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); } else { element = ElementLocator.GetLocator(pageName, locatorName, values); } if (element != null) { try { wait.Until(WaitHelper.ExpectedConditions.PresenceOfAllElementsLocatedBy(element)); driver.FindElement(element).Clear(); driver.FindElement(element).SendKeys(text); Report.Pass("Entered : " + text + " in " + locatorName); log.Info("Entered : " + text + " in " + locatorName); } catch (WebDriverException wd) { Report.Fail("Exception on the element " + locatorName + " " + wd.Message); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } } else { Report.Fail(locatorName + " not found"); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } }
public static void Click(String pageName, String locatorName) { By element = ElementLocator.GetLocator(pageName, locatorName); if (element != null) { try { wait.Until(WaitHelper.ExpectedConditions.ElementToBeClickable(element)); IWebElement webelement = driver.FindElement(element); IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript("arguments[0].click()", webelement); Report.Pass("Clicked On " + locatorName); log.Info("Clicked On " + locatorName); } catch (WebDriverException wd) { Report.Fail("Exception on the element " + locatorName + " " + wd.Message); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } } else { Report.Fail(locatorName + " not found"); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } }
/// <summary> /// This method will return value of given atttribute /// </summary> /// <param name="pageName"></param> /// <param name="locatorName"></param> /// <param name="attributeValue"></param> /// <param name="values"></param> /// <returns></returns> public static string GetAttributeValue(string pageName, string locatorName, String attributeValue, params String[] values) { string text = "false"; By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); } else { element = ElementLocator.GetLocator(pageName, locatorName, values); } if (element != null) { try { wait.Until(WaitHelper.ExpectedConditions.PresenceOfAllElementsLocatedBy(element)); text = driver.FindElement(element).GetAttribute(attributeValue); Report.Pass(locatorName + "Attribute " + attributeValue + " value is: " + text); log.Info(locatorName + "Attribute " + attributeValue + " value is: " + text); } catch (WebDriverException wd) { Report.Fail("Exception on the element " + locatorName + " " + wd.Message); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } } else { Report.Fail(locatorName + " not found "); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } return(text); }
/// <summary> /// This method moves the control to an element /// </summary> /// <param name="pageName"></param> /// <param name="locatorName"></param> /// <param name="values"></param> public static void MoveToElement(string pageName, string locatorName, params String[] values) { By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); } else { element = ElementLocator.GetLocator(pageName, locatorName, values[0]); } if (element != null) { try { wait.Until(WaitHelper.ExpectedConditions.PresenceOfAllElementsLocatedBy(element)); Actions actions = new Actions(driver); actions.MoveToElement(driver.FindElement(element)).Click().Build().Perform(); Report.Pass(locatorName + " is in Focus"); log.Info(locatorName + " is in Focus"); } catch (WebDriverException wd) { Report.Fail("Exception on the element " + locatorName + " " + wd.Message); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } } else { Report.Fail((locatorName + " not found")); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } }
/// <summary> /// This method click on a control /// </summary> /// <param name="pageName"></param> /// <param name="locatorName"></param> /// <param name="values"></param> public static void Click(string pageName, string locatorName, params string[] values) { By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); } else { element = ElementLocator.GetLocator(pageName, locatorName, values); } if (element != null) { //try //{ wait.Until(WaitHelper.ExpectedConditions.ElementToBeClickable(element)); JavaScriptKeywords.HighlightElement(driver.FindElement(element)); driver.FindElement(element).Click(); Report.Pass("Clicked On " + locatorName); log.Info("Clicked On " + locatorName); //}catch(WebDriverException wd) //{ // Report.Fail("Exception on the element "+ locatorName+" "+wd.Message); // log.Error(TestContext.CurrentContext.Test.MethodName + " Exception " + wd.Message); //} } else { Report.Fail(locatorName + " not found"); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } }
/// <summary> /// Gets a value indicating whether or not this element is visible. /// When expected result is false /// </summary> /// <param name="pageName"></param> /// <param name="locatorName"></param> /// <param name="values"></param> /// <returns></returns> public static Boolean IsElementNotVisible(string pageName, string locatorName, params String[] values) { bool exists = false; By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); } else { element = ElementLocator.GetLocator(pageName, locatorName, values); } if (element != null) { try { IWebElement el = driver.FindElement(element); exists = el.Displayed; if (exists == true) { JavaScriptKeywords.HighlightElement(el); Report.Fail("Element " + locatorName + " Exists on screen: " + exists.ToString()); log.Error("Element" + locatorName + " Exists on screen: " + exists.ToString()); } else { Report.Pass("Element " + locatorName + " Exists on screen: " + exists.ToString()); log.Info("Element" + locatorName + " Exists on screen: " + exists.ToString()); } } catch (NoSuchElementException e) { Report.Pass("Element " + locatorName + " exists on screen: " + exists.ToString()); log.Info("Element" + locatorName + " Exists on screen: " + exists.ToString()); } catch (Exception e) { Report.Fail("Locator name : " + locatorName + " , " + e.Message); log.Error("Locator name : " + locatorName + " , " + e.Message); } } return(exists); }
/// <summary> /// Gets a value indicating whether or not this element is present. /// </summary> /// <param name="pageName"></param> /// <param name="locatorName"></param> /// <param name="values"></param> /// <returns></returns> public static Boolean IsElementPresent(string pageName, string locatorName, params String[] values) { bool exists = false; By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); } else { element = ElementLocator.GetLocator(pageName, locatorName, values); } if (element != null) { try { IWebElement el = driver.FindElement(element); wait.Until(WaitHelper.ExpectedConditions.ElementExists(element)); exists = el.Displayed; if (exists == true) { Report.Pass("Element " + locatorName + " Exists on screen: " + exists.ToString()); log.Info("Element" + locatorName + " Exists on screen: " + exists.ToString()); } else { Report.Fail("Element " + locatorName + " Exists on screen: " + exists.ToString()); log.Error("Element" + locatorName + " Exists on screen: " + exists.ToString()); } } catch (NoSuchElementException e) { Report.Fail("Unable to locate element: " + locatorName); log.Error(e.Message); } catch (Exception e) { Report.Fail("Locator name : " + locatorName + " , " + e.Message); log.Error("Locator name : " + locatorName + " , " + e.Message); } } return(exists); }
/// <summary> /// This method press the specified key from Keyboard /// </summary> /// <param name="pageName"></param> /// <param name="locatorName"></param> /// <param name="values"></param> public static void PressKey(string pageName, string locatorName, string keyAction, params String[] values) { By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); } else { element = ElementLocator.GetLocator(pageName, locatorName, values); } wait.Until(WaitHelper.ExpectedConditions.PresenceOfAllElementsLocatedBy(element)); switch (keyAction) { //delete the text by selecting existing text case "DeleteAllText": { driver.FindElement(element).SendKeys(Keys.Control + "a"); driver.FindElement(element).SendKeys(Keys.Delete); Report.Pass("Delete the text form " + locatorName); log.Info("Delete the text form " + locatorName); break; } //press enter key case "Enter": { driver.FindElement(element).SendKeys(Keys.Enter); Report.Pass("Enter Key is press on " + locatorName); log.Info("Enter Key is press on " + locatorName); break; } default: { Report.Fail(locatorName + " Text not cleared"); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); break; } } }
public static void MoveAndClickAtSpecificPosition(string pageName, string locatorName, int w, int widthmultiple, int h, int heightmultiple, params String[] values) { By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); } else { element = ElementLocator.GetLocator(pageName, locatorName, values); } if (element != null) { try { wait.Until(WaitHelper.ExpectedConditions.PresenceOfAllElementsLocatedBy(element)); IWebElement ele = driver.FindElement(element); int height = ele.Size.Height; int width = ele.Size.Width; int move_width = (width / w) * widthmultiple; int move_height = (height / h) * heightmultiple; Actions actions = new Actions(driver); actions.MoveToElement(ele, move_width, move_height).Click().Build().Perform(); //actions.MoveToElement(ele).MoveByOffset(move_width, move_height).Click().Build().Perform(); Report.Pass(locatorName + " is in Focus"); log.Info(locatorName + " is in Focus"); } catch (WebDriverException wd) { Report.Fail("Exception on the element " + locatorName + " " + wd.Message); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } } else { Report.Fail((locatorName + " not found")); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } }
/// <summary> /// This method Taps on a control /// </summary> /// <param name="pageName"></param> /// <param name="locatorName"></param> /// <param name="values"></param> public static void Tap(string pageName, string locatorName, params String[] values) { By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); } else { element = ElementLocator.GetLocator(pageName, locatorName, values); } if (element != null) { TouchAction touch = null; if (ConfigurationManager.AppSettings["os"].ToLower().Equals("android")) { AndroidDriver <AndroidElement> androidDriver = (AndroidDriver <AndroidElement>)driver; touch = new TouchAction(androidDriver); } else if (ConfigurationManager.AppSettings["os"].ToLower().Equals("ios")) { IOSDriver <IOSElement> iOSDriver = (IOSDriver <IOSElement>)driver; touch = new TouchAction(iOSDriver); } wait.Until(WaitHelper.ExpectedConditions.ElementToBeClickable(element)); IWebElement el = driver.FindElement(element); touch.Tap(el).Perform(); //el.Click(); Report.Pass("Tapped On " + locatorName); log.Info("Tapped On " + locatorName); } else { Report.Fail(locatorName + " not found"); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } }
/// <summary> /// This method gets the text of a control /// </summary> /// <param name="pageName"></param> /// <param name="locatorName"></param> /// <param name="values"></param> /// <returns></returns> public static string GetText(string pageName, string locatorName, params string[] values) { string text = ""; By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); //JavaScriptKeywords.HighlightElement(driver.FindElement(element)); } else { element = ElementLocator.GetLocator(pageName, locatorName, values); } if (element != null) { try { wait.Until(WaitHelper.ExpectedConditions.PresenceOfAllElementsLocatedBy(element)); text = driver.FindElement(element).Text; Report.Info("Innertext of " + locatorName + " : " + text); log.Info("Innertext of " + locatorName + " : " + text); } catch (WebDriverException wd) { Report.Fail("Exception on the element " + locatorName + " " + wd.Message); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } } else { Report.Fail(locatorName + " not found"); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } return(text); }
/// <summary> /// This method check that the control is readonly /// </summary> /// <param name="by">Element locator</param> /// <returns>It return the boolean object</returns> public static Boolean IsReadOnly(string pageName, string locatorName, params String[] values) { Boolean isreadonly = false; By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); } else { element = ElementLocator.GetLocator(pageName, locatorName, values); } if (element != null) { try { wait.Until(WaitHelper.ExpectedConditions.PresenceOfAllElementsLocatedBy(element)); string text = driver.FindElement(element).GetAttribute("readonly"); isreadonly = Convert.ToBoolean(text); Report.Pass(locatorName + " is ReadOnly : " + isreadonly); log.Info(locatorName + " is ReadOnly : " + isreadonly); } catch (WebDriverException wd) { Report.Fail("Exception on the element " + locatorName + " " + wd.Message); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } } else { Report.Fail(locatorName + " not found"); log.Error(TestContext.CurrentContext.Test.MethodName + " " + locatorName + " not found"); } return(isreadonly); }
///// <summary> ///// This method click on a control ///// </summary> ///// <param name="pageName"></param> ///// <param name="locatorName"></param> ///// <param name="elementname"></param> ///// <param name="values"></param> //public static void Click(string pageName, string locatorName, string elementname,params string[] values) //{ // By element = null; // if (values.Length == 0) // { // element = ElementLocator.GetLocator(pageName, locatorName); // } // else // { // element = ElementLocator.GetLocator(pageName, locatorName, values); // } // if (element != null) // { // //try // //{ // wait.Until(WaitHelper.ExpectedConditions.ElementToBeClickable(element)); // JavaScriptKeywords.HighlightElement(driver.FindElement(element)); // driver.FindElement(element).Click(); // Report.Pass("Clicked On " + elementname); // log.Info("Clicked On " + elementname); // //}catch(WebDriverException wd) // //{ // // Report.Fail("Exception on the element "+ locatorName+" "+wd.Message); // // log.Error(TestContext.CurrentContext.Test.MethodName + " Exception " + wd.Message); // //} // } // else // { // Report.Fail(elementname + " not found"); // log.Error(TestContext.CurrentContext.Test.MethodName + " " + elementname + " not found"); // } //} /// <summary> /// This method select the data from Drop Down /// </summary> /// <param name="pageName"></param> /// <param name="locatorName"></param> /// <param name="selectText"></param> /// <param name="values"></param> public static void SelectValueFromDropdown(string pageName, string locatorName, String selectText, params String[] values) { By element = null; if (values.Length == 0) { element = ElementLocator.GetLocator(pageName, locatorName); } else { element = ElementLocator.GetLocator(pageName, locatorName, values); } if (element != null) { try { wait.Until(WaitHelper.ExpectedConditions.ElementToBeClickable(element)); //element = wait.Until(WaitHelper.ExpectedConditions.ElementIsVisible(by)); SelectElement select = new SelectElement(driver.FindElement(element));//Used to select from the drop down select.SelectByText(selectText); Report.Pass("Element Selected :" + locatorName); log.Info("Element Selected :" + locatorName); } catch (WebDriverException wd) { Report.Fail("Exception on the element " + locatorName + " " + wd.Message); log.Error(TestContext.CurrentContext.Test.MethodName + " " + selectText + " not found"); } } else { Report.Fail(values[0] + " not found"); log.Error(TestContext.CurrentContext.Test.MethodName + " " + values[0] + " not found"); } }