我尝试使用侦听器、extentreportgen、基类和主测试类来生成扩展报告,但是没有生成。第一个是我的主要测试类,第二个是我的Listers类,第三个是我的基类,最后一个是我的扩展报告Gen类。
登录主测试代码
import java.io.IOException;
import org.testng.annotations.Test;
import extentListners.Listeners;
import pages.loginPage;
public class LoginTest extends Listeners{
@Test
public void LogInTest() throws IOException {
driver = initialiseDriver();
loginPage log = new loginPage(driver);
log.user("pooja@katchintech.com");
log.pass("Cust@123");
log.loginbt();
log.logoutbt();
driver.quit();
}
}
监听者类
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 resources.BaseClass;
public class Listeners extends BaseClass implements ITestListener{
ExtentReports extent = ExtentReportGen.ExtentReportGenerator();
public static ExtentTest test;
@Override
public void onTestStart(ITestResult result) {
System.out.println("Test case :"+result.getName());
test = extent.createTest(result.getName());
}
public void onTestSuccess(ITestResult result) {
System.out.println("Test case passed :"+result.getName());
test.log(Status.PASS, "Test case passed");
}
@Override
public void onTestFailure(ITestResult result) {
System.out.println("Test case failed :"+result.getName());
test.fail(result.getThrowable());
}
@Override
public void onTestSkipped(ITestResult result) {
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
}
@Override
public void onTestFailedWithTimeout(ITestResult result) {
}
@Override
public void onStart(ITestContext context) {
System.out.println("Execution started :"+context.getName());
}
@Override
public void onFinish(ITestContext context) {
System.out.println("Execution finished :"+context.getName());
extent.flush();
}
}
BaseClass
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import io.github.bonigarcia.wdm.WebDriverManager;
public class BaseClass {
public WebDriver driver;
public ExtentReports extent = new ExtentReports();
public ExtentSparkReporter spark;
public ExtentTest test;
@BeforeTest
public WebDriver initialiseDriver() throws IOException {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("C:\Users\thora\eclipse-workspace\Biskane_NEW\src\main\java\resources\data.properties");
prop.load(fis);
String browsername = prop.getProperty("browser");
switch(browsername){
case "chrome":
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.get("https://qa.biskane.com/login");
driver.manage().window().maximize();
break;
case "firefox":
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
driver.get("https://qa.biskane.com/login");
driver.manage().window().maximize();
break;
case "edge":
WebDriverManager.edgedriver().setup();
driver = new EdgeDriver();
driver.get("https://qa.biskane.com/login");
driver.manage().window().maximize();
break;
default:
System.out.println("browser: " + browsername + " is invalid, launching FireFox browser as browser of choice..");
WebDriverManager.firefoxdriver().setup();
driver = new FirefoxDriver();
driver.get("https://qa.biskane.com/login");
driver.manage().window().maximize();
break;
}
return driver;
}
}
ExtentReportGen类
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
public class ExtentReportGen {
static ExtentReports extent ;
public static ExtentReports ExtentReportGenerator() {
String path = System.getProperty("user.dir")+"//ExtentReports//Biskanereport.html";
ExtentHtmlReporter reporter = new ExtentHtmlReporter(path);
reporter.config().setReportName("BiskaneTestReport");
reporter.config().setTheme(Theme.STANDARD);
extent = new ExtentReports();
extent.attachReporter(reporter);
extent.setSystemInfo("OS", "Windows");
extent.setSystemInfo("Organization name", "Appzlogic");
extent.setSystemInfo("Executed by", "...);
return extent;
}
}
我还有另一个问题,当我在测试类中运行日志时,它会打开2个Chrome窗口,你们能帮我解决这个问题吗?
2条答案
按热度按时间6l7fqoea1#
请尝试以下代码,
在BaseTest中,@Beforetest将在我们的@Test方法之前运行,因此它将打开一个浏览器窗口。在此之后,在您的@Test方法中,您将再次调用initalizeDriver(),它将运行一个额外的浏览器。
ibps3vxo2#
您是否尝试过调试测试并查看是否调用了Listeners类中的方法,以及是否执行了这些方法?