Basic Widgets

This comprehensive tutorial demonstrates all the fundamental widgets in Widgetastic.core using the framework’s real testing pages. You’ll learn to interact with web elements through practical examples using testing/html/testing_page.html - the same file used to test the framework itself.

In widgetastic, a widget represents any interactive or non-interactive element on a web page. Unlike traditional automation approaches that work directly with raw elements, widgets provide a higher-level, object-oriented abstraction.

Learning Objectives

By completing this tutorial, you will:

  • ✅ Basic understanding of core widget

  • ✅ Understand the widget read/fill interface

  • ✅ Handle widget state and validation

Text Widget

The Text widget extracts text content from a web element.

Basic Text Widget Examples

 1"""Basic Text Widget Example
 2
 3This example demonstrates how to use the Text widget to extract text content.
 4"""
 5
 6from widgetastic.widget import Text
 7from widgetastic.exceptions import NoSuchElementException
 8
 9# In-line Initialization of Text widget
10main_title = Text(parent=browser, locator=".//h1[@id='wt-core-title']")  # noqa: F821
11
12# Widget operations
13print(f"Title is displayed: {main_title.is_displayed}")
14print(f"Title is enabled: {main_title.is_enabled}")
15print(f"Using .text property: '{main_title.text}'")
16print(f"Using .read() method: '{main_title.read()}'")
17
18# Handling Non-Existing Elements
19non_existing_element = Text(browser, locator='.//div[@id="non-existing-element"]')  # noqa: F821
20print(f"Non-existing element is displayed: {non_existing_element.is_displayed}")
21try:
22    non_existing_element.read()
23except NoSuchElementException:
24    print("NoSuchElementException raised as expected")

Note

While inline widget initialization (as shown above) works for learning and debugging, production code should use View classes to organize widgets. Views provide better structure, reusability, and maintainability for real automation projects.

Input Widgets

Widgetastic provides specialized widgets for some types of HTML input elements. Each input widget is optimized for its specific use case while maintaining a consistent interface.

TextInput Widget

The TextInput widget handles standard text input elements like text, email, number, textarea, etc.

Basic TextInput Operations:

 1"""Basic TextInput Widget Example
 2
 3This example demonstrates basic TextInput operations.
 4"""
 5
 6from widgetastic.widget import TextInput
 7
 8# Inline initialization for learning
 9text_input = TextInput(parent=browser, id="input")  # noqa: F821
10
11# Widget operations
12print(f"Text input is displayed: {text_input.is_displayed}")
13print(f"Text input is enabled: {text_input.is_enabled}")
14
15text_input.fill("Hello World")
16print(f"After fill, .value returns: '{text_input.value}'")
17print(f"After fill, .read() returns: '{text_input.read()}'")

TextInput with Different Element Types

 1"""TextInput with Different Element Types
 2
 3This example shows how TextInput works with different HTML input types.
 4"""
 5
 6from widgetastic.widget import TextInput
 7
 8# Number input
 9number_input = TextInput(parent=browser, locator='.//input[@id="input_number"]')  # noqa: F821
10number_input.fill("42")
11print(f"Number input read value: {number_input.read()}")
12
13# Textarea (multi-line)
14textarea = TextInput(parent=browser, id="textarea_input")  # noqa: F821
15multiline_text = "Line 1\nLine 2\nLine 3"
16textarea.fill(multiline_text)
17print(f"Textarea read value: {textarea.read()}")

TextInput State Management

 1"""TextInput State Management Example
 2
 3This example demonstrates checking widget state and fill behavior.
 4"""
 5
 6from widgetastic.widget import TextInput
 7
 8# Check if element exists and is accessible
 9enabled_input = TextInput(parent=browser, id="input1")  # noqa: F821
10disabled_input = TextInput(parent=browser, name="input1_disabled")  # noqa: F821
11
12print(f"Enabled input is displayed: {enabled_input.is_displayed}")
13print(f"Enabled input is enabled: {enabled_input.is_enabled}")
14print(f"Disabled input is enabled: {disabled_input.is_enabled}")
15
16# Fill success checking
17result1 = enabled_input.fill("new value")
18print(f"First fill('new value') returned: {result1}")
19
20# Try to fill same value - no change detected and returns False
21result2 = enabled_input.fill("new value")
22print(f"Second fill('new value') returned: {result2}")

Note

Read/Fill Interface Guidelines:

  • The fill() method MUST return True if it changed anything, False if no change occurred

  • Whatever is returned from read() must be compatible with fill()

  • Round-trip requirement: widget.fill(widget.read()) must work at any time

  • This ensures widgets can be read and restored to their previous state reliably

FileInput Widget

The FileInput widget handles file upload inputs.

 1"""FileInput Widget Example
 2
 3This example demonstrates file upload operations.
 4"""
 5
 6from widgetastic.widget import FileInput
 7
 8file_input = FileInput(browser, id="fileinput")  # noqa: F821
 9
10print(f"File input is displayed: {file_input.is_displayed}")
11print(f"File input is enabled: {file_input.is_enabled}")
12
13# File upload operations
14result = file_input.fill("/etc/resolv.conf")
15print(f"File upload result: {result}")

ColourInput Widget

The ColourInput widget handles HTML5 color picker inputs.

 1"""ColourInput Widget Example
 2
 3This example demonstrates HTML5 color picker operations.
 4"""
 5
 6from widgetastic.widget import ColourInput
 7
 8colour_input = ColourInput(browser, id="colourinput")  # noqa: F821
 9
10# Color operations
11colour_input.fill("#ff0000")
12print(f"After fill('#ff0000'), read returns: {colour_input.read()}")
13
14# Set different colors with colour setter property
15colour_input.colour = "#00ff00"
16print(f"After setting colour to '#00ff00': {colour_input.colour}")

Checkbox Widget

The Checkbox widget handles checkbox elements.

 1"""Checkbox Widget Example
 2
 3This example demonstrates checkbox operations.
 4"""
 5
 6from widgetastic.widget import Checkbox
 7
 8enabled_checkbox = Checkbox(browser, id="input2")  # noqa: F821
 9disabled_checkbox = Checkbox(browser, id="input2_disabled")  # noqa: F821
10
11# Check is_displayed and is_enabled
12print(f"Enabled checkbox is displayed: {enabled_checkbox.is_displayed}")
13print(f"Disabled checkbox is displayed: {disabled_checkbox.is_displayed}")
14
15print(f"Enabled checkbox is enabled: {enabled_checkbox.is_enabled}")
16print(f"Disabled checkbox is enabled: {disabled_checkbox.is_enabled}")
17
18# Filling and reading checkboxes
19enabled_checkbox.fill(True)
20print(f"After fill(True), read returns: {enabled_checkbox.read()}")
21
22enabled_checkbox.fill(False)
23print(f"After fill(False), read returns: {enabled_checkbox.read()}")

Select Widget

The Select widget handles HTML select elements.

 1"""Select Widget Example
 2
 3This example demonstrates select dropdown operations.
 4"""
 5
 6from widgetastic.widget import Select
 7
 8single_select = Select(browser, name="testselect1")  # noqa: F821
 9multi_select = Select(browser, name="testselect2")  # noqa: F821
10
11# Reading selected values
12print(f"Single select current value: {single_select.read()}")
13
14# Get all available options
15print(f"All available options: {single_select.all_options}")
16
17# Select by visible text
18single_select.fill("Bar")
19print(f"After fill('Bar'): {single_select.read()}")
20
21# Select by value
22single_select.fill(("by_value", "foo"))
23print(f"After fill by value 'foo': {single_select.read()}")
24
25# Multiple selection
26multi_select.fill(["Foo", "Baz"])
27print(f"Multiple select values: {multi_select.read()}")

Image Widget

The Image widget provides access to HTML image elements.

Image Examples from Testing Page

 1"""Image Widget Example
 2
 3This example demonstrates accessing HTML image elements.
 4"""
 5
 6from widgetastic.widget import Image
 7
 8full_image = Image(browser, locator="#test-image-full")  # noqa: F821
 9
10# Check image visibility
11print(f"Image is displayed: {full_image.is_displayed}")
12
13# Accessing image attributes
14print(f"Image src: {full_image.src}")
15print(f"Image alt: {full_image.alt}")
16print(f"Image title: {full_image.title}")