好的,所以我使用 POM
在 Selenium/Maven
中创建一种框架。该项目有三个包
- 页面(包括我将浏览的不同网页)
- 测试(将调用函数并执行)和
- Util(包含一个 Library 类,其中包含我想从 Test 和 Pages 包中的类调用的函数)
这是我的 Library 类的代码:
package util;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class Lib {
//Question is regarding the method below
public static WebElement get(WebDriver driver, String url) {
WebElement link = driver.get(url);
return link;
}
public static WebElement findelement(WebDriver driver, String xpath) {
WebElement e = driver.findElement(By.xpath(xpath));
return e;
}
public static void sleep(WebDriver driver, int sec) throws InterruptedException {
Thread.sleep(sec*1000);
}
}
我希望能够从测试类中的 Lib 类调用 get 方法(第一个)。这是我的测试类代码:
package test;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import util.Lib;
public class Test extends Lib{
static WebDriver driver = new ChromeDriver();
@Before
public void before() {
System.out.println("Before Class starts here");
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\TQ\\sel\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
Lib l = new Lib();
l.get(driver, "https://www.google.com/");
}
}
我知道我可以轻松地在测试类中执行 driver.get(url)
但我想知道为什么它不像我的 lib 类中的其他函数那样工作。
这是我收到的错误消息“无法从 void 转换为 WebElement”
。
提前致谢。
最佳答案
该方法无效。它应该看起来像:
//Question is regarding the method below
public static void get(WebDriver driver, String url) {
driver.get(url);
}
仅此而已。在这种情况下,您不需要返回任何东西。
关于java - 如何在 Selenium 中创建将从另一个类调用的 driver.get 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48107853/