我使用Maven运行此Selenium脚本以获取范围报告,但出现错误:
FAILED CONFIGURATION: @BeforeTest startReport
java.lang.NullPointerException
at java.base/java.io.File.<init>(File.java:276)
at com.aventstack.extentreports.reporter.BasicFileReporter.init(BasicFileReporter.java:106)
at com.aventstack.extentreports.reporter.ExtentHtmlReporter.<init>(ExtentHtmlReporter.java:30)
at com.river.cgyProject.CgyExtentReports.startReport(CgyExtentReports.java:37)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.beforeRun(TestRunner.java:641)
at org.testng.TestRunner.run(TestRunner.java:609)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
SKIPPED CONFIGURATION: @BeforeMethod setup
SKIPPED CONFIGURATION: @AfterMethod screenShot
SKIPPED CONFIGURATION: @BeforeMethod setup
SKIPPED CONFIGURATION: @AfterMethod screenShot
SKIPPED CONFIGURATION: @AfterTest endReport
SKIPPED: verifyAddContract
SKIPPED: verifyLogin
我的代码是:
public class CgyExtentReports {
public WebDriver driver;
public ExtentHtmlReporter htmlReporter;
public ExtentReports extent;
public ExtentTest logger;
@BeforeTest
public void startReport() {
htmlReporter = new ExtentHtmlReporter(System.getProperty("/home/rivertech/Dev/workspace/apache/cgyProject/target/"));
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setSystemInfo("Host Name", "RiverCgy");
extent.setSystemInfo("Environment", "Test");
extent.setSystemInfo("User Name", "Preetham");
htmlReporter.config().setDocumentTitle("Title of the Document");
htmlReporter.config().setReportName("Name of the Report Comes here ");
htmlReporter.config().setTheme(Theme.STANDARD);
}
@BeforeMethod
public void setup() {
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("demo");
}
@Test
public void verifyLogin() {
logger = extent.createTest("To verify Cloudigy Login");
driver.findElement(By.xpath("//input[@class='form-control inputgray emailInput']")).sendKeys("admin@cloudigy.io");
driver.findElement(By.id("password")).sendKeys("test1234");
driver.findElement(By.id("loginBtn")).click();
}
@Test
public void verifyAddContract() {
driver.findElement(By.id("contracts")).click();
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElement(By.id("pageDropDown")).click();
driver.findElement(By.linkText("20")).click();
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
WebElement table = driver.findElement(By.xpath("//*[@id=\"ec2\"]"));
List<WebElement> rows_table = table.findElements(By.tagName("tr"));
int rows_count = rows_table.size();
for (int row=0; row<rows_count; row++){
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
int columns_count = Columns_row.size();
for (int column=0; column<columns_count; column++){
String celltext = Columns_row.get(column).getText();
System.out.println(celltext);
}
}
}
@AfterMethod
public void screenShot(ITestResult result) {
if(ITestResult.FAILURE==result.getStatus()) {
try {
TakesScreenshot screenshot=(TakesScreenshot)driver;
File src=screenshot.getScreenshotAs(OutputType.FILE);
String filename= new SimpleDateFormat("yyyyMMddhhmmss'.png'").format(new Date());
File dest=new File("/home/rivertech/Dev/workspace/apache/cgyProject/target/" + filename);
FileHandler.copy(src,dest);
System.out.println("Screenshot taken");
}catch(Exception e){
System.out.println("Exception while taking screenshot"+e.getMessage());
}
}
}
@AfterTest
public void endReport() {
extent.flush();
}
}
答案 0 :(得分:0)
只需提及:
String path=System.getProperty("user.dir")+"\\Reports\\index.html";