Title text reads modernizing conventional test automation with TestGrid. The graphics on the top right and bottom left corners depict test automation for mobile and web applications, respectively.

使用 TestGrid 现代化传统测试自动化

作者头像TestGrid阅读时间 13 分钟

团队使用各种工具和脚本自动执行其软件测试已成为一种标准做法。然而,传统的测试自动化方法并没有像应用程序开发实践那样快速发展。这种差距凸显了现代化测试自动化方法以跟上更快的软件开发周期的必要性。

TestGrid 的统一测试平台旨在通过提供易于访问的端到端测试自动化和对 Selenium 和 Appium 等流行工具的无缝支持来简化测试流程。

在本文中,我们将涵盖 Web 和移动应用程序的自动化测试的传统方法和更新方法。我们将首先研究常用的测试自动化方法,了解它们的不足之处,并讨论提高其效率的方法。然后,我们将深入探讨更现代的方法。

使用传统的测试自动化方法

测试自动化侧重于编写用户旅程和应用程序功能的脚本,以验证应用程序按预期工作。传统的测试自动化方法主要依赖于编写测试脚本。

这种方法的一个主要缺点是,随着应用程序功能的增长,这些脚本可能难以维护,从而导致高维护开销。脚本也可能变得不稳定,导致间歇性失败。

让我们看一下两种广泛采用的测试自动化方法,即使用 Selenium 和 Appium。

使用 Selenium 自动执行 Web 应用程序测试

Selenium 是一种流行的测试自动化工具,开发人员和测试人员使用它为 Web 应用程序编写自动化的 UI 测试。这些测试可以在不同的 Web 浏览器中执行,并且可以用 Selenium 支持的多种语言之一编写。

让我们看一个使用 Selenium 编写自动化测试的示例。我们将使用一个 演示测试网站 来执行以下步骤

  1. 启动您选择的浏览器。
  2. 导航到 demoqa.com 的登录页面。
  3. 输入用户名和密码。
  4. 单击 **登录** 按钮。
  5. 验证用户是否已登录,以及用户名是否显示在屏幕上。

**注意:** 您可以使用 **新用户** 按钮创建一个用户,以便您拥有有效的测试自动化场景凭据。

确保您已具备开始使用 Selenium 的必要先决条件。您可以按照 使用 Java 设置 Selenium 的分步指南 在您的系统上进行操作。

这是我们将用于自动执行上述步骤的 Selenium 代码。

java
public class Login {
  @Test
  public void loginTest() throws InterruptedException {

    WebDriver driver = new ChromeDriver(); // Instantiating the webdriver instance
    driver.get("https://demoqa.com/login"); // Launching the website
    driver.manage().window().maximize(); // Maximizing browser window

    String user = "your user name";
    String pass = "your password";

    // Locating web elements for username, password, and the Login button
    driver.findElement(By.id("userName")).sendKeys(user);
    driver.findElement(By.id("password")).sendKeys(pass);
    driver.findElement(By.id("login")).click();

    // Using sleep to load web elements on page
    Thread.sleep(5000);

    // Capturing the displayed username after login
    String uName = driver.findElement(By.id("userName-value")).getText();

    // Asserting that the logged-in username matches the expected username
    Assert.assertEquals(uName, user);

    // Locating the web element for the Logout button and closing the webdriver instance
    driver.findElement(By.id("submit")).click();
    driver.quit();
  }
}

如注释中所述,在这段代码中,我们正在启动我们的网站、定位 Web 元素,然后对它们执行必要的操作。执行完这段代码后,您应该会看到成功的执行日志。

上述自动化测试验证了关键的用户登录功能。但是,构建和维护此类测试可能很耗时。应用程序的任何微小更改都可能需要更新跨测试套件的此代码,从而增加开销。此外,传统的测试自动化方法可能限制了测试环境的组合。

使用 Selenium 和 TestGrid 自动执行 Web 应用程序测试

让我们使用 TestGrid 的真实设备云 在云浏览器和我们选择的平台上运行上一节中所示的 Selenium 代码。TestGrid 允许您重用现有的 Selenium 测试脚本并扩展您的自动化测试执行。

登录 TestGrid 并按照 在 Web 浏览器上执行本地 Selenium 代码的分步指南 进行操作。

下图显示了 TestGrid 的真实设备云仪表板。在我们的示例中,我们选择了 Linux 平台上的 Firefox 浏览器。

Screenshot of TestGrid's real device cloud dashboard

在按照上面链接的指南中的步骤并设置所需的选项后,我们得到以下代码

java
public class TestGridDemoQaLogin {

  @Test
  public void loginTest() throws InterruptedException, MalformedURLException {

    // Setting the desired capabilities to use the TestGrid platform
    DesiredCapabilities dc = new DesiredCapabilities();
    dc.setCapability("browserName", "firefox");
    dc.setCapability("platformName", "linux");
    dc.setCapability("tg:userToken", "Your Token");
    dc.setCapability("tg:udid", "201");

    // Instantiating the remote webdriver instance and passing desired capabilities to it
    WebDriver driver = new RemoteWebDriver(new URL("Your Browser Run URL(Public)"),dc);

    // Remaining steps are the same as in the previous Selenium code
    driver.get("https://demoqa.com/login");
    driver.manage().window().maximize();

    String user = "your user name";
    String pass = "your password";

    driver.findElement(By.id("userName")).sendKeys(user);
    driver.findElement(By.id("password")).sendKeys(pass);
    driver.findElement(By.id("login")).click();

    Thread.sleep(5000);

    String uName = driver.findElement(By.id("userName-value")).getText();

    Assert.assertEquals(uName, user);

    driver.findElement(By.id("submit")).click();
    driver.quit();
  }
}

当您在系统上本地执行此代码时,您会注意到它是在 TestGrid 的真实设备云上执行的。您可以从门户网站查看远程执行情况。您还可以通过简单地选择您执行测试的设备,在 **自动化会话** 选项卡上查看执行状态、录制和运行摘要。

使用 Appium 自动执行移动应用程序测试

Appium 是一种流行的开源自动化框架,用于测试原生、混合和移动 Web 应用程序。编写 Appium 脚本需要了解移动操作系统以及 Java、C++ 和 Python 等编程语言。对于移动应用程序的自动化测试,由于技术栈的快速发展以及设备和操作系统的更新,维护测试脚本可能具有挑战性。

在此演示中,我们使用基本的计算器应用程序。我们将使用 Appium 自动执行以下用例测试

  1. 启动计算器应用程序。
  2. 执行两个数字的乘法运算。
  3. 记录数学运算的结果。
  4. 断言结果。

我们在这里使用 Appium 和 Java。测试运行在虚拟设备(模拟器)上。您可以使用真实设备或虚拟设备来执行测试。请按照此 Appium 教程 进行设置和其他先决条件。

这是我们使用的 Appium 代码,它将在系统上本地执行,以执行上述步骤。

java
package appium;

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;

public class Calculator {

  @Test
  public void calculator() throws MalformedURLException {

    DesiredCapabilities dc = new DesiredCapabilities();

    // Setting the desired capabilities for the android device
    dc.setCapability("platformName", "android");
    dc.setCapability("platformVersion","14");
    dc.setCapability("deviceName", "Pixel6_TestGrid");
    dc.setCapability("automationName", "UiAutomator2");

    // Setting capability for the application we want to test
    dc.setCapability("app", "/Users/macair/Downloads/calculator.apk");

    // Instantiating Android Driver and using Appium server host and port
    AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), dc);

    // Locating numbers, mathematical operators, and the equals button in the Calculator app
    driver.findElement(AppiumBy.id("com.google.android.calculator:id/digit_4")).click();
    driver.findElement(AppiumBy.id("com.google.android.calculator:id/op_mul")).click();
    driver.findElement(AppiumBy.id("com.google.android.calculator:id/digit_9")).click();
    driver.findElement(AppiumBy.id("com.google.android.calculator:id/eq")).click();

    // Storing the result in a string variable
    String result = driver.findElement(
      AppiumBy.id("com.google.android.calculator:id/result_final")
    ).getText();
    System.out.println("The result is - "+result);

    Assert.assertEquals(result, "36");
    driver.quit();
  }
}

我们的测试代码运行得很好。但是,它需要具有必要技能和知识的专业人员来实施。此外,执行这些测试的设备可用性有限,这可能会使实现最佳测试覆盖率具有挑战性。

使用 Appium 和 TestGrid 自动执行移动应用程序测试

让我们看看如何在真实设备云上执行 Appium 测试脚本。使用我们本地 Appium 移动测试的方法与我们在上面针对 Web 应用程序看到的非常相似。您可以按照此 在设备云上执行本地 Appium 代码的分步指南 进行操作。

让我们看一下更新后的代码,其中包括与我们选择的 TestGrid 设备一致的 DesiredCapabilities。例如,我们指定了 udidsystemPortuserToken

java
package appium;

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;

public class CalculatorTestGrid {

  @Test
  public void calculator() throws MalformedURLException {

    DesiredCapabilities dc = new DesiredCapabilities();

    // Setting the desired capabilities for the android device
    dc.setCapability("platformName", "android");
    dc.setCapability("platformVersion","12");
    dc.setCapability("deviceName", "Samsung Galaxy S21");
    dc.setCapability("automationName", "UiAutomator2");
    dc.setCapability("udid", "R5CRC28KGKB");
    dc.setCapability("tg:systemPort", "4005");
    dc.setCapability("tg:userToken", "Your user token");

    // Since the application is installed on the device, we directly use
    // the package name and main activity
    dc.setCapability("appPackage", "com.google.android.calculator");
    dc.setCapability("appActivity", "com.android.calculator2.Calculator");

    AndroidDriver driver = new AndroidDriver(new URL("Your TestGrid Appium URL"), dc);

    // Remaining steps are the same as in the previous Appium code
    driver.findElement(AppiumBy.id("com.google.android.calculator:id/digit_4")).click();
    driver.findElement(AppiumBy.id("com.google.android.calculator:id/op_mul")).click();
    driver.findElement(AppiumBy.id("com.google.android.calculator:id/digit_9")).click();
    driver.findElement(AppiumBy.id("com.google.android.calculator:id/eq")).click();

    String result = driver.findElement(
      AppiumBy.id("com.google.android.calculator:id/result_final")
    ).getText();
    System.out.println("The result is - "+result);

    Assert.assertEquals(result, "36");
    driver.quit();
  }
}

在执行完此更新后的代码后,您应该能够在您的 IDE 中看到成功的执行日志。您还可以通过导航到左侧面板的 **自动化会话** 选项卡并选择您执行代码的设备来查看执行结果(或正在进行的执行摘要)。

您将能够看到每个执行步骤的状态,以及它们的屏幕截图。此外,您还将能够查看这些测试的日志和视觉测试结果(如果已启用)。

利用 TestGrid 的现代自动化测试解决方案

TestGrid 的无代码测试自动化平台 允许编程知识很少或没有编程知识的用户开始自动执行测试。通过使用无代码自动化,您可以可视化地记录用户旅程。录制会自动生成测试脚本,因此不需要编码。此功能还有助于大幅减少脚本维护开销。

使用 TestGrid 的测试脚本生成器进行 Web 应用程序测试

让我们看看如何使用 TestGrid 的 无代码 Web 测试用例编写器 自动执行 demoqa 网站的相同“登录”用例,而无需任何代码。在使用测试用例生成器添加测试用例时,请按照提示的步骤操作。输入 URL,选择浏览器和网页,然后根据需要向页面上的不同元素添加操作。

正如您在下图中看到的,当您单击要与之交互的 Web 元素的区域时,该元素的定位器会自动检测到。您还会看到指定的不同定位器,您可以选择所需的定位器。您还可以选择手动编写定位器,以防系统无法为您的 Web 元素检测到定位器。

Screenshot showing Testgrid's auto-detected web element locator after clicking on the element

拥有所需的测试数据后,您可以添加登录操作,并使用必要的详细信息为您的测试用例生成测试功能。然后,您可以运行您的测试用例。随着执行的进行,您可以开始观察生成的日志和测试状态。

Screenshot of TestGrid's test execution showing live logs and test status updates

单击左侧面板中的 **构建摘要**,以查看详细信息,例如通过和失败的步骤数量、执行步骤的屏幕截图(如果已启用)、开始和结束时间以及测试状态。

Screenshot of TestGrid's Build Summary for web login test case showing the number of tests (passed or failed), number of steps (passed or failed) screenshots, execution time, and status

执行完成后,您可以访问左侧面板中 **事务分析** 中提供的各种分析报告。

TestGrid 的无代码测试生成器提供的另一个功能是“录制和回放”。它允许您将您的应用程序或浏览器操作转换为强大的自动化测试。在创建项目和模块后,您可以单击 **使用无代码添加测试用例** 来使用此功能。您将在右侧面板中注意到一个 **开始录制** 按钮,单击它将开始录制您的操作。完成后,您可以单击 **停止**。请注意,**实时视图** 和 **元素选择器** 在执行期间处于锁定状态。

Screenshot of TestGrid's record and play dashboard

停止录制后,您可以编辑 **操作** 列中的参数,甚至使用 **元素选择器** 对使用录制添加的步骤进行更多更改。

然后,您可以导航回 **测试用例** 并像以前一样执行测试。您将能够看到执行情况以及不同的分析报告中的结果。使用 **网络日志**,您可以查看发送到域的请求以及每个请求所花费的时间。

Screenshot of network log report on TestGrid's dashboard

本节演示了如何轻松地为您的用例编写无脚本自动化测试,并在需要时添加自定义脚本。TestGrid 的测试脚本生成器可以帮助您提高现代 Web 应用程序自动化测试的速度和可靠性。

使用 TestGrid 的测试脚本生成器进行移动应用程序测试

与 Web 应用程序测试一样,您可以使用 TestGrid 的 无脚本移动应用程序测试用例编写 来测试移动应用程序。使用此功能,您可以快速开始自动化移动应用程序,而无需手动设置运行自动化测试所需的服务器、客户端、设备和其他配置。

单击 **使用测试用例生成器添加测试函数** 按钮后,您将看到一个弹出窗口,要求您提供要测试的应用程序的 apk/ipa/bundle ID。上传应用程序,选择要在其上执行测试的设备,然后单击 **运行**。

Screenshot of TestGrid's Test Script Generator for mobile apps

接下来的步骤与 Web 应用程序的测试用例创建类似。

保存对两个数字应用乘法运算并检查结果的步骤后,将添加测试函数。请注意,您可以直接编写测试用例,而无需像我们在下面所做的那样编写测试函数。您可以为跨测试用例重复的步骤创建测试函数。这些函数可以作为通用函数重复使用。

Screenshot of TestGrid's dashboard where steps are being defined for a test case to perform a multiplication in the Calculator app

运行测试用例后,选择要在其上执行测试的构建和设备。合并和编译后,您可以看到实时执行情况以及生成的日志。

要探索有关执行的任何详细信息,您可以查看 **构建摘要**。

Screenshot of TestGrid's Build Summary for multiplication test case showing the number of tests (passed or failed), number of steps (passed or failed) screenshots, execution time, and status

与 Web 应用程序类似,您也可以对移动应用程序使用“录制和回放”功能。

此外,您可以利用 TestGrid 提供的多个报告和日志,这使得分析执行性能变得容易。Insights 功能显示与执行相关的各种性能参数的图形表示,例如 CPU 负载、内存使用情况、请求和响应数据大小等。

Screenshot of four graphs showing various performance metrics during test execution on TestGrid

此外,**交易分析** 提供有关性能基准的各种可下载报告,包括设备信息。

本节演示了如何利用详细的报告、分析和易于执行的功能来扩展您的自动化范围。借助 TestGrid 的真实设备云,您可以将测试范围扩展到各种设备。无论您是建立测试框架还是从头开始,TestGrid 都使您能够从上次中断的地方继续进行。

使用 TestGrid 的视觉测试功能

视觉测试超越了功能检查,使您能够在敏捷开发过程中检测视觉回归。

TestGrid 通过智能测试自动化帮助使 视觉测试 可扩展。它使团队能够跨所有平台自动捕获屏幕截图,并使用基于 AI 的图像比较来查找差异。深入的热图和指标以及分析仪表板帮助测试团队跨 Web 和移动应用程序自动化视觉验证。此功能通过确保设计一致性来帮助测试团队实现出色的用户体验。

Graphic illustration of visual testing with TestGrid

按照 在 Selenium 或 Appium 代码中配置视觉测试的步骤 后,您可以在 **自动化会话** 结果中为选定设备的 **视觉测试** 选项卡中查看视觉测试结果。

视觉测试在确保任何应用程序的整体质量方面发挥着关键作用。通过对用户界面进行彻底测试,并着重关注细节,可以提高用户满意度和留存率。TestGrid 提供将视觉测试功能轻松集成到现有测试中的方法,从而提供了一种提高被测系统整体覆盖率的方法。

结论

随着软件交付的加速,测试实践需要更新以保持相关性。现代应用程序需要高质量的基准,而传统的测试自动化方法却缺乏这些基准。为 Selenium 或 Appium 编写测试脚本既复杂又耗时。频繁的更改使这些脚本不可靠,并增加了维护开销。

TestGrid 增强了传统功能,以提供面向未来的测试自动化。它提供现代功能,例如无代码自动化、轻松集成以及强大的分析和报告功能。利用计算机视觉和 AI 的无脚本测试有助于实现规模和稳定性。测试团队可以轻松高效地实现跨浏览器、设备和平台的测试覆盖范围,并在任何业务影响之前可视化 UI 问题。

这篇文章由 TestGrid 赞助。TestGrid 是领先的端到端自动化测试解决方案提供商,提供云端和本地部署解决方案。TestGrid 专注于简化测试流程,提供尖端功能,使团队能够显着节省成本和时间,同时加快其上市策略。

与 MDN 保持联系

获取 MDN 时事通讯,不错过有关最新 Web 开发趋势、技巧和最佳实践的任何更新。