我正在将Cucumber与Java结合使用,并且试图将值传递给本地存储。我也在使用WebDriverManager,当我尝试使用JavaScripExecutor执行某些脚本时,打开了浏览器的另一个实例。
我尝试在WebDriverManager中实现JavaScriptExecutor,但是得到的结果与以前相同。
public class WebDriverManager {
private WebDriver driver;
private static DriverType driverType;
private static EnvironmentType environmentType;
private static final String CHROME_DRIVER_PROPERTY = "webdriver.chrome.driver";
private JavascriptExecutor js;
public WebDriverManager() {
driverType = FileReaderManager.getInstance().getConfigReader().getBrowser();
environmentType = FileReaderManager.getInstance().getConfigReader().getEnvironment();
}
public WebDriver getDriver() throws MalformedURLException {
if(driver == null) driver = createDriver();
return driver;
}
public JavascriptExecutor getJavaScriptExecutor() {
js = (JavascriptExecutor) driver;
return js;
}
private WebDriver createDriver() throws MalformedURLException {
switch (environmentType) {
case LOCAL : driver = createLocalDriver();
break;
case REMOTE : driver = createRemoteDriver();
break;
}
return driver;
}
我希望能够在已经存在的浏览器实例中使用此执行程序。
答案 0 :(得分:0)
我从团队成员那里得到了一些帮助,我们已经弄清楚了。
public WebDriverManager() throws MalformedURLException {
driverType = FileReaderManager.getInstance().getConfigReader().getBrowser();
environmentType = FileReaderManager.getInstance().getConfigReader().getEnvironment();
driver = createDriver();
}
通过将驱动程序变量传递给构造函数,并通过在类中创建适当的构造函数,我设法使JavaScriptExecutor使用WebDriver的现有实例。示例:
JavascriptExecutor js;
public AcceptanceHelper(WebDriver driver) {
this.driver = driver;
this.wait = new WebDriverWait(driver, 15);
this.js = (JavascriptExecutor) driver;
}