跳至内容

屏幕截图

在程序执行期间或之后,你可以随时截取其虚拟终端上显示的内容的屏幕截图。

favoritecolor.py

import sys

answer = input("favorite color:")

sys.stdout.write(answer.upper())
sys.stdout.flush()

使用代码

from icommandlib import ICommand, IProcessTimeout
from commandlib import python

process = ICommand(python("favoritecolor.py")).run()
process.wait_until_output_contains("favorite color:")
process.send_keys("red\n")

process.wait_until_output_contains("RED")

截取屏幕截图

with open("screenshot-before-finish.txt", "w") as handle:
    handle.write(process.screenshot())

process.wait_for_finish()

with open("stripshot-after-finish.txt", "w") as handle:
    handle.write(process.stripshot())
  • 当代码运行完成时。

然后,screenshot-before-finish.txt 的文件内容将为

favorite color:red
RED

然后,stripshot-after-finish.txt 的文件内容将为

favorite color:red
RED

等待 stripshot 匹配字符串并成功

process.wait_for_stripshot_to_match("favorite color:red\nRED")
process.wait_for_finish()
  • 当代码运行完成时。

等待 stripshot 匹配字符串并失败

使用代码

from icommandlib import ICommand, IProcessTimeout
from commandlib import python

process = ICommand(python("favoritecolor.py")).run()
process.wait_until_output_contains("favorite color:")
try:
    process.wait_for_stripshot_to_match("notthis", timeout=0.1)
except IProcessTimeout as error:
    with open("timeout-stripshot.txt", "w") as handle:
        handle.write("Did not match. This was the output instead:\n")
        handle.write(error.stripshot)
  • 当代码运行完成时。

然后,timeout-stripshot.txt 的文件内容将为

Did not match. This was the output instead:
favorite color:

可执行规范

screenshot.story storytests. 自动生成的文档。