我正在使用C#
并使用NUNIT 3.0
框架处理selenium。我有大约200个测试用例,我不想为所有300个测试用例设置try catch,因为在任何测试用例中都可能发生此异常。我需要的是在我的项目中全局处理它。
如果任何人提供任何输入,我们将不胜感激。请问是否需要其他任何东西。
我的设置类代码格式为:
namespace HUB_REGRESSION
{
[SetUpFixture]
public class BaseSetup
{
public static IWebDriver driver = new FirefoxDriver();
[OneTimeSetUp]
public void Setup()
{
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
/* Code to visit site URL and Login */
//- I assume Global Exception handling code will go here as i have globally defined the implicit wait time-//
}
[OneTimeTearDown]
public void TearDown()
{
driver.Quit();
}
}
}
答案 0 :(得分:0)
我总是为IWebDriver和IWebElement添加扩展。请参阅以下2个示例扩展方法:
public static class WebDriverExtension
{
public static ReadOnlyCollection<IWebElement> FindElementsBy(this IWebDriver driver, By by, int timeoutSecond = 0)
{
IWait<IWebDriver> wait = new DefaultWait<IWebDriver>(driver);
wait.Timeout = TimeSpan.FromSeconds(timeout);
wait.PollingInterval = TimeSpan.FromMilliseconds(300);
try
{
wait.Until(d => d.FindElements(by).Count > 0);
return driver.FindElements(by);
}
catch (Exception)
{
throw new NoSuchElementException("Unable to find element, locator: \"" + by.ToString() + "\".");
}
}
public static ReadOnlyCollection<IWebElement> FindElementsBy(this IWebElement element, By by, int timeout = 0)
{
IWait<IWebElement> wait = new DefaultWait<IWebElement>(element);
wait.Timeout = TimeSpan.FromSeconds(timeout);
wait.PollingInterval = TimeSpan.FromMilliseconds(300);
try
{
wait.Until(e => e.FindElements(by).Count > 0);
return element.FindElements(by);
}
catch (Exception)
{
throw new NoSuchElementException("Unable to find element, locator: \"" + by.ToString() + "\".");
}
}
}
在您的测试中,您可以使用所有扩展方法:
public void ATestMethod()
{
IWebElement element = driver.FindElementsBy(By.Id("anID"), 5).First();
element.FindElementsBy(By.Id("ID2"), 3)
}