从基类调用时,selenium驱动程序返回null

um6iljoc  于 2023-02-04  发布在  其他
关注(0)|答案(2)|浏览(166)

我有一个基类,其中包含使用@Before和@After cumb注解的webdriver设置。我将基类扩展到其他类以共享webdriver。我的其他类在我的设置类之前执行,并返回空异常。以下代码片段基类/父类代码

package StepDef;

import Util.Constants;
import Util.GetConfigProp;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.IOException;
import java.util.Properties;

public class BaseUtil {
    public WebDriver driver;
    GetConfigProp prop = new GetConfigProp();

    @Before
    public void setUp() throws InterruptedException, IOException {
        System.setProperty(Constants.CHROME_DRIVER_PROPERTY, "src/test/resources/drivers/chromedriver.exe");
        driver = new ChromeDriver();
        Thread.sleep(2000);
        driver.manage().window().maximize();
        driver.navigate().to(prop.readProp());

    }
    @After
     public void quitApplication(){
         driver.quit();

    }
}

这是我的子类****

package Pages;

import StepDef.BaseUtil;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

import java.io.IOException;

public class LoginPage extends BaseUtil {
    @FindBy(how = How.ID,using = "user-name")
    public WebElement txtUserNameBox;

    @FindBy(how = How.ID,using = "password")
    public WebElement txtPassword;

    public LoginPage() throws IOException, InterruptedException {

        PageFactory.initElements(driver,this);

    }

    public void login(String userName, String password)
    {
        txtUserNameBox.sendKeys(userName);
        txtPassword.sendKeys(password);

    }
}

如果需要,这是我的步骤定义类

package StepDef;

import Pages.LoginPage;
import cucumber.api.java8.En;

import java.io.IOException;

public class MyStepdefs implements En {
    public MyStepdefs() throws IOException, InterruptedException {
        LoginPage loginObj=new LoginPage();
        Given("^the user is on home page$", () -> {
        });
        When("^user enters ([^\\\"]*) and ([^\\\"]*) correctly$", (String username,String password) -> {
            loginObj.login(username,password);
        });
        Then("^user login successfully$", () -> {
        });
    }
}

这是例外****

java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy7.sendKeys(Unknown Source)
    at Pages.LoginPage.login(LoginPage.java:26)
    at StepDef.MyStepdefs.lambda$new$1(MyStepdefs.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)

我不知道问题出在哪里。谢谢你的帮助。

pqwbnv8z

pqwbnv8z1#

不要从测试类扩展页面类。JUnit从测试类示例化一个对象,然后使用@Before中的代码注入驱动程序字段。
然后,当你初始化你的页面对象,它创建不同的对象,你的webdriver仍然是空的。

cnjp1d6j

cnjp1d6j2#

LoginPage loginObj=new LoginPage(driver);

相关问题