简体   繁体   English

尝试在另一个 class 中使用一种方法时 Null 指针异常

[英]Null pointer exception while trying to use one method in another class

I'm currently working on automating facebook login and logout, but I have problems.我目前正在自动化 facebook 登录和注销,但我遇到了问题。 Basically, I have created a FacebookLogOutTest class, that needs to run FacebookLogIn first, and then continue.基本上,我创建了一个FacebookLogOutTest class,需要先运行FacebookLogIn ,然后再继续。 These are the classes:这些是类:

FacebookLogIn

public class FacebookLogInTest {
    WebDriver driver;

    @Test
    public void facebookLogIn() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.get("https://www.facebook.com/");
        WebElement username = driver.findElement(By.id("email"));
        WebElement password = driver.findElement(By.id("pass"));
        WebElement login = driver.findElement(By.xpath("//*[@name='login']"));
        username.sendKeys("xxxxxx");
        password.sendKeys("xxxxxx");
        login.click();


    }
}
FabecookLogOut
public class FacebookLogOutTest {
    WebDriver driver;

    @Test
    public void facebookLogOut() {
        FacebookLogInTest fbLogin = new FacebookLogInTest();
        fbLogin.facebookLogIn();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        WebElement profileButton = driver.findElement(By.xpath(("//*[@aria-label='Your page']")));
        profileButton.click();
        WebElement logoutButton = driver.findElement(By.xpath("//span[text()='Log out']"));
        logoutButton.click();
    }
}

But when I'm trying to run the log out class, it throws an exception and I don't know why.但是,当我尝试注销 class 时,它会引发异常,我不知道为什么。 The log in class works properly. class 中的日志工作正常。 What should I do?我应该怎么办?

Here's the stacktrace:这是堆栈跟踪:

java.lang.NullPointerException
    at FacebookLogOutTest.facebookLogOut(FacebookLogOutTest.java:18)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

The problem is that you never assign a driver instance to the driver variable in facebookLogIn() .问题是您永远不会将驱动程序实例分配给facebookLogIn()中的driver变量。 Each test should stand alone, eg you shouldn't be calling facebookLogIn() from facebookLogOut() .每个测试都应该独立,例如你不应该从facebookLogOut()调用facebookLogIn() () 。 Each test should have it's own driver instance and should be run on it's own browser instance to keep your tests super clean.每个测试都应该有它自己的驱动程序实例,并且应该在它自己的浏览器实例上运行,以保持你的测试超级干净。

Each test should look like this:每个测试应如下所示:

  1. Launch browser启动浏览器
  2. Do stuff做东西
  3. Close browser关闭浏览器

You have the right idea in trying to reuse code but investigate page objects instead.您尝试重用代码但调查页面对象的想法是正确的。 Create a page object for the login page (to log in) and one for the header (to click the profile icon and log out).为登录页面(登录)和 header(单击配置文件图标并注销)创建一个页面 object。

Now your test will look like:现在您的测试将如下所示:

WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.get("https://www.facebook.com/");

new HomePage(driver).logIn(username, password);

Header header = new Header(driver);
header.clickProfileIcon();
header.clickLogOut();

Very simple, very easy to read and lots of code reuse.非常简单,非常容易阅读和大量的代码重用。 Now when you create your second test, you can start with the same few lines of code setting up the driver and logging in and then get on to whatever the new test is supposed to cover.现在,当您创建第二个测试时,您可以从设置驱动程序和登录的相同的几行代码开始,然后继续进行新测试应该涵盖的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM