How to use not_to_have_class method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test_ui.py

Source: test_ui.py Github

copy
1import re
2from playwright.sync_api import expect
3from mathesar.tests.integration.utils.locators import get_table_entry
4from mathesar.tests.integration.utils.table_actions import create_empty_table, close_tab
5from mathesar.tests.integration.utils.locators import get_tab
6
7
8def test_tabs(page, base_schema_url):
9    page.goto(base_schema_url)
10
11    # Create Table 0
12    create_empty_table(page)
13    table_0_entry = get_table_entry(page, "Table 0")
14    table_0_tab = get_tab(page, "Table 0")
15    close_tab(table_0_tab)
16
17    # Create Table 1
18    create_empty_table(page)
19    table_1_entry = get_table_entry(page, "Table 1")
20    table_1_tab = get_tab(page, "Table 1")
21    close_tab(table_1_tab)
22
23    # No tabs should be open
24    expect(table_0_tab).not_to_be_visible()
25    expect(table_1_tab).not_to_be_visible()
26
27    # Open Table 0
28    table_0_entry.click()
29    expect(table_0_entry.locator(".item")).to_have_class(re.compile("active"))
30    expect(table_0_tab).to_have_class(re.compile("active"))
31
32    # Open Table 1
33    table_1_entry.click()
34    expect(table_0_entry.locator(".item")).not_to_have_class(re.compile("active"))
35    expect(table_1_entry.locator(".item")).to_have_class(re.compile("active"))
36    expect(table_0_tab).not_to_have_class(re.compile("active"))
37    expect(table_1_tab).to_have_class(re.compile("active"))
38
39    # Switch to tab for Table 0
40    table_0_tab.click()
41    expect(table_0_entry.locator(".item")).to_have_class(re.compile("active"))
42    expect(table_1_entry.locator(".item")).not_to_have_class(re.compile("active"))
43    expect(table_0_tab).to_have_class(re.compile("active"))
44    expect(table_1_tab).not_to_have_class(re.compile("active"))
45
46    # Close tab for Table 0
47    close_tab(table_0_tab)
48    expect(table_0_entry.locator(".item")).not_to_have_class(re.compile("active"))
49    expect(table_1_entry.locator(".item")).to_have_class(re.compile("active"))
50    expect(table_0_tab).not_to_be_visible()
51    expect(table_1_tab).to_have_class(re.compile("active"))
52
53    # Close tab for Table 1
54    close_tab(table_1_tab)
55    expect(table_0_entry.locator(".item")).not_to_have_class(re.compile("active"))
56    expect(table_1_entry.locator(".item")).not_to_have_class(re.compile("active"))
57    expect(table_0_tab).not_to_be_visible()
58    expect(table_1_tab).not_to_be_visible()
59
Full Screen

test_records.py

Source: test_records.py Github

copy
1import re
2
3from playwright.sync_api import expect
4
5first_pk_cell_in_table = ".row .cell.is-pk >> nth=0"
6
7
8def test_add_row(page, go_to_patents_data_table):
9    # Note: This is the New Record button at the top of the table. I also tried
10    # to write a separate test for adding a row from the row placeholder at the
11    # bottom of the table, but that proved difficult to write because of the
12    # VirtualList so I abandoned it.
13
14    # This assertion is here to make sure that we wait for the records to load
15    # before clicking the "New Record" button. Otherwise the "created" class
16    # won't be applied to the row correctly due to a race condition bug.
17    # https://github.com/centerofci/mathesar/issues/1281
18    expect(page.locator("text=KSC-12871")).to_be_visible()
19
20    page.click("button:has-text('New Record')")
21    expect(page.locator(".row.created .cell.is-pk:has-text('1,394')")).to_be_visible()
22
23
24def test_sort_table_by_column(page, go_to_patents_data_table):
25    page.click("button:has-text('Title')")
26    page.click("button:has-text('Sort Descending')")
27    page.click("button:has-text('Status')")
28    page.click("button:has-text('Sort Ascending')")
29    expect(page.locator(first_pk_cell_in_table)).to_have_text("729")
30
31
32def test_increment_pagination(page, go_to_patents_data_table):
33    page.click("[aria-label='Goto Page 2']")
34    expect(page.locator(first_pk_cell_in_table)).to_have_text("501")
35
36
37def test_edit_cell(page, go_to_patents_data_table):
38    row = page.locator(".row:has-text('ARC-14231-3')")
39    input = row.locator("textarea")
40    all_changes_saved = page.locator("text=All changes saved")
41    row.locator(".cell:has-text('Issued')").dblclick()
42    new_value = "TEST"
43    input.fill(new_value)
44    page.keyboard.press("Escape")
45    expect(all_changes_saved).to_be_visible()
46    cell = row.locator(f".cell:has-text('{new_value}')")
47    expect(cell).to_have_class(re.compile("modified"))
48
49
50def test_delete_multiple_rows(page, go_to_patents_data_table):
51    page.hover(".row:has-text('ARC-14281-1')")
52    page.check(".row:has-text('ARC-14281-1') input[type='checkbox']")
53    page.hover(".row:has-text('ARC-14512-1')")
54    page.check(".row:has-text('ARC-14512-1') input[type='checkbox']")
55    page.click("button:has-text('Delete 2 records')")
56    expect(page.locator("text=ARC-14281-1")).not_to_be_visible()
57    expect(page.locator("text=ARC-14512-1")).not_to_be_visible()
58
59
60def test_keyboard_Cell_Moving(page, go_to_patents_data_table):
61    cell1 = page.locator(".cell:has-text('6445390') .cell-wrapper")
62    cell2 = page.locator(".cell:has-text('ARC-14275-1') .cell-wrapper")
63    cell3 = page.locator(".cell:has-text('6606612') .cell-wrapper")
64    cell1.click()
65    expect(cell1).to_have_class(re.compile("is-active"))
66    page.keyboard.press('ArrowLeft')
67    expect(cell1).not_to_have_class(re.compile("is-active"))
68    expect(cell2).to_have_class(re.compile("is-active"))
69    page.keyboard.press('ArrowRight')
70    expect(cell1).to_have_class(re.compile("is-active"))
71    expect(cell2).not_to_have_class(re.compile("is-active"))
72    page.keyboard.press('ArrowDown')
73    expect(cell1).not_to_have_class(re.compile("is-active"))
74    expect(cell3).to_have_class(re.compile("is-active"))
75    page.keyboard.press('ArrowUp')
76    expect(cell1).to_have_class(re.compile("is-active"))
77    expect(cell3).not_to_have_class(re.compile("is-active"))
78
Full Screen

test_boolean_type.py

Source: test_boolean_type.py Github

copy
1import re
2from playwright.sync_api import expect
3from mathesar.tests.integration.utils.table_actions import open_column_options_and_verify_type, get_cell_selector
4from mathesar.tests.integration.utils.component_actions import change_select_input_value
5from mathesar.tests.integration.utils.validators import expect_tab_to_be_visible
6
7
8display_tab_selector = ".type-option-tab:has-text('Display')"
9display_as_option_selector = "span:has-text('Display as') button"
10use_custom_label_option_selector = "span:has-text('Use Custom Labels') input[type='checkbox']"
11true_custom_label_option_selector = "span:has-text('Label for TRUE') input[type='text']"
12false_custom_label_option_selector = "span:has-text('Label for FALSE') input[type='text']"
13
14
15def expect_table_to_open(page):
16    expect_tab_to_be_visible(page, "All datatypes table")
17
18
19def test_boolean_options(page, go_to_all_types_table):
20    expect_table_to_open(page)
21    open_column_options_and_verify_type(page, "boolean_cb", "Boolean", "BOOLEAN")
22    open_column_options_and_verify_type(page, "boolean_dd", "Boolean", "BOOLEAN")
23
24
25def test_boolean_display_options(page, go_to_all_types_table):
26    expect_table_to_open(page)
27    open_column_options_and_verify_type(page, "boolean_cb", "Boolean", "BOOLEAN")
28    page.locator(display_tab_selector).click()
29    display_as_option_locator = page.locator(display_as_option_selector)
30    custom_label_option_locator = page.locator(use_custom_label_option_selector)
31    expect(display_as_option_locator).to_be_visible()
32    expect(display_as_option_locator).to_contain_text("Checkbox", use_inner_text=True)
33    expect(custom_label_option_locator).not_to_be_visible()
34    open_column_options_and_verify_type(page, "boolean_dd", "Boolean", "BOOLEAN")
35    page.locator(display_tab_selector).click()
36    display_as_option_locator = page.locator(display_as_option_selector)
37    expect(display_as_option_locator).to_be_visible()
38    expect(display_as_option_locator).to_contain_text("Dropdown", use_inner_text=True)
39    expect(custom_label_option_locator).to_be_visible()
40    assert page.is_checked(use_custom_label_option_selector) is False
41
42
43def test_boolean_disp_checkbox_to_dropdown(page, table_with_all_types, go_to_all_types_table):
44    expect_table_to_open(page)
45    open_column_options_and_verify_type(page, "boolean_cb", "Boolean", "BOOLEAN")
46    page.locator(display_tab_selector).click()
47    display_as_option_locator = page.locator(display_as_option_selector)
48    custom_label_option_locator = page.locator(use_custom_label_option_selector)
49    expect(display_as_option_locator).to_be_visible()
50    expect(display_as_option_locator).to_contain_text("Checkbox", use_inner_text=True)
51    expect(custom_label_option_locator).not_to_be_visible()
52    change_select_input_value(page, display_as_option_locator, "Dropdown")
53    expect(custom_label_option_locator).to_be_visible()
54    assert page.is_checked(use_custom_label_option_selector) is False
55    page.check(use_custom_label_option_selector)
56    true_custom_label_option_locator = page.locator(true_custom_label_option_selector)
57    false_custom_label_option_locator = page.locator(false_custom_label_option_selector)
58    expect(true_custom_label_option_locator).to_be_visible()
59    expect(true_custom_label_option_locator).to_have_value("true")
60    expect(false_custom_label_option_locator).to_be_visible()
61    expect(false_custom_label_option_locator).to_have_value("false")
62    page.fill(false_custom_label_option_selector, "")
63    expect(page.locator(".type-options-content")).to_contain_text("This is a required field", use_inner_text=True)
64    expect(false_custom_label_option_locator).to_have_class(re.compile("has-error"))
65    page.fill(true_custom_label_option_selector, "Truthy Value")
66    page.fill(false_custom_label_option_selector, "Falsy Value")
67    page.click("button:has-text('Save')")
68    expect(page.locator(".dropdown.column-opts-content")).not_to_be_visible()
69    second_row_cell_selector = get_cell_selector(page, table_with_all_types, 2, "boolean_cb")
70    expect(page.locator(second_row_cell_selector)).to_contain_text("Truthy Value", use_inner_text=True)
71    third_row_cell_selector = get_cell_selector(page, table_with_all_types, 3, "boolean_cb")
72    expect(page.locator(third_row_cell_selector)).to_contain_text("Falsy Value", use_inner_text=True)
73
74
75def test_boolean_disp_dropdown_to_checkbox(page, table_with_all_types, go_to_all_types_table):
76    expect_table_to_open(page)
77    open_column_options_and_verify_type(page, "boolean_dd", "Boolean", "BOOLEAN")
78    page.locator(display_tab_selector).click()
79    display_as_option_locator = page.locator(display_as_option_selector)
80    custom_label_option_locator = page.locator(use_custom_label_option_selector)
81    expect(display_as_option_locator).to_be_visible()
82    expect(display_as_option_locator).to_contain_text("Dropdown", use_inner_text=True)
83    expect(custom_label_option_locator).to_be_visible()
84    change_select_input_value(page, display_as_option_locator, "Checkbox")
85    expect(custom_label_option_locator).not_to_be_visible()
86    page.click("button:has-text('Save')")
87    expect(page.locator(".dropdown.column-opts-content")).not_to_be_visible()
88    second_row_cell_selector = get_cell_selector(page, table_with_all_types, 2, "boolean_dd")
89    second_row_checkbox_selector = f"{second_row_cell_selector} [type=checkbox]"
90    assert page.is_checked(second_row_checkbox_selector) is True
91    third_row_cell_selector = get_cell_selector(page, table_with_all_types, 3, "boolean_dd")
92    third_row_checkbox_selector = f"{third_row_cell_selector} [type=checkbox]"
93    assert page.is_checked(third_row_checkbox_selector) is False
94
95
96def test_boolean_cell_checkbox_input(page, table_with_all_types, go_to_all_types_table):
97    expect_table_to_open(page)
98    first_row_cell_selector = get_cell_selector(page, table_with_all_types, 1, "boolean_cb")
99    first_row_checkbox_selector = f"{first_row_cell_selector} [type=checkbox]"
100    assert page.locator(first_row_checkbox_selector).element_handle().evaluate("node => node.indeterminate") is True
101    page.check(first_row_checkbox_selector)
102    assert page.locator(first_row_checkbox_selector).element_handle().evaluate("node => node.indeterminate") is False
103    assert page.is_checked(first_row_checkbox_selector) is True
104    page.uncheck(first_row_checkbox_selector)
105    assert page.is_checked(first_row_checkbox_selector) is False
106    page.check(first_row_checkbox_selector)
107    assert page.is_checked(first_row_checkbox_selector) is True
108    second_row_cell_selector = get_cell_selector(page, table_with_all_types, 2, "boolean_cb")
109    second_row_checkbox_selector = f"{second_row_cell_selector} [type=checkbox]"
110    assert page.is_checked(second_row_checkbox_selector) is True
111    third_row_cell_selector = get_cell_selector(page, table_with_all_types, 3, "boolean_cb")
112    third_row_checkbox_selector = f"{third_row_cell_selector} [type=checkbox]"
113    assert page.is_checked(third_row_checkbox_selector) is False
114
115
116def test_boolean_cell_checkbox_click_behavior(page, table_with_all_types, go_to_all_types_table):
117    expect_table_to_open(page)
118    cell_selector = get_cell_selector(page, table_with_all_types, 1, "boolean_cb")
119    checkbox_selector = f"{cell_selector} [type=checkbox]"
120    expect(page.locator(cell_selector)).not_to_have_class(re.compile("is-active"))
121    assert page.locator(checkbox_selector).element_handle().evaluate("node => node.indeterminate") is True
122    page.click(cell_selector)
123    expect(page.locator(cell_selector)).to_have_class(re.compile("is-active"))
124    assert page.locator(checkbox_selector).element_handle().evaluate("node => node.indeterminate") is True
125    page.click(cell_selector)
126    assert page.is_checked(checkbox_selector) is True
127    page.click(cell_selector)
128    assert page.is_checked(checkbox_selector) is False
129
130
131def test_boolean_cell_checkbox_key_behavior(page, table_with_all_types, go_to_all_types_table):
132    expect_table_to_open(page)
133    cell_selector = get_cell_selector(page, table_with_all_types, 1, "boolean_cb")
134    checkbox_selector = f"{cell_selector} [type=checkbox]"
135    page.click(cell_selector)
136    expect(page.locator(cell_selector)).to_have_class(re.compile("is-active"))
137    assert page.locator(checkbox_selector).element_handle().evaluate("node => node.indeterminate") is True
138    page.keyboard.press("Enter")
139    assert page.is_checked(checkbox_selector) is True
140    page.keyboard.press("Enter")
141    assert page.is_checked(checkbox_selector) is False
142
143
144def test_boolean_cell_dropdown_input(page, table_with_all_types, go_to_all_types_table):
145    expect_table_to_open(page)
146    first_row_cell_selector = get_cell_selector(page, table_with_all_types, 1, "boolean_dd")
147    first_row_cell_locator = page.locator(first_row_cell_selector)
148    expect(first_row_cell_locator).to_contain_text("NULL", use_inner_text=True)
149    expect(first_row_cell_locator).not_to_have_class(re.compile("is-active"))
150    page.click(first_row_cell_selector)
151    expect(first_row_cell_locator).to_have_class(re.compile("is-active"))
152    dropdown_id = page.locator(f"{first_row_cell_selector} .cell-wrapper").get_attribute("aria-controls")
153    dropdown_selector = f".dropdown.single-select-cell-dropdown:has(ul#{dropdown_id})"
154    expect(page.locator(dropdown_selector)).not_to_be_visible()
155    page.click(first_row_cell_selector)
156    expect(page.locator(f"{first_row_cell_selector} .cell-wrapper")).to_be_focused()
157    expect(page.locator(dropdown_selector)).to_be_visible()
158    expect(page.locator(f"{dropdown_selector} li")).to_contain_text(["true", "false"])
159    page.click(f"{dropdown_selector} li:has-text('true')")
160    expect(page.locator(dropdown_selector)).not_to_be_visible()
161    expect(first_row_cell_locator).to_contain_text("true", use_inner_text=True)
162    expect(page.locator(f"{first_row_cell_selector} .cell-wrapper")).to_be_focused()
163    page.click(first_row_cell_selector)
164    expect(page.locator(dropdown_selector)).to_be_visible()
165    page.click(f"{dropdown_selector} li:has-text('false')")
166    expect(page.locator(dropdown_selector)).not_to_be_visible()
167    expect(first_row_cell_locator).to_contain_text("false", use_inner_text=True)
168    expect(page.locator(f"{first_row_cell_selector} .cell-wrapper")).to_be_focused()
169    second_row_cell_selector = get_cell_selector(page, table_with_all_types, 2, "boolean_dd")
170    expect(page.locator(second_row_cell_selector)).to_contain_text("true", use_inner_text=True)
171    third_row_cell_selector = get_cell_selector(page, table_with_all_types, 3, "boolean_dd")
172    expect(page.locator(third_row_cell_selector)).to_contain_text("false", use_inner_text=True)
173
174
175def test_boolean_cell_dropdown_key_behavior(page, table_with_all_types, go_to_all_types_table):
176    expect_table_to_open(page)
177    cell_selector = get_cell_selector(page, table_with_all_types, 1, "boolean_dd")
178    cell_locator = page.locator(cell_selector)
179    page.click(cell_selector)
180    expect(cell_locator).to_have_class(re.compile("is-active"))
181    dropdown_id = page.locator(f"{cell_selector} .cell-wrapper").get_attribute("aria-controls")
182    dropdown_selector = f".dropdown.single-select-cell-dropdown:has(ul#{dropdown_id})"
183    expect(page.locator(dropdown_selector)).not_to_be_visible()
184    expect(page.locator(f"{cell_selector} .cell-wrapper")).to_be_focused()
185    page.keyboard.press("Enter")
186    expect(page.locator(dropdown_selector)).to_be_visible()
187    page.keyboard.press("ArrowDown")
188    page.keyboard.press("Enter")
189    expect(page.locator(dropdown_selector)).not_to_be_visible()
190    expect(cell_locator).to_contain_text("true", use_inner_text=True)
191    expect(page.locator(f"{cell_selector} .cell-wrapper")).to_be_focused()
192
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)