java 在BaseClass中初始化的WebDriver不会在PageClass中继承

oymdgrw7  于 2023-03-16  发布在  Java
关注(0)|答案(3)|浏览(131)

我正在用TestNG编写一个简单的Selenium代码。我已经在基类中初始化了驱动程序,页面类和测试类都在扩展基类,但是驱动程序没有在页面类中继承。但是,相同的驱动程序在测试类中继承。有人能解释一下为什么会发生这种情况吗?编辑:从页面类中删除了WebDriver驱动程序,但它给出了相同的错误:java.lang.NullPointerException:无法调用“org.openqa.selenium.WebDriver.get(字符串)”,因为“此.driver”为空

基类

public class BaseClass {
protected WebDriver driver;
protected String username;
protected String password;
protected String browserName;
protected String url;

@BeforeSuite
public void setUp() throws IOException, InterruptedException {
    username = "admin";
    password = "admin";
    browserName = "chrome";
    url = "https://practice-cybertekschool.herokuapp.com/";

    // get the driver setp and type and the browser
    if (browserName.equalsIgnoreCase("Chrome")) {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
    } else if (browserName.equalsIgnoreCase("FireFox")) {
        WebDriverManager.firefoxdriver().setup();
        driver = new FirefoxDriver();
    } else if (browserName.equalsIgnoreCase("Edge")) {
        WebDriverManager.edgedriver().setup();
        driver = new EdgeDriver();
    }

    driver.manage().deleteAllCookies();
    driver.manage().window().maximize();

    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
    driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
    driver.get(url);

}

@AfterSuite
public void tearDown() {
    driver.quit();
}

}

页面类

public class BasicAuthPages extends BaseClass {

    By congratulationMessage = By.xpath("//p[contains(text(),'Congratulations')]");
    

// for authentication
public boolean authentication() {

    driver.get("https://"+username+":"+password+"@practice-cybertekschool.herokuapp.com/basic_auth");
    return authenticationMessage();
}

// to get authentication message
private boolean authenticationMessage() {
    String message = driver.findElement(congratulationMessage).getText();
    //System.out.println(message);
    if (message.equals("Congratulations! You must have the proper credentials.")) {
        return true;
    }

    else {
        return false;
    }
}

}

测试类别

public class Tests extends BaseClass {
    BasicAuthPages basicAuthPages;
    

@Test
public void authTest() {
    basicAuthPages = new BasicAuthPages();
    Boolean success = basicAuthPages.authentication();
    // System.out.println(username+" :"+password);
    Assert.assertTrue(success);
}
}

在运行这个函数时,它会给出java.lang.NullPointerException异常:无法调用“org.openqa.selenium.WebDriver.get(String)”,因为“this.driver”是空异常。但是,驱动程序在Test类中初始化,如果我进行以下更改,它将运行。

基类

public class BaseClass {
protected WebDriver driver;
protected String username;
protected String password;
protected String browserName;
protected String url;

@BeforeSuite
public void setUp() throws IOException, InterruptedException {
    username = "admin";
    password = "admin";
    browserName = "chrome";
    url = "https://practice-cybertekschool.herokuapp.com/";

    // get the driver setp and type and the browser
    if (browserName.equalsIgnoreCase("Chrome")) {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
    } else if (browserName.equalsIgnoreCase("FireFox")) {
        WebDriverManager.firefoxdriver().setup();
        driver = new FirefoxDriver();
    } else if (browserName.equalsIgnoreCase("Edge")) {
        WebDriverManager.edgedriver().setup();
        driver = new EdgeDriver();
    }

    driver.manage().deleteAllCookies();
    driver.manage().window().maximize();

    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
    driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
    driver.get(url);

}

@AfterSuite
public void tearDown() {
    driver.quit();
}

}

页面类

public class BasicAuthPages extends BaseClass {
WebDriver driver;
By congratulationMessage = By.xpath("//p[contains(text(),'Congratulations')]");

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

// for authentication
public boolean authentication(String username, String password) {
    driver.get("https://"+username+":"+password+"@practice-cybertekschool.herokuapp.com/basic_auth");
    return authenticationMessage();
}

// to get authentication message
private boolean authenticationMessage() {
    String message = driver.findElement(congratulationMessage).getText();
    //System.out.println(message);
    if (message.equals("Congratulations! You must have the proper credentials.")) {
        return true;
    }

    else {
        return false;
    }
}

}

测试类别

public class Tests extends BaseClass {
BasicAuthPages basicAuthPages;
CheckBoxPages checkBoxPages;

@Test
public void authTest() {
    basicAuthPages = new BasicAuthPages(driver);
    Boolean success = basicAuthPages.authentication(username, password);
    // System.out.println(username+" :"+password);
    Assert.assertTrue(success);
}
}

请有人解释一下为什么驱动程序在第一种情况下没有被继承?驱动程序在第二种情况下被继承,在测试类中被继承,为什么不在页面类的第一种情况下被继承?提前感谢

zujrkrfu

zujrkrfu1#

您会得到这个 java.lang.NullPointerException,因为在您的第一个“BasicAuthPages”类中,您已经声明了Webdriver driver,但是这个驱动程序没有初始化。
然而,在第二个“BasicAuthPages”类中,“driver”是在“BasicAuthPages”类的构造函数下初始化的,因此它工作正常。
在第一种情况下,由于您正在扩展基类,因此无需在页面类'BasicAuthPages'中再次声明Webdriver driver。由于您只声明了驱动程序但未初始化,因此在尝试访问驱动程序时会收到NullPOinterException。

wljmcqd8

wljmcqd82#

如果你想并行运行测试(需要一个大的测试套件尽可能快地运行),那么驱动程序最好在测试类中初始化
如果Page Object类继承自Base类,则在初始化期间传入驱动程序

ymzxtsji

ymzxtsji3#

尝试在BaseAuthPages构造函数中使用super()关键字,如下所示:

public class BasicAuthPages extends BaseClass {

//WebDriver driver;
public BasicAuthPages() {
        super();
}   
By congratulationMessage = By.xpath("//p[contains(text(),'Congratulations')]");

super关键字用于引用父类对象。在您的示例中,引用BaseClass对象。

相关问题