正如我们所知,知道测试失败背后的原因非常重要,我们需要在测试用例失败时捕获屏幕截图。测试失败背后的原因可能是我们自动化脚本中的应用程序或错误中的错误,以便了解在测试用例故障上捕获屏幕截图非常重要的确切问题。
我已经写了一篇关于如何拍摄截图的帖子,您可以在此引用该帖子。在此帖子中,我们将在测试用例故障时看到如何捕获屏幕截图。
1. iTestresult接口 - 这将为我们提供测试用例执行状态和测试案例名称。
2. @Aftermethod. - Testng的注释将在之后执行
每次测试执行都无论如何 测试案例通过或失败@aftermethod将
always execute.
让我们看看完整的代码 -
package captureScreenshot; // Import all classes and interface import java.io.File; import library.Utility; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.ITestResult; import org.testng.annotations.AfterMethod; import org.testng.annotations.Test; public class Facebook ScreenshotExample { // Create Webdriver reference WebDriver driver;
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe"); driver=new ChromeDriver();
@Test public void captureScreenshot() throws Exception { // Initiate Chrome browser // Maximize the browser driver.manage().window().maximize(); // Pass application url driver.get("http://www.facebook.com"); // Here we are forcefully passing wrong id so that it will fail our testcase driver.findElement(By.xpath(".//*[@id='emailasdasdas']")).sendKeys(" QA自动化 "); } // It will execute after every test execution @Aftermethod. public void tearDown(ITestResult result) { // Here will compare if test is failing then only it will enter into if condition if(ITestResult.FAILURE==result.getStatus()) { try { // Create refernce of TakesScreenshot TakesScreenshot ts=(TakesScreenshot)driver; // Call method to capture screenshot File source=ts.getScreenshotAs(OutputType.FILE); // Copy files to specific location here it will save all screenshot in our project home directory and // result.getName() will return name of test case so that screenshot name will be same FileUtils.copyFile(source, new File("./ScreenShots/"+result.getName()+".png")); System.out.println("Screenshot taken"); } catch (Exception e) { System.out.println("Exception while taking screenshot "+e.getMessage()); } } // close application driver.quit(); } }
非常有用的信息,谢谢与我们分享。 Marathahalli的Tally课程
回复 删除在使用Selenium Webdriver测试失败时,如何捕获屏幕截图时,我们是Web开发,网页设计和的专业人员 软件开发.
回复 删除Tally Course in Delhi, Noida & Gurgaon Online or Classroom at Best Fees in India. Choose a wide range of Advanced Course at 高科技解决方案. Get a Call & Take a Free Demo Classes-+91-9311002620, +91-11-40504400. More info-//www.htsindia.com/Courses/Tally/tally-training-course
回复 删除诺伊达德里的最佳课程研究所& Gurgaon
诺伊达德里的理论研究所&Gurgaon在德里,NCR中的最低费用。比较所有内容,位置,评论,反馈,当然是持续时间。现在打电话&获得免费演示课程 - + 91-9311002620,+ 91-11-40504400。
回复 删除更多信息- 诺伊达德里的理论研究所& Gurgaon
感到乐意阅读你的博客。这种在不同博客上写的这种概念内容总是欣赏。大多数积分可以从博客中学到。
回复 删除谢谢
联盟营销指南
非常有趣的内容!谢谢!!
回复 删除在这里打击 Azure培训