Selenium webdriver @测试未执行-如果我在测试xml文件中添加TestNG Listener,我的@Test方法将不会执行

Selenium webdriver @测试未执行-如果我在测试xml文件中添加TestNG Listener,我的@Test方法将不会执行,selenium-webdriver,testng,extentreports,testng.xml,testng-annotation-test,Selenium Webdriver,Testng,Extentreports,Testng.xml,Testng Annotation Test,我使用TestNG,并使用扩展报告进行报告。我试图添加TestNG侦听器,以便在成功和失败时执行一些操作。我已经创建了一个测试侦听器类,并在套件文件中提到了侦听器;如果是从套件xml文件中删除listener标记,它的工作方式类似于charm 监听器类 public class TestListener implements ITestListener { @Override public void onTestStart(ITestResult result) {

我使用TestNG,并使用扩展报告进行报告。我试图添加TestNG侦听器,以便在成功和失败时执行一些操作。我已经创建了一个测试侦听器类,并在套件文件中提到了侦听器;如果是从套件xml文件中删除listener标记,它的工作方式类似于charm

监听器类

public class TestListener implements ITestListener
{

    @Override
    public void onTestStart(ITestResult result) 
    {
        // TODO Auto-generated method stub
        System.out.println("OnTestStart");

    }

    @Override
    public void onTestSuccess(ITestResult result) 
    {
        // TODO Auto-generated method stub
        Reporter.log(Status.INFO, "Passed");

    }

    @Override
    public void onTestFailure(ITestResult result) 
    {
        // TODO Auto-generated method stub
        Reporter.log(Status.INFO, "Failed");
    }
}
public class TestScripts extends BaseTest
{

    @Test
    @Parameters("Browser")
    public void loginScenario(String browser)
    {
        LoginPageSteps lp = new LoginPageSteps(browser);
        lp.loginScenario();
    }
    @Test
    @Parameters("Browser")
    public void a(String browser)
    {
        Reporter.log(Status.PASS, "A Passed");
    }
    @Test
    @Parameters("Browser")
    public void b(String browser)
    {
        Reporter.log(Status.ERROR, "B Failed");
    }
    @Test
    @Parameters("Browser")
    public void c(String browser)
    {
        Reporter.log(Status.ERROR, "C Failed");
    }
}
基本测试

public class BaseTest 
{
    @BeforeSuite
    public void preStep(ITestContext txt)
    {
        System.out.println("in before suite");
        Reporter.startReport(txt);
        System.out.println("out");
    }
    @BeforeMethod(alwaysRun=true)
    public void setUp()
    {
        System.out.println("Before Method");
        Reporter.setup();
    }
    @AfterMethod
    public void closeUp(ITestResult tr)
    {
        System.out.println("After Method");
    }
    @AfterSuite
    public void postStep()
    {
        System.out.println("After Suite");
    }
}
测试类

public class TestListener implements ITestListener
{

    @Override
    public void onTestStart(ITestResult result) 
    {
        // TODO Auto-generated method stub
        System.out.println("OnTestStart");

    }

    @Override
    public void onTestSuccess(ITestResult result) 
    {
        // TODO Auto-generated method stub
        Reporter.log(Status.INFO, "Passed");

    }

    @Override
    public void onTestFailure(ITestResult result) 
    {
        // TODO Auto-generated method stub
        Reporter.log(Status.INFO, "Failed");
    }
}
public class TestScripts extends BaseTest
{

    @Test
    @Parameters("Browser")
    public void loginScenario(String browser)
    {
        LoginPageSteps lp = new LoginPageSteps(browser);
        lp.loginScenario();
    }
    @Test
    @Parameters("Browser")
    public void a(String browser)
    {
        Reporter.log(Status.PASS, "A Passed");
    }
    @Test
    @Parameters("Browser")
    public void b(String browser)
    {
        Reporter.log(Status.ERROR, "B Failed");
    }
    @Test
    @Parameters("Browser")
    public void c(String browser)
    {
        Reporter.log(Status.ERROR, "C Failed");
    }
}
套件文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Default Suite" parallel="tests">
  <listeners>
    <listener class-name="testng.framework.utilities.TestListener"/>
  </listeners>
  <test thread-count="10" name="GUI" parallel="tests">
    <classes>
      <class name="testng.alltest.TestScripts">
        <parameter name="Browser" value="Chrome"/>
        <methods>
          <include name="a"/>
          <include name="b"/>
          <include name="c"/>
          <include name="loginScenario"/>
        </methods>
      </class>
    </classes>
  </test>
</suite>

输出

package stackoverflow;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class TestListener implements ITestListener {

@Override
public void onTestStart(ITestResult iTestResult) {
    System.out.println("onTestStart");
}

@Override
public void onTestSuccess(ITestResult iTestResult) {
    System.out.println("onTestSuccess");
}

@Override
public void onTestFailure(ITestResult iTestResult) {
    System.out.println("onTestFailure");
}

@Override
public void onTestSkipped(ITestResult iTestResult) {
    System.out.println("onTestSkipped");
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
    System.out.println("onTestFailedButWithinSuccessPercentage");
}

@Override
public void onStart(ITestContext iTestContext) {
    System.out.println("onTestFailedButWithinSuccessPercentage");
}

@Override
public void onFinish(ITestContext iTestContext) {
    System.out.println("onFinish");
}
}
package stackoverflow;

import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;

public class BaseTest {
    @BeforeSuite
    public void preStep(ITestContext txt)
    {
        System.out.println("in before suite");
        System.out.println("out");
    }
    @BeforeMethod(alwaysRun=true)
    public void setUp()
    {
        System.out.println("Before Method");
    }
    @AfterMethod
    public void closeUp(ITestResult tr)
    {
        System.out.println("After Method");
    }
    @AfterSuite
    public void postStep()
    {
        System.out.println("After Suite");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Default Suite" parallel="tests">
    <listeners>
        <listener class-name="listener.TestListener"/>
    </listeners>
    <test thread-count="10" name="GUI" parallel="tests">
        <classes>
            <class name="stackoverflow.TestScripts">
                <parameter name="Browser" value="Chrome"/>
                <methods>
                    <include name="a"/>
                    <include name="b"/>
                    <include name="c"/>
                    <include name="loginScenario"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

请纠正我遗漏的内容,提前谢谢

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>ORORA</groupId>
  <artifactId>UIAutomation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>UIAutomation</name>
  <url>http://maven.apache.org</url>
  <developers>
        <developer>
            <id>Deepak Mahalingam</id>
            <name>Deepak</name>
            <email>mahalingamd@hcl.com</email>
            <organization>HCL</organization>
            <roles>
                <role>Automation Tester</role>
            </roles>
        </developer>
    </developers>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
        <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>TestSuite\TestDesktop.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <mainClass>testng.framework.utilities.BuildTestXML</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
            <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>4.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>3.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180813</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
  </dependencies>
</project>

4.0.0
奥罗拉
自动化
0.0.1-快照
罐子
自动化
http://maven.apache.org
迪帕克·马哈林根
迪帕克
mahalingamd@hcl.com
盐酸
自动化测试仪
UTF-8
1.8
1.8
org.apache.maven.plugins
maven surefire插件
3.0.0-M3
TestSuite\TestDesktop.xml
org.codehaus.mojo
execmaven插件
1.6.0
testng.framework.utilities.BuildTestXML
朱尼特
朱尼特
3.8.1
测试
org.seleniumhq.selenium
硒爪哇
3.14.0
org.testng
testng
6.14.3
com.aventstack
扩展端口
4.0.9
com.sun.mail
javax.mail
1.6.0
放心吧
放心
3.3.0
org.json
json
20180813
org.apache.poi
poi
4.1.0
org.apache.poi
poi ooxml
4.1.0
公地io
公地io
2.6
你的听众和记者似乎有问题 类导入语句

如果您使用的是TestNG的Reporter类,那么就不存在这样的问题 方法为“设置”或启动

我可以通过如下修改TestListener和BaseTest类来运行脚本:

package stackoverflow;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class TestListener implements ITestListener {

@Override
public void onTestStart(ITestResult iTestResult) {
    System.out.println("onTestStart");
}

@Override
public void onTestSuccess(ITestResult iTestResult) {
    System.out.println("onTestSuccess");
}

@Override
public void onTestFailure(ITestResult iTestResult) {
    System.out.println("onTestFailure");
}

@Override
public void onTestSkipped(ITestResult iTestResult) {
    System.out.println("onTestSkipped");
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
    System.out.println("onTestFailedButWithinSuccessPercentage");
}

@Override
public void onStart(ITestContext iTestContext) {
    System.out.println("onTestFailedButWithinSuccessPercentage");
}

@Override
public void onFinish(ITestContext iTestContext) {
    System.out.println("onFinish");
}
}
package stackoverflow;

import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;

public class BaseTest {
    @BeforeSuite
    public void preStep(ITestContext txt)
    {
        System.out.println("in before suite");
        System.out.println("out");
    }
    @BeforeMethod(alwaysRun=true)
    public void setUp()
    {
        System.out.println("Before Method");
    }
    @AfterMethod
    public void closeUp(ITestResult tr)
    {
        System.out.println("After Method");
    }
    @AfterSuite
    public void postStep()
    {
        System.out.println("After Suite");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Default Suite" parallel="tests">
    <listeners>
        <listener class-name="listener.TestListener"/>
    </listeners>
    <test thread-count="10" name="GUI" parallel="tests">
        <classes>
            <class name="stackoverflow.TestScripts">
                <parameter name="Browser" value="Chrome"/>
                <methods>
                    <include name="a"/>
                    <include name="b"/>
                    <include name="c"/>
                    <include name="loginScenario"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>
我的基本测试类:

package stackoverflow;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class TestListener implements ITestListener {

@Override
public void onTestStart(ITestResult iTestResult) {
    System.out.println("onTestStart");
}

@Override
public void onTestSuccess(ITestResult iTestResult) {
    System.out.println("onTestSuccess");
}

@Override
public void onTestFailure(ITestResult iTestResult) {
    System.out.println("onTestFailure");
}

@Override
public void onTestSkipped(ITestResult iTestResult) {
    System.out.println("onTestSkipped");
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
    System.out.println("onTestFailedButWithinSuccessPercentage");
}

@Override
public void onStart(ITestContext iTestContext) {
    System.out.println("onTestFailedButWithinSuccessPercentage");
}

@Override
public void onFinish(ITestContext iTestContext) {
    System.out.println("onFinish");
}
}
package stackoverflow;

import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;

public class BaseTest {
    @BeforeSuite
    public void preStep(ITestContext txt)
    {
        System.out.println("in before suite");
        System.out.println("out");
    }
    @BeforeMethod(alwaysRun=true)
    public void setUp()
    {
        System.out.println("Before Method");
    }
    @AfterMethod
    public void closeUp(ITestResult tr)
    {
        System.out.println("After Method");
    }
    @AfterSuite
    public void postStep()
    {
        System.out.println("After Suite");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Default Suite" parallel="tests">
    <listeners>
        <listener class-name="listener.TestListener"/>
    </listeners>
    <test thread-count="10" name="GUI" parallel="tests">
        <classes>
            <class name="stackoverflow.TestScripts">
                <parameter name="Browser" value="Chrome"/>
                <methods>
                    <include name="a"/>
                    <include name="b"/>
                    <include name="c"/>
                    <include name="loginScenario"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>
My TestNg.xml

package stackoverflow;

import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

public class TestListener implements ITestListener {

@Override
public void onTestStart(ITestResult iTestResult) {
    System.out.println("onTestStart");
}

@Override
public void onTestSuccess(ITestResult iTestResult) {
    System.out.println("onTestSuccess");
}

@Override
public void onTestFailure(ITestResult iTestResult) {
    System.out.println("onTestFailure");
}

@Override
public void onTestSkipped(ITestResult iTestResult) {
    System.out.println("onTestSkipped");
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
    System.out.println("onTestFailedButWithinSuccessPercentage");
}

@Override
public void onStart(ITestContext iTestContext) {
    System.out.println("onTestFailedButWithinSuccessPercentage");
}

@Override
public void onFinish(ITestContext iTestContext) {
    System.out.println("onFinish");
}
}
package stackoverflow;

import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;

public class BaseTest {
    @BeforeSuite
    public void preStep(ITestContext txt)
    {
        System.out.println("in before suite");
        System.out.println("out");
    }
    @BeforeMethod(alwaysRun=true)
    public void setUp()
    {
        System.out.println("Before Method");
    }
    @AfterMethod
    public void closeUp(ITestResult tr)
    {
        System.out.println("After Method");
    }
    @AfterSuite
    public void postStep()
    {
        System.out.println("After Suite");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="10" name="Default Suite" parallel="tests">
    <listeners>
        <listener class-name="listener.TestListener"/>
    </listeners>
    <test thread-count="10" name="GUI" parallel="tests">
        <classes>
            <class name="stackoverflow.TestScripts">
                <parameter name="Browser" value="Chrome"/>
                <methods>
                    <include name="a"/>
                    <include name="b"/>
                    <include name="c"/>
                    <include name="loginScenario"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>


您可以发布pom依赖项吗?@Sureshmani请查找pom.xmlfile@Deepak_Mahalingam:您是否提供了完整的TestListener类代码。。。正如我所看到的,ITestListeners的许多方法没有在您的应用程序中实现class@Deepak_Mahalingam:您是否可以提供您在“TestListener”和“BaseTest”中使用的Reporter类的导入语句?如果您将Reporter类用于数据块报告,则其仅适用于4以下的数据块报告版本。请检查Reporter类依赖项并相应更新。