public DataTable getWebHtmlTable(objIdentifier obj) { DataTable dt = new DataTable(); IWebElement iElement = getElement(obj); IList <IWebElement> trElement = iElement.FindElements(By.TagName("tr")); IList <IWebElement> thElement = iElement.FindElements(By.TagName("th")); int counter = 0; foreach (IWebElement idc in thElement) { dt.Columns.Add(idc.Text, typeof(string)); } foreach (IWebElement idr in trElement) { if (counter != 0) { int colindex = 0; DataRow dr = dt.NewRow(); foreach (IWebElement dc in idr.FindElements(By.TagName("td"))) { dr[colindex] = dc.Text; colindex++; } dt.Rows.Add(dr); } counter++; } return(dt); }
public void ElementAction(objIdentifier oI, Enum Action) { ElementAction(oI, Action, ""); }
public void ElementAction(objIdentifier oI, Enum action, string ActionValue) { string StepStartTime = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff"); IWebElement WebElement = getElement(oI); switch (action.ToString()) { case "Click": WebElement.Click(); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")); break; case "jsClick": JS = (IJavaScriptExecutor)driver; JS.ExecuteScript("document." + oI.jsId + oI.IdValue + "\").click();"); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")); break; case "jsSetText": JS = (IJavaScriptExecutor)driver; JS.ExecuteScript("document." + oI.jsId + oI.IdValue + "\").style.value=\"" + ActionValue + "\";"); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")); break; case "Clear": WebElement.Clear(); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")); break; case "NavigateToElement": OpenQA.Selenium.Interactions.Actions actions = new OpenQA.Selenium.Interactions.Actions(driver); actions.MoveToElement(WebElement); actions.Perform(); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")); break; case "VerifyText": if (WebElement.Text.Equals(ActionValue)) { spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Expected Text = " + ActionValue + " Actual Text = " + WebElement.Text + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + "text verification passed"); } else { ProjectDetails["CurrentTestResult"] = "Fail"; spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Expected Text = " + ActionValue + " Actual Text = " + WebElement.Text + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + "text verification failed"); } break; case "InnerHtml": if (WebElement.Text.Equals(ActionValue)) { spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Expected Text = " + ActionValue + " Actual Text = " + WebElement.Text + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + "text verification passed"); } else { ProjectDetails["CurrentTestResult"] = "Fail"; spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Expected Text = " + ActionValue + " Actual Text = " + WebElement.Text + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + "text verification failed"); } break; case "VerifyContains": if (WebElement.Text.Contains(ActionValue)) { spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Expected Text = " + ActionValue + " Actual Text = " + WebElement.Text + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + "contains verification passed"); } else { ProjectDetails["CurrentTestResult"] = "Fail"; spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Expected Text = " + ActionValue + " Actual Text = " + WebElement.Text + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + "contains verification failed"); } break; case "VerifyTitle": if (driver.Title.Equals(ActionValue)) { spReporter.WriteLine("Waiting Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Expected Text = " + ActionValue + " Actual Text = " + driver.Title + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + "title verification passed"); } else { ProjectDetails["CurrentTestResult"] = "Fail"; spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Expected Text = " + ActionValue + " Actual Text = " + driver.Title + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + "title verification failed"); } break; case "SendKeys": WebElement.SendKeys(ActionValue); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + " Value is " + ActionValue + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")); break; case "ClearAndSendKeys": WebElement.Clear(); WebElement.SendKeys(ActionValue); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + " Value is " + ActionValue + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")); break; case "ClearAndSendKeysAndTab": WebElement.Clear(); WebElement.SendKeys(ActionValue); WebElement.SendKeys(Keys.Tab); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + " Value is " + ActionValue + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")); break; case "SelectByIndex": new SelectElement(WebElement).SelectByIndex(Int32.Parse(ActionValue)); System.Threading.Thread.Sleep(250); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + " Action was SelectByText(" + ActionValue + ")"); break; case "SelectByText": new SelectElement(WebElement).SelectByText(ActionValue); System.Threading.Thread.Sleep(250); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + " Action was SelectByText(" + ActionValue + ")"); break; case "SelectByValue": new SelectElement(WebElement).SelectByValue(ActionValue); System.Threading.Thread.Sleep(250); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + " Action was SelectByText(" + ActionValue + ")"); break; case "SetProperty": JS = (IJavaScriptExecutor)driver; JS.ExecuteScript("document." + oI.jsId + oI.IdValue + "\").style." + oI.IdProperty + "=\"" + oI.IdPropertyValue + "\";"); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")); break; case "Check": try { if (!WebElement.GetAttribute("checked").Equals("true")) { WebElement.Click(); } } catch { } spReporter.WriteLine("Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + " Action was Checked(" + ActionValue + ")"); break; case "Displayed": int synccounter = 0; while (!WebElement.Displayed) { System.Threading.Thread.Sleep(100); synccounter = synccounter + 1; if (synccounter > 5) { ActionValue = "Not Displayed"; break; } } spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + " Action was CheckDisplayed(" + ActionValue + ")"); break; case "WaitResolveStaleElementAndClick": for (int i = 0; i < 60; i++) { try { WebElement.Click(); i = 60; } catch (Exception e) { if (e.Message.Contains("stale element")) { System.Threading.Thread.Sleep(100); WebElement = getElement(oI); } else { i = 60; } } } spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + " Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff")); break; case "Uncheck": WebElement.Click(); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + " Action was Uncheck(" + ActionValue + ")"); break; case "ClickHandleModal": WebElement.Click(); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + " Action was ClickHandleModal() "); break; case "Submit": WebElement.Submit(); spReporter.WriteLine("Object was " + oI.IdName.ToString() + " Action was " + action.ToString() + "Started at : " + StepStartTime + " Completed at: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss:fff") + " Action was ClickHandleModal() "); break; default: break; } }
public IWebElement getElement(objIdentifier objI) { if (objI.IdName == null) { objI.IdName = string.Empty; } IWebElement cWebElement = driver.FindElement(By.TagName("HTML")); int timer = 0; switch (objI.IdType.ToString()) { case "Id": objI.jsId = "getElementById(\""; try { while (timer < elementFindRepeatTime) { cWebElement = driver.FindElement(By.Id(objI.IdValue.ToString())); timer = elementFindRepeatTime; } } catch { timer = timer + 1; System.Threading.Thread.Sleep(100); } break; case "CssSelector": objI.jsId = "querySelector(\""; try { while (timer < elementFindRepeatTime) { cWebElement = driver.FindElement(By.CssSelector(objI.IdValue.ToString())); timer = elementFindRepeatTime; } } catch { timer = timer + 1; System.Threading.Thread.Sleep(100); } break; case "ClassName": objI.jsId = "getElementByClassName(\""; try { while (timer < elementFindRepeatTime) { cWebElement = driver.FindElement(By.ClassName(objI.IdValue.ToString())); timer = elementFindRepeatTime; } } catch { timer = timer + 1; System.Threading.Thread.Sleep(100); } break; case "Name": //js will not work. generate css objI.jsId = "getElementByName(\""; try { while (timer < elementFindRepeatTime) { cWebElement = driver.FindElement(By.Name(objI.IdValue.ToString())); timer = elementFindRepeatTime; } } catch { timer = timer + 1; System.Threading.Thread.Sleep(100); } break; case "LinkText": //js will not work. generate css objI.jsId = "querySelector(\""; try { while (timer < elementFindRepeatTime) { cWebElement = driver.FindElement(By.LinkText(objI.IdValue.ToString())); timer = elementFindRepeatTime; } } catch { timer = timer + 1; System.Threading.Thread.Sleep(100); } break; case "TagName": objI.jsId = "getElementByTagName(\""; try { while (timer < elementFindRepeatTime) { cWebElement = driver.FindElement(By.TagName(objI.IdValue.ToString())); timer = elementFindRepeatTime; } } catch { timer = timer + 1; System.Threading.Thread.Sleep(100); } break; case "XPath": //js will not work. generate css objI.jsId = "getElementByXPath(\""; try { while (timer < elementFindRepeatTime) { cWebElement = driver.FindElement(By.XPath(objI.IdValue.ToString())); timer = elementFindRepeatTime; } } catch { timer = timer + 1; System.Threading.Thread.Sleep(100); } break; case "PartialLinkText": //js will not work. generate css objI.jsId = "getElementByPartialLinkText(\""; try { while (timer < elementFindRepeatTime) { cWebElement = driver.FindElement(By.PartialLinkText(objI.IdValue.ToString())); timer = elementFindRepeatTime; } } catch { timer = timer + 1; System.Threading.Thread.Sleep(100); } break; default: objI.jsId = "querySelector(\""; cWebElement = null; break; } return(cWebElement); }