首页 > 解决方案 > 执行测试时出现 java.lang.NullPointerException 错误

问题描述

在TestNG的帮助下初始化了一个浏览器@BeforeClass和注释Quit()中的浏览器,@AfterClass但出现以下错误,

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "this.driver" is null

@BeforeTest当我在注释中初始化浏览器时它工作正常

代码

package Demo;

import org.apache.hc.core5.util.Asserts;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;


public class OrangeDemo {

    WebDriver driver;
    
    @BeforeClass
    public void initializebrowser()
    
    {
        WebDriverManager.chromedriver().setup();
        WebDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
    }
    
    @Test
    public void launchapp()
    {
    
        driver.get("https://opensource-demo.orangehrmlive.com/index.php/auth/login");
    }
    
    @Test
    public void credentialdetails()
    {
        driver.findElement(By.id("txtUsername")).sendKeys("Admin");
        driver.findElement(By.id("txtPassword")).sendKeys("admin123");
        driver.findElement(By.id("btnLogin")).click();
        
    }
    @Test 
    public void verifylogin()
    {
        String Actualtext=driver.findElement(By.id("welcome")).getText();
        Assert.assertEquals(Actualtext, "Welcome Paul");
        System.out.println("Login is successful");
    }
    @Test
    public void navigatetomyinfo()
    {
        driver.findElement(By.id("menu_pim_viewMyDetails")).click();
        
    }
    @Test
    public void validateinfo()
    {
        String actualtext=driver.findElement(By.xpath("//div[@id='profile-pic']/h1")).getText();
        Assert.assertEquals(actualtext, "Paul Collings");
        System.out.println("Landed on info page");
        
    }
    @AfterClass
    public void teardown()
    {
        driver.quit();
    }
}

标签: selenium-webdrivertestng

解决方案


推荐阅读