如何将屏幕截图附加到Java Selenium中的Extent报告

huangapple go评论122阅读模式
英文:

How to attach screenshot to Extent Report in java selenium

问题

以下是翻译好的代码部分:

  1. package com.qa.ExtentReportListener;
  2. import java.io.IOException;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.testng.IReporter;
  8. import org.testng.IResultMap;
  9. import org.testng.ISuite;
  10. import org.testng.ISuiteResult;
  11. import org.testng.ITestContext;
  12. import org.testng.ITestResult;
  13. import org.testng.Reporter;
  14. import org.testng.xml.XmlSuite;
  15. import com.aventstack.extentreports.ExtentReports;
  16. import com.aventstack.extentreports.ExtentTest;
  17. import com.aventstack.extentreports.Status;
  18. import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
  19. import com.aventstack.extentreports.reporter.configuration.ChartLocation;
  20. import com.aventstack.extentreports.reporter.configuration.Theme;
  21. import com.crm.qa.util.TestUtil;
  22. public class ExtentTestNGIReporterListener implements IReporter {
  23. private static final String OUTPUT_FOLDER = "test-output/";
  24. private static final String FILE_NAME = "Extent.html";
  25. private ExtentReports extent;
  26. private ExtentTest test;
  27. public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
  28. init();
  29. for (ISuite suite : suites) {
  30. Map<String, ISuiteResult> result = suite.getResults();
  31. for (ISuiteResult r : result.values()) {
  32. ITestContext context = r.getTestContext();
  33. buildTestNodes(context.getFailedTests(), Status.FAIL);
  34. buildTestNodes(context.getSkippedTests(), Status.SKIP);
  35. buildTestNodes(context.getPassedTests(), Status.PASS);
  36. }
  37. }
  38. for (String s : Reporter.getOutput()) {
  39. extent.setTestRunnerOutput(s);
  40. }
  41. extent.flush();
  42. }
  43. private void init() {
  44. ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
  45. htmlReporter.config().setDocumentTitle("ExtentReports - Created by TestNG Listener");
  46. htmlReporter.config().setReportName("ExtentReports - Created by TestNG Listener");
  47. htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
  48. htmlReporter.config().setTheme(Theme.STANDARD);
  49. extent = new ExtentReports();
  50. extent.attachReporter(htmlReporter);
  51. extent.setReportUsesManualConfiguration(true);
  52. }
  53. private void buildTestNodes(IResultMap tests, Status status) {
  54. if (tests.size() > 0) {
  55. for (ITestResult result : tests.getAllResults()) {
  56. test = extent.createTest(result.getMethod().getMethodName());
  57. for (String group : result.getMethod().getGroups())
  58. test.assignCategory(group);
  59. if (result.getThrowable() != null) {
  60. test.log(status, result.getThrowable());
  61. } else {
  62. test.log(status, "Test " + status.toString().toLowerCase() + "ed");
  63. }
  64. test.getModel().setStartTime(getTime(result.getStartMillis()));
  65. test.getModel().setEndTime(getTime(result.getEndMillis()));
  66. }
  67. }
  68. }
  69. // ...(略去了下面的 down 方法以及日期处理函数 getTime)
  70. }
  1. // 在另一个类中
  2. public static String takeScreenshotAtEndOfTest() throws IOException {
  3. String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
  4. TakesScreenshot ts = (TakesScreenshot) driver;
  5. File source = ts.getScreenshotAs(OutputType.FILE);
  6. String destination = System.getProperty("user.dir") + "/screenshots/" + dateName + ".png";
  7. File finalDestination = new File(destination);
  8. FileHandler.copy(source, finalDestination);
  9. return destination;
  10. }

请注意,以上只是对你提供的代码的翻译,其中可能包含代码注释和一些特定于代码功能的术语。如果需要进一步的解释或有任何问题,请随时提问。

英文:

I am trying to attach screenshot for failed testcases from my path to Extent Report but somehow i am not able to attach into it.

I have tried my possible solution but it fails .
I have used extent report version 3

here is mine full code in separate extenreport class :

  1. package com.qa.ExtentReportListener;
  2. import java.io.IOException;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.testng.IReporter;
  8. import org.testng.IResultMap;
  9. import org.testng.ISuite;
  10. import org.testng.ISuiteResult;
  11. import org.testng.ITestContext;
  12. import org.testng.ITestResult;
  13. import org.testng.Reporter;
  14. import org.testng.xml.XmlSuite;
  15. import com.aventstack.extentreports.ExtentReports;
  16. import com.aventstack.extentreports.ExtentTest;
  17. import com.aventstack.extentreports.Status;
  18. import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
  19. import com.aventstack.extentreports.reporter.configuration.ChartLocation;
  20. import com.aventstack.extentreports.reporter.configuration.Theme;
  21. import com.crm.qa.util.TestUtil;
  22. public class ExtentTestNGIReporterListener implements IReporter {
  23. private static final String OUTPUT_FOLDER = &quot;test-output/&quot;;
  24. private static final String FILE_NAME = &quot;Extent.html&quot;;
  25. private ExtentReports extent;
  26. private ExtentTest test;
  27. public void generateReport(List&lt;XmlSuite&gt; xmlSuites, List&lt;ISuite&gt; suites, String outputDirectory) {
  28. init();
  29. for (ISuite suite : suites) {
  30. Map&lt;String, ISuiteResult&gt; result = suite.getResults();
  31. for (ISuiteResult r : result.values()) {
  32. ITestContext context = r.getTestContext();
  33. buildTestNodes(context.getFailedTests(), Status.FAIL);
  34. buildTestNodes(context.getSkippedTests(), Status.SKIP);
  35. buildTestNodes(context.getPassedTests(), Status.PASS);
  36. }
  37. }
  38. for (String s : Reporter.getOutput()) {
  39. extent.setTestRunnerOutput(s);
  40. }
  41. extent.flush();
  42. }
  43. private void init() {
  44. ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(OUTPUT_FOLDER + FILE_NAME);
  45. htmlReporter.config().setDocumentTitle(&quot;ExtentReports - Created by TestNG Listener&quot;);
  46. htmlReporter.config().setReportName(&quot;ExtentReports - Created by TestNG Listener&quot;);
  47. htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
  48. htmlReporter.config().setTheme(Theme.STANDARD);
  49. extent = new ExtentReports();
  50. extent.attachReporter(htmlReporter);
  51. extent.setReportUsesManualConfiguration(true);
  52. }
  53. private void buildTestNodes(IResultMap tests, Status status) {
  54. if (tests.size() &gt; 0) {
  55. for (ITestResult result : tests.getAllResults()) {
  56. test = extent.createTest(result.getMethod().getMethodName());
  57. for (String group : result.getMethod().getGroups())
  58. test.assignCategory(group);
  59. if (result.getThrowable() != null) {
  60. test.log(status, result.getThrowable());
  61. }
  62. else {
  63. test.log(status, &quot;Test &quot; + status.toString().toLowerCase() + &quot;ed&quot;);
  64. }
  65. test.getModel().setStartTime(getTime(result.getStartMillis()));
  66. test.getModel().setEndTime(getTime(result.getEndMillis()));
  67. }
  68. }
  69. }
  70. public void down(ITestResult result) throws IOException{
  71. if(result.getStatus()==ITestResult.FAILURE){
  72. test.log(Status.FAIL, &quot;TEST CASE FAILED IS &quot;+result.getName()); //to add name in extent report
  73. test.log(Status.FAIL, &quot;TEST CASE FAILED IS &quot;+result.getThrowable()); //to add error/exception in extent report
  74. String screenshotPath = TestUtil.takeScreenshotAtEndOfTest();
  75. test.fail(&quot;Test Case failed check screenshot below&quot;+test.addScreenCaptureFromPath(screenshotPath));
  76. //extentTest.log(Status.FAIL, MediaEntityBuilder.createScreenCaptureFromPath(screenshotPath).build()); //to add screenshot in extent report
  77. //extentTest.fail(&quot;details&quot;).addScreenCaptureFromPath(screenshotPath);
  78. }
  79. else if(result.getStatus()==ITestResult.SKIP){
  80. test.log(Status.SKIP, &quot;Test Case SKIPPED IS &quot; + result.getName());
  81. }
  82. else if(result.getStatus()==ITestResult.SUCCESS){
  83. test.log(Status.PASS, &quot;Test Case PASSED IS &quot; + result.getName());
  84. }
  85. extent.flush();
  86. }
  87. private Date getTime(long millis) {
  88. Calendar calendar = Calendar.getInstance();
  89. calendar.setTimeInMillis(millis);
  90. return calendar.getTime();
  91. }
  92. }

here is my util class which contain screenshot method:

  1. public static String takeScreenshotAtEndOfTest() throws IOException {
  2. String dateName = new SimpleDateFormat(&quot;yyyyMMddhhmmss&quot;).format(new Date());
  3. TakesScreenshot ts = (TakesScreenshot)driver;
  4. File source = ts.getScreenshotAs(OutputType.FILE);
  5. String destination = System.getProperty(&quot;user.dir&quot;) + &quot;/screenshots/&quot; + dateName
  6. + &quot;.png&quot;;
  7. File finalDestination = new File(destination);
  8. FileHandler.copy(source, finalDestination);
  9. return destination;
  10. }

答案1

得分: 1

after 方法中使用以下代码:

  1. if (result.getStatus() == result.FAILURE || result.getStatus() == result.SKIP) {
  2. String screenshotPath = util.captureScreenshot(driver, result.getName());
  3. result.setAttribute("screenshotPath", screenshotPath); // 设置变量/属性 screenshotPath 的值为截图的路径
  4. }

并且在 buildtestnodes 中添加以下代码:

  1. if (result.getStatus() == result.FAILURE || result.getStatus() == result.SKIP) {
  2. String screenshotPath = (String) result.getAttribute("screenshotPath");
  3. test.log(status, test.addScreenCapture(screenshotPath));
  4. }
英文:

Use this code in after method

  1. if(result.getStatus()==result.FAILURE || result.getStatus()==result.SKIP) {
  2. String screenshotPath = util.captureScreenshot(driver, result.getName());
  3. result.setAttribute(&quot;screenshotPath&quot;, screenshotPath); //sets the value the variable/attribute screenshotPath as the path of the sceenshot
  4. }

and add below code in buildtestnodes

  1. if(result.getStatus()==result.FAILURE || result.getStatus()==result.SKIP) {
  2. String screenshotPath=(String)
  3. result.getAttribute(&quot;screenshotPath&quot;);
  4. test.log(status, test.addScreenCapture(screenshotPath));
  5. }

答案2

得分: 0

Sure, here's the translated content:

如果您想为失败的测试用例拍摄屏幕截图并使用测试类名,请使用以下代码段。

如何将屏幕截图附加到Java Selenium中的Extent报告

如何将屏幕截图附加到Java Selenium中的Extent报告

英文:

If you want take screen shots for failed test cases with test class name use below code segment.

如何将屏幕截图附加到Java Selenium中的Extent报告

如何将屏幕截图附加到Java Selenium中的Extent报告

答案3

得分: 0

Extent Report已经提供了获取屏幕截图的工具。请参考以下链接:

https://extentreports.com/docs/versions/3/java/#automatic-screenshot-management

同时找到截图和相应的代码:

  1. ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("extent.html");
  2. htmlReporter.config().setAutoCreateRelativePathMedia(true);
  3. test1.fail("details", MediaEntityBuilder.createScreenCaptureFromPath("1.png").build());
  4. test2.fail("details", MediaEntityBuilder.createScreenCaptureFromPath("2.png").build());

如何使用Extent Report进行截图

英文:

Extent Report already provide the utility to take the screenshot .Please refer the below link:

https://extentreports.com/docs/versions/3/java/#automatic-screenshot-management

also find the snapshot and code for same:

  1. ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(&quot;extent.html&quot;);
  2. htmlReporter.config().setAutoCreateRelativePathMedia(true);
  3. test1.fail(&quot;details&quot;, MediaEntityBuilder.createScreenCaptureFromPath(&quot;1.png&quot;).build());
  4. test2.fail(&quot;details&quot;, MediaEntityBuilder.createScreenCaptureFromPath(&quot;2.png&quot;).build());

how to take Screenshot using extent report

答案4

得分: 0

尝试使用以下代码:

  1. logger.log(Status.FAIL, logger.addScreenCaptureFromPath("YOUR PATH"));

更新:
如果这不起作用,只需使用标记将内部日志记录器转换。

  1. import com.aventstack.extentreports.markuputils.Markup;
英文:

Try using this :

  1. logger.log(Status.FAIL, logger.addScreenCaptureFromPath(&quot;YOUR PATH&quot;));

UPDATE :
If this doesn't work, just cast the inner logger with markup.
import com.aventstack.extentreports.markuputils.Markup;

答案5

得分: 0

  1. package com.helper;
  2. import org.testng.annotations.Test;
  3. import com.aventstack.extentreports.AnalysisStrategy;
  4. import com.aventstack.extentreports.ExtentReports;
  5. import com.aventstack.extentreports.ExtentTest;
  6. import com.aventstack.extentreports.MediaEntityBuilder;
  7. import com.aventstack.extentreports.Status;
  8. import com.aventstack.extentreports.reporter.ExtentSparkReporter;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.text.DateFormat;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Calendar;
  16. import java.util.Date;
  17. import org.apache.commons.codec.binary.Base64;
  18. import org.apache.commons.io.FileUtils;
  19. import org.openqa.selenium.By;
  20. import org.openqa.selenium.OutputType;
  21. import org.openqa.selenium.TakesScreenshot;
  22. import org.openqa.selenium.WebDriver;
  23. import org.openqa.selenium.chrome.ChromeDriver;
  24. import org.testng.ITestResult;
  25. import org.testng.annotations.AfterMethod;
  26. import org.testng.annotations.BeforeMethod;
  27. import org.testng.annotations.Test;
  28. public class TestingEdgeDriver {
  29. ExtentReports extent;
  30. ExtentTest test;
  31. WebDriver driver;
  32. ExtentTest Parent;
  33. ExtentTest child1, child;
  34. @BeforeMethod
  35. public void setup() {
  36. DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH-mm-ss");
  37. String destDir = dateFormat.format(new Date());
  38. ExtentSparkReporter esp = new ExtentSparkReporter(System.getProperty("user.dir") +
  39. "/ExtentReport/ExtentReports_" + destDir + "/SwarupExtentReport.html");
  40. extent = new ExtentReports();
  41. extent.attachReporter(esp);
  42. extent.setAnalysisStrategy(AnalysisStrategy.SUITE);
  43. }
  44. @Test(testName = "Chrome browser Testing")
  45. public void chromeBrowser() throws IOException {
  46. Parent = extent.createTest("CMO");
  47. child1 = Parent.createNode("Test1");
  48. child = child1.createNode("Chrome browser Testing");
  49. System.out.println("The tread value for Chrome browser is " + Thread.currentThread().getId());
  50. System.setProperty("webdriver.chrome.driver", "E:\\chromedriver_win32 (2)\\chromedriver.exe");
  51. driver = new ChromeDriver();
  52. child.log(Status.PASS, "Chrome browser has opened",
  53. MediaEntityBuilder.createScreenCaptureFromPath(capture(driver)).build());
  54. driver.get("https://www.icicibank.com");
  55. child.log(Status.PASS, "Expected was its should open the bank website",
  56. MediaEntityBuilder.createScreenCaptureFromPath(capture(driver)).build());
  57. child.log(Status.PASS, "Need to open the URL " + "http://www.icicibank.com");
  58. driver.manage().window().maximize();
  59. child.log(Status.PASS, "Test Case is passed",
  60. MediaEntityBuilder.createScreenCaptureFromPath(capture(driver)).build());
  61. }
  62. @AfterMethod
  63. public void getResult(ITestResult result) {
  64. if (result.getStatus() == ITestResult.SUCCESS) {
  65. child.log(Status.PASS, "Test case is passed " + result.getStatus() + " " + result.getTestClass() + " " + result.getName());
  66. child.log(Status.PASS, "Test case is passed " + result.getTestName());
  67. }
  68. if (result.getStatus() == ITestResult.FAILURE) {
  69. child.log(Status.FAIL, "Test case is failed at below location " + result.getThrowable());
  70. }
  71. extent.flush();
  72. }
  73. public static String captureBase64(WebDriver driver) throws IOException {
  74. String encodedBase64 = null;
  75. FileInputStream fileInputStream = null;
  76. File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  77. File Dest = new File("src/../BStackImages/" + System.currentTimeMillis() + ".png");
  78. String errflpath = Dest.getAbsolutePath();
  79. FileUtils.copyFile(scrFile, Dest);
  80. try {
  81. fileInputStream = new FileInputStream(Dest);
  82. byte[] bytes = new byte[(int) Dest.length()];
  83. fileInputStream.read(bytes);
  84. encodedBase64 = new String(Base64.encodeBase64(bytes));
  85. } catch (FileNotFoundException e) {
  86. e.printStackTrace();
  87. }
  88. return "data:image/png;base64," + encodedBase64;
  89. }
  90. public static String capture(WebDriver driver) throws IOException {
  91. File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  92. File Dest = new File("src/../BStackImages/" + System.currentTimeMillis() + ".png");
  93. String errflpath = Dest.getAbsolutePath();
  94. FileUtils.copyFile(scrFile, Dest);
  95. return errflpath;
  96. }
  97. }
英文:
  1. package com.helper;
  2. import org.testng.annotations.Test;
  3. import com.aventstack.extentreports.AnalysisStrategy;
  4. import com.aventstack.extentreports.ExtentReports;
  5. import com.aventstack.extentreports.ExtentTest;
  6. import com.aventstack.extentreports.MediaEntityBuilder;
  7. import com.aventstack.extentreports.Status;
  8. import com.aventstack.extentreports.reporter.ExtentSparkReporter;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.text.DateFormat;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Calendar;
  16. import java.util.Date;
  17. import org.apache.commons.codec.binary.Base64;
  18. import org.apache.commons.io.FileUtils;
  19. import org.openqa.selenium.By;
  20. import org.openqa.selenium.OutputType;
  21. import org.openqa.selenium.TakesScreenshot;
  22. import org.openqa.selenium.WebDriver;
  23. import org.openqa.selenium.chrome.ChromeDriver;
  24. import org.openqa.selenium.edge.EdgeDriver;
  25. import org.openqa.selenium.firefox.FirefoxDriver;
  26. import org.openqa.selenium.ie.InternetExplorerDriver;
  27. import org.openqa.selenium.opera.OperaDriver;
  28. import org.testng.IResultMap;
  29. import org.testng.ITest;
  30. import org.testng.ITestContext;
  31. import org.testng.ITestResult;
  32. import org.testng.Reporter;
  33. import org.testng.annotations.AfterMethod;
  34. import org.testng.annotations.BeforeMethod;
  35. import org.testng.annotations.Listeners;
  36. import org.testng.annotations.Test;
  37. public class TestingEdgeDriver {
  38. ExtentReports extent;
  39. ExtentTest test;
  40. WebDriver driver;
  41. ExtentTest Parent;
  42. ExtentTest child1,child;
  43. @BeforeMethod
  44. public void setup(){
  45. DateFormat dateFormat = new SimpleDateFormat(&quot;yyyy.MM.dd HH-mm-ss&quot;);
  46. String destDir = dateFormat.format(new Date());
  47. ExtentSparkReporter esp=new ExtentSparkReporter(System.getProperty(&quot;user.dir&quot;)+&quot;/ExtentReport/ExtentReports_&quot;+destDir+&quot;/SwarupExtentReport.html&quot;);
  48. extent=new ExtentReports();
  49. extent.attachReporter(esp);
  50. extent.setAnalysisStrategy(AnalysisStrategy.SUITE);
  51. }
  52. @Test (testName=&quot;Chrome browser Testing&quot;)public void chromeBrowser() throws IOException{
  53. /*extent.attachReporter(spark);
  54. extent.createTest(&quot;chromeBrowser&quot;).log(Status.PASS , &quot;This is logging event for the setup and it is passed&quot;);
  55. extent.flush();*/
  56. Parent=extent.createTest(&quot;CMO&quot;);
  57. child1=Parent.createNode(&quot;Test1&quot;);
  58. child=child1.createNode(&quot;Chrome browser Testing&quot;);
  59. System.out.println(&quot;The tread value for Chrome browser is &quot;+ Thread.currentThread().getId());
  60. System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;E:\\chromedriver_win32 (2)\\chromedriver.exe&quot;);
  61. driver=new ChromeDriver();
  62. child.log(Status.PASS, &quot;Chrome browser has opened&quot;,MediaEntityBuilder.createScreenCaptureFromPath(capture(driver)).build());
  63. driver.get(&quot;https://www.icicibank.com&quot;);
  64. child.log(Status.PASS,&quot;Expected was its should open the bank website&quot;,MediaEntityBuilder.createScreenCaptureFromPath(capture(driver)).build());
  65. child.log(Status.PASS, &quot;Need to open the URL &quot;+&quot; http://www.icicibank.com&quot;);
  66. driver.manage().window().maximize();
  67. child.log(Status.PASS, &quot;Test Case is passed&quot;,MediaEntityBuilder.createScreenCaptureFromPath(capture(driver)).build());
  68. }
  69. @AfterMethod
  70. public void getResult(ITestResult result){
  71. if(result.getStatus()==ITestResult.SUCCESS){
  72. child.log(Status.PASS, &quot;Test case is passed &quot;+result.getStatus()+&quot; &quot;+result.getTestClass()+&quot; &quot;+result.getName());
  73. child.log(Status.PASS, &quot;Test case is passed &quot;+result.getTestName());
  74. }
  75. if(result.getStatus()==ITestResult.FAILURE){
  76. child.log(Status.FAIL, &quot;Test case is failed at below location &quot;+result.getThrowable());
  77. }
  78. extent.flush();
  79. }
  80. /*@Test public void operaTesting(){
  81. System.out.println(&quot;The tread value for Opera browser is &quot;+ Thread.currentThread().getId());
  82. System.setProperty(&quot;webdriver.opera.driver&quot;,&quot;E:\\operadriver_win32\\operadriver_win32\\operadriver.exe&quot;);
  83. Reporter.log(&quot;opera driver has been set&quot;,true);
  84. driver=new OperaDriver();
  85. driver.manage().window().maximize();
  86. driver.get(&quot;https://www.irctc.co.in&quot;);
  87. }
  88. @Test public void FirefoxTesting(){
  89. System.out.println(&quot;The tread value for Firefox browser is &quot;+ Thread.currentThread().getId());
  90. System.setProperty(&quot;webdriver.gecko.driver&quot;,&quot;E:\\gecodriver\\geckodriver-v0.29.0-win32\\geckodriver.exe&quot;);
  91. Reporter.log(&quot;Gecko Driver has been set&quot;,true);
  92. driver=new FirefoxDriver();
  93. driver.manage().window().maximize();
  94. Reporter.log(&quot;firefox driver has been initialsed&quot;,true);
  95. driver.get(&quot;https://www.primevideo.com/&quot;);
  96. }
  97. @Test public void InternetExplorerTesting(){
  98. System.out.println(&quot;The tread value for IE browser is &quot;+ Thread.currentThread().getId());
  99. System.setProperty(&quot;webdriver.ie.driver&quot;,&quot;E:\\IEDriverServer_Win32_3.4.0\\IEDriverServer.exe&quot;);
  100. Reporter.log(&quot;IE driver has been set&quot;,true);
  101. driver=new InternetExplorerDriver();
  102. driver.manage().window().maximize();
  103. driver.get(&quot;https://www.hotstar.com/in&quot;);
  104. }*/
  105. public static String captureBase64(WebDriver driver) throws IOException {
  106. String encodedBase64 = null;
  107. FileInputStream fileInputStream = null;
  108. File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  109. File Dest = new File(&quot;src/../BStackImages/&quot; + System.currentTimeMillis()
  110. + &quot;.png&quot;);
  111. String errflpath = Dest.getAbsolutePath();
  112. FileUtils.copyFile(scrFile, Dest);
  113. try {
  114. fileInputStream =new FileInputStream(Dest);
  115. byte[] bytes =new byte[(int)Dest.length()];
  116. fileInputStream.read(bytes);
  117. encodedBase64 = new String(Base64.encodeBase64(bytes));
  118. }catch (FileNotFoundException e){
  119. e.printStackTrace();
  120. }
  121. return &quot;data:image/png;base64,&quot;+encodedBase64;
  122. }
  123. public static String capture(WebDriver driver) throws IOException {
  124. File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  125. File Dest = new File(&quot;src/../BStackImages/&quot; + System.currentTimeMillis()
  126. + &quot;.png&quot;);
  127. String errflpath = Dest.getAbsolutePath();
  128. FileUtils.copyFile(scrFile, Dest);
  129. return errflpath;
  130. }
  131. }

答案6

得分: -1

使用以下的Java示例:

  1. WebDriver driver = new FirefoxDriver();
  2. driver.get("http://www.google.com/");
  3. // 将截图存储在当前项目目录中。
  4. String screenShot = System.getProperty("user.dir") + "\\Artifacts\\FileName.png";
  5. // 调用Webdriver来点击截图。
  6. File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  7. // 保存截图。
  8. FileUtils.copyFile(scrFile, new File(screenShot));
英文:

Use this following example is in Java

  1. WebDriver driver = new FirefoxDriver();
  2. driver.get(&quot;http://www.google.com/&quot;);
  3. // Store the screenshot in current project dir.
  4. String screenShot = System.getProperty(&quot;user.dir&quot;)+&quot;\\Artifacts\\FileName.png&quot;;
  5. // Call Webdriver to click the screenshot.
  6. File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  7. // Save the screenshot.
  8. FileUtils.copyFile(scrFile, new File(screenShot));

huangapple
  • 本文由 发表于 2020年4月7日 12:47:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61073005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定