在TestNG XML文件中实现范围报告时发生java.lang.ClassCastException错误

时间:2018-08-11 04:34:10

标签: selenium testng extentreports page-factory

我在告诉java.lang.ClassCastException时出错:[我的扩展报告类名称]在将TestNG XML文件作为测试套件运行时无法转换为org.testng.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 个答案:

答案 0 :(得分:2)

TestNG正在按设计工作。

无论何时将以下条目添加到您的testng套件xml文件中

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

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

您的班级不这样做,这就是触发异常的原因。

请仔细阅读相关范围报告documentation,以了解如何正确使用范围报告。

答案 1 :(得分:0)

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

在您的情况下,testNG报告类为“ ExtentReportListener”,因此您必须输入:

public class FinalReport  implements IReporter 
{}