尝试运行 TestNg 测试时出现空指针异常

问题描述 投票:0回答:0

观察:
尝试运行 testng 脚本时,出现以下错误消息:a null pointer exception at

    String value = prop.get(key).toString();

org.testng.TestNGException:  Cannot instantiate class melos.tests.integration.affiliates.hierarchy     at org.testng.internal.objects.InstanceCreator.newInstance(InstanceCreator.java:41)     at org.testng.ITestObjectFactory.newInstance(ITestObjectFactory.java:18)    at org.testng.internal.objects.SimpleObjectDispenser.instantiateUsingDefaultConstructor(SimpleObjectDispenser.java:178)     at org.testng.internal.objects.SimpleObjectDispenser.createInstance(SimpleObjectDispenser.java:87)  at org.testng.internal.objects.SimpleObjectDispenser.dispense(SimpleObjectDispenser.java:40)    at org.testng.internal.objects.GuiceBasedObjectDispenser.dispense(GuiceBasedObjectDispenser.java:28)    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:106)     at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:136)   at org.testng.TestClass.getInstances(TestClass.java:129)    at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:109)     at org.testng.TestClass.init(TestClass.java:101)    at org.testng.TestClass.<init>(TestClass.java:66)   at org.testng.TestRunner.initMethods(TestRunner.java:464)   at org.testng.TestRunner.init(TestRunner.java:336)  at org.testng.TestRunner.init(TestRunner.java:289)  at org.testng.TestRunner.<init>(TestRunner.java:179)    at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:643)  at org.testng.SuiteRunner.init(SuiteRunner.java:225)    at org.testng.SuiteRunner.<init>(SuiteRunner.java:115)  at org.testng.TestNG.createSuiteRunner(TestNG.java:1349)    at org.testng.TestNG.createSuiteRunners(TestNG.java:1325)   at org.testng.TestNG.runSuitesLocally(TestNG.java:1167)     at org.testng.TestNG.runSuites(TestNG.java:1099)    at org.testng.TestNG.run(TestNG.java:1067)  at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109) Caused by: java.lang.reflect.InvocationTargetException     at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)     at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)  at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)  at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)    at org.testng.internal.objects.InstanceCreator.newInstance(InstanceCreator.java:38)     ... 25 more Caused by: java.lang.NullPointerException   at melos.methods.propReader.melosSetup(propReader.java:16)  at melos.pageobjects.affiliates.hierarchyObj.<init>(hierarchyObj.java:19)   at melos.tests.integration.affiliates.hierarchy.<init>(hierarchy.java:14)   ... 30 more

我不确定为什么会抛出此错误消息。使用将登录的测试时工作正常。

这是我的属性阅读器文件:

package melos.methods;

import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream; import java.util.Properties;

public class propReader {

static Properties prop = new Properties();

public static String melosSetup(String key) throws Exception {
    String propPath = System.getProperty("user.dir") + "\\src\\test\\java\\melos\\configs\\setup.properties";
    FileInputStream fis = new FileInputStream(propPath);
    prop.load(fis);

    String value = prop.get(key).toString();

    if (StringUtils.isEmpty(value)) {
        throw new Exception("Value is not specified for key: " + key + "in properties file");
    }
    return value;
}

这是我的页面对象文件:

package melos.pageobjects.affiliates;

import melos.methods.propReader;
import melos.pageobjects.testbase.baseClass;
import melos.pageobjects.testbase.driverFactory;
import org.openqa.selenium.By;
import org.testng.annotations.BeforeMethod;

public class hierarchyObj extends baseClass {

    By affMenu = By.xpath(propReader.melosSetup("sideAffiliates"));
    By affSearchMenu = By.xpath(propReader.melosSetup("searchField"));
    By affSearchButton = By.xpath(propReader.melosSetup("searchButton"));
    By affTaskListHierarchy = By.xpath(propReader.melosSetup("tasklistHierarchy"));
    By affParametersHierarchy = By.xpath(propReader.melosSetup("parametersHierarchy"));
    By results = By.xpath(propReader.melosSetup("localResult"));
    By council = By.xpath(propReader.melosSetup("intgo"));
    By district = By.xpath(propReader.melosSetup("westdistrict"));
    By area = By.xpath(propReader.melosSetup("southwest "));
    By local = By.xpath(propReader.melosSetup("local"));

    public hierarchyObj() throws Exception {
    }

    public void affiliate_tasklist_hierarchy() throws Exception {
        clicks(driverFactory.getInstance().getDriver().findElement(affMenu), "Affiliate Side Menu");
        Thread.sleep(1000);
        sndKeys(driverFactory.getInstance().getDriver().findElement(affSearchMenu), "Menu Field", "UBC01977");
        Thread.sleep(1000);
        clicks(driverFactory.getInstance().getDriver().findElement(affSearchButton), "Search Button Click");
        Thread.sleep(5000);
        String originalWindow = driverFactory.getInstance().getDriver().getWindowHandle();
        for (String windowHandle : driverFactory.getInstance().getDriver().getWindowHandles()) {
            if (!originalWindow.contentEquals(windowHandle)) {
                driverFactory.getInstance().getDriver().switchTo().window(windowHandle);
                break;
            }
        }
        Thread.sleep(5000);
        clicks(driverFactory.getInstance().getDriver().findElement(affTaskListHierarchy), "Hierarchy in Task List");
        Thread.sleep(5000);
        clicks(driverFactory.getInstance().getDriver().findElement(results), "Local 1977 Result");
        Thread.sleep(5000);
    }
    
}

这是我的测试文件:

package melos.tests.integration.affiliates;

import melos.pageobjects.affiliates.hierarchyObj;
import melos.pageobjects.landing.userLoginObj;
import melos.pageobjects.testbase.baseClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class hierarchy extends baseClass {


    userLoginObj login = new userLoginObj();
    hierarchyObj hierarchy = new hierarchyObj();

    public hierarchy() throws Exception {
    }

    @Test
    public void hierarchyList() throws Exception {
        login.userLogin();
        Thread.sleep(5000);
        hierarchy.affiliate_tasklist_hierarchy();
    }
}

这是我的 affiliates.xml 文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Affiliate Regression Tests" thread-count="10" parallel="methods" >
    <test name="Affiliate Page Tests">
        <classes>
            <class name="melos.tests.integration.affiliates.hierarchy">
            </class>
        </classes>
    </test>
</suite>

我想了解解决此问题的最佳方法是什么。这些测试不会发生这种情况:

页面对象文件:

package melos.pageobjects.landing;

import melos.methods.propReader;
import melos.pageobjects.testbase.baseClass;
import melos.pageobjects.testbase.driverFactory;
import org.apache.commons.codec.binary.Base64;
import org.openqa.selenium.By;

public class userLoginObj extends baseClass {

    By username = By.xpath(propReader.melosSetup("username"));
    By password = By.xpath(propReader.melosSetup("password"));
    By loginBtn = By.cssSelector(propReader.melosSetup("loginBtn"));
    public userLoginObj() throws Exception {

    }

    public void userLogin() throws Exception {

        sndKeys(driverFactory.getInstance().getDriver().findElement(username),
                "Username Field", decodeusername("YWRtaW5xYQ=="));

        sndKeys(driverFactory.getInstance().getDriver().findElement(password),
                "Password Field", decodepassword("cWF0ZXN0MjIhIQ=="));

        clicks(driverFactory.getInstance().getDriver().findElement(loginBtn),"LoginButton");

    }

    static String decodeusername(String username) {
        byte[] decodedstring = Base64.decodeBase64(username.getBytes());
        return(new String(decodedstring));
    }

    static String decodepassword(String password ) {
        byte[] decodedstring = Base64.decodeBase64(password.getBytes());
        return(new String(decodedstring));
    }

和我的测试文件:

package melos.tests.integration.landing;


import melos.pageobjects.landing.userLoginObj;
import melos.pageobjects.testbase.baseClass;
import org.testng.annotations.Test;

public class userLogin extends baseClass {

    userLoginObj login = new userLoginObj();

    public userLogin() throws Exception {
    }
    @Test
    public void loginMelos() throws Exception {
        login.userLogin();
        Thread.sleep(5000);

    }
}

和我的 xml 文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Landing Page Regression Tests" thread-count="10" parallel="methods" >
    <test name="Landing Page Tests">
        <classes>
            <class name="melos.tests.integration.landing.userLogin">
            </class>
        </classes>
    </test>
</suite>
java selenium-webdriver properties testng
© www.soinside.com 2019 - 2024. All rights reserved.