| 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 interacting with a button created |
| 13 |
using ButtonEditor. |
| 14 |
|
| 15 |
The GUI being tested is written in the demo under the same name (minus the |
| 16 |
preceding 'test') in the outer directory. |
| 17 |
""" |
| 18 |
|
| 19 |
import os |
| 20 |
import runpy |
| 21 |
import unittest |
| 22 |
|
| 23 |
from traitsui.testing.api import DisplayedText, MouseClick, UITester |
| 24 |
|
| 25 |
#: Filename of the demo script |
| 26 |
FILENAME = "ButtonEditor_simple_demo.py" |
| 27 |
|
| 28 |
#: Path of the demo script |
| 29 |
DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", FILENAME) |
| 30 |
|
| 31 |
|
| 32 |
class TestButtonEditorSimpleDemo(unittest.TestCase): |
| 33 |
def test_button_editor_simple_demo(self): |
| 34 |
demo = runpy.run_path(DEMO_PATH)["demo"] |
| 35 |
|
| 36 |
tester = UITester() |
| 37 |
with tester.create_ui(demo) as ui: |
| 38 |
button = tester.find_by_name(ui, "my_button_trait") |
| 39 |
for index in range(5): |
| 40 |
button.perform(MouseClick()) |
| 41 |
self.assertEqual(demo.click_counter, index + 1) |
| 42 |
|
| 43 |
click_counter = tester.find_by_name(ui, "click_counter") |
| 44 |
displayed_count = click_counter.inspect(DisplayedText()) |
| 45 |
self.assertEqual(displayed_count, '5') |
| 46 |
|
| 47 |
demo.click_counter = 10 |
| 48 |
displayed_count = click_counter.inspect(DisplayedText()) |
| 49 |
self.assertEqual(displayed_count, '10') |
| 50 |
|
| 51 |
|
| 52 |
# Run the test(s) |
| 53 |
unittest.TextTestRunner().run( |
| 54 |
unittest.TestLoader().loadTestsFromTestCase(TestButtonEditorSimpleDemo) |
| 55 |
) |