selenium-webdriver - 执行测试时出现 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();
}
}
解决方案
推荐阅读
- python - 图表后面的网格
- python - 当前正在针对此资源进行有冲突的条件操作。(已创建存储桶)
- node.js - 尝试使用 Postgres 将我的应用程序部署到 heroku。让它在本地主机上工作,但不知道如何在我拥有的 heroku 服务器上启动它
- python - 需要删除特定数据帧单元格中的 NaN
- ios - 引用内部 XCFramework 时,框架无法在模拟器上构建
- google-sheets - 将列中的所有单词大写
- php - 如何在php中为多个页面设置搜索过滤器
- php - Prestashop 模块 - 控制器字段列表 - 过滤器/搜索框不起作用/不显示过滤结果
- wso2-am - WSO2:重写响应
- laravel - Laravel、PHPUnit、docker-compose 和 env 变量优先级