10F 高级 WebDriver – 截屏

    嗨冠军! 屏幕截图。 在软件测试的拥挤街道上,另一个经常听到的术语。 如果您的环境中出现错误,而开发中没有错误,则无法用截图来证明,这是一个好测试! 因此,现在是时候该了解如何使用 Selenium WebDriver 来抓取了。

    我知道您想到了几个问题。 最重要的是,“如果是手动测试,那么我只要按一下键盘上的按钮,然后抓取一个漂亮的屏幕截图即可。 但是,当我实现自动化时,如何获得相同的结果?”

    猜猜看,这很简单! 只需按照 3 个步骤进行操作,您就可以使用屏幕截图。 如果您像我一样,可能会急切地想看看它在代码中是如何工作的。 我的目标是取悦,所以请不要再拖延……

    使用 Selenium WebDriver 提供的TakesScreenshot接口。 将 WebDriver 对象强制转换为TakesScreenshot类型。

    当您在TakesScreenshot下面看到一条弯曲的线时,只需单击import org.openqa.selenium.TakesScreenshot;包,您将没有任何错误。

    步骤 2:

    要将屏幕截图获取为图像文件,请调用“getScreenshotAs”方法。

    波浪线? - 点击:

    1. import org.openqa.selenium.OutputType;
    2. import java.io.File;

    线条更弯曲? - 点击:

    1. import java.io.IOException;
    2. import org.apache.commons.io.FileUtils;
    3. import java.text.SimpleDateFormat;
    4. import java.util.Date;

    确保完全按照指定的方式导入包。 通常,您可能需要下载org.apache.commons.io jar(在撰写本文时为)并将其添加到项目的构建路径中。 之前,我们已经多次看到此过程,因此,我不再重复(请参阅本文第 3 步)。

    另外,请注意,我们在代码中将图像另存为.jpg文件。 也可以将其另存为.png文件。

    它可以很简单,

    还是一样复杂

    1. try {
    2. // Specify the destination where the image will be saved
    3. File dest = new File("\\Selenium\\screenshots\\" + testCaseName + "_" + timestamp() + ".jpg");
    4. // Copy the screenshot to destination
    5. FileUtils.copyFile(src, dest);
    6. } catch (IOException ex) {
    7. System.out.println(ex.getMessage());
    8. }
    9. public static String timestamp() {
    10. // Timestamp to make each screenshot name unique
    11. return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
    12. }

    在我们的示例中,我们将使用复杂的版本。 因为我们要将所有与屏幕快照相关的代码放在一个单独的方法中(在新类中),并在每次我们希望捕获屏幕快照时调用它。 否则,我们将不得不为每种情况做相同的歌舞。

    概览

    使用两种方法创建一个名为“”的新类。

    1. public static void capture(String testCaseName, WebDriver driver) – 具有捕获屏幕快照并将其保存到所需位置的所有代码。
    2. public static String timestamp() – 用于生成时间戳并将其提供给上述方法,以使每个保存的屏幕截图都是唯一的。
    1. 打开 Firefox 浏览器。
    2. 导航到 Google 帐户创建页面
    3. 输入“fname01”作为名字
    4. 按名称找到姓氏文本框
    5. 输入“lname01”作为姓氏
    6. 截取页面截图并将其保存到某个位置。

    JUnit 代码:

    2. Screenshot.java类(执行示例方案部分中详细介绍的步骤)

    1. package com.blog.junitTests;
    2. import java.util.concurrent.TimeUnit;
    3. import org.junit.After;
    4. import org.junit.Before;
    5. import org.junit.Test;
    6. import org.openqa.selenium.By;
    7. import org.openqa.selenium.WebDriver;
    8. import org.openqa.selenium.WebElement;
    9. import org.openqa.selenium.firefox.FirefoxDriver;
    10. public class ScreenshotTest {
    11. //Declaring variables
    12. private WebDriver driver;
    13. private String baseUrl;
    14. private String testCaseName = "ScreenshotTest";
    15. @Before
    16. public void setUp() throws Exception{
    17. // Selenium version 3 beta releases require system property set up
    18. System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\"
    19. + "Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");
    20. // Create a new instance for the class FirefoxDriver
    21. // that implements WebDriver interface
    22. driver = new FirefoxDriver();
    23. // Assign the URL to be invoked to a String variable
    24. baseUrl = "https://accounts.google.com/SignUp";
    25. }
    26. @Test
    27. public void testPageTitle() throws Exception{
    28. // Open baseUrl in Firefox browser window
    29. driver.get(baseUrl);
    30. // Locate First Name text box by id and
    31. // assign it to a variable of type WebElement
    32. WebElement firstName = driver.findElement(By.id("firstName"));
    33. // Clear the default placeholder or any value present
    34. firstName.clear();
    35. // Enter/type the value to the text box
    36. firstName.sendKeys("fname01");
    37. // Locate last name text box by name
    38. WebElement lastName = driver.findElement(By.name("lastName"));
    39. // Clear and enter a value
    40. lastName.clear();
    41. lastName.sendKeys("lname01");
    42. //Take a screenshot
    43. SaveScreenshot.capture(testCaseName, driver);
    44. }
    45. @After
    46. public void tearDown() throws Exception{
    47. // Close the Firefox browser
    48. driver.close();
    49. }
    50. }

    在 Eclipse IDE 中,“JUnit”窗格清楚地显示了测试用例“ScreenshotTest.java”已通过,并且控制台没有错误。

    如代码中所指定,屏幕快照以上述格式的名称保存在“”路径中。

    Saved Screenshot

    这是捕获的屏幕截图,

    现在是时候在您的测试过程中实现这一点并为自己可视化魔术了。