我有一个自动化项目,我一直在闲暇时间工作,我一直在尝试将JavascriptExecutor合并到我的页面对象模型中,因为我选择的网站的某些部分在点击时无效在标签上。 目前这就是我在PageObject类中设置代码的方式(我的Page-Object Model的超类):
public class PageObject {
protected Driver driver;
public Actions act;
public JavascriptExecutor js = (JavascriptExecutor) this.driver;
PageObject(Driver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
this.act = new Actions(driver);
}
}
这是Driver类(实现WebDriver)的布局:
public class Driver implements WebDriver {
public WebDriver driver;
String browserName;
public JavascriptExecutor js;
public Driver(String browserName) {
this.browserName = browserName;
if (browserName.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "./resources/webdrivers/chromedriver_win32/chromedriver.exe");
this.driver = new ChromeDriver();
this.js = (JavascriptExecutor) this.driver;
}
// Below are other if conditions for different browsers.
目前,我正在使用js获取NullpointerException,但我知道如果我在PageObject构造函数中初始化js,我会遇到ClassCastException。 (我在Maven项目中设置了这些类。) 我在网上看了一遍,这似乎不是一个常见的话题,现在问我,我完全不知道如何解决这个问题,或者甚至可能。还有其他人遇到过这种情况吗?
更新:找出问题所在。
public class PageObject {
protected Driver driver;
public Actions act;
PageObject(Driver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
this.act = new Actions(driver);
}
}
public class Driver implements WebDriver {
public WebDriver driver;
String browserName;
public static JavascriptExecutor js;
public Driver(String browserName) {
this.browserName = browserName;
if (browserName.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "./resources/webdrivers/chromedriver_win32/chromedriver.exe");
this.driver = new ChromeDriver();
js = (JavascriptExecutor) this.driver;
}
// Below are other if conditions for different browsers.
我不得不将js更改为静态,然后无论我在哪里有一部分不可点击的网站,我都会调用Driver.js执行我的框架测试的任何网页所需的任何命令。