UI test with Python

Environment setup:

  1. Install Python 3.7.

  2. install package “pywinauto”.

  3. https://pywinauto.readthedocs.io/en/latest/code/pywinauto.application.html
    Install “Inspect” for view control structure. (Windows 10 installed
    under “C:\Program Files (x86)\Windows
    Kits\10\bin\10.0.17763.0\x64\inspect.exe”)

      If there is no inspect app in your envrionment, please install it use "Windows Software Development Kit" in here and check the two checkbox as below page.
    

How to use “Inspact”:

  1. Launch “Inspect.exe” under install path.
  2. Open the app which you want test. (For example: Ssms.exe).
  3. You will find the controls for the test app.
    Use “pywinauto” package to test Ssms:

Example Code:
It is will click “Spotlight” menu item on Ssms and print the second menu items under “Spotlight” menu item.

import pywinauto
import time
import os
import signal
import subprocess
import sys


def main(pbitFile,pbixFile):
    reportFile =  r'%s' % pbitFile 
    p = subprocess.Popen(r'C:\Program Files\Microsoft Power BI Desktop\bin\PBIDesktop.exe')

    time.sleep(15)

    base = os.path.basename(reportFile)
    filename_withoutextension = os.path.splitext(base)[0]

    app = pywinauto.application.Application().connect(
        title='Untitled - Power BI Desktop', timeout=120)

    try:
        app.window(best_match='Untitled - Power BI Desktop').set_focus()
        time.sleep(30)   
        app.window(best_match='Untitled - Power BI Desktop').type_keys('^o')
        app.Open.edit1.set_text(reportFile)
        app.Open.Open.double_click()  # opening large file
        # make sure "Open" dialog became invisible
        app.Open.wait_not('visible')
        potentialWindow = app.window(best_match='Potential security risk')
        if potentialWindow.exists():
            potentialWindow.type_keys('{ENTER}')        
        time.sleep(3)    
        fileQueryWindow = app.window(best_match='File uses DirectQuery')
        if fileQueryWindow.exists():
            fileQueryWindow.Click()
            fileQueryWindow.type_keys('{TAB 1}')
            fileQueryWindow.type_keys('{ENTER}')
        app.window(best_match=f"{filename_withoutextension}").set_focus()
        app.window(best_match=f"{filename_withoutextension}").type_keys(
            '{ENTER 2}')
        app.window(best_match=f"{filename_withoutextension}").wait_not(
            'visible')
        time.sleep(30)
        filterPane = app.window(best_match='Introducing the new filter pane')
        if filterPane.exists():
            filterPane.type_keys('{ENTER}')        
        app.window(best_match='Untitled - Power BI Desktop').type_keys('^s')
        app.SaveAs.edit1.set_text(pbixFile)
        app.SaveAs.Save.double_click()
        time.sleep(30)
    except:
        print("Unexpected error:", sys.exc_info()[0])
        exit(1)        
    finally:
        os.kill(p.pid, signal.SIGTERM)


if __name__ == "__main__":
    main(sys.argv[1],sys.argv[2])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 中进行 UI 自动化断言的常用工具是 pytest 和 unittest,可以使用它们来编写测试用例并进行断言。 以下是一个示例: ```python import pytest from selenium import webdriver @pytest.fixture() def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_login_page_loads(browser): browser.get("https://example.com/login") assert "Login" in browser.title def test_login_with_valid_credentials(browser): browser.get("https://example.com/login") username_field = browser.find_element_by_name("username") password_field = browser.find_element_by_name("password") submit_button = browser.find_element_by_name("submit") username_field.send_keys("testuser") password_field.send_keys("testpassword") submit_button.click() assert "Welcome" in browser.title ``` 在这个示例中,我们首先定义了一个 fixture `browser`,用于启动和关闭浏览器,并将其传递给测试用例。然后我们定义了两个测试用例,第一个测试用例测试登录页面是否成功加载,第二个测试用例测试使用有效凭据登录是否成功。 在每个测试用例中,我们执行了一系列操作,并使用 `assert` 语句进行断言。例如,在第一个测试用例中,我们使用 `assert` 语句检查标题中是否包含 "Login",如果不包含,则测试失败。在第二个测试用例中,我们使用 `assert` 语句检查标题中是否包含 "Welcome",如果不包含,则测试失败。 此外,我们还可以使用其他断言方法来检查 UI 元素的属性和状态,例如检查文本框中的值和按钮的禁用状态等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值