我已经在C#中使用硒Web驱动程序编写了一些测试用例。 我使用ORDER属性按顺序运行测试用例。它是按顺序运行的。但是测试不会等待先前的测试完成。
我也使用过WebDriverWait。但这并不等待。
参考链接:https://github.com/nunit/docs/wiki/Order-Attribute
请找到以下代码段:
[Test, Order(1)]
public void OpenUserPage()
{
try
{
var wait = new WebDriverWait(_webDriver, new TimeSpan(2000));
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("NavigationMenu")));
var userPageEle = _webDriver.FindElement(By.ClassName("icon-Users"));
userPageEle.Click();
}
catch (Exception e)
{
SendMail.SendErrorEmail(e.StackTrace, "OpenUserPage()");
}
}
[Test, Order(2)]
public void AddUser()
{
try
{
var waitForAddUser= new WebDriverWait(_webDriver, new TimeSpan(5000));
waitForAddUser.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("[ng-click*=AddUserBtnClick]")));
var addUser = _webDriver.FindElement(By.CssSelector("[ng-click*=AddUserBtnClick]"));
addUser.Click();
var element = _webDriver.FindElement(By.XPath("//input[@id='Role_hidden']"));
var permissionSet = _webDriver.FindElement(By.XPath("//input[@id='RolePermissionSet_hidden']"));
var FirstName = _webDriver.FindElement(By.Name("FirstName"));
FirstName.SendKeys("Sangeetha");
var LastName = _webDriver.FindElement(By.Name("Last Name"));
LastName.SendKeys("Periyaiah");
var UserName = _webDriver.FindElement(By.Name("UserName"));
UserName.SendKeys("SangeetheP");
var Password = _webDriver.FindElement(By.Name("Password"));
Password.SendKeys("Sync@123");
var Email = _webDriver.FindElement(By.Name("Email"));
Email.SendKeys("sangee900@gmail.com");
var RoleList = _webDriver.FindElement(By.Name("RoleList"));
element.SendKeys("SuperAdmin");
var RolePermissionList = _webDriver.FindElement(By.Name("RolePermissionList"));
permissionSet.SendKeys("SuperAdmin");
var MobileNumber = _webDriver.FindElement(By.Name("MobileNumber"));
MobileNumber.SendKeys("8220867882");
var AreaCode = _webDriver.FindElement(By.Name("AreaCode"));
AreaCode.SendKeys("600002");
var Address = _webDriver.FindElement(By.Name("Address"));
Address.SendKeys("3/761, Ab Block");
var submit = _webDriver.FindElement(By.CssSelector("[ng-click*=AddUpdateUser]"));
submit.Click();
}
catch (Exception e)
{
SendMail.SendErrorEmail(e.StackTrace, "AddUser()");
}
}
[Test, Order(3)]
public void ApplyFilter()
{
try
{
var waitForSearch = new WebDriverWait(_webDriver, new TimeSpan(55000));
waitForSearch.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector(".form-control.ng-pristine.ng-valid")));
var searchUser = _webDriver.FindElement(By.CssSelector(".form-control.ng-pristine.ng-valid"));
searchUser.SendKeys("as");
var waitFilter = new WebDriverWait(_webDriver, new TimeSpan(55000));
waitFilter.Until(ExpectedConditions.ElementToBeClickable((By.CssSelector("[ng-click*=filterUserBtnClick]"))));
var applyUserFilter = _webDriver.FindElement(By.CssSelector("[ng-click*=filterUserBtnClick]"));
applyUserFilter.Click();
var waitResetFilter = new WebDriverWait(_webDriver, new TimeSpan(55000));
waitResetFilter.Until(ExpectedConditions.ElementToBeClickable((By.CssSelector("[ng-click*=resetFilter]"))));
var resetFilter = _webDriver.FindElement(By.CssSelector("[ng-click*=resetFilter]"));
resetFilter.Click();
}
catch (Exception e)
{
SendMail.SendErrorEmail(e.StackTrace, "ApplyFilter()");
}
}
请分享其他解决方案。
预先感谢
答案 0 :(得分:0)
由于您没有提到测试用例的确切结果,但是在阅读了代码之后,我会提出两种解决方法。
如果webdriverwait代码行引发某些异常并且不起作用,那么您可以参考以下代码
var waitForAddUser= new WebDriverWait(_webDriver, new TimeSpan(5000));
waitForAddUser.IgnoreExceptionTypes(typeof(NoSuchElementException),
typeof(ElementNotVisibleException));
waitForAddUser.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("[ng-
click*=AddUserBtnClick]")));
使用[Setup()] CommonWait(){Thread.sleep(5000)); }
答案 1 :(得分:0)
我找到了解决方法。
Selenium Web驱动程序提供了一个回调,该回调试图在给定的分钟数/秒/小时内找到目标元素。
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
Func<IWebDriver, bool> waitForElement = new Func<IWebDriver, bool>((IWebDriver Web) =>
{
Console.WriteLine(Web.FindElement(By.Id("target")).GetAttribute("innerHTML"));
return true;
});
wait.Until(waitForElement);
参考链接: https://www.toolsqa.com/selenium-webdriver/c-sharp/advance-explicit-webdriver-waits-in-c/