<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 10G 高級 WebDriver – 將屏幕截圖保存到 Word 文檔 > 原文: [https://javabeginnerstutorial.com/selenium/10g-advanced-webdriver-saving-screenshots-to-word-document/](https://javabeginnerstutorial.com/selenium/10g-advanced-webdriver-saving-screenshots-to-word-document/) 嗨冠軍! 希望您度過愉快的時光[截屏并將其保存在本地](https://javabeginnerstutorial.com/selenium/10f-advanced-webdriver-taking-screenshot/)。 今天,讓我們看看如何創建 Word 文檔,并將在測試用例中捕獲的所有圖像插入其中。 每個測試用例都有一個單獨的文檔,不僅可以幫助我們保持工作空間井井有條,而且搜索特定的屏幕快照也很容易。 最好的部分是,我們將編寫代碼,以便所有這些事情自動發生而無需任何人工干預。 請允許我直截了當。 ## 步驟 1: 下載幾個 JAR,使我們的工作更加輕松。 **`java2word-3.3.jar`** - 幫助我們創建 Word 文檔并以所需方式對其進行操作。 **`xstream-1.3.1.jar`** – 處理圖片 讓我們繼續從 [https://code.google.com/archive/p/java2word/downloads](https://code.google.com/archive/p/java2word/downloads) (在撰寫本文時的下載位置)下載這兩個 JAR 文件。 我還將這些 JAR 以及本文中處理的所有其他代碼文件一起放在我們的 [GitHub 倉庫](https://github.com/JBTAdmin/Selenium)中。 ## 步驟 2: 將這些 JAR 添加到我們的項目構建路徑中。 之前,我們已經多次看到此過程,因此,我不再重復(有關詳細說明,請參閱[文章](https://javabeginnerstutorial.com/selenium/9b-webdriver-eclipse-setup/)的步驟 3)。 ## 步驟 3: 創建一個類“`SaveDocument.java`”。 將以下代碼添加到類文件中, ```java import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import word.api.interfaces.IDocument; import word.w2004.Document2004; import word.w2004.Document2004.Encoding; import word.w2004.elements.BreakLine; import word.w2004.elements.Heading1; import word.w2004.elements.Heading3; import word.w2004.elements.Image; import word.w2004.elements.Paragraph; import word.w2004.style.HeadingStyle.Align; public class SaveDocument { public static void createDoc(String testCaseName, String[] imgFileNames) { // Create a document object IDocument myDoc = new Document2004(); myDoc.encoding(Encoding.UTF_8); // Inserts one breakline myDoc.addEle(BreakLine.times(1).create()); // Add client logo to document header myDoc.getHeader().addEle(Image.from_FULL_LOCAL_PATHL("/Selenium/Logo.png") .setHeight("30") .setWidth("20") .getContent()); // Add Project name to document header myDoc.getHeader().addEle(Heading3.with(" ProjectName").withStyle().align(Align.RIGHT).create()); // Specify Test case name as document heading myDoc.addEle(Heading1.with(testCaseName + " Test Case").withStyle().align(Align.CENTER).create()); myDoc.addEle(BreakLine.times(1).create()); // Add a description paragraph myDoc.addEle(Paragraph .with("Following are the related screenshots") .create()); myDoc.addEle(BreakLine.times(1).create()); // Add company name to document footer myDoc.getFooter().addEle( Paragraph.with("@CompanyName").create()); // Loop through all the image files for(String file:imgFileNames){ // Insert each image file to the document myDoc.addEle(Image.from_FULL_LOCAL_PATHL( "/Selenium/screenshots/" + file + ".png") .setHeight("350") .setWidth("500") .getContent()); // Insert 2 linebreaks at the end of each inserted image myDoc.addEle(BreakLine.times(2).create()); } // Insert an image from web myDoc.addEle(Image .from_WEB_URL("http://www.google.com/images/logos/ps_logo2.png")); // Create the word document specifying a location File fileObj = new File("\\Selenium\\" + testCaseName + ".doc"); PrintWriter writer = null; try { writer = new PrintWriter(fileObj); } catch (FileNotFoundException e) { e.printStackTrace(); } String myWord = myDoc.getContent(); writer.println(myWord); writer.close(); // Print a confirmation image to console System.out.println("Word document created successfully!"); } } ``` 每行都提供了注釋,以使代碼易于說明。 `public static void createDoc(String testCaseName, String[] imgFileNames)` -此方法有兩個參數。 第一個是一個字符串,它指定測試用例的名稱。 這將是將要創建的單詞文檔的名稱。 第二個參數是作為該測試用例的一部分捕獲的所有屏幕快照名稱的數組。 在這種方法中,我們將創建一個文檔對象并執行諸如 * 添加標題,段落 * 在標題中添加客戶徽標和公司名稱 * 在頁腳中添加公司名稱 * 插入作為特定測試用例一部分捕獲的所有屏幕截圖 * 從網上插入圖片(只是為了證明這種情況也是可行的) * 將單詞文檔和測試用例的名稱保存在特定位置 ## 步驟 4: 對“`SaveScreenshot.java`”文件進行了一些修改。 對我們在[先前文章](https://javabeginnerstutorial.com/selenium/10f-advanced-webdriver-taking-screenshot/)中創建的“`SaveScreenshot.java`”類進行了一些更改。 1. 刪除生成時間戳的函數,并 2. 文件擴展名從“`.jpg`”更改為“`.png`” 現在的代碼看起來像這樣, ```java 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 class SaveScreenshot { public static void capture(String screenshotName, WebDriver driver) { // Cast driver object to TakesScreenshot TakesScreenshot screenshot = (TakesScreenshot) driver; // Get the screenshot as an image File File src = screenshot.getScreenshotAs(OutputType.FILE); try { // Specify the destination where the image will be saved File dest = new File("\\Selenium\\screenshots\\" + screenshotName + ".png"); // Copy the screenshot to destination FileUtils.copyFile(src, dest); } catch (IOException ex) { System.out.println(ex.getMessage()); } } } ``` ## 示例場景 1. 打開 Firefox 瀏覽器。 2. 導航到 Google 帳戶創建頁面 3. 通過 ID 找到名字文本框 4. 輸入“`fname01`”作為名字 5. 截取屏幕截圖,并將其命名為“`testCaseName+1`” 6. 按名稱找到姓氏文本框 7. 輸入“`lname01`”作為姓氏 8. 截取屏幕截圖并將其命名為“`testCaseName+2`” 9. 在指定位置創建一個 word 文檔,并將這兩個屏幕截圖都插入其中。 ### JUnit 代碼: `WordDocWithScreenshotTest.java`類 ```java import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import com.blog.utilities.SaveDocument; import com.blog.utilities.SaveScreenshot; public class WordDocWithScreenshotTest { //Declaring variables private WebDriver driver; private String baseUrl; private String testCaseName = "WordDocWithScreenshot"; @Before public void setUp() throws Exception{ // Selenium version 3 beta releases require system property set up System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\" + "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe"); // Create a new instance for the class FirefoxDriver // that implements WebDriver interface driver = new FirefoxDriver(); // Implicit wait for 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // Assign the URL to be invoked to a String variable baseUrl = "https://accounts.google.com/SignUp"; } @Test public void testPageTitle() throws Exception{ // Open baseUrl in Firefox browser window driver.get(baseUrl); // Locate First Name text box by id and // assign it to a variable of type WebElement WebElement firstName = driver.findElement(By.id("firstName")); // Clear the default placeholder or any value present firstName.clear(); // Enter/type the value to the text box firstName.sendKeys("fname01"); //Take a screenshot SaveScreenshot.capture(testCaseName + "1", driver); // Locate last name text box by name WebElement lastName = driver.findElement(By.name("lastName")); // Clear and enter a value lastName.clear(); lastName.sendKeys("lname01"); // Take a screenshot SaveScreenshot.capture(testCaseName + "2", driver); // Create a word document and include all screenshots SaveDocument.createDoc(testCaseName, new String[]{testCaseName + "1",testCaseName + "2"}); } @After public void tearDown() throws Exception{ // Close the Firefox browser driver.close(); } } ``` 如果遵循注釋,該代碼是不言自明的。 Eclipse 的輸出如下, ![Document Eclipse output](https://img.kancloud.cn/f7/87/f7872cf72a51d0ea55c0206a33954818_810x377.png) 在 Eclipse IDE 中,“JUnit”窗格清楚地顯示了測試用例“`WordDocWithScreenshotTest.java`”已通過,并且控制臺沒有錯誤。 按預期打印“Word 文檔創建成功”。 按照代碼中的指定,屏幕快照將以上述格式的名稱保存到“`E:/Selenium/screenshots`”路徑中。 ![Saved screenshots](https://img.kancloud.cn/6d/24/6d24b6b97a96e825a431452d5ecfeb92_389x239.png) 還將創建單詞文檔并將其保存在指定的位置“ `E:/Selenium/`”中。 該文件如下所示, ![Created Word Document](https://img.kancloud.cn/bf/62/bf62c3344b00df9e9ce94e41fa1c2ba5_1005x619.png) 創建的 Word 文檔,所有代碼文件和 JAR 文件都放置在 [GitHub 倉庫](https://github.com/JBTAdmin/Selenium)中,以便于訪問。 您可以為倉庫加注星標和分支以方便使用。 請仔細閱讀“`README.md`”文件以獲取明確說明。 編碼愉快! 祝你今天愉快!
                  <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>

                              哎呀哎呀视频在线观看