【问题标题】:Is it possible to dor UI test of a desktop application using appium and python language?是否可以使用 appium 和 python 语言对桌面应用程序进行 UI 测试?
【发布时间】:2022-08-18 00:07:50
【问题描述】:

我将使用 appium 对桌面应用程序进行 UI 自动化测试。我的偏好是使用 appium python 客户端。但是,没有针对桌面应用程序的 python 测试示例,所有示例都专用于 android 或 ios 应用程序。如果没有示例代码,我真的不知道如何开始我的项目。谁能告诉我,是否可以在 python 中为桌面应用程序进行 UI 自动化测试。如果您也向我发送示例链接,我将不胜感激。

    标签: appium ui-testing python-appium appium-desktop


    【解决方案1】:

    是的你可以!

    您需要从此处下载并安装 WinAppDriver:https://github.com/Microsoft/WinAppDriver/releases,并且必须在您的 PC 上启用开发人员模式,这将告诉您如何操作:https://github.com/microsoft/WinAppDriver

    您还需要确保在 v3.141.0 上使用 Selenium,在 v1.3.0 上使用 appium-python-client

    pip install selenium==3.141.0
    pip install appium-python-client==1.3.0
    

    您可以使用或不使用 Appium 来运行测试。

    要在不确保 Appium 未运行的情况下运行,请尝试以下示例(修改自 https://github.com/microsoft/WinAppDriver/tree/master/Samples/Python):

    import unittest
    from appium import webdriver
    import os
    
    class SimpleCalculatorTests(unittest.TestCase):
    
        @classmethod
        def setUpClass(self):
            # WinAppDriver can be run independently instead if required
            os.startfile('C:\\Program Files (x86)\\Windows Application Driver\\WinAppDriver.exe')    
            
            #set up appium
            desired_caps = {}
            desired_caps["app"] = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"
            desired_caps['platformName'] = "Windows"
            self.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', desired_capabilities= desired_caps)
    
        @classmethod
        def tearDownClass(self):
            self.driver.quit()
    
        def getresults(self):
            displaytext = self.driver.find_element_by_accessibility_id("CalculatorResults").text
            displaytext = displaytext.strip("Display is " )
            displaytext = displaytext.rstrip(' ')
            displaytext = displaytext.lstrip(' ')
            return displaytext
    
        def test_initialize(self):
            self.driver.find_element_by_name("Clear").click()
            self.driver.find_element_by_name("Seven").click()
            self.assertEqual(self.getresults(),"7")
            self.driver.find_element_by_name("Clear").click()
    
        def test_addition(self):
            self.driver.find_element_by_name("One").click()
            self.driver.find_element_by_name("Plus").click()
            self.driver.find_element_by_name("Seven").click()
            self.driver.find_element_by_name("Equals").click()
            self.assertEqual(self.getresults(),"8")
    
    if __name__ == '__main__':
        suite = unittest.TestLoader().loadTestsFromTestCase(SimpleCalculatorTests)
        unittest.TextTestRunner(verbosity=2).run(suite)
    

    要使用 Appium 运行,请尝试以下示例:

    import unittest
    from appium import webdriver
    
    class SimpleCalculatorTests(unittest.TestCase):
    
        @classmethod
        def setUpClass(self):
            #set up appium
            desired_caps = {}
            desired_caps["app"] = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"
            desired_caps['platformName'] = "Windows"
            #self.driver = webdriver.Remote(command_executor='http://127.0.0.1:4723/wd/hub', desired_capabilities= desired_caps)
    
        @classmethod
        def tearDownClass(self):
            self.driver.quit()
    
        def getresults(self):
            displaytext = self.driver.find_element_by_accessibility_id("CalculatorResults").text
            displaytext = displaytext.strip("Display is " )
            displaytext = displaytext.rstrip(' ')
            displaytext = displaytext.lstrip(' ')
            return displaytext
    
        def test_initialize(self):
            self.driver.find_element_by_name("Clear").click()
            self.driver.find_element_by_name("Seven").click()
            self.assertEqual(self.getresults(),"7")
            self.driver.find_element_by_name("Clear").click()
    
        def test_addition(self):
            self.driver.find_element_by_name("One").click()
            self.driver.find_element_by_name("Plus").click()
            self.driver.find_element_by_name("Seven").click()
            self.driver.find_element_by_name("Equals").click()
            self.assertEqual(self.getresults(),"8")
    
    if __name__ == '__main__':
        suite = unittest.TestLoader().loadTestsFromTestCase(SimpleCalculatorTests)
        unittest.TextTestRunner(verbosity=2).run(suite)
    

    【讨论】:

      猜你喜欢
      • 2020-08-19
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      • 2014-10-28
      • 2022-08-16
      • 2016-02-10
      相关资源
      最近更新 更多