Java 当我尝试使用另一个类中的驱动程序实例来标识当前类中的元素时,会得到Null.pointer.exception

Java 当我尝试使用另一个类中的驱动程序实例来标识当前类中的元素时,会得到Null.pointer.exception,java,selenium-webdriver,testng,selenium-chromedriver,Java,Selenium Webdriver,Testng,Selenium Chromedriver,在下面的LogOutfunc类中, 我已经为LoginPage类创建了对象,当我运行LogOutfunc类程序时,会传递Launch()方法。但是, LogOut()方法因java.lang.NullPointerException失败 在login()之后,url将导航到一个页面,该页面包含LogOut()方法中存在的对象,但它没有标识该对象。请向我建议如何在当前类中使用驱动程序实例的解决方案 1。LogOutfunc类 package TestNG; import org.openqa.s

在下面的LogOutfunc类中, 我已经为LoginPage类创建了对象,当我运行LogOutfunc类程序时,会传递Launch()方法。但是, LogOut()方法因java.lang.NullPointerException失败

在login()之后,url将导航到一个页面,该页面包含LogOut()方法中存在的对象,但它没有标识该对象。请向我建议如何在当前类中使用驱动程序实例的解决方案

1。LogOutfunc类

package TestNG;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class LogOutfunc {

    public WebDriver driver;

    @Test()
    public void Launch() throws NoAlertPresentException, InterruptedException {
        LoginPage tc1 = new LoginPage();
        tc1.setUp();
        tc1.Login("test_Sharmila","Welcome123","Sharmila");
    }

    @Test()
        public void LogOut() {
        System.out.println("Logout Functionality");
        driver.findElement(By.xpath("//img[@id=\"usrmenu\"]]")).click();
        driver.findElement(By.xpath("//span[@class='logoutx']//a[text()='Logout']")).click();
        System.out.println("Logged out from the page");
package TestNG;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class LoginPage {

    String driverPath = "C:\\Selenium\\Driver\\chromedriver.exe";
    public WebDriver driver;
    String baseUrl = "https://www.myguruavatar.com/demo";
    String expectedtUrl = "https://www.myguruavatar.com/demo/index.php?option=com_avatarprofile&view=avatarprofileform";

    @BeforeTest
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", driverPath);
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(baseUrl);
    }

    @Test
    public void Login(String userName, String password, String Name)
            throws NoAlertPresentException, InterruptedException {
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//input[@id=\"modlgn-username\"]")).sendKeys(userName);
        driver.findElement(By.xpath("//input[@id=\"modlgn-passwd\"]")).sendKeys(password);
        driver.findElement(By.xpath("//*[@id=\"login-form\"]/fieldset/div/p[2]/input")).click();
        driver.findElement(By.xpath("//input[@id=\"hmelogin\"]")).click();

        if (driver.getCurrentUrl().contains(expectedtUrl)) {
            System.out.println("Login Success");



        } else {
            System.out.println("Login Failure");

2。登录页面类

package TestNG;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class LogOutfunc {

    public WebDriver driver;

    @Test()
    public void Launch() throws NoAlertPresentException, InterruptedException {
        LoginPage tc1 = new LoginPage();
        tc1.setUp();
        tc1.Login("test_Sharmila","Welcome123","Sharmila");
    }

    @Test()
        public void LogOut() {
        System.out.println("Logout Functionality");
        driver.findElement(By.xpath("//img[@id=\"usrmenu\"]]")).click();
        driver.findElement(By.xpath("//span[@class='logoutx']//a[text()='Logout']")).click();
        System.out.println("Logged out from the page");
package TestNG;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class LoginPage {

    String driverPath = "C:\\Selenium\\Driver\\chromedriver.exe";
    public WebDriver driver;
    String baseUrl = "https://www.myguruavatar.com/demo";
    String expectedtUrl = "https://www.myguruavatar.com/demo/index.php?option=com_avatarprofile&view=avatarprofileform";

    @BeforeTest
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", driverPath);
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(baseUrl);
    }

    @Test
    public void Login(String userName, String password, String Name)
            throws NoAlertPresentException, InterruptedException {
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//input[@id=\"modlgn-username\"]")).sendKeys(userName);
        driver.findElement(By.xpath("//input[@id=\"modlgn-passwd\"]")).sendKeys(password);
        driver.findElement(By.xpath("//*[@id=\"login-form\"]/fieldset/div/p[2]/input")).click();
        driver.findElement(By.xpath("//input[@id=\"hmelogin\"]")).click();

        if (driver.getCurrentUrl().contains(expectedtUrl)) {
            System.out.println("Login Success");



        } else {
            System.out.println("Login Failure");

请告诉我如何在当前类中使用驱动程序实例。

因为驱动程序为空。看这里

public class LogOutfunc {

public WebDriver driver;
您正在检查该类中的驱动程序是否为空。Launch方法将工作,因为该类中存在有效的驱动程序

因此,您需要改变实施方式


简单的方法是LogOutfunc扩展LoginPage并删除此行公共WebDriver驱动程序;在LogoutFun课程中。

你的问题不清楚。请把标题弄清楚。