我是 Selenium 新手,也是 Java 新手。所以也许我错过了一些明显的东西,但我现在已经在这个问题上旋转了一段时间,无法继续前进并且完全绝望。请帮忙!
这是我的设置:
我的自定义驱动程序类实现 WebDriver 并设置属性:
public class Driver implements WebDriver {
private WebDriver driver;
private String browserName;
public Driver(String browserName) throws Exception {
this.browserName = browserName;
if(browserName.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver");
this.driver = new ChromeDriver();
}
else if(browserName.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver");
this.driver = new FirefoxDriver();
}
else {
throw new Exception("Browser is not correct");
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
<...>
}
BaseTest 类获取属性并在 @BeforeClass 方法内创建驱动程序的新实例(运行测试时使用 maven 命令传递的浏览器名称):
String browserName = getParamater("browser");
driver = new Driver(browserName);
在 @Test 内的 Test 类中,我创建新的 Actions 并将来自 BaseTest 的驱动程序传递给那里:
Actions builder = new Actions(driver);
Action mouseOverHome = builder
.moveToElement(pb.testGoodle)
.build();
mouseOverHome.perform();
这段代码不起作用 -> 没有执行任何操作(没有鼠标悬停或任何其他操作),也没有错误。
但是,如果我在 @Test 本身中创建并定义新的 WebDriver:
System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver");
WebDriver driver = new FirefoxDriver();
Action 完美运行。 任何想法或提示非常感谢!
最佳答案
解决了!解决!问题在于页面对象中的操作声明困惑!页面对象中的内容如下:
protected Driver driver;
public static Actions act;
public PlanBuilder(Driver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
this.act = new Actions(driver);
}
public void rightClick (WebElement element) throws InterruptedException {
// Actions builder = new Actions(basedriver);
Action openContextMenu = driver.act
.contextClick(element)
.build();
openContextMenu.perform();
Thread.sleep(4000);
}
关于java - 除非在 @Test 中声明驱动程序实例,否则无法使 Actions.class 方法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49416839/