gpt4 book ai didi

java - 网络驱动程序。使用参数化类后无法运行测试

转载 作者:行者123 更新时间:2023-12-02 06:48:52 24 4
gpt4 key购买 nike

我需要实现一件事:每次开始测试时,我都必须登录系统。我创建了一个应该运行此登录过程的类。我能够向此类发送正确的值,并且它能够登录,但之后,我在第二个类(正在运行测试本身)上收到 NullPointerException。看起来根本看不到浏览器窗口。请帮助我以一种允许我在任意数量的类中重用 Login 类的方式编写此类。

登录一个:

public class Login {

private static WebDriver driver;
public static void Find_Fields (String path,String login, String password) {
driver = Driver.get(path);
WebElement login_field = driver.findElement(By.id("abc"));
login_field.sendKeys(login);
//Find Password Field on Start page
WebElement password_field = driver.findElement(By.id("abc"));
password_field.sendKeys(password);
//Find Login Button
WebElement login_button = driver.findElement(By.xpath("abc"));
login_button.click();
}
}

public class Testing {

private static WebDriver driver;
@BeforeClass
public static void a_setup(){
//here I'm trying to run Login with parameters
Login fields = new Login();
fields.Find_Fields(LOGIN_PATH, LOGIN, PASSWORD);
}

@Test
public void b_Press_Login_Button(){
//Here I'm trying to start testing in session started from a_setup()
WebElement keyword = driver.findElement("..."));
keyword.sendKeys("...");
}

@AfterClass
public static void Close(){
driver.quit();
driver.close();
}
}

最佳答案

您有两个独立的 WebDriver 字段,每个类对应一个字段。您已经在 Login 中初始化了该引用,但没有将该引用传递回测试类。解决方案之一是简单地将 WebDriver 作为参数传递给 Login 构造函数:

public class Login {

private WebDriver driver;

public void Find_Fields (String path,String login, String password) {
driver.get(path);
//(...) rest of this method is unchanged
}

public Login(WebDriver driver) {
this.driver = driver;
}
}

public class Testing {

private static WebDriver driver;
@BeforeClass
public static void a_setup(){
driver = new FirefoxDriver(); //initialize the driver
Login fields = new Login(driver); //pass the instance of WebDriver to login class
fields.Find_Fields(LOGIN_PATH, LOGIN, PASSWORD);
}

//(...) rest of the class remains unchanged
}

关于java - 网络驱动程序。使用参数化类后无法运行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18282314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com