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.
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.