跳至内容

自定义屏幕条件

在这个示例中,使用自定义函数来检测各种屏幕终端条件。

当您正在等待屏幕上出现无法通过查找文本块来检查的内容时,这很有用。

favoritecolor.py

import sys
import time

answer = input("favorite color:")

with open("color.txt", "w") as handle:
    handle.write(answer)

sys.exit(0)

带有代码

from icommandlib import ICommand
from commandlib import python

class ExampleException(Exception):
      """
      This is a demonstration exception's docstring.

      It spreads across multiple lines.
      """
      pass

def check_for_favorite_color(screen):
    return "favorite color" in screen.text

def check_with_error(screen):
    raise ExampleException()

process = ICommand(python("favoritecolor.py")).run()

检查喜欢的颜色

process.wait_until(check_for_favorite_color)
process.send_keys("blue\n")
process.wait_for_finish()
  • 当代码运行到完成时。

color.txt 的文件内容将为

blue

使用错误检查

process.wait_until(check_with_error)

将引发类型为 __main__.ExampleException 的异常,消息为

None

等待程序完成时

process.wait_until(check_for_favorite_color)
process.send_keys("blue\n")
process.wait_for_finish()
process.wait_until(check_for_favorite_color)

将引发类型为 icommandlib.exceptions.AlreadyExited 的异常,消息为

Process already exited with 0. Output:
favorite color:blue

可执行规范

custom-screen-condition.story storytests 自动生成的文档。