运行硒测试时的无指针异常。我正在使用页面对象模型

发布于 2025-01-27 08:07:15 字数 5636 浏览 1 评论 0 原文

以下是我的testbase类:

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();
    }
    
            
    }

这是我的登录页面类:

/**
 * 
 */
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 

Below is my Testbase 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"));
        
        
    }
}

My VerifyLaydownlogintest as below:

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();
    }
    
            
    }

And this is my Login page 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(); 
                 
         
         }
}

This is error of 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)

I tried to resolve this but unable to find the root cause. Where am I going wrong?

My config.properties

url = https://uat-heineken.base.website/#!/

username = ketaki.naik
password = 123456

browser = chrome 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拒绝两难 2025-02-03 08:07:15

您正在使用 testng 作为测试自动化框架。

testng 中,执行将根据您在 test 类中定义的注释进行。

@beforemethod 的优先级高于 @test @aftermethod

,并且如果您查看 @beforemethod

@BeforeMethod
public void setUp()
{
        initialization();
        loginpage= new LoginPage();
}

您正在调用的第一个方法是 initialization(); ,该方法中的第一行是 string browsername = prop.getProperty(“ browser”);

您正在调用 prop 在此处摘自 public static属性prop; ,它是一个静态变量,您在那里没有初始化,因此您可以获得 npe 。由于 Prop 只是您要访问的变量。

您应该在 testbase 类中创建一个对象 @beforemethod

类似的东西:

@BeforeMethod
public void setUp()
{
        TestBase base = new TestBase();
        base.initialization();
        loginpage= new LoginPage();
        
}

现在,一旦创建了类似上述测试库的实例,以下构造函数将被调用

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 静态变量,该变量的分配值将持续存在。

You are using TestNG as your test automation framework.

In TestNG, execution will take place according to the annotation that you define in your test class.

@BeforeMethod has higher priority than @Test or @AfterMethod

and if you take a look at your @BeforeMethod

@BeforeMethod
public void setUp()
{
        initialization();
        loginpage= new LoginPage();
}

the first method that you are calling is initialization(); and the first line in that method is String browserName=prop.getProperty("browser");

You are calling prop here from public static Properties prop; and it is a static variable and you've not initialized it there, hence you are getting the NPE. Since prop is just a variable that you are trying to access.

You should rather create an object of TestBase class in your @BeforeMethod

Something like this:

@BeforeMethod
public void setUp()
{
        TestBase base = new TestBase();
        base.initialization();
        loginpage= new LoginPage();
        
}

Now once you create an instance of TestBase like above, the below constructor will get invoked

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();
    }
}

And since prop is a static variable, the assigned value to this variable will persist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文