Show API reference for

Testing element classes

The Block class has the same methods and attributes as AppTest. A Block instance represents a container of elements just as AppTest represents the entire app. For example, Block.button will produce a WidgetList of Button in the same manner as AppTest.button.

ChatMessage, Column, and Tab all inherit from Block. For all container classes, parameters of the original element can be obtained as properties. For example, ChatMessage.avatar and Tab.label.

Element base class for testing.

This class's methods and attributes are universal for all elements implemented in testing. For example, Caption, Code, Text, and Title inherit from Element. All widget classes also inherit from Element, but have additional methods specific to each widget type. See the AppTest class for the full list of supported elements.

For all element classes, parameters of the original element can be obtained as properties. For example, Button.label, Caption.help, and Toast.icon.

Class description[source]

st.testing.v1.element_tree.Element(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

Attributes

The value or contents of the element.

A representation of st.audio_input.

The recording is one WAV file, (filename, content, mime_type), with MIME type audio/wav. .upload() replaces any staged recording. Unlike FileUploader, this widget does not accept a list or append files.

sample_rate is the value passed to st.audio_input. The widget default is 16000. None means the script passed sample_rate=None, so the browser chooses the rate.

Class description[source]

st.testing.v1.element_tree.AudioInput(proto, root)

Methods

clear()

Clear the recording.

run(*, timeout=None)

Run the AppTest script which contains the element.

set_value(file)

Set the recording, or None to clear it.

upload(filename, content, mime_type="audio/wav")

Stage one recording, replacing any recording already staged.

Attributes

The current recording.

Example

at = AppTest.from_string('''
    import streamlit as st
    audio = st.audio_input("Record")
    if audio:
        st.write(audio.name)
''')
at.run()
at.audio_input[0].set_value(("clip.wav", b"RIFF", "audio/wav"))
at.run()
at.markdown[0].value
'clip.wav'

A representation of st.button and st.form_submit_button.

Class description[source]

st.testing.v1.element_tree.Button(proto, root)

Methods

click()

Set the value of the button to True.

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the button.

Attributes

The value of the button. (bool)

A representation of st.pills and st.segmented_control.

Class description[source]

st.testing.v1.element_tree.ButtonGroup(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

select(v)

Add a selection to the widget.

Set the value of the widget.

Remove a selection from the widget.

Attributes

The widget's formatting function for displaying options. (callable)

The formatted string values for the current selection.

The indices of the currently selected values from the options. (list)

The currently selected value(s) from the options.

A representation of st.camera_input.

The photo is one JPEG file, (filename, content, mime_type). .upload() replaces any staged photo. Unlike FileUploader, this widget does not accept a list or append files.

AppTest accepts a photo only when:

  • the filename ends in .jpg
  • the MIME type is image/jpeg

resolution is the value passed to st.camera_input: "480p", "720p", "1080p", or None when the script omits it.

Class description[source]

st.testing.v1.element_tree.CameraInput(proto, root)

Methods

clear()

Clear the photo.

run(*, timeout=None)

Run the AppTest script which contains the element.

set_value(file)

Set the photo, or None to clear it.

upload(filename, content, mime_type="image/jpeg")

Stage one photo, replacing any photo already staged.

Attributes

The photo committed by the last run().

Example

at = AppTest.from_string('''
    import streamlit as st
    photo = st.camera_input("Take a picture")
    if photo:
        st.write(photo.name)
''')
at.run()
at.camera_input[0].set_value(("shot.jpg", b"jpeg", "image/jpeg"))
at.run()
at.markdown[0].value
'shot.jpg'

A representation of st.chat_input.

Class description[source]

st.testing.v1.element_tree.ChatInput(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the widget.

Attributes

The pending or last submitted chat input value.

A representation of st.checkbox.

Class description[source]

st.testing.v1.element_tree.Checkbox(proto, root)

Methods

check()

Set the value of the widget to True.

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the widget.

Set the value of the widget to False.

Attributes

The value of the widget. (bool)

A representation of st.color_picker.

Class description[source]

st.testing.v1.element_tree.ColorPicker(proto, root)

Methods

pick(v)

Set the value of the widget as a hex string. May omit the "#" prefix.

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the widget as a hex string.

Attributes

The currently selected value as a hex string. (str)

A representation of st.date_input.

Class description[source]

st.testing.v1.element_tree.DateInput(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the widget.

Attributes

The value of the widget. (date or Tuple of date)

A representation of st.datetime_input.

Class description[source]

st.testing.v1.element_tree.DateTimeInput(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the widget.

Attributes

The current value of the widget. (datetime)

A representation of st.download_button.

Class description[source]

st.testing.v1.element_tree.DownloadButton(proto, root)

Methods

click()

Set the value of the download button to True.

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the download button.

Attributes

The value of the download button. (bool)

A representation of st.feedback.

Class description[source]

st.testing.v1.element_tree.Feedback(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the feedback widget. (int or None)

Attributes

The currently selected feedback value. (int or None)

A representation of st.file_uploader.

Files are provided as tuples of (filename, content, mime_type).

Class description[source]

st.testing.v1.element_tree.FileUploader(proto, root)

Methods

clear()

Clear all uploaded files.

run(*, timeout=None)

Run the AppTest script which contains the element.

set_value(files)

Set the uploaded file(s) for testing.

upload(filename, content, mime_type="application/octet-stream")

Upload a single file for testing.

Attributes

Whether directory uploads are accepted. (bool)

Whether multiple files can be uploaded. (bool)

Allowed file types for upload. (list of str)

The current uploaded file(s).

Example

at = AppTest.from_string('''
    import streamlit as st
    uploaded = st.file_uploader("Upload a file")
    if uploaded:
        st.write(f"Uploaded: {uploaded.name}")
''')
at.run()
at.file_uploader[0].set_value(("data.csv", b"col1,col2\n1,2", "text/csv"))
at.run()
at.markdown[0].value
'Uploaded: data.csv'

A representation of st.menu_button.

Class description[source]

st.testing.v1.element_tree.MenuButton(proto, root)

Methods

click(v)

Click an option by value, simulating user selection.

click_index(index)

Click an option by index, simulating user selection.

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the selected option value.

Attributes

The widget's formatting function for displaying options. (callable)

The selected option value, or None if no option was clicked. (Any)

A representation of st.multiselect.

Class description[source]

st.testing.v1.element_tree.Multiselect(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

select(v)

Add a selection to the widget. Do nothing if the value is already selected. If testing a multiselect widget with repeated options, use set_value instead.

Set the value of the multiselect widget. (list)

Remove a selection from the widget. Do nothing if the value is not already selected. If a value is selected multiple times, the first instance is removed.

Attributes

The widget's formatting function for displaying options. (callable)

The indices of the currently selected values from the options. (list)

The currently selected values from the options. (list)

The currently selected values from the options. (list)

A representation of st.number_input.

Class description[source]

st.testing.v1.element_tree.NumberInput(proto, root)

Methods

Decrement the st.number_input widget as if the user clicked "-".

Increment the st.number_input widget as if the user clicked "+".

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the st.number_input widget.

Attributes

The current value of the st.number_input widget.

A representation of st.pagination.

.value is the current page (1-indexed). set_value and select choose a page and raise AppTestError if:

  • the widget is disabled
  • the page is not an int from 1 through num_pages (bool is invalid)
Class description[source]

st.testing.v1.element_tree.Pagination(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

select(page)

Select page as if the user clicked that page button.

Set the current page (1-indexed).

Attributes

The current page, starting at 1.

A representation of st.radio.

Class description[source]

st.testing.v1.element_tree.Radio(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the selection by value.

Attributes

The widget's formatting function for displaying options. (callable)

The index of the current selection. (int)

The currently selected value from the options. (Any)

A representation of st.select_slider.

Class description[source]

st.testing.v1.element_tree.SelectSlider(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

set_range(lower, upper)

Set the ranged selection by values.

Set the (single) selection by value.

Attributes

The widget's formatting function for displaying options. (callable)

The currently selected value or range. (Any or Sequence of Any)

A representation of st.selectbox.

Class description[source]

st.testing.v1.element_tree.Selectbox(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

select(v)

Set the selection by value.

select_index(index)

Set the selection by index.

Set the selection by value.

Attributes

The widget's formatting function for displaying options. (callable)

The index of the current selection. (int)

The currently selected value from the options. (Any)

A representation of st.slider.

Class description[source]

st.testing.v1.element_tree.Slider(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

set_range(lower, upper)

Set the ranged value of the slider.

Set the (single) value of the slider.

Attributes

The currently selected value or range. (Any or Sequence of Any)

A representation of st.text_area.

Class description[source]

st.testing.v1.element_tree.TextArea(proto, root)

Methods

input(v)

Set the value of the widget only if the value does not exceed the maximum allowed characters.

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the widget.

Attributes

The current value of the widget. (str)

A representation of st.text_input.

Class description[source]

st.testing.v1.element_tree.TextInput(proto, root)

Methods

input(v)

Set the value of the widget only if the value does not exceed the maximum allowed characters.

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the widget.

Attributes

The current value of the widget. (str)

A representation of st.time_input.

Class description[source]

st.testing.v1.element_tree.TimeInput(proto, root)

Methods

Select the previous available time.

Select the next available time.

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the widget.

Attributes

The current value of the widget. (time)

A representation of st.toggle.

Class description[source]

st.testing.v1.element_tree.Toggle(proto, root)

Methods

run(*, timeout=None)

Run the AppTest script which contains the element.

Set the value of the widget.

Attributes

The current value of the widget. (bool)

forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.