1
1
from pathlib import Path
2
2
3
3
import pytest
4
-
5
- from playwright .sync_api import Page , expect
6
-
7
4
from e2e_utils import StreamlitRunner
5
+ from playwright .sync_api import Page , expect
8
6
9
7
ROOT_DIRECTORY = Path (__file__ ).parent .parent .absolute ()
10
8
BASIC_EXAMPLE_FILE = ROOT_DIRECTORY / "material_login" / "example.py"
11
9
10
+
12
11
@pytest .fixture (autouse = True , scope = "module" )
13
12
def streamlit_app ():
14
13
with StreamlitRunner (BASIC_EXAMPLE_FILE ) as runner :
@@ -22,41 +21,53 @@ def go_to_app(page: Page, streamlit_app: StreamlitRunner):
22
21
page .get_by_role ("img" , name = "Running..." ).is_hidden ()
23
22
24
23
24
+ def _get_username_error_text (frame : Page ):
25
+ return frame .get_by_text ("username must be a valid email" )
26
+
27
+
28
+ def _get_password_error_text (frame : Page ):
29
+ return frame .get_by_text ("password is a required field" )
30
+
31
+
25
32
def test_should_render_material_login (page : Page ):
26
- frame = page .frame_locator (
27
- 'iframe[title="material_login\\ .material_login"]'
28
- )
29
- component_return_value = page .get_by_test_id ('stMarkdownContainer' )
30
- component_return_value_json = page .get_by_test_id ('stJson' )
33
+ frame = page .frame_locator ('iframe[title="material_login\\ .material_login"]' )
34
+ component_return_value = page .get_by_test_id ("stMarkdownContainer" )
35
+ component_return_value_json = page .get_by_test_id ("stJson" )
31
36
32
37
email_field = frame .get_by_placeholder ("username@domain.com" )
33
38
password_field = frame .get_by_placeholder ("your password" )
34
- invalid_username_text = frame .get_by_text ("username must be a valid email" )
35
- invalid_password_text = frame .get_by_text ("password is a required field" )
36
39
login_button = frame .get_by_role ("button" , name = "Login" )
37
40
cancel_button = frame .get_by_role ("button" , name = "Cancel" )
38
41
42
+ expect (_get_username_error_text (frame )).not_to_be_attached ()
43
+ expect (_get_password_error_text (frame )).not_to_be_attached ()
44
+
39
45
email_field .click ()
40
46
email_field .fill ("hello" )
47
+ # blur the email field to trigger validation
48
+ email_field .blur ()
41
49
42
- expect (invalid_username_text ).to_be_visible ()
43
- expect (invalid_password_text ). to_be_visible ()
50
+ expect (_get_username_error_text ( frame ) ).to_be_visible ()
51
+ expect (_get_password_error_text ( frame )). not_to_be_attached ()
44
52
expect (login_button ).to_be_disabled ()
45
53
46
54
email_field .click ()
47
55
email_field .fill ("hello@hello.com" )
48
56
password_field .click ()
49
57
password_field .fill ("password" )
50
- expect (invalid_username_text ).to_be_hidden ()
51
- expect (invalid_password_text ).to_be_hidden ()
52
- expect (component_return_value ).to_have_text ('None' )
58
+ # blur the password field to trigger validation
59
+ password_field .blur ()
60
+
61
+ expect (_get_username_error_text (frame )).not_to_be_attached ()
62
+ expect (_get_password_error_text (frame )).not_to_be_attached ()
63
+ expect (component_return_value ).to_have_text ("None" )
53
64
login_button .click ()
54
65
55
- expect (component_return_value_json ).to_contain_text (" \" username\" : \ " hello@hello.com\" " )
56
- expect (component_return_value_json ).to_contain_text (" \" password\" : \ " password\" " )
66
+ expect (component_return_value_json ).to_contain_text ('" username": "hello@hello.com"' )
67
+ expect (component_return_value_json ).to_contain_text ('" password": "password"' )
57
68
cancel_button .click ()
58
69
expect (email_field ).to_be_empty ()
59
70
expect (password_field ).to_be_empty ()
60
- expect (invalid_username_text ). to_be_hidden ()
61
- expect (invalid_password_text ). to_be_hidden ()
62
- expect (component_return_value_json ).to_have_text ('{}' )
71
+ expect (_get_username_error_text ( frame )). not_to_be_attached ()
72
+ expect (_get_password_error_text ( frame )). not_to_be_attached ()
73
+ expect (component_return_value_json ).to_have_text ("{}" )
0 commit comments