监听失败自动截图
截图程序
package com.sheehan.second_maven;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class baseDriver {
WebDriver driver;
public baseDriver() {
driver = new FirefoxDriver();
driver.get("http://www.imooc.com");
driver.manage().window().maximize();
}
public void ScreenShot(){
long date = System.currentTimeMillis(); // 获取当前时间戳
String path = String.valueOf(date);
String curPath = System.getProperty("user.dir");
path = path + ".png";
String screenPath = curPath + "/" + path;
File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(screen, new File(screenPath));
} catch (IOException e){
e.printStackTrace();
}
}
}
TestNg中有一个TestListenerAdapter类会对所要测试的程序进行监听,我们需要对其中测试失败的处理方法进行修改
package com.sheehan.second_maven;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
public class TestngListenerScreenShot extends TestListenerAdapter{
@Override
public void onTestSuccess(ITestResult tr) {
super.onTestSuccess(tr);
}
@Override
public void onTestFailure(ITestResult tr) {
super.onTestFailure(tr);
takeScreenShot(tr); // 这里添加处理方法
}
private void takeScreenShot(ITestResult tr) {
baseDriver b = (baseDriver) tr.getInstance();
b.ScreenShot();
}
@Override
public void onTestSkipped(ITestResult tr) {
super.onTestSkipped(tr);
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {
super.onTestFailedButWithinSuccessPercentage(tr);
}
@Override
public void onStart(ITestContext testContext) {
super.onStart(testContext);
}
@Override
public void onFinish(ITestContext testContext) {
super.onFinish(testContext);
}
}
在编写的测试类前添加修饰
package com.sheehan.second_maven;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners({TestngListenerScreenShot.class}) //添加修饰
public class ScreenShotTest extends baseDriver{
public void Loginscript() throws Exception{
String username = "#########"; //此处为用户名
String userpass = "#########"; //此处为密码
String emailElement = "email";
String passwordElement = "password";
String buttonElement = "moco-btn";
String headerElement = "header-avator";
String nameElement = "name";
driver.findElement(By.id("js-signin-btn")).click();
Thread.sleep(3000);
WebElement user = driver.findElement(By.name(emailElement));
user.isDisplayed();
WebElement password = driver.findElement(By.name(passwordElement));
password.isDisplayed();
user.sendKeys(username);;
password.sendKeys(userpass);
WebElement loginButton = driver.findElement(By.className(buttonElement));
loginButton.isDisplayed();
loginButton.click();
Thread.sleep(3000);
WebElement header = driver.findElement(By.id(headerElement));
header.isDisplayed();
Actions actions = new Actions(driver);
actions.moveToElement(header).perform();
WebElement present = null;
present = driver.findElement(By.className("1card-top")); //应为“card-top”,设置错误可以触发测试失败,从而观测到截图的产生
String userInfo = present.findElement(By.className(nameElement)).getText();
System.out.println(userInfo);
driver.close();
}
@Test
public void loginpage() throws Exception {
this.Loginscript();
}
}