繁体   English   中英

运行Selenium时获取空指针异常-使用Pagefactory的TestNG脚本

[英]Getting Null Pointer Exception while running Selenium - TestNG Scripts using pagefactory

我创建了3个不同的类,例如SelectBrowserTest,GmailLoginPOTest和SampleTest(这是我的测试用例)。 我试图从SelectBrowserTest和GmailLoginPOTest调用方法到SampleTest。 但是,我一直在获取Null指针异常。 有谁能帮助解决这个问题?

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;
}

SelectBrowserTest:

    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());

        }
    } 

}

这三个是我的班级文件。 运行脚本时,出现以下错误消息。

输出:

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)

请尝试以下代码。在您的SampleTest类中,驱动程序为null。这就是为什么出现null指针异常的原因。

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");

    }
}

SelectBrowserTest .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());

        }
    }

}

暂无
暂无

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

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