当前位置: 首页 > 知识库问答 >
问题:

java.lang.在TestNG XML文件中实现程度报告时出现ClassCastExcture错误

丌官承
2023-03-14

我在告诉java时出错了。lang.ClassCastException:[My extent report class name]无法强制转换到组织。testng。将TestNG XML文件作为测试套件运行时使用ITestNGListener。

我已经使用页面工厂设计技术,使用MAVEN和TestNG自动化了一个网页,它由6个页面类组成,对象在一个包中初始化。我还在另一个包中编写了扩展报告侦听器类。除此之外,我在另一个包中还有一个基类,它是所有6页对象初始化类的超类。我已经为3个页面类编写了测试用例,基类也是这些类的超级类。

我通过添加所有3页测试用例和添加范围报告类作为这个XML文件的监听器来生成TestNG XML文件。

我将通过包含下面每个包中的一个类来展示我的框架结构。

页面对象初始化包-登录类

package com.crm.qa.pages;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.crm.qa.base.TestBase;
import com.crm.qa.util.TestUtil;

public class LoginPage extends TestBase {

@FindBy(name="username")
WebElement userName;

@FindBy(name="password")
WebElement password;

@FindBy(xpath="//input[@type='submit']")
WebElement loginBtn;

@FindBy(xpath="//button[contains(text(),'Sign Up')]")
WebElement signupBtn;

@FindBy(xpath="//img[@class = 'img-responsive']")
WebElement crmLogo;

//Initializing the page objects
public LoginPage() {

    PageFactory.initElements(driver, this);

}

public String validateLoginPageTitle() {

    return driver.getTitle();

}

public boolean validateCRMLogo() {

    return crmLogo.isDisplayed();
}

public HomePage login (String un, String pwd) {

    userName.sendKeys(un);
    password.sendKeys(pwd);

    loginBtn.submit();
    driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);


//  Actions act = new Actions(driver);
//  act.moveToElement(loginBtn).click().build().perform();


    return new HomePage();
}

}

基础封装-测试基础类

package com.crm.qa.base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import com.crm.qa.util.TestUtil;
import com.crm.qa.util.WebEventListener;

public class TestBase {

public static WebDriver driver;
public static Properties prop;
public static EventFiringWebDriver e_driver;
public static WebEventListener eventListener;


public TestBase() {

    try {

        prop = new Properties();

        FileInputStream ip = new FileInputStream("C:\\Users\\i7\\git\\TestDesignFramework1\\Suresh.com.automationLearning\\src"
                + "\\main\\java\\com\\crm\\qa\\config\\config.properties");

        prop.load(ip);
    }

    catch (FileNotFoundException e) {
        e.printStackTrace();

    }

    catch (IOException e) {
        e.printStackTrace();
    }


}


public static void initialization () {

    String browserName = prop.getProperty("browser");

    if(browserName.equals("chrome")) {

        System.setProperty("webdriver.chrome.driver", "E:\\C\\Selenium\\Chrome Driver\\Extract\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    else if (browserName.equals("firefox")) {

        System.setProperty("webdriver.gecko.driver", "E:\\C\\Selenium\\GeckoDriver\\Extract\\geckodriver.exe");
        driver = new FirefoxDriver();
    }

    e_driver = new EventFiringWebDriver(driver);
    eventListener = new WebEventListener();
    e_driver.register(eventListener);
    driver = e_driver;


    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();


    driver.get(prop.getProperty("url"));
    driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
}


}

测试用例包-登录页面测试类

package com.crm.qa.pages.testcases;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.Assert;
import com.crm.qa.base.TestBase;
import com.crm.qa.pages.HomePage;
import com.crm.qa.pages.LoginPage;

public class LoginPageTest extends TestBase {

LoginPage loginPage;
HomePage homepage;  

public LoginPageTest() {

    super(); 

}


@BeforeMethod
public void setUp() {

    initialization();
    loginPage = new LoginPage(); 

}

@Test(priority = 1)
public void loginPageTitle() {

//  extentTest = extent.createTest("loginPageTitle");

    String title = loginPage.validateLoginPageTitle();

    Assert.assertEquals(title, "#1 Free CRM software in the "
            + "cloud for sales and service");

}

@Test(priority = 2)
public void crmLogoImageTest() {

//  extentTest = extent.createTest("crmLogoImageTest");

    boolean flag = loginPage.validateCRMLogo();
    Assert.assertTrue(flag);
}



@Test(priority = 3)
public void loginTest() {

//  extentTest = extent.createTest("loginTest");

    homepage = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
    System.out.println("Successfully login to the home page of freeCRM");
}

@AfterMethod
public void tearDown() {

    driver.quit();

}


}

测试Util包-扩展报告侦听器类

package com.crm.qa.ExtentReport;

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;

public class ExtentReportListener {

public static ExtentHtmlReporter htmlReporter;
public static ExtentReports extent;
public static ExtentTest extentTest;

@BeforeSuite
public void setUp() {

    htmlReporter = new ExtentHtmlReporter("C:\\Users\\i7\\git\\TestDesignFramework1\\Suresh.com.automationLearning\\Reporting\\ExtentReporting.html");
    extent = new ExtentReports();
    extent.attachReporter(htmlReporter);

}

@AfterMethod
public void getResult(ITestResult result) {

    if (result.getStatus()==ITestResult.FAILURE) {

        extentTest.fail(MarkupHelper.createLabel(result.getName()+" Test Case Failed", ExtentColor.RED));
        extentTest.fail(result.getThrowable());
    }

    else if (result.getStatus()==ITestResult.SUCCESS) {

        extentTest.pass(MarkupHelper.createLabel(result.getName()+" Test Case Passed", ExtentColor.GREEN));
        extentTest.pass(result.getThrowable());

    }

    else {

        extentTest.skip(MarkupHelper.createLabel(result.getName()+" Test Case Skipped", ExtentColor.ORANGE));
        extentTest.skip(result.getThrowable());

    }


}

@AfterSuite
public void tearDown() {

    extent.flush(); 

}

}

TestNG XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Free CRM Test Application Regression Test Suite">

<listeners>

<listener class-name="com.crm.qa.ExtentReport.ExtentReportListener">
</listener>

</listeners>


<test thread-count="5" name="Free CRM app regression test cases">
<classes>
  <class name="com.crm.qa.pages.testcases.LoginPageTest"/>
  <class name="com.crm.qa.pages.testcases.HomePageTest"/>
  <class name="com.crm.qa.pages.testcases.ContactsPageTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

共有2个答案

冯沛
2023-03-14

解决方案是在testNG Report类中使用“实现IER”。

在您的情况下,testNG报告类是"ExentReportListener",因此您必须键入:

public class FinalReport  implements IReporter 
{}
卢鸿博
2023-03-14

TestNG正在按设计工作。

无论何时向testng suite xml文件中添加一个条目,如下面的条目

<listeners>
    <listener class-name="com.crm.qa.ExtentReport.ExtentReportListener"/>
</listeners>

TestNG期望类实现的一个子接口org.testng.ITestNGListener

你的类不这样做,这就是触发异常的原因。

请浏览相关的程度报告留档,以了解如何正确使用程度报告。

 类似资料: