<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 在 Selenium WebDriver 中自定義 PDF &電子郵件 TestNG 報告 > 原文: [https://www.guru99.com/pdf-emails-and-screenshot-of-test-reports-in-selenium.html](https://www.guru99.com/pdf-emails-and-screenshot-of-test-reports-in-selenium.html) 在研究其他內容之前,我們首先要了解- ## 為什么我們需要報告? 當我們使用 Selenium 或任何其他自動化工具時,我們正在 Web 應用程序上執行操作。 但是,我們自動化的目的不僅僅是練習被測應用程序。 作為自動化測試人員,我們應該測試應用程序,查找錯誤并將其報告給開發團隊或更高級別的管理層。 在這里,報告對于軟件[測試](/software-testing.html)流程非常重要 ## TestNG 報告 TestNG 庫提供了非常方便的報告功能。 執行后, [Testng](/all-about-testng-and-selenium.html) 將在項目的根目錄下生成一個 test-output 文件夾。 此文件夾包含兩種類型的報告- **Index.html:**這是當前執行的完整報告,其中包含諸如錯誤,組,時間,報告程序日志,測試 XML 文件的信息。 ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/63/8c/638c6bd95eae70e231462dbc80d3e040_622x363.png "PDF , Emails and Screenshot of Test Reports in Selenium") **emailable-report.html:**這是當前測試執行的摘要報告,其中以綠色(通過測試用例)和紅色(對于失敗測試用例)突出顯示[測試用例](/test-case.html)消息。 。 ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/0f/96/0f9663ef2d186c78908392e029b74d31_548x208.png "PDF , Emails and Screenshot of Test Reports in Selenium") ## 如何自定義 TestNG 報告 TestNG 的報告非常方便,但是有時候,我們在報告中需要的數據更少,或者想要以 pdf,excel 等其他格式顯示報告,或者想要更改報告的布局。 我們可以通過兩種方式自定義 TestNG 報告 * 使用 ITestListener 接口: * 使用 IReporter 界面: ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/94/39/94398bf0c195fbfb9c6a77a999e93f59_449x299.png "PDF , Emails and Screenshot of Test Reports in Selenium") ### ITestListener 接口 當需要自定義實時報告時,可以使用此界面。 換句話說,如果我們要在 TetNG 套件中執行大量測試用例,并且想要獲取每個測試用例的報告,那么在每個測試用例之后,我們都需要實現 ITestListener 接口。 該接口將覆蓋 onTestFailure,onTestStart,onTestSkipped 方法以發送當前測試用例的正確狀態。 ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/c6/a5/c6a53ed5fa854a5bbe7267e7ab905ec9_430x158.png "PDF , Emails and Screenshot of Test Reports in Selenium") 這是我們將要遵循的步驟 * 創建一個名為 RealGuru99Report 的類,并在其中實現 iTestListener。 * 實現 iTestListener 的方法 * 創建測試方法,并將 RealGuru99Report 類添加為 Test Method 類中的偵聽器。 ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/f5/58/f558acda64e900b8f818b25f6951ffb9_322x141.png "PDF , Emails and Screenshot of Test Reports in Selenium") **代碼示例** RealGuru99TimeReport.java 是實時報告類。 它將實現 ITestListener 接口進行報告 ``` package testNGReport.realTimeReport; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; public class RealGuru99TimeReport implements ITestListener{ @Override public void onStart(ITestContext arg0) { System.out.println("Start Of Execution(TEST)->"+arg0.getName()); } @Override public void onTestStart(ITestResult arg0) { System.out.println("Test Started->"+arg0.getName()); } @Override public void onTestSuccess(ITestResult arg0) { System.out.println("Test Pass->"+arg0.getName()); } @Override public void onTestFailure(ITestResult arg0) { System.out.println("Test Failed->"+arg0.getName()); } @Override public void onTestSkipped(ITestResult arg0) { System.out.println("Test Skipped->"+arg0.getName()); } @Override public void onFinish(ITestContext arg0) { System.out.println("END Of Execution(TEST)->"+arg0.getName()); } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) { // TODO Auto-generated method stub } } ``` **TestGuru99RealReport.java 是真實報告**的測試用例 ``` package testNGReport.realTimeReport; import org.testng.Assert; import org.testng.annotations.Listeners; import org.testng.annotations.Test; @Listeners(RealGuru99TimeReport.class) public class TestGuru99RealReport { @Test public void testRealReportOne(){ Assert.assertTrue(true); } @Test public void testRealReportTwo(){ Assert.assertTrue(false); } //Test case depends on failed testcase= testRealReportTwo @Test(dependsOnMethods="testRealReportTwo") public void testRealReportThree(){ } } ``` 輸出看起來像- ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/16/b4/16b4f28bda36f48f5d20881e289a29d8_624x383.png "PDF , Emails and Screenshot of Test Reports in Selenium") ### IReporter 界面 如果要自定義 TestNG 生成的最終測試報告,則需要實現 IReporter 接口。 該接口只有一種方法可以實現 generateReport。 此方法在列表< ISuite >中具有完整測試執行的所有信息,我們可以使用它生成報告。 **Code Example** **Guru99Reporter.java 是用于自定義報告**的文件 ``` package testNGReport.iReporterReport; import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; import org.testng.IReporter; import org.testng.IResultMap; import org.testng.ISuite; import org.testng.ISuiteResult; import org.testng.ITestContext; import org.testng.ITestNGMethod; import org.testng.xml.XmlSuite; public class Guru99Reporter implements IReporter{ @Override public void generateReport(List<XmlSuite> arg0, List<ISuite> arg1, String outputDirectory) { // Second parameter of this method ISuite will contain all the suite executed. for (ISuite iSuite : arg1) { //Get a map of result of a single suite at a time Map<String,ISuiteResult> results = iSuite.getResults(); //Get the key of the result map Set<String> keys = results.keySet(); //Go to each map value one by one for (String key : keys) { //The Context object of current result ITestContext context = results.get(key).getTestContext(); //Print Suite detail in Console System.out.println("Suite Name->"+context.getName() + "::Report output Ditectory->"+context.getOutputDirectory() +"::Suite Name->"+ context.getSuite().getName() +"::Start Date Time for execution->"+context.getStartDate() +"::End Date Time for execution->"+context.getEndDate()); //Get Map for only failed test cases IResultMap resultMap = context.getFailedTests(); //Get method detail of failed test cases Collection<ITestNGMethod> failedMethods = resultMap.getAllMethods(); //Loop one by one in all failed methods System.out.println("--------FAILED TEST CASE---------"); for (ITestNGMethod iTestNGMethod : failedMethods) { //Print failed test cases detail System.out.println("TESTCASE NAME->"+iTestNGMethod.getMethodName() +"\nDescription->"+iTestNGMethod.getDescription() +"\nPriority->"+iTestNGMethod.getPriority() +"\n:Date->"+new Date(iTestNGMethod.getDate())); } } } } } ``` **TestGuru99ForReporter.java 是自定義報告的演示** ``` package testNGReport.iReporterReport; import org.testng.Assert; import org.testng.annotations.Listeners; import org.testng.annotations.Test; //Add listener to listen report and write it when testcas finished @Listeners(value=Guru99Reporter.class) public class TestGuru99ForReporter { @Test(priority=0,description="testReporterOne") public void testReporterOne(){ //Pass test case Assert.assertTrue(true); } @Test(priority=1,description="testReporterTwo") public void testReporterTwo(){ //Fail test case Assert.assertTrue(false); } } ``` 輸出將像- ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/33/47/334768f9451d680861841949c54052f5_624x294.png "PDF , Emails and Screenshot of Test Reports in Selenium") ## PDF 和報告電子郵件 上面的報告實現非常簡單明了,可幫助您開始進行報告定制。 但是在公司環境中,您將需要創建高度定制的報告。 這是我們將要處理的方案 1. 以 PDF 格式創建自定義報告 2. 僅截取有關錯誤的屏幕截圖。 鏈接到 PDF 屏幕截圖 3. 發送 PDF 電子郵件 PDF 報告如下所示 ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/fc/35/fc35c0730ae7c27d0b892619679b3c76_624x359.png "PDF , Emails and Screenshot of Test Reports in Selenium") 要創建 pdf 報告,我們需要一個 [Java](/java-tutorial.html) API, **IText** 。 在處下載[。 還有另一個自定義偵聽器類,它實際上是在實現此 IText jar 并為我們創建 pdf 報告。 在此處下載](http://mirrors.ibiblio.org/pub/mirrors/maven2/com/lowagie/itext/2.1.7/itext-2.1.7.jar)[](https://groups.google.com/group/testng-users/attach/3bc22fcb3e8cc39c/JyperionListener.java?part=0.1&authuser=0) 上圖顯示了生成的 PDF 報告的默認格式。 您可以自定義 這是我們將如何處理的方法 步驟 1)創建一個基類 步驟 2)自定義 JypersionListerner.Java(PDF 創建代碼) 步驟 3)創建一個 TestGuru99PDFEmail.java,它將執行測試用例,創建 PDF 步驟 4)將代碼附加到 TestGuru99PDFEmail.java 以通過電子郵件發送 PDF 報告 **讓我們看一下這些步驟** 步驟 1)創建基類 該基類具有創建 WebDriver 和獲取屏幕截圖的功能 ``` package PDFEmail; 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.firefox.FirefoxDriver; public class BaseClass { static WebDriver driver; public static WebDriver getDriver(){ if(driver==null){ WebDriver driver ; System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe"); driver = new FirefoxDriver(); } return driver; } /** * This function will take screenshot * @param webdriver * @param fileWithPath * @throws Exception */ public static void takeSnapShot(WebDriver webdriver,String fileWithPath) throws Exception{ //Convert web driver object to TakeScreenshot TakesScreenshot scrShot =((TakesScreenshot)webdriver); //Call getScreenshotAs method to create image file File SrcFile=scrShot.getScreenshotAs(OutputType.FILE); //Move image file to new destination File DestFile=new File(fileWithPath); //Copy file at destination FileUtils.copyFile(SrcFile, DestFile); } } ``` 步驟 2)自定義 JypersionListener.java 我們將堅持默認的報告格式。 但是我們將進行 2 種自定義 * 添加代碼以指示 JypersionListener 對錯誤進行截圖 * 將截圖截圖的鏈接附加到 PDF 報告中 ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/df/36/df369e96a90ace659fdf010e88b6dabc_624x209.png "PDF , Emails and Screenshot of Test Reports in Selenium") 添加代碼以將屏幕截圖附加到 PDF 報告 ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/b6/91/b69182abecbad00fc2804507b8057d8b_624x138.png "PDF , Emails and Screenshot of Test Reports in Selenium") 步驟 3)創建一個 TestGuru99PDFEmail.java,它將執行測試用例,創建 PDF * 在這里,我們將添加 JyperionListener.class 作為偵聽器 * 我們將執行 3 個測試用例。 * 使用 Assert.assertTrue,我們將僅通過一個而使 2 個測試用例失敗。 * 僅根據我們的定制為失敗的測試用例截屏 ``` package PDFEmail; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import org.openqa.selenium.WebDriver; import org.testng.Assert; import org.testng.annotations.AfterSuite; import org.testng.annotations.Listeners; import org.testng.annotations.Test; import reporter.JyperionListener; //Add listener for pdf report generation @Listeners(JyperionListener.class) public class TestGuru99PDFReport extends BaseClass { WebDriver driver; //Testcase failed so screen shot generate @Test public void testPDFReportOne(){ driver = BaseClass.getDriver(); driver.get("http://google.com"); Assert.assertTrue(false); } //Testcase failed so screen shot generate @Test public void testPDFReporTwo(){ driver = BaseClass.getDriver(); driver.get("http:/guru99.com"); Assert.assertTrue(false); } //Test test case will be pass, so no screen shot on it @Test public void testPDFReportThree(){ driver = BaseClass.getDriver(); driver.get("http://demo.guru99.com"); Assert.assertTrue(true); } ``` Step 4) Append code to TestGuru99PDFEmail.java to send PDF report via email * 我們將使用注釋@AfterSuite 發送 PDF 報告的電子郵件 * 我們將使用 Gmail 發送電子郵件 * 要啟用電子郵件,需要導入許多庫文件,例如 mail.jar,pop3.jar,smtpt.jar 等。 * 在執行此操作之前,請輸入收件人的電子郵件地址和密碼 ``` //After complete execution send pdf report by email @AfterSuite public void tearDown(){ sendPDFReportByGMail("This email address is being protected from spambots. You need JavaScript enabled to view it. ", "password", "This email address is being protected from spambots. You need JavaScript enabled to view it.", "PDF Report", ""); } /** * Send email using java * @param from * @param pass * @param to * @param subject * @param body */ private static void sendPDFReportByGMail(String from, String pass, String to, String subject, String body) { Properties props = System.getProperties(); String host = "smtp.gmail.com"; props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", host); props.put("mail.smtp.user", from); props.put("mail.smtp.password", pass); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props); MimeMessage message = new MimeMessage(session); try { //Set from address message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); //Set subject message.setSubject(subject); message.setText(body); BodyPart objMessageBodyPart = new MimeBodyPart(); objMessageBodyPart.setText("Please Find The Attached Report File!"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(objMessageBodyPart); objMessageBodyPart = new MimeBodyPart(); //Set path to the pdf report file String filename = System.getProperty("user.dir")+"\\Default test.pdf"; //Create data source to attach the file in mail DataSource source = new FileDataSource(filename); objMessageBodyPart.setDataHandler(new DataHandler(source)); objMessageBodyPart.setFileName(filename); multipart.addBodyPart(objMessageBodyPart); message.setContent(multipart); Transport transport = session.getTransport("smtp"); transport.connect(host, from, pass); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (AddressException ae) { ae.printStackTrace(); } catch (MessagingException me) { me.printStackTrace(); } } } ``` **在此處下載完整的項目** 注意:當我們單擊 pdf 中的屏幕快照鏈接時,它將顯示安全對話框。 我們必須允許此對話框打開 pdf。 ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/4e/e7/4ee794024b978d79bb6594efa2a34da2_624x279.png "PDF , Emails and Screenshot of Test Reports in Selenium") 這樣生成的電子郵件將如下所示 ![TestNG Report: Customization, PDF & Email of Report in Selenium WebDriver](https://img.kancloud.cn/49/d7/49d75d17458aefc3056871c848912e7a_624x446.png "PDF , Emails and Screenshot of Test Reports in Selenium") 摘要: * TestNG 具有內置的報告功能。 * 在完全執行測試用例之后,TestNG 在項目的根目錄中生成一個 test-output 文件夾。 * 在 test-output 文件夾中,有兩個主要報告,index.html 和 emailable-report.html。 * 要自定義 TestNG 報告,我們需要實現兩個接口 ITestListener 和 IReporter。 * 如果我們需要在兩次執行之間獲取報告,則需要 ITestListener。 * 為了在完成執行后創建最終報告,我們需要實現 IReporter。 * 截取屏幕截圖,在 Selenium WebDriver 中,我們需要將 cast WebDriver 鍵入到 TakesScreenShot 接口。 * 要生成 pdf 報告,我們需要在項目中添加 IText jar。 [在本教程中下載用于演示的 Selenium 項目文件](https://drive.google.com/uc?export=download&id=0B_vqvT0ovzHcUFVqWUJWRGpibmc)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看