【问题标题】:selenium driver returns null when calling from base class从基类调用时,selenium 驱动程序返回 null
【发布时间】:2020-10-19 00:45:15
【问题描述】:

我有一个使用@Before 和@After 黄瓜注释设置webdriver 的基类。我将我的基类扩展到其他类以共享 webdriver。 我的其他类在我的设置类之前执行并返回空异常。 下面的代码sn-p 基类/父类代码

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)

我不确定问题出在哪里。 感谢您的帮助。

【问题讨论】:

  • 我认为@Before 从未被调用过。您是否看到浏览器打开并导航到 url ?如果你使用黄瓜钩,那么你必须将它们添加到你的跑步者文件的胶水路径中。
  • 是的,它打开和关闭浏览器。这就是为什么即使驱动程序调用也返回空异常的问题。我调试并在钩子之前执行了 LoginPage 类,不知道为什么

标签: java selenium selenium-webdriver nullpointerexception cucumber


【解决方案1】:

不要从测试类扩展您的页面类。 JUnit 从测试类实例化一个对象,然后使用来自@Before 的代码注入驱动程序字段。

然后,当您初始化页面对象时,它会创建 不同 对象,其中您的 webdriver 仍然为空。

【讨论】:

  • 谢谢。如果不扩展类,如何使用共享驱动程序?你能告诉我吗。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
相关资源
最近更新 更多