| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
""" |
| 12 |
This example demonstrates how to test a simple application using UI Tester. |
| 13 |
|
| 14 |
The GUI being tested is written in the demo under the same name (minus the |
| 15 |
preceding 'test') in the outer directory. |
| 16 |
""" |
| 17 |
|
| 18 |
import os |
| 19 |
import runpy |
| 20 |
import unittest |
| 21 |
|
| 22 |
from traitsui.testing.api import ( |
| 23 |
DisplayedText, |
| 24 |
KeyClick, |
| 25 |
KeySequence, |
| 26 |
MouseClick, |
| 27 |
UITester, |
| 28 |
) |
| 29 |
|
| 30 |
#: Filename of the demo script |
| 31 |
FILENAME = "converter.py" |
| 32 |
|
| 33 |
#: Path of the demo script |
| 34 |
DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", FILENAME) |
| 35 |
|
| 36 |
|
| 37 |
class TestConverter(unittest.TestCase): |
| 38 |
def test_converter(self): |
| 39 |
demo = runpy.run_path(DEMO_PATH)["popup"] |
| 40 |
tester = UITester() |
| 41 |
with tester.create_ui(demo) as ui: |
| 42 |
input_amount = tester.find_by_name(ui, "input_amount") |
| 43 |
output_amount = tester.find_by_name(ui, "output_amount") |
| 44 |
for _ in range(4): |
| 45 |
input_amount.perform(KeyClick("Backspace")) |
| 46 |
input_amount.perform(KeySequence("14.0")) |
| 47 |
self.assertEqual( |
| 48 |
output_amount.inspect(DisplayedText())[:4], |
| 49 |
"1.16", |
| 50 |
) |
| 51 |
tester.find_by_id(ui, "Undo").perform(MouseClick()) |
| 52 |
self.assertEqual( |
| 53 |
output_amount.inspect(DisplayedText()), |
| 54 |
"1.0", |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
# Run the test(s) |
| 59 |
unittest.TextTestRunner().run( |
| 60 |
unittest.TestLoader().loadTestsFromTestCase(TestConverter) |
| 61 |
) |