一、前置条件

1. 准备工具

工具名称 描述 下载地址
WinAppDriver.exe 支持桌面软件UI自动化测试的服务 https://github.com/Microsoft/WinAppDriver/releasesWinAppDriverUiRecorder
inspect.exe 控件识别工具,可以获取桌面软件各元素的属性参数,一般情况下,在路径 C:\Program Files (x86)\Windows Kits\10\bin\x64\\x64 下,如果没有,需要单独下载window sdk安装 window sdk安装,下载地址https://developer.microsoft.com/en-us/windows/downloads/sdk-archive/,安装时在最后一步install之前,只需选择”Windows SDK Signing Tools for Desktop Apps“和”Windows SDK for UWP Managed Apps“两项
WinAppDriverUiRecorder(UI记录器) 区别于inspect.exe,UI记录器工具可以检查UI元素并检索它们的XPath表达式,也可以为某些操作生成对应的C#代码。 https://github.com/Microsoft/WinAppDriver/releases

2. 环境搭建

  1. win10操作系统下,在设置 -> 更新和安全 -> 开发者选项下设置为“开发人员模式”;
  2. 下载、安装 WinAppDriver.exe
  3. 准备好inspect.exe、WinAppDriverUiRecorder 查询工具。

二、Java自动化测试框架搭建

1. pom依赖

<dependencies>
    <!-- appium自动化测试工具 -->
    <dependency>
    	<groupId>io.appium</groupId>
    	<artifactId>java-client</artifactId>
    	<version>7.0.0</version>
    	<scope>test</scope>
    </dependency>
    <!-- testng测试框架 -->
    <dependency>
    	<groupId>org.testng</groupId>
    	<artifactId>testng</artifactId>
    	<version>7.1.0</version>
    	<scope>test</scope>
    </dependency>
    <!-- 基于selenium的自动化测试工具 -->
    <dependency>
    	<groupId>org.seleniumhq.selenium</groupId>
    	<artifactId>selenium-java</artifactId>
    	<version>3.14.0</version>
    </dependency>
</dependencies>
<!-- 定位testng.xml,执行指定测试操作 -->
<build>
  <plugins>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-plugin</artifactId>
           <version>2.19</version>
           <configuration>
              <suiteXmlFiles>
                 <suiteXmlFile>src/test/java/com/wyj/qt/ui/test/testcase/testng.xml</suiteXmlFile>
              </suiteXmlFiles>
           </configuration>
     </plugin>
  </plugins>
</build>

2. testng测试框架

使用TestNG框架进行自动化测试
testng.xml:TestNG测试脚本,可以通过该文件指定运行部分测试脚本

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite verbose="0" name="TestMainWindowCase" parallel="false">
    <test name="QtUiTest" preserve-order="false">
    	<!-- 指定包测试-->
        <!--<packages>
            <package name="com.wyj.qt.ui.test.testcase" />
        </packages>-->
        <!-- 指定类测试-->
       <!--<classes>
            <class name="com.wyj.qt.ui.test.testcase.MainWindowTest"/>
        </classes>-->
        <!-- 指定方法测试-->
        <classes>
            <class name="com.wyj.qt.ui.test.testcase.MainWindowTest" >
                <methods>
                    <include name="textAreaTest" />
                </methods>
            </class>
        </classes>
    </test>
</suite>

3. 测试准备

  1. 启动WinAppDriver.exe;
    在这里插入图片描述

  2. 初始化WindowsDriver:

    private static final String QT_UI_APPID = "C:\\...\\qt-test-ui.exe";
    private static WindowsDriver driver = null;
    @BeforeClass
    public static void setup() throws IOException {
    	try {
    		DesiredCapabilities capabilities = new DesiredCapabilities();
    		capabilities.setCapability("app", QT_UI_APPID);
    		capabilities.setCapability("platformName", "Windows");
    		driver = new WindowsDriver(new URL("http://127.0.0.1:4723"), capabilities);
    		driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
        } catch (MalformedURLException e) {
    		e.printStackTrace();
    	} finally {
    

    三、 QT控件测试

    WindowsDriver可以通过多种方式定位到软件中的某一个元素(WebElement),常用查找方法和inspect.exe中的属性对应关系如下表:

    方法inspect.exe中对应的属性名
    findElementByNameName
    findElementByClassNameClassName
    findElementByAccessibilityIdAutomationId

    在QT软件中,需要每个控件指定控件的name,用findElementByName方法确定某一元素。

    以测试QT桌面软件为例,测试各常用控件
    在这里插入图片描述

    1. 输入框测试
      在这里插入图片描述

       // 根据inspect.exe确定name
       WebElement element = this.driver.findElementByName("accessiableName_LineEdit");
       // 模拟鼠标点击,聚焦
       element.click();
       // 简单的模拟输入
       element.sendKeys("test");
       // 获取输入后的值
       String content = element.getText();
       // 与期望值进行比较
       Assert.assertEquals("test", content);
      
    2. 下拉框测试

       // 根据inspect.exe确定name
       WebElement element = this.driver.findElementByName("accessiableName_ComboBox Down");
       // 模拟鼠标点击,弹出下拉选项
       element.click();
       // 确定需要选择的选项(qt软件中,下拉框的选项的name与其显示的值一致)
       String selectValue = "贲门";
       // 通过findElement,树形结构向下检索需要定位的element
       WebElement element1 =  element.findElement(By.name("AccessiableName_QComboBoxListView AccessiableName_QComboBoxListView")).findElement(By.name(selectValue));
       // 模拟鼠标选择点击事件
       element1.click();
       // 获取输入后的值
       String content = element.getText();
       // 与期望值进行比较
       Assert.assertEquals(selectValue, content);
      
    3. 弹窗测试

      // 根据inspect.exe确定name
      WebElement element = this.driver.findElementByName("accessiableName_PushButton");
      // 模式鼠标点击按钮,弹出弹框
      element.click();
      // 获取当前窗体的句柄,并获得最顶层messageBox name
      String messageBox = this.driver.getWindowHandles().toArray()[0].toString();
      // 定位到顶层messageBox
      this.driver.switchTo().window(messageBox);
      // 根据inspect.exe确定弹窗按钮
      WebElement element1 = this.driver.findElementByName("OK Enter");
      // 模拟点击弹窗上的按钮
      element1.click();
      
    4. 单选测试

      // 根据inspect.exe确定name
      WebElement element = this.driver.findElementByName("accessiableName_RadioButton");
      // 模拟单选选中
      element.click();
      // 获取单选选中后的值
      String value = element.getAttribute("SelectionItem.IsSelected");
      boolean radioValue = "True".equals(value);
      Assert.assertEquals(true, radioValue);
      // 模拟取消选中
      element.click();
      value = element.getAttribute("SelectionItem.IsSelected");
      boolean radioValue = "True".equals(value);
      Assert.assertEquals(false, radioValue);
      
    5. 多选测试

      // 根据inspect.exe确定name
      WebElement element = this.driver.findElementByName("accessiableName_CheckBox");
      // 模拟多选框选中
      element.click();
      // 获取多选框后的值
      String value = element.getAttribute("Toggle.ToggleState");
      boolean checkValue = "1".equals(value);
      Assert.assertEquals(true, radioValue);
      // 模拟取消选中
      element.click();
      value = element.getAttribute("Toggle.ToggleState");
      boolean checkValue = "1".equals(value);
      Assert.assertEquals(false, checkValue);
      
    6. 文本框测试

      // 根据inspect.exe确定name
      WebElement element = this.driver.findElementByName("accessiableName_TextEdit");
      // 模拟鼠标定位聚焦
      element.click();
      // 清空文本框
      element.clear();
      // element可以做简单的点击、输入操作,比较复杂的操作可以借助Actions对象来完成
      Actions actions = new Actions(this.driver);
      // 模拟键盘输入值
      actions.sendKeys(element, “123”);
      // 执行
      actions.perform();
      // 获取文本框的录入值
      String value = element.getText();
      // 与期望值进行比较
      Assert.assertEquals("123", value);
      
    7. 菜单测试
      在这里插入图片描述

      // 借助WinAppDriverUiRecorder工具,可以获取元素的xml,可以通过xpath的方式定位元素
      WebElement element = this.driver.findElementByXPath("//MenuItem[@Name='File']");
      // 模式鼠标点击
      element.click();
      // 由于弹出菜单选项后,并不能在当前窗体中定位弹出的菜单,于是新建一个桌面的driver对象去重新定位需要操作的菜单选项
      DesiredCapabilities appCapabilities = new DesiredCapabilities();
      appCapabilities.setCapability("app", "Root");
      appCapabilities.setCapability("deviceName", "WindowsPC");
      WindowsDriver destopRoot = new WindowsDriver<WindowsElement>(new URL(windowsApplicationDriverUrl), appCapabilities);
      // 重新定位新的菜单按钮
      WebElement element1 = destopRoot.findElementByName("test1");
      element1.click();
      

      四、测试报告

      全部执行完测试方法后,会在项目target/surefire-reports/目录下生成测试报告emailable-report.html,可以查看通过和失败的用例及执行情况。

      桌面软件自动化测试操作步骤说明一、前置条件1. 准备工具工具名称描述下载地址WinAppDriver.exe支持桌面软件UI自动化测试的服务https://github.com/Microsoft/WinAppDriver/releasesWinAppDriverUiRecorderinspect.exe控件识别工具,可以获取桌面软件各元素的属性参数,一般情况下,在路径C:\Program Files (x86)\Windows Kits\10\bin\x64\\x6
      Windows 应用程序驱动程序 Windows 应用程序驱动程序 (WinAppDriver) 是一种支持 Windows 应用程序上类似 Selenium 的 UI 测试自动化的服务。此服务支持在Windows 10 PC上测试通用 Windows 平台 (UWP)、Windows 窗体 (WinForms)、Windows Presentation Foundation (WPF)和经典 Windows (Win32)应用程序。 安装并运行 WinAppDriver 从https://githu
      一、前置条件 1. 准备工具 WinAppDriver.exe 支持桌面软件UI自动化测试的服务 https://github.com/Microsoft/WinAppDriver/releasesWinAppDriverUiRecorder inspect.exe 控件识别工具,可以获取桌面软件各元素的属性参数,一般情况下,在路径C:\Program Files (x86)\Windows Kits\10\bin\x64\\x64下,如果没有,需要单独下载window sdk安装...
      # 配置Root窗口 desired_caps = {'app': 'Root', 'deviceName': 'windowsPC', 'platformName': 'windows'} # 创建Root窗口会话 driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', desired_capabilities=desir
      为了实现Qt程序的linux下界面自动化测试,之前在linux的Accessibility方向上研究了一段时间,如dogtail (即at -spi) 以及at-spi2还有Qt-at-spi,对了还有Linux Desktop Testing Project(LDTP),但因为种种原因暂未能拿出一个可行的测试方案。        后来发现了sikuli(http://sikuli.org/