public bool RunTestWithErrorReport(SeInstruction seInstruction) { bool passed; try { passed = RunTest(seInstruction); } catch (Exception ex) { var screenShot = _webDriver.TakeScreenshot(); var email = new MailMessage("*****@*****.**", "*****@*****.**", "Selenium Test Error", "A error occurred when running test case " + _seFilePath); email.Attachments.Add(new Attachment(new MemoryStream(screenShot.AsByteArray), seInstruction.ToString(), "image/png")); EmailService.Instance.Send(email); passed = false; } return(passed); }
public bool RunTest(SeInstruction seInstruction) { string targetType = ""; string targetValue = ""; // xpath=(//input[@name='Portal'])[3] if (seInstruction.Target.StartsWith("xpath=")) { targetType = "xpath"; targetValue = seInstruction.Target.Replace("xpath=", ""); } // //button[@type='submit'] else if (seInstruction.Target.StartsWith("//")) { targetType = "xpath"; targetValue = seInstruction.Target; } // /logout?type=manual&72=635911354550395204 else if (seInstruction.Target.StartsWith("/")) { targetValue = seInstruction.Target; } // link=Home or id=Username else { var target = seInstruction.Target.Split('='); targetType = target[0]; targetValue = target[1]; } IWebElement element = null; SeTargetEnum t; Enum.TryParse(targetType, true, out t); switch (t) { case SeTargetEnum.None: break; case SeTargetEnum.Css: element = _webDriver.FindElement(By.CssSelector(targetValue)); break; case SeTargetEnum.Id: element = _webDriver.FindElement(By.Id(targetValue)); break; case SeTargetEnum.Link: element = _webDriver.FindElement(By.LinkText(targetValue)); break; case SeTargetEnum.XPath: // xpath=(//input[@name='Portal'])[3] if (targetValue.StartsWith("xpath")) { var xpath = targetValue.Substring(targetValue.IndexOf("(", StringComparison.Ordinal) + 1, targetValue.LastIndexOf(")", StringComparison.Ordinal) - targetValue.IndexOf("(", StringComparison.Ordinal) - 1); var index = int.Parse(targetValue.Substring(targetValue.LastIndexOf("[", StringComparison.Ordinal) + 1, targetValue.LastIndexOf("]", StringComparison.Ordinal) - targetValue.LastIndexOf("[", StringComparison.Ordinal) - 1)); element = _webDriver.FindElements(By.XPath(xpath))[index - 1]; } else { element = _webDriver.FindElement(By.XPath(targetValue)); } break; } //if (element != null) //{ // _webDriver.ExecuteJavaScript<IWebElement>("arguments[0].setAttribute('value', 'Set to this text.');", element); //} // command SeCommandeEnum c; Enum.TryParse(seInstruction.Command, true, out c); switch (c) { case SeCommandeEnum.None: break; case SeCommandeEnum.Click: element.Click(); break; case SeCommandeEnum.ClickAndWait: element.Click(); System.Threading.Thread.Sleep(1000); break; case SeCommandeEnum.Open: _webDriver.Navigate().GoToUrl(targetValue.StartsWith("http") ? targetValue : _baseUrl + targetValue); System.Threading.Thread.Sleep(1000); break; case SeCommandeEnum.Pause: System.Threading.Thread.Sleep(int.Parse(targetValue)); break; case SeCommandeEnum.Select: element.Select(seInstruction.Value); break; case SeCommandeEnum.Type: element.Type(seInstruction.Value); break; } // click on body to get out of current action _webDriver.FindElement(By.TagName("body")).Click(); return(true); }