Skip to content

Commit

Permalink
Basic tests for each components (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pbelczyk authored Sep 4, 2023
1 parent 6a95382 commit f4ea096
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 28 deletions.
33 changes: 29 additions & 4 deletions cookiecutter/{{ cookiecutter.package_name }}/e2e/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,40 @@ def go_to_app(page: Page, streamlit_app: StreamlitRunner):


def test_should_render_template(page: Page):
frame = page.frame_locator(
frame_0 = page.frame_locator(
'iframe[title="my_component\\.my_component"]'
).nth(0)
frame_1 = page.frame_locator(
'iframe[title="my_component\\.my_component"]'
).nth(1)

st_markdown_0 = page.get_by_role('paragraph').nth(0)
st_markdown_1 = page.get_by_role('paragraph').nth(1)

expect(st_markdown_0).to_contain_text("You've clicked 0 times!")

frame_0.get_by_role("button", name="Click me!").click()

expect(st_markdown_0).to_contain_text("You've clicked 1 times!")
expect(st_markdown_1).to_contain_text("You've clicked 0 times!")

frame_1.get_by_role("button", name="Click me!").click()
frame_1.get_by_role("button", name="Click me!").click()

expect(st_markdown_0).to_contain_text("You've clicked 1 times!")
expect(st_markdown_1).to_contain_text("You've clicked 2 times!")

page.get_by_label("Enter a name").click()
page.get_by_label("Enter a name").fill("World")
page.get_by_label("Enter a name").press("Enter")

expect(frame_1.get_by_text("Hello, World!")).to_be_visible()

expect(page.get_by_text("You've clicked 0 times!").first).to_be_visible()
frame_1.get_by_role("button", name="Click me!").click()

frame.get_by_role("button", name="Click me!").click()
expect(st_markdown_0).to_contain_text("You've clicked 1 times!")
expect(st_markdown_1).to_contain_text("You've clicked 3 times!")

expect(page.get_by_text("You've clicked 1 times!").first).to_be_visible()

def test_should_change_iframe_height(page: Page):
frame = page.frame_locator('iframe[title="my_component\\.my_component"]').nth(1)
Expand Down
4 changes: 3 additions & 1 deletion examples/CustomDataframe/e2e/test_custom_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def test_should_render_dataframe(page: Page):
cell_in_frame = frame.get_by_role("cell", name="Jason")
expect(cell_in_frame).to_be_visible()

st_table = page.get_by_test_id('stTable')

frame.get_by_role("button", name="Return dataframe").click()
cell_generated = page.get_by_role("cell", name="Jason")
cell_generated = st_table.get_by_role("cell", name="Jason")
expect(cell_generated).to_be_visible()
29 changes: 24 additions & 5 deletions examples/MaterialLogin/e2e/test_material_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,37 @@ def test_should_render_material_login(page: Page):
frame = page.frame_locator(
'iframe[title="material_login\\.material_login"]'
)
component_return_value = page.get_by_test_id('stMarkdownContainer')
component_return_value_json = page.get_by_test_id('stJson')

email_field = frame.get_by_placeholder("[email protected]")
password_field = frame.get_by_placeholder("your password")
invalid_username_text = frame.get_by_text("username must be a valid email")
invalid_password_text = frame.get_by_text("password is a required field")
login_button = frame.get_by_role("button", name="Login")
cancel_button = frame.get_by_role("button", name="Cancel")

email_field.click()
email_field.fill("hello")

expect(invalid_username_text).to_be_visible()
expect(invalid_password_text).to_be_visible()
expect(login_button).to_be_disabled()

email_field.click()
email_field.fill("[email protected]")
password_field.click()
password_field.fill("password")
frame.get_by_role("button", name="Login").click()

page.get_by_text("\"username\":\"[email protected]\"")
frame.get_by_role("button", name="Cancel").click()
expect(invalid_username_text).to_be_hidden()
expect(invalid_password_text).to_be_hidden()
expect(component_return_value).to_have_text('None')
login_button.click()

expect(component_return_value_json).to_contain_text("\"username\":\"[email protected]\"")
expect(component_return_value_json).to_contain_text("\"password\":\"password\"")
cancel_button.click()
expect(email_field).to_be_empty()
expect(password_field).to_be_empty()

expect(invalid_username_text).to_be_hidden()
expect(invalid_password_text).to_be_hidden()
expect(component_return_value_json).to_have_text('{}')
13 changes: 10 additions & 3 deletions examples/RadioButton/e2e/test_radio_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def go_to_app(page: Page, streamlit_app: StreamlitRunner):
page.get_by_role("img", name="Running...").is_hidden()


def test_should_render_user_input(page: Page, assert_snapshot):
frame_0 = page.frame_locator(
def test_should_render_user_input(page: Page):
frame = page.frame_locator(
'iframe[title="radio_button\\.radio_button"]'
)

radio_group = frame_0.get_by_role("radiogroup")
radio_group = frame.get_by_role("radiogroup")
first_radio = radio_group.get_by_text("one bat")
second_radio = radio_group.get_by_text("TWO bats")
text = page.get_by_text("This many")
Expand All @@ -42,3 +42,10 @@ def test_should_render_user_input(page: Page, assert_snapshot):
expect(first_radio).not_to_be_checked()
expect(second_radio).to_be_checked()
expect(text).to_have_text("This many: TWO bats")

# check if click on checked option will not uncheck it
second_radio.check()

expect(first_radio).not_to_be_checked()
expect(second_radio).to_be_checked()
expect(text).to_have_text("This many: TWO bats")
36 changes: 29 additions & 7 deletions examples/SelectableDataTable/e2e/test_selectable_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,37 @@ def test_should_render_selectable_data_table(page: Page):
'iframe[title="selectable_data_table\\.selectable_data_table"]'
)

main_checkbox = frame.get_by_role("row", name="First Name Last Name Age").get_by_role("checkbox")
first_row_checkbox = frame.get_by_role("row", name="Jason Miller 42").get_by_role("checkbox")
expect(frame.get_by_role('checkbox')).to_have_count(6)
root_checkbox = frame.get_by_role('checkbox').nth(0)
first_row_checkbox = frame.get_by_role('checkbox').nth(1)
second_row_checkbox = frame.get_by_role('checkbox').nth(2)
third_row_checkbox = frame.get_by_role('checkbox').nth(3)
fourth_row_checkbox = frame.get_by_role('checkbox').nth(4)
fifth_row_checkbox = frame.get_by_role('checkbox').nth(5)
none_root_checkboxes = [
first_row_checkbox,
second_row_checkbox,
third_row_checkbox,
fourth_row_checkbox,
fifth_row_checkbox
]

root_checkbox.check()
for checkbox in none_root_checkboxes:
expect(checkbox).to_be_checked()

main_checkbox.check()
expect(first_row_checkbox).to_be_checked()
first_row_checkbox.uncheck()
expect(root_checkbox).not_to_be_checked()
assert root_checkbox.get_attribute('data-indeterminate') == "true"

expect(page.get_by_text("0:0")).to_be_visible()
for checkbox in none_root_checkboxes:
checkbox.uncheck()

first_row_checkbox.uncheck()
expect(main_checkbox).not_to_be_checked()
expect(root_checkbox).not_to_be_checked()
assert root_checkbox.get_attribute('data-indeterminate') == 'false'

for checkbox in none_root_checkboxes:
checkbox.check()

expect(root_checkbox).to_be_checked()
assert root_checkbox.get_attribute('data-indeterminate') == 'false'
33 changes: 29 additions & 4 deletions template-reactless/e2e/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,40 @@ def go_to_app(page: Page, streamlit_app: StreamlitRunner):


def test_should_render_template(page: Page):
frame = page.frame_locator(
frame_0 = page.frame_locator(
'iframe[title="my_component\\.my_component"]'
).nth(0)
frame_1 = page.frame_locator(
'iframe[title="my_component\\.my_component"]'
).nth(1)

st_markdown_0 = page.get_by_role('paragraph').nth(0)
st_markdown_1 = page.get_by_role('paragraph').nth(1)

expect(st_markdown_0).to_contain_text("You've clicked 0 times!")

frame_0.get_by_role("button", name="Click me!").click()

expect(st_markdown_0).to_contain_text("You've clicked 1 times!")
expect(st_markdown_1).to_contain_text("You've clicked 0 times!")

frame_1.get_by_role("button", name="Click me!").click()
frame_1.get_by_role("button", name="Click me!").click()

expect(st_markdown_0).to_contain_text("You've clicked 1 times!")
expect(st_markdown_1).to_contain_text("You've clicked 2 times!")

page.get_by_label("Enter a name").click()
page.get_by_label("Enter a name").fill("World")
page.get_by_label("Enter a name").press("Enter")

expect(frame_1.get_by_text("Hello, World!")).to_be_visible()

expect(page.get_by_text("You've clicked 0 times!").first).to_be_visible()
frame_1.get_by_role("button", name="Click me!").click()

frame.get_by_role("button", name="Click me!").click()
expect(st_markdown_0).to_contain_text("You've clicked 1 times!")
expect(st_markdown_1).to_contain_text("You've clicked 3 times!")

expect(page.get_by_text("You've clicked 1 times!").first).to_be_visible()

def test_should_change_iframe_height(page: Page):
frame = page.frame_locator('iframe[title="my_component\\.my_component"]').nth(1)
Expand Down
33 changes: 29 additions & 4 deletions template/e2e/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,40 @@ def go_to_app(page: Page, streamlit_app: StreamlitRunner):


def test_should_render_template(page: Page):
frame = page.frame_locator(
frame_0 = page.frame_locator(
'iframe[title="my_component\\.my_component"]'
).nth(0)
frame_1 = page.frame_locator(
'iframe[title="my_component\\.my_component"]'
).nth(1)

st_markdown_0 = page.get_by_role('paragraph').nth(0)
st_markdown_1 = page.get_by_role('paragraph').nth(1)

expect(st_markdown_0).to_contain_text("You've clicked 0 times!")

frame_0.get_by_role("button", name="Click me!").click()

expect(st_markdown_0).to_contain_text("You've clicked 1 times!")
expect(st_markdown_1).to_contain_text("You've clicked 0 times!")

frame_1.get_by_role("button", name="Click me!").click()
frame_1.get_by_role("button", name="Click me!").click()

expect(st_markdown_0).to_contain_text("You've clicked 1 times!")
expect(st_markdown_1).to_contain_text("You've clicked 2 times!")

page.get_by_label("Enter a name").click()
page.get_by_label("Enter a name").fill("World")
page.get_by_label("Enter a name").press("Enter")

expect(frame_1.get_by_text("Hello, World!")).to_be_visible()

expect(page.get_by_text("You've clicked 0 times!").first).to_be_visible()
frame_1.get_by_role("button", name="Click me!").click()

frame.get_by_role("button", name="Click me!").click()
expect(st_markdown_0).to_contain_text("You've clicked 1 times!")
expect(st_markdown_1).to_contain_text("You've clicked 3 times!")

expect(page.get_by_text("You've clicked 1 times!").first).to_be_visible()

def test_should_change_iframe_height(page: Page):
frame = page.frame_locator('iframe[title="my_component\\.my_component"]').nth(1)
Expand Down

0 comments on commit f4ea096

Please sign in to comment.