【发布时间】:2016-05-06 08:40:35
【问题描述】:
我目前正在尝试为网站创建一些自动化测试,我遇到了一个问题,即每当我调用我的基类时,它都会创建一个新的 FirefoxDriver 实例。因此,每当我在我的步骤中调用一个继承基类的页面时,它都会加载一个新的驱动程序实例,因此它不再在以前的驱动程序上实现自动化。
namespace RAA_AutomationTests
{
using OpenQA.Selenium.Firefox;
public abstract class BasePage
{
protected static IWebDriver driver;
protected BasePage()
{
//cant keep creating a new driver need to change this
driver = new FirefoxDriver();
}
public void click(By locator)
{
Find(locator).Click();
}
public IWebElement Find(By locator)
{
//ValidateSelector(locator); will update css selectors, however not to cause any extra delays by checking this programmatically.
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(ExpectedConditions.ElementIsVisible(locator));
return driver.FindElement(locator);
}
}
}
这是我的基类中的所有代码,我只需要找到一种使用已经打开的驱动程序而不是创建新驱动程序的方法。 我有一个钩子文件,它可以为我打开浏览器,所以我只需要基类中的驱动程序就可以使用它。
【问题讨论】:
-
“为我打开浏览器的钩子文件”意味着这是另一个通过 selenium 打开 Firefox 的类吗?
-
是的,我有另一个文件,它使用 [BeforeScenario] 打开浏览器我只使用基类来实现点击和等待功能,但我需要告诉它使用哪个驱动程序才能使其工作。
-
您在页面上继承基类,因此每次创建 Page 类的对象时都会创建 Firefox 对象。如此好的方法将您的浏览器配置方法与页面分开,它应该在测试类级别而不是页面类级别。
-
你能给我举个例子吗?
标签: c# selenium selenium-webdriver instance specflow