<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之旅 廣告
                # 10F 高級 WebDriver – 截屏 > 原文: [https://javabeginnerstutorial.com/selenium/10f-advanced-webdriver-taking-screenshot/](https://javabeginnerstutorial.com/selenium/10f-advanced-webdriver-taking-screenshot/) 嗨冠軍! 屏幕截圖。 在軟件測試的擁擠街道上,另一個經常聽到的術語。 如果您的環境中出現錯誤,而開發中沒有錯誤,則無法用截圖來證明,這是一個好測試! 因此,現在是時候該了解如何使用 Selenium WebDriver 來抓取了。 我知道您想到了幾個問題。 最重要的是,“如果是手動測試,那么我只要按一下鍵盤上的`PrntScr`按鈕,然后抓取一個漂亮的屏幕截圖即可。 但是,當我實現自動化時,如何獲得相同的結果?” 猜猜看,這很簡單! 只需按照 3 個步驟進行操作,您就可以使用屏幕截圖。 如果您像我一樣,可能會急切地想看看它在代碼中是如何工作的。 我的目標是取悅,所以請不要再拖延…… ## 步驟 1: 使用 Selenium WebDriver 提供的`TakesScreenshot`接口。 將 WebDriver 對象強制轉換為`TakesScreenshot`類型。 當您在`TakesScreenshot`下面看到一條彎曲的線時,只需單擊`import org.openqa.selenium.TakesScreenshot;`包,您將沒有任何錯誤。 ```java // Cast driver object to TakesScreenshot TakesScreenshot screenshot = (TakesScreenshot) driver; ``` ## 步驟 2: 要將屏幕截圖獲取為圖像文件,請調用“`getScreenshotAs`”方法。 波浪線? - 點擊: ```java import org.openqa.selenium.OutputType; import java.io.File; ``` ```java // Get the screenshot as an image File File src = screenshot.getScreenshotAs(OutputType.FILE); ``` ## 步驟 3: 將生成的圖像文件復制到您選擇的目標位置。 使用`FileUtils`類的`copyFile`方法可以輕松完成此操作。 重要的是要注意,此方法將引發`IOException`。 因此,作為一種好習慣,請將這段代碼包裝在`try-catch`塊中。 線條更彎曲? - 點擊: ```java import java.io.IOException; import org.apache.commons.io.FileUtils; import java.text.SimpleDateFormat; import java.util.Date; ``` 確保完全按照指定的方式導入包。 通常,您可能需要下載`org.apache.commons.io` jar(在撰寫本文時為[下載位置](https://jar-download.com/explore-java-source-code.php?a=commons-io&g=commons-io&v=2.5&downloadable=1))并將其添加到項目的構建路徑中。 之前,我們已經多次看到此過程,因此,我不再重復(請參閱[本文第 3 步](https://javabeginnerstutorial.com/selenium/9b-webdriver-eclipse-setup/))。 另外,請注意,我們在代碼中將圖像另存為`.jpg`文件。 也可以將其另存為`.png`文件。 它可以很簡單, ```java // Copy the screenshot to destination FileUtils.copyFile(src, new File(“\\screenshot\\test.jpg”)); ``` 還是一樣復雜 ```java try { // Specify the destination where the image will be saved File dest = new File("\\Selenium\\screenshots\\" + testCaseName + "_" + timestamp() + ".jpg"); // Copy the screenshot to destination FileUtils.copyFile(src, dest); } catch (IOException ex) { System.out.println(ex.getMessage()); } public static String timestamp() { // Timestamp to make each screenshot name unique return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date()); } ``` 在我們的示例中,我們將使用復雜的版本。 因為我們要將所有與屏幕快照相關的代碼放在一個單獨的方法中(在新類中),并在每次我們希望捕獲屏幕快照時調用它。 否則,我們將不得不為每種情況做相同的歌舞。 ## 概覽 使用兩種方法創建一個名為“`SaveScreenshot.java`”的新類。 1. `public static void capture(String testCaseName, WebDriver driver)` – 具有捕獲屏幕快照并將其保存到所需位置的所有代碼。 2. `public static String timestamp()` – 用于生成時間戳并將其提供給上述方法,以使每個保存的屏幕截圖都是唯一的。 ## 示例場景 1. 打開 Firefox 瀏覽器。 2. 導航到 Google 帳戶創建頁面 3. 通過 ID 找到名字文本框 4. 輸入“`fname01`”作為名字 5. 按名稱找到姓氏文本框 6. 輸入“`lname01`”作為姓氏 7. 截取頁面截圖并將其保存到某個位置。 ## JUnit 代碼: 1. ### `SaveScreenshot.java`類 ```java package com.blog.junitTests; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; 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 testCaseName, 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\\" + testCaseName + "_" + timestamp() + ".jpg"); // Copy the screenshot to destination FileUtils.copyFile(src, dest); } catch (IOException ex) { System.out.println(ex.getMessage()); } } public static String timestamp() { // Timestamp to make each screenshot name unique return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date()); } } ``` ### 2\. `Screenshot.java`類(執行示例方案部分中詳細介紹的步驟) ```java package com.blog.junitTests; 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; public class ScreenshotTest { //Declaring variables private WebDriver driver; private String baseUrl; private String testCaseName = "ScreenshotTest"; @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"); // 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, driver); } @After public void tearDown() throws Exception{ // Close the Firefox browser driver.close(); } } ``` 為每行代碼提供了注釋,因此它是不言自明的。 ![Eclipse output](https://img.kancloud.cn/6e/97/6e9785ef5479f4376d3e7005415d1815_815x362.png) 在 Eclipse IDE 中,“JUnit”窗格清楚地顯示了測試用例“`ScreenshotTest.java`”已通過,并且控制臺沒有錯誤。 如代碼中所指定,屏幕快照以上述格式的名稱保存在“`E:/Selenium/screenshots`”路徑中。 ![Saved Screenshot](https://img.kancloud.cn/ff/56/ff56ae237d3e85edb232fc0eb2aec08c_669x257.png) 這是捕獲的屏幕截圖, ![Captured Screenshot](https://img.kancloud.cn/05/2e/052e3c8d1d1df8e6ddadfa36dc5298ee_1033x587.png) 現在是時候在您的測試過程中實現這一點并為自己可視化魔術了。 祝屏幕截圖愉快!??
                  <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>

                              哎呀哎呀视频在线观看