python - 如何使用 pywinauto 运行简单的 pytest-bdd 测试?
问题描述
我正在尝试设置一个 pytest-bdd pywinauto 测试,但我无法让它工作。
我找不到任何使用 pywinauto 的简单示例。pytest-bdd 的官方文档只有一个 selenium 示例,但我不明白,因为我不熟悉它。
这是我的项目结构,只有两个文件在同一个文件夹中: foo.feature test_foo.py
这是foo.feature
文件内容:
Feature: Type stuff
The user should be able to type stuff into notepad.
Scenario: Type a sentence
Given Application object exists
When I start notepad
And Focus on the window
And I type some stuff
Then Typed stuff should be visible
这是test_foo.py
文件内容:
import pytest
from pytest_bdd import scenario, given, when, then, parsers
from pywinauto.application import Application
from pywinauto.keyboard import send_keys
@scenario('foo.feature', 'Type a sentence')
def test_foo():
pass
@pytest.fixture()
def app():
return Application()
@given("Application object exists")
def app_exists(app):
assert 'app' in locals()
@when("I start notepad")
def start_notepad(app):
app.start("notepad.exe")
@when("Focus on the window")
def notepad_focus(app):
app.top_window().set_focus()
@when("I type some stuff")
def type_stuff():
send_keys("Hello, world!", with_spaces=True)
@then("Typed stuff should be visible")
def typed_stuff_visible(app):
edit_field = app.top_window().window(class_name='Edit')
assert edit_field.texts()[0] == 'Hello, world!'
出于某种原因,我无法让“then”夹具工作。测试失败并出现以下错误:
E pytest_bdd.exceptions.StepDefinitionNotFoundError: Step definition is not found: When "I type some stuff
E Then Typed stuff should be visible". Line 9 in scenario "Type a sentence" in the feature "C:\Users\...\foo.feature
如果我从文件中删除“then”测试foo.feature
,则测试运行并将文本输入记事本。由于某种原因,“then”完全搞砸了,没有任何东西输入到编辑器中。
解决方案
推荐阅读
- group-by - 在confluent kafka ksql中按column_name进行分组时名称为空错误
- javascript - Javascript 函数限制
- c# - 如果 BinaryWriter 只打开,则关闭它
- android - Ionic pro 框架错误 InAppBillingService 不存在 - Android Build
- javascript - React Native - 来自休息的 FlatList 数据仅显示一半
- mysql - 没有中间表的雄辩的多对多关系
- java - 如何在肥皂请求中设置时区
- c# - 如何从应用程序配置中读取文本框的值
- php - 无法保存新数据,以及数组到字符串的转换?
- javascript - 客户端未在 Socket.IO 中接收事件