0

0

无法调用“com.aventstack.extentreports.ExtentTest.info(String)”,因为“extentlisteners.ExtentListeners.test”为空

WBOY

WBOY

发布时间:2024-02-11 16:39:08

|

873人浏览过

|

来源于stackoverflow

转载

在php开发中,我们经常会遇到各种问题和错误提示。其中一个常见的问题是“无法调用‘com.aventstack.extentreports.extenttest.info(string)’,因为‘extentlisteners.extentlisteners.test’为空”。这个错误提示可能让人感到困惑,不知道如何解决。在本文中,php小编百草将为大家解析这个问题的原因,并提供解决方法,帮助大家顺利解决这个错误。

问题内容

我正在学习 selenium,主题是范围报告。现在,当我在框架中实现以前的代码时,我收到此错误。这段代码在重新启动 ide 后可以工作,但现在不再工作了。 这是我的代码。

基础测试文件:

package base;

import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.ioexception;
import java.sql.sqlexception;
import java.time.duration;
import java.util.properties;

import org.apache.log4j.propertyconfigurator;
import org.openqa.selenium.by;
import org.openqa.selenium.webdriver;
import org.openqa.selenium.chrome.chromedriver;
import org.openqa.selenium.firefox.firefoxdriver;
import org.openqa.selenium.support.ui.webdriverwait;
import org.testng.annotations.aftersuite;
import org.testng.annotations.beforesuite;
import org.testng.log4testng.logger;

import extentlisteners.extentlisteners;
import utilities.dbmanager;
import utilities.excelreader;
import utilities.monitoringmail;

public class basetest {
    
    public static webdriver driver;
    public static properties or = new properties();
    public static properties config = new properties();
    public static fileinputstream fis;
    public static excelreader excel = new excelreader("./src/test/resources/excel/testdata.xlsx");
    public static logger log = logger.getlogger(basetest.class);
    public static monitoringmail mail = new monitoringmail();
    public static webdriverwait wait;
    
    
    //findele finds the element based on the select type and enters the value : id/xpath/css
    public void type(string key, string value) {
        
        if(key.endswith("id")) {
            driver.findelement(by.id(or.getproperty(key))).sendkeys(value);;
        }else if(key.endswith("xpath")) {
            driver.findelement(by.xpath(or.getproperty(key))).sendkeys(value);
        }else if(key.endswith("css")) {
            driver.findelement(by.cssselector(or.getproperty(key))).sendkeys(value);
        }
        log.info("typing in an element : " + key + " , entered the values as : " + value);

        extentlisteners.test.info("typing in an element : " + key + " , entered the values as : " + value); //getting error on this line
        
    }
    
    //clickele clicks on the element based on the select type : id/xpath/css
    public void click(string key) {
        
        if(key.endswith("id")) {
            driver.findelement(by.xpath(or.getproperty(key))).click();
        }else if(key.endswith("xpath")) {
            driver.findelement(by.xpath(or.getproperty(key))).click();
        }else if(key.endswith("css")) {
            driver.findelement(by.xpath(or.getproperty(key))).click();
        }
        log.info("clicking on element : " + key);
        extentlisteners.test.info("clicking on element : " + key);
        
    }
    
    
    @beforesuite
    public void setup() {
        
        if(driver == null) {
            
            propertyconfigurator.configure("./src/test/resources/properties/log4j.properties"); //configuring log4j
            log.info("test execution started");
            
            
            try {
                fis = new fileinputstream("./src/test/resources/properties/config.properties"); //loading config file into fis
            } catch (filenotfoundexception e) {
                e.printstacktrace();
            } 
            try {
                config.load(fis); //loading fis to config properties
                log.info("config.properties file loaded");
            } catch (ioexception e) {
                e.printstacktrace();
            }
            
            
            try {
                fis = new fileinputstream("./src/test/resources/properties/or.properties"); //loading or file into fis
            } catch (filenotfoundexception e) {
                e.printstacktrace();
            } 
            try {
                or.load(fis); //loading fis to or properties
                log.info("config.properties file loaded");
            } catch (ioexception e) {
                e.printstacktrace();
            }
            
            
            //launching browser by checking which browser to launch from config file
            if(config.getproperty("browser").equals("chrome")){
                driver = new chromedriver();
                log.info("chrome browser launched");
            }
            else if(config.getproperty("browser").equals("firefox")){
                driver = new firefoxdriver();
                log.info("firefox browser launched");
            }
            
            
            //navigating to url provided in config file
            driver.get(config.getproperty("testsiteurl"));
            log.info("navigating to test site : " + config.getproperty("testsiteurl"));
            
            
            //maximizing browser window
            driver.manage().window().maximize();
            
            //applying implicit wait
            driver.manage().timeouts().implicitlywait(duration.ofseconds(integer.parseint(config.getproperty("implicit.wait"))));
            
            //applying explicit wait
            wait = new webdriverwait(driver, duration.ofseconds(integer.parseint(config.getproperty("explicit.wait"))));
            
            
            //jdbc connection
            try {
                dbmanager.setmysqldbconnection();
                log.info("database connection established");
            } catch (classnotfoundexception e) {
                // todo auto-generated catch block
                e.printstacktrace();
            } catch (sqlexception e) {
                // todo auto-generated catch block
                e.printstacktrace();
            }
        }
        
    }
    
    @aftersuite
    public void teardown() {
        driver.quit();
        log.info("test execution completed");
    }

}

登录测试文件:

package testcases;

import org.testng.annotations.dataprovider;
import org.testng.annotations.test;

import base.basetest;

public class logintest extends basetest {
    
    @test(dataprovider = "data")
    public void dologin(string username, string password) {
        
        type("username_id",username);
        type("password_id",password);
        click("loginbtn_xpath");
        
    }
    
    @dataprovider(name="data")
    public object[][] getdata() {
        
        string sheetname = "logintest";
        int rownum = excel.getrowcount(sheetname);
        int colnum = excel.getcolumncount(sheetname);
        
        excel.getcelldata(sheetname, colnum, rownum);
        
        object[][] data = new object[rownum-1][colnum];
        int row,col;
        for(row=2;row<=rownum;row++){
            for(col=0;col

范围监听器:

package extentlisteners;

import java.util.date;

import org.testng.isuite;
import org.testng.isuitelistener;
import org.testng.itestcontext;
import org.testng.itestlistener;
import org.testng.itestresult;

import com.aventstack.extentreports.extentreports;
import com.aventstack.extentreports.extenttest;
import com.aventstack.extentreports.status;
import com.aventstack.extentreports.markuputils.extentcolor;
import com.aventstack.extentreports.markuputils.markup;
import com.aventstack.extentreports.markuputils.markuphelper;



public class extentlisteners implements itestlistener, isuitelistener {

    static date d = new date();
    static string filename = "extent_" + d.tostring().replace(":", "_").replace(" ", "_") + ".html";

    private static extentreports extent = extentmanager
            .createinstance(".\\reports\\" + filename);

    public static extenttest test;
    
    

    public void onteststart(itestresult result) {

        test = extent
                .createtest(result.gettestclass().getname() + "     @testcase : " + result.getmethod().getmethodname());
    

    }

    public void ontestsuccess(itestresult result) {

        string methodname = result.getmethod().getmethodname();
        string logtext = "" + "test case:- " + methodname.touppercase() + " passed" + "";
        markup m = markuphelper.createlabel(logtext, extentcolor.green);
        test.pass(m);

    }

    public void ontestfailure(itestresult result) {
        

        ///test.fail(result.getthrowable().getmessage());
        /*try {
            extentmanager.capturescreenshot();
        } catch (ioexception e) {
            // todo auto-generated catch block
            e.printstacktrace();
        }*/
        string methodname=result.getmethod().getmethodname();
        string logtext=""+"test case:- "+ methodname.touppercase()+ " failed"+"";        
    
    

        //test.fail("" + "screenshot of failure" + "
",mediaentitybuilder.createscreencapturefrompath(extentmanager.filename) // .build()); markup m = markuphelper.createlabel(logtext, extentcolor.red); test.log(status.fail, m); } public void ontestskipped(itestresult result) { string methodname = result.getmethod().getmethodname(); string logtext = "" + "test case:- " + methodname + " skipped" + ""; markup m = markuphelper.createlabel(logtext, extentcolor.amber); test.skip(m); } public void ontestfailedbutwithinsuccesspercentage(itestresult result) { // todo auto-generated method stub } public void onstart(itestcontext context) { } public void onfinish(itestcontext context) { if (extent != null) { extent.flush(); } } public void onstart(isuite suite) { // todo auto-generated method stub } public void onfinish(isuite suite) { // todo auto-generated method stub } }

我尝试注释 extentlistener 行,它工作正常,因此提取数据或将数据插入到元素中没有问题。这段代码工作过一次,所以我很困惑为什么它不工作并抛出这个错误。

错误消息:

FAILED: testCases.LoginTest.doLogin("[email protected]", "kjsdfnvjndklsv")
java.lang.NullPointerException: Cannot invoke "com.aventstack.extentreports.ExtentTest.info(String)" because "extentlisteners.ExtentListeners.test" is null
at base.BaseTest.type(BaseTest.java:64)
at testCases.LoginTest.doLogin(LoginTest.java:13)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:664)
at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:227)
at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:957)
at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:200)
at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148)
at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:848)
at org.testng.TestRunner.run(TestRunner.java:621)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:443)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:437)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:397)
at org.testng.SuiteRunner.run(SuiteRunner.java:336)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1280)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1200)
at org.testng.TestNG.runSuites(TestNG.java:1114)
at org.testng.TestNG.run(TestNG.java:1082)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

解决方法

看来你的代码没有任何问题。

中英双语红色大气外贸企业网站源码1.1
中英双语红色大气外贸企业网站源码1.1

注意:需要在本地调试我们的网站的必须安装配置IIS,不可以使用ASP调试工具.exe或小旋风asp或APMServ等这类工具调试,因为这类简易的IIS替代工具,去掉了很多功能,有些语句是不支持的。 【程序】ASP 【数据库】ACCESS (只要支持ASP的空间均自带此数据库) 【前台】全部生成.html静态页面 本程序专为企业网站进行打造,三大特色无与伦比: ☆全后台操作☆前台所有内容均可以后台

下载

“extentlisteners.test”为 null 的原因可能仅与 onteststart 在测试之前没有运行有关。

确保您的侦听器已添加到 testng xml 中:



    
        
    
    
        
            
        
    

或者,如果您以编程方式创建套件,请不要忘记在其中添加侦听器:

TestNG testng = new TestNG();
    testng.setTestClasses(new Class[] { LoginTest.class });
    testng.addListener(new ExtentListener());
    testng.run();

此外,您可以在 onteststart 方法中的测试初始化​​行上设置断点,并在调试模式下执行测试以检查为其设置的值。

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php文件怎么打开
php文件怎么打开

打开php文件步骤:1、选择文本编辑器;2、在选择的文本编辑器中,创建一个新的文件,并将其保存为.php文件;3、在创建的PHP文件中,编写PHP代码;4、要在本地计算机上运行PHP文件,需要设置一个服务器环境;5、安装服务器环境后,需要将PHP文件放入服务器目录中;6、一旦将PHP文件放入服务器目录中,就可以通过浏览器来运行它。

2524

2023.09.01

php怎么取出数组的前几个元素
php怎么取出数组的前几个元素

取出php数组的前几个元素的方法有使用array_slice()函数、使用array_splice()函数、使用循环遍历、使用array_slice()函数和array_values()函数等。本专题为大家提供php数组相关的文章、下载、课程内容,供大家免费下载体验。

1600

2023.10.11

php反序列化失败怎么办
php反序列化失败怎么办

php反序列化失败的解决办法检查序列化数据。检查类定义、检查错误日志、更新PHP版本和应用安全措施等。本专题为大家提供php反序列化相关的文章、下载、课程内容,供大家免费下载体验。

1493

2023.10.11

php怎么连接mssql数据库
php怎么连接mssql数据库

连接方法:1、通过mssql_系列函数;2、通过sqlsrv_系列函数;3、通过odbc方式连接;4、通过PDO方式;5、通过COM方式连接。想了解php怎么连接mssql数据库的详细内容,可以访问下面的文章。

952

2023.10.23

php连接mssql数据库的方法
php连接mssql数据库的方法

php连接mssql数据库的方法有使用PHP的MSSQL扩展、使用PDO等。想了解更多php连接mssql数据库相关内容,可以阅读本专题下面的文章。

1416

2023.10.23

html怎么上传
html怎么上传

html通过使用HTML表单、JavaScript和PHP上传。更多关于html的问题详细请看本专题下面的文章。php中文网欢迎大家前来学习。

1234

2023.11.03

PHP出现乱码怎么解决
PHP出现乱码怎么解决

PHP出现乱码可以通过修改PHP文件头部的字符编码设置、检查PHP文件的编码格式、检查数据库连接设置和检查HTML页面的字符编码设置来解决。更多关于php乱码的问题详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1445

2023.11.09

php文件怎么在手机上打开
php文件怎么在手机上打开

php文件在手机上打开需要在手机上搭建一个能够运行php的服务器环境,并将php文件上传到服务器上。再在手机上的浏览器中输入服务器的IP地址或域名,加上php文件的路径,即可打开php文件并查看其内容。更多关于php相关问题,详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1306

2023.11.13

Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

36

2026.01.14

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号