Interactive Testing¶
In some circumstances tests can not be completely automated.
htf offers interactive testing which allows you to interact with your tests via your web browser.
This feature can be used to script and revision manual tests for example.
You can find more information in the Interactive Testing documentation.
Solve Arithmetic Problems¶
This demo lets you solve arithmetic problems.
The demo is located in interaction/test_solve_arithmetic_problems.py.
#
# Copyright (c) 2023, HILSTER - https://hilster.io
# All rights reserved.
#
import htf
import htf.assertions as assertions
from operator import add, sub, mul
from random import randint, choice
def test_solve_arithmetic_problems(
interaction: htf.fixtures.interaction,
) -> None:
"""
This test shows how to interactively solve simple arithmetic problems.
Args:
interaction (htf.fixtures.interaction): the interaction fixture
"""
try:
for number_solved in range(1, 6): # max 5 equations can be solved
a, b = randint(1, 10), randint(1, 10) # pick two random numbers
operator, func = choice([("+", add), ("-", sub), ("*", mul)]) # pick a random operator
answer = interaction.input_dialog(
title="Solve this equation", text="{} {} {} = ?".format(a, operator, b), validator=htf.int_validator
) # only accept valid integers as inputs
assertions.assert_equal(answer, func(a, b)) # check if the answer is correct
if number_solved < 5:
again = interaction.yes_no_dialog(title="Again", text="Would you like to solve another one?")
if again == "no": # stop if the user has had enough
break
except AssertionError:
number_solved -= 1
raise
finally:
interaction.message_dialog(title="Congratulations", text="You solved {} equations!".format(number_solved))
if __name__ == "__main__":
htf.main(interactive=True)
To use interactive mode, add -i to the htf call.
cd interaction
htf -i -o test_solve_arithmetic_problems.py
Note
If the default TCP/IP port 8080 is not usable on your system you can change it by appending
--interactive-port <port> to the command, e.g.:
htf -i -o --interactive-port 12345 test_solve_arithmetic_problems.py
The dialogs will appear in the test report afterwards.
Interactive arithmetic prompts
Download recording · Download transcript
Interactive arithmetic prompts
==============================
Visual transcript and walkthrough of the silent recording.
Based on reviewed screen captions/interface actions and the associated written example.
Original recording: demo_interactive_arithmetic.mp4
1. Change to the interaction directory and start the arithmetic example with htf's -i
interactive option.
2. The terminal displays an interactive-session address and QR code. Open the displayed
session in a browser.
3. Answer the example arithmetic prompts and complete the interaction.
4. The generated HTML report retains the prompts, responses, and final test result.
Captcha Solver¶
This demo lets you solve captchas.
It is located in interaction/test_captcha_solver.py.
#
# Copyright (c) 2023, HILSTER - https://hilster.io
# All rights reserved.
#
import htf
import htf.assertions as assertions
from captcha.image import ImageCaptcha # type: ignore
from random import randint
def test_solve_captchas(
interaction: htf.fixtures.interaction,
) -> None:
"""
This test shows how to interactively solve captchas.
Args:
interaction (htf.fixtures.interaction): the interaction fixture
"""
try:
for number_solved in range(1, 6): # max 5 captchas can be solved
image = ImageCaptcha()
captcha_value = str(randint(1000, 10000))
image.write(captcha_value, "captcha.png")
captcha = """
.. |captcha| image:: captcha.png
"""
answer = interaction.input_dialog(
title="Captcha", text="Please solve the following captcha:\n\n|captcha|" + captcha
)
assertions.assert_equal(answer, captcha_value)
if number_solved < 5:
again = interaction.yes_no_dialog(title="Again", text="Would you like to solve another one?")
if again == "no": # stop if the user has had enough
break
except AssertionError:
number_solved -= 1
raise
finally:
interaction.message_dialog(title="Congratulations", text="You solved {} captchas!".format(number_solved))
if __name__ == "__main__":
htf.main(interactive=True)
To use interactive mode add -i to the htf call.
cd interaction
htf -i -o test_captcha_solver.py
The dialogs will appear in the test report afterwards.
Interactive CAPTCHA demonstration
Download recording · Download transcript
Interactive CAPTCHA demonstration
=================================
Visual transcript and walkthrough of the silent recording.
Based on reviewed screen captions/interface actions and the associated written example.
Original recording: demo_interactive_captcha.mp4
1. Change to the interaction directory and start the CAPTCHA example with htf's -i
interactive option.
2. Open the interactive-session address displayed by the example.
3. The demonstration asks the operator to enter the characters shown in its example
prompt.
4. After completing the example, open the generated HTML report to inspect the recorded
interaction and test result.