0

我创建了 3 个不同的类,例如 SelectBrowserTest、GmailLoginPOTest 和 SampleTest(这是我的测试用例)。我试图调用从 SelectBrowserTest 和 GmailLoginPOTest 到 SampleTest 的方法。但是,我不断收到空指针异常。任何人都可以帮助解决这个问题吗?

代码

样品测试:

package TestCases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.*;  
import TCPSampleProject.SelectBrowserTest;
import pageObjects.GmailLoginPOTest;

public class SampleTest{

    static WebDriver driver;
    private static GmailLoginPOTest g1;

    @BeforeMethod
    public void OpenBrowser() {
        SelectBrowserTest open = new SelectBrowserTest();
        open.ChromeBrowser();

    }

    @AfterMethod
    public void CloseBrowser() {
        SelectBrowserTest close = new SelectBrowserTest();
        close.Close();
    }

    @Test(priority = 0)
    public static void GmailLogin() {

            g1 = PageFactory.initElements(driver, GmailLoginPOTest.class);

            g1.GmailLink.click();
            System.out.println("Gmail Link clicked");
            g1.SignInLink.click();
            g1.EmailIDTextBox.sendKeys("rvigneshprabu");
            g1.EmailNextButton.click();
            g1.PasswordTextBox.sendKeys("Password");
            g1.PswdNextButton.getText();
            System.out.println("Gmail Login Completed");

    }
  }

GmailLoginPOTest:

package pageObjects;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;


public class GmailLoginPOTest{


    @FindBy(linkText = "Gmail")
    public WebElement GmailLink;

    @FindBy(xpath = ".//a[contains(text(),'Sign In')]")
    public WebElement SignInLink;

    @FindBy(id = "identifierId")
    public WebElement EmailIDTextBox;

    @FindBy(id = "identifierNext")
    public WebElement EmailNextButton;

    @FindBy(name = "password")
    public WebElement PasswordTextBox;

    @FindBy(id = "passwordNext")
    public WebElement PswdNextButton;
}

选择浏览器测试:

    package TCPSampleProject;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;



public class SelectBrowserTest {

    WebDriver driver;

    public void ChromeBrowser() {
        System.setProperty("webdriver.chrome.driver",
                "//Users//vigneshprabur//Documents//Automation//Drivers//chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.com");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    }

    public void Close() {
        driver.quit();
        System.out.println("Browser Closed");

    }

    public void ScreenShot() {

        try {
            File ScrShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(ScrShot, new File("/Users/vigneshprabur/Documents/Automation/Screenshots/image_"+System.currentTimeMillis()+".png"));
            System.out.println("Screenshot Taken");
        }
        catch (IOException e) {
            System.out.println("Exception While Taking Screenshot" + e.getMessage());

        }
    } 

}

这 3 个是我的类文件。运行脚本时收到以下错误消息。

输出:

FAILED CONFIGURATION: @AfterMethod CloseBrowser
java.lang.NullPointerException
    at TCPSampleProject.SelectBrowserTest.Close(SelectBrowserTest.java:29)
    at TestCases.SampleTest.CloseBrowser(SampleTest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:455)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: GmailLogin
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.$Proxy9.click(Unknown Source)
    at TestCases.SampleTest.GmailLogin(SampleTest.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
4

1 回答 1

0

请尝试下面的代码。在您的 SampleTest 类中,驱动程序为空。这就是您遇到空指针异常的原因。

SampleTest.java

package TestCases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.*;
import TCPSampleProject.SelectBrowserTest;
import pageObjects.GmailLoginPOTest;

public class SampleTest{

    static WebDriver driver;
    private static GmailLoginPOTest g1;
    SelectBrowserTest open;

    @BeforeMethod
    public void OpenBrowser() {
        open = new SelectBrowserTest();
        open.ChromeBrowser();
        driver = open.getDriver();
    }

    @AfterMethod
    public void CloseBrowser() {
        open.Close();
    }

    @Test(priority = 0)
    public static void GmailLogin() {
        g1 = PageFactory.initElements(driver, GmailLoginPOTest.class);
        g1.GmailLink.click();
        System.out.println("Gmail Link clicked");
        g1.SignInLink.click();
        g1.EmailIDTextBox.sendKeys("rvigneshprabu");
        g1.EmailNextButton.click();
        g1.PasswordTextBox.sendKeys("Password");
        g1.PswdNextButton.getText();
        System.out.println("Gmail Login Completed");

    }
}

选择浏览器测试.java

package TCPSampleProject;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;



public class SelectBrowserTest {

    WebDriver driver;

    public WebDriver getDriver() {
        return driver;
    }

    public void ChromeBrowser() {
        System.setProperty("webdriver.chrome.driver",
                "//Users//vigneshprabur//Documents//Automation//Drivers//chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.com");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    }

    public void Close() {
        driver.quit();
        System.out.println("Browser Closed");

    }

    public void ScreenShot() {

        try {
            File ScrShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(ScrShot, new File("/Users/vigneshprabur/Documents/Automation/Screenshots/image_"+System.currentTimeMillis()+".png"));
            System.out.println("Screenshot Taken");
        }
        catch (IOException e) {
            System.out.println("Exception While Taking Screenshot" + e.getMessage());

        }
    }

}
于 2018-09-11T09:21:17.103 回答