Selenium中的线程“ main”中获取异常java.lang.NullPointerException

时间:2019-12-10 06:39:26

标签: java maven selenium selenium-webdriver

获取"main" java.lang.NullPointerException

我是Selenium的新手,并且已经按照Youtube / Udemy教程开始了POM结构。 我收到此错误。

我的POM类代码:

package Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Guru99Homepage {
    WebDriver driver;

    public Guru99Homepage(WebDriver driver) {
        this.driver = driver;
    }

    public void browserlaunch() {
        System.setProperty("webdriver.chrome.driver", "/Users/saurabh.gupta/eclipse-workspace/Project/Driver/chromedriver");
        driver = new ChromeDriver();
    }

    public void openguruwebsite() {
        driver.get("http://demo.guru99.com/V4");
    }

}

第二个POM类别:

package Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class Guru99LoginPage {
    WebDriver driver;

    public Guru99LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    public void username(String User_name) {
        driver.findElement(By.xpath("//input[@name='uid']")).sendKeys(User_name);
    }

    public void Password(String Pass) {
        driver.findElement(By.xpath("//input[@name='password']")).sendKeys(Pass);
    }

    public void clickat_loginbutton() {
        driver.findElement(By.xpath(" //input[@name='btnLogin']")).click();
    }
}

测试用例:

package TestCases;
import org.openqa.selenium.WebDriver;
import Pages.Guru99Homepage;
import Pages.Guru99LoginPage;

public class Guru99Login{

    public static void main(String[] args) {
        WebDriver driver = null;
        Guru99Homepage homepage = new Guru99Homepage(driver);
        Guru99LoginPage lgnpage = new Guru99LoginPage(driver);

        //Browserlaunch
        homepage.browserlaunch();

        //WebsiteOpen
        homepage.openguruwebsite();
        //driver.manage().window().maximize();
        lgnpage.username("mngr47659");
        lgnpage.Password("123456@");
        lgnpage.clickat_loginbutton();
    }
}

“我能够打开浏览器,但之后我会收到以下错误消息”

INFO: Detected dialect: W3C
Exception in thread "main" java.lang.NullPointerException
    at TestCases.Guru99Login.main(Guru99Login.java:31)

请帮助我,以便我在您的支持下开始pom结构。

谢谢

1 个答案:

答案 0 :(得分:0)

在主函数中,您已初始化 WebDriver driver = null; 。相反,请将其设置为WebDriver驱动程序;