TestNG-进阶
2019-10-25 本文已影响0人
小眼睛的露鹿酱
TestNG 执行
-
在eclipse中创建一个Java project
-
创建Package
-
创建java 的class
创建两个Class: DemoA 和DemoB
-
导入TestNG lib
- 创建lib folder和data folder, 导入需要的selenium jar包记忆chrome的driver
- demoA与DemoB的代码如下
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
public class DemoA {
WebDriver chromeDriver = new ChromeDriver();
@Test
public void run() {
// set driver
String driverPath = "D:\\EclipseSpace\\testngproject2\\data\\chromedriver.exe";
System.setProperty("webdriver.chrome.marionette", driverPath);
//
String baseURL = "http://www.google.co.in";
Reporter.log("start open browser");
chromeDriver.manage().window().maximize();
chromeDriver.get(baseURL);
Reporter.log("input selenium");
chromeDriver.findElement(By.name("q")).sendKeys("selenium");
chromeDriver.findElement(By.xpath("//*[@id=\"tsf\"]/div[2]/div[1]/div[3]/center/input[1]")).click();
Reporter.log("check page search out");
String newtitleString = chromeDriver.getTitle();
Assert.assertEquals("selenium - Google Search", newtitleString);
}
@AfterClass
public void closedBrowser() {
chromeDriver.close();
}
}
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.Test;
public class DemoB {
@Test
public void run() {
// set driver
String driverPath = "D:\\EclipseSpace\\testngproject2\\data\\chromedriver.exe";
System.setProperty("webdriver.chrome.marionette", driverPath);
WebDriver chromeDriver = new ChromeDriver();
//
String baseURL = "http://www.google.co.in";
Reporter.log("start open browser");
chromeDriver.manage().window().maximize();
chromeDriver.get(baseURL);
Reporter.log("check title");
String title = chromeDriver.getTitle();
Reporter.log(title);
}
}
-
生成testng.xml文件
-
Run As testng
===============================================
Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
TestNG生成报告
-
test-output file中会有一个文件是emailable-report.html
-
index.html
自定义报告内容 以及截图
自定义报告内容可以通过创建新的class(继承IReport 和 ITestListener)
1. 继承ITestListener
新建一个package com.thers; 创建一个新的class 继承ITestListener
package com.others;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class ItestLdemo1 implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("* START * :"+result.getName());
}
@Override
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("*** passed :"+result.getName() );
}
@Override
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("---- failed :" +result.getName());
}
@Override
public void onTestSkipped(ITestResult result) {
// TODO Auto-generated method stub
System.out.println(" skiped :"+result.getName());
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub
ITestListener.super.onTestFailedButWithinSuccessPercentage(result);
}
@Override
public void onTestFailedWithTimeout(ITestResult result) {
// TODO Auto-generated method stub
ITestListener.super.onTestFailedWithTimeout(result);
}
@Override
public void onStart(ITestContext context) {
// TODO Auto-generated method stub
ITestListener.super.onStart(context);
}
@Override
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
ITestListener.super.onFinish(context);
}
}
将Listener注释添加到相关的class中
然后执行一下得到下面的 result
- 继承IReport
创建一个class 继承IReport
package com.others;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.text.AbstractDocument.Content;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.Reporter;
import org.testng.xml.XmlSuite;
public class ireportDemo1 implements IReporter{
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
// TODO Auto-generated method stub
IReporter.super.generateReport(xmlSuites, suites, outputDirectory);
for(int i=0;i<suites.size();i++) {
Map<String, ISuiteResult> results = suites.get(i).getResults();
for (String key:results.keySet()) {
ISuiteResult value = results.get(key);
ITestContext context = value.getTestContext();
String s = "Suit Name"+context.getName()
+"::::" +"Report output filr >"+context.getOutputDirectory()
+"\n::::"+"Suit name >"+context.getSuite().getName()
+"\n::::"+"start execution >"+context.getStartDate()
+"\n::::"+"endTime >"+ context.getEndDate()
+"\n::::"+" passed >"+ context.getPassedTests().size()
+"\n::::"+" fialed >"+ context.getFailedTests().size();
System.out.println(s);
}
}
}
}
输出结果为:
- PDF 和Email
使用PDF的jar包
4.1 创建一个Base的java 类 里面有screenshot的设置
package com.others;
import java.io.File;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.google.common.io.Files;
public class base {
static WebDriver driver;
public static WebDriver getDriver(){
if(driver==null){
WebDriver driver ;
String driverPath = "D:\\EclipseSpace\\testngproject2\\data\\chromedriver.exe";
System.setProperty("webdriver.chrome.marionette", driverPath);
driver = new ChromeDriver();
}
return driver;
}
public static void takeScreenshot (WebDriver dirver, String outputFile) throws Exception{
TakesScreenshot srcshot = ((TakesScreenshot) dirver);
File srcFile = srcshot.getScreenshotAs(OutputType.FILE);
File descFile = new File(outputFile);
Files.copy(srcFile, descFile);
}
}