自动化截图
Automation screenshot
我目前正在从事一个自动化项目,我创建了这个简单的程序来报告测试信息并截取屏幕截图。
package ReportTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import ReportTest.Utitlity;
public class ReportTest {
ExtentReports report;
ExtentTest logger;
WebDriver driver;
@Test
public void verifyBlogTitle()
{
report =new ExtentReports("C:\Users\reganc3\desktop\report\LearnAutomation.html");
logger=report.startTest("VerifyBlogTitle");
driver = new FirefoxDriver();
driver.manage().window().maximize();
logger.log(LogStatus.INFO, "Browser started");
driver.get("http://www.learn-automation.com");
logger.log(LogStatus.INFO, "Application is up and running");
String title = driver.getTitle();
Assert.assertTrue(title.contains("Selenium"));
logger.log(LogStatus.PASS, "Title Verified");
}
@AfterMethod
public void tearDown(ITestResult result)
{
if(result.getStatus()==ITestResult.FAILURE)
{
String screenshot_path = Utitlity.captureScreenshot(driver, result.getName());
logger.log(LogStatus.FAIL, "TitleVerification", screenshot_path);
}
report.endTest(logger);
report.flush();
driver.get("C:\Users\reganc3\desktop\report\LearnAutomation.html");
}
}
这是我的实用程序 class,它被调用以截取屏幕截图
package ReportTest;
import java.io.File;
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.WebDriverException;
import java.util.*;
public class Utitlity {
public static String captureScreenshot(WebDriver driver, String screenshotName)
{
try
{
TakesScreenshot ts=(TakesScreenshot)driver;
File source=ts.getScreenshotAs(OutputType.FILE);
String dest = " C:\Users\reganc3\desktop\ " +screenshotName+ ".png";
File destination=new File(dest);
FileUtils.copyFile(source, destination);
System.out.println("Screenshot taken");
return dest;
}
catch (Exception e)
{
System.out.println("Exception while taking screenshot "+e.getMessage());
return e.getMessage();
}
}
}
我收到以下错误
**截屏时出现异常 null
我对 Selenium 和 TestNG 很陌生,所以我基本上是在找人来阐明这一点,并给我一些正确方向的指导,并了解我的代码实际发生了什么错误。
提前致谢。
我目前正在使用这种方法在我的测试中截取屏幕截图:
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;
public void takeScreenShot(WebDriver driver, String methodName) {
File screenshotFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshotFile,new
File("Screenshots\"+methodName+" "+GetTimeStampValue()+".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
以及获取时间戳的方法:
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
public String GetTimeStampValue()throws IOException{
Calendar cal = Calendar.getInstance();
Date time=cal.getTime();
String timestamp=time.toString();
String systime=timestamp.replace(":", "-");
return systime;
}
我为我的代码使用了错误版本的 ExtentReport。
我使用的代码是针对较新版本的 ExtentReport,而我拥有的 Maven 依赖项指向旧版本。当我将依赖项更新到最新版本时,代码本身运行良好。
Robot r=new Robot();
r.keyPress(KeyEvent.VK_PRINTSCREEN);
r.keyRelease(KeyEvent.VK_PRINTSCREEN);
我目前正在从事一个自动化项目,我创建了这个简单的程序来报告测试信息并截取屏幕截图。
package ReportTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import ReportTest.Utitlity;
public class ReportTest {
ExtentReports report;
ExtentTest logger;
WebDriver driver;
@Test
public void verifyBlogTitle()
{
report =new ExtentReports("C:\Users\reganc3\desktop\report\LearnAutomation.html");
logger=report.startTest("VerifyBlogTitle");
driver = new FirefoxDriver();
driver.manage().window().maximize();
logger.log(LogStatus.INFO, "Browser started");
driver.get("http://www.learn-automation.com");
logger.log(LogStatus.INFO, "Application is up and running");
String title = driver.getTitle();
Assert.assertTrue(title.contains("Selenium"));
logger.log(LogStatus.PASS, "Title Verified");
}
@AfterMethod
public void tearDown(ITestResult result)
{
if(result.getStatus()==ITestResult.FAILURE)
{
String screenshot_path = Utitlity.captureScreenshot(driver, result.getName());
logger.log(LogStatus.FAIL, "TitleVerification", screenshot_path);
}
report.endTest(logger);
report.flush();
driver.get("C:\Users\reganc3\desktop\report\LearnAutomation.html");
}
}
这是我的实用程序 class,它被调用以截取屏幕截图
package ReportTest;
import java.io.File;
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.WebDriverException;
import java.util.*;
public class Utitlity {
public static String captureScreenshot(WebDriver driver, String screenshotName)
{
try
{
TakesScreenshot ts=(TakesScreenshot)driver;
File source=ts.getScreenshotAs(OutputType.FILE);
String dest = " C:\Users\reganc3\desktop\ " +screenshotName+ ".png";
File destination=new File(dest);
FileUtils.copyFile(source, destination);
System.out.println("Screenshot taken");
return dest;
}
catch (Exception e)
{
System.out.println("Exception while taking screenshot "+e.getMessage());
return e.getMessage();
}
}
}
我收到以下错误
**截屏时出现异常 null
我对 Selenium 和 TestNG 很陌生,所以我基本上是在找人来阐明这一点,并给我一些正确方向的指导,并了解我的代码实际发生了什么错误。
提前致谢。
我目前正在使用这种方法在我的测试中截取屏幕截图:
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;
public void takeScreenShot(WebDriver driver, String methodName) {
File screenshotFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshotFile,new
File("Screenshots\"+methodName+" "+GetTimeStampValue()+".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
以及获取时间戳的方法:
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
public String GetTimeStampValue()throws IOException{
Calendar cal = Calendar.getInstance();
Date time=cal.getTime();
String timestamp=time.toString();
String systime=timestamp.replace(":", "-");
return systime;
}
我为我的代码使用了错误版本的 ExtentReport。
我使用的代码是针对较新版本的 ExtentReport,而我拥有的 Maven 依赖项指向旧版本。当我将依赖项更新到最新版本时,代码本身运行良好。
Robot r=new Robot();
r.keyPress(KeyEvent.VK_PRINTSCREEN);
r.keyRelease(KeyEvent.VK_PRINTSCREEN);