首页 > 解决方案 > 如何使用 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”完全搞砸了,没有任何东西输入到编辑器中。

标签: pythonpytestpywinauto

解决方案


推荐阅读