运行 selenium 测试时出现空指针异常。我正在使用页面对象模型
Null pointer exception while running selenium test. I am using Page object model
下面是我的测试库class:
public class TestBase {
public static WebDriver driver; //Initialise web-driver
public static Properties prop;
public TestBase() {
try {
prop= new Properties();
FileInputStream ip= new FileInputStream("/Users/admin/Desktop/BASE_AutomationTests/src/main/java/com/base/Config/config.properties");
prop.load(ip);
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
//** Write the code for reading the property of browser
public static void initialization() {
String browserName=prop.getProperty("browser");
if (browserName.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "/C:/Users/admin/Desktop/BASE-Automation/chromedriver_win32/chromedriver.exe/");
driver = new ChromeDriver();
}
driver.manage().window().maximize();
driver.get(prop.getProperty("url"));
}
}
我的 VerifyLaydownlogintest 如下:
public class VerifyLaydownLoginTest extends TestBase {
//
LoginPage loginpage;
HomePage homepage;
//**Creating a constructor with Super so that after construction is called the Super keay word initialise properties of TestBase class first
//** Super keyword = to call super class constructor i.e TestBase class
public VerifyLaydownLoginTest()
{
super();
}
@BeforeMethod
public void setUp()
{
initialization();
loginpage= new LoginPage();
}
@Test(priority= 1)
public void loginTest() {
loginpage.loginToBase(prop.getProperty("username"),prop.getProperty("password"));
}
@Test(priority=2)
public void loginLaydownTest()
{
homepage=loginpage.changeLogin(); //**changeLogin method is returning you the object of Homepage class, so we can store it in a object
}
@AfterMethod
public void tearDown()
{
driver.close();
}
}
这是我的登录页面class:
/**
*
*/
package com.base.Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.base.Baseclass.TestBase;
/**
* @author ketaki
* This class will store all the locators and methods of Login page
*
*/
public class LoginPage extends TestBase{
@FindBy(id="inputUsername") // Same as writing driver.findelement
WebElement username;
@FindBy(id="inputPassword")
WebElement password;
@FindBy(name="_submit")
WebElement submit_button;
//*** Initialise all these elements with the help pf page factory
public LoginPage () {
PageFactory.initElements(driver, this);
}
//** Write all the login functions methods
public void loginToBase(String userid, String pass)
{
username.sendKeys(userid);
password.sendKeys(pass);
submit_button.click();
}
public HomePage changeLogin()
{
driver.navigate().to("https://uat-heineken.base.website/?_switch_user=alaric.james");
driver.get("https://uat-heineken.base.website/#!/budget/?tab=edit");
return new HomePage();
}
}
这是 NPE:
的错误
RemoteTestNG] detected TestNG version 6.14.3
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
FAILED CONFIGURATION: @BeforeMethod setUp
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.manage()" because "com.base.Baseclass.TestBase.driver" is null
at com.base.Baseclass.TestBase.initialization(TestBase.java:42)
at VerifyLaydownLoginTest.setUp(VerifyLaydownLoginTest.java:40)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
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:458)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:523)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
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)
我试图解决这个问题,但找不到根本原因。我哪里错了?
我的config.properties
url = https://uat-heineken.base.website/#!/
username = ketaki.naik
password = 123456
browser = chrome
您正在使用 TestNG
作为测试自动化框架。
在 TestNG
中,将根据您在 test
class.
中定义的注释执行
@BeforeMethod
的优先级高于 @Test
或 @AfterMethod
如果你看看你的 @BeforeMethod
@BeforeMethod
public void setUp()
{
initialization();
loginpage= new LoginPage();
}
您调用的第一个方法是 initialization();
,该方法的第一行是 String browserName=prop.getProperty("browser");
您正在从 public static Properties prop;
调用此处的 prop
,它是一个静态变量,您没有在那里初始化它,因此您得到 NPE
。由于 prop
只是您尝试访问的 variable
。
您应该在 @BeforeMethod
中创建一个 TestBase
class 的对象
像这样:
@BeforeMethod
public void setUp()
{
TestBase base = new TestBase();
base.initialization();
loginpage= new LoginPage();
}
现在,一旦您像上面那样创建了一个 TestBase 实例,就会调用下面的构造函数
public TestBase() {
try {
prop= new Properties();
FileInputStream ip= new FileInputStream("/Users/admin/Desktop/BASE_AutomationTests/src/main/java/com/base/Config/config.properties");
prop.load(ip);
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
并且由于 prop
是一个 static variable
,分配给该变量的值将保留。
下面是我的测试库class:
public class TestBase {
public static WebDriver driver; //Initialise web-driver
public static Properties prop;
public TestBase() {
try {
prop= new Properties();
FileInputStream ip= new FileInputStream("/Users/admin/Desktop/BASE_AutomationTests/src/main/java/com/base/Config/config.properties");
prop.load(ip);
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
//** Write the code for reading the property of browser
public static void initialization() {
String browserName=prop.getProperty("browser");
if (browserName.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "/C:/Users/admin/Desktop/BASE-Automation/chromedriver_win32/chromedriver.exe/");
driver = new ChromeDriver();
}
driver.manage().window().maximize();
driver.get(prop.getProperty("url"));
}
}
我的 VerifyLaydownlogintest 如下:
public class VerifyLaydownLoginTest extends TestBase {
//
LoginPage loginpage;
HomePage homepage;
//**Creating a constructor with Super so that after construction is called the Super keay word initialise properties of TestBase class first
//** Super keyword = to call super class constructor i.e TestBase class
public VerifyLaydownLoginTest()
{
super();
}
@BeforeMethod
public void setUp()
{
initialization();
loginpage= new LoginPage();
}
@Test(priority= 1)
public void loginTest() {
loginpage.loginToBase(prop.getProperty("username"),prop.getProperty("password"));
}
@Test(priority=2)
public void loginLaydownTest()
{
homepage=loginpage.changeLogin(); //**changeLogin method is returning you the object of Homepage class, so we can store it in a object
}
@AfterMethod
public void tearDown()
{
driver.close();
}
}
这是我的登录页面class:
/**
*
*/
package com.base.Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.base.Baseclass.TestBase;
/**
* @author ketaki
* This class will store all the locators and methods of Login page
*
*/
public class LoginPage extends TestBase{
@FindBy(id="inputUsername") // Same as writing driver.findelement
WebElement username;
@FindBy(id="inputPassword")
WebElement password;
@FindBy(name="_submit")
WebElement submit_button;
//*** Initialise all these elements with the help pf page factory
public LoginPage () {
PageFactory.initElements(driver, this);
}
//** Write all the login functions methods
public void loginToBase(String userid, String pass)
{
username.sendKeys(userid);
password.sendKeys(pass);
submit_button.click();
}
public HomePage changeLogin()
{
driver.navigate().to("https://uat-heineken.base.website/?_switch_user=alaric.james");
driver.get("https://uat-heineken.base.website/#!/budget/?tab=edit");
return new HomePage();
}
}
这是 NPE:
RemoteTestNG] detected TestNG version 6.14.3
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
FAILED CONFIGURATION: @BeforeMethod setUp
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.manage()" because "com.base.Baseclass.TestBase.driver" is null
at com.base.Baseclass.TestBase.initialization(TestBase.java:42)
at VerifyLaydownLoginTest.setUp(VerifyLaydownLoginTest.java:40)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
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:458)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:523)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
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)
我试图解决这个问题,但找不到根本原因。我哪里错了?
我的config.properties
url = https://uat-heineken.base.website/#!/
username = ketaki.naik
password = 123456
browser = chrome
您正在使用 TestNG
作为测试自动化框架。
在 TestNG
中,将根据您在 test
class.
@BeforeMethod
的优先级高于 @Test
或 @AfterMethod
如果你看看你的 @BeforeMethod
@BeforeMethod
public void setUp()
{
initialization();
loginpage= new LoginPage();
}
您调用的第一个方法是 initialization();
,该方法的第一行是 String browserName=prop.getProperty("browser");
您正在从 public static Properties prop;
调用此处的 prop
,它是一个静态变量,您没有在那里初始化它,因此您得到 NPE
。由于 prop
只是您尝试访问的 variable
。
您应该在 @BeforeMethod
TestBase
class 的对象
像这样:
@BeforeMethod
public void setUp()
{
TestBase base = new TestBase();
base.initialization();
loginpage= new LoginPage();
}
现在,一旦您像上面那样创建了一个 TestBase 实例,就会调用下面的构造函数
public TestBase() {
try {
prop= new Properties();
FileInputStream ip= new FileInputStream("/Users/admin/Desktop/BASE_AutomationTests/src/main/java/com/base/Config/config.properties");
prop.load(ip);
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
并且由于 prop
是一个 static variable
,分配给该变量的值将保留。