def test_assertions_response_is_ok_fail(page: Page, server: Server) -> None: response = page.request.get(server.PREFIX + "/unknown") with pytest.raises(AssertionError) as excinfo: expect(response).to_be_ok() error_message = str(excinfo.value) assert ("→ GET " + server.PREFIX + "/unknown") in error_message assert "← 404 Not Found" in error_message
def test_locator_query_should_filter_by_regex_with_quotes( page: Page, server: Server ) -> None: page.set_content('<div>Hello "world"</div><div>Hello world</div>') expect(page.locator("div", has_text=re.compile('Hello "world"'))).to_have_text( 'Hello "world"' )
def test_locator_query_should_filter_by_regex_and_regexp_flags( page: Page, server: Server ) -> None: page.set_content('<div>Hello "world"</div><div>Hello world</div>') expect( page.locator("div", has_text=re.compile('hElLo "world', re.IGNORECASE)) ).to_have_text('Hello "world"')
def test_assertions_page_to_have_url_with_base_url(browser: Browser, server: Server) -> None: page = browser.new_page(base_url=server.PREFIX) page.goto("/empty.html") expect(page).to_have_url("/empty.html") expect(page).to_have_url(re.compile(r".*/empty\.html")) page.close()
def test_ignore_case_regex(page: Page, server: Server, method: str) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<div id=target>apple BANANA</div><div>orange</div>") getattr(expect(page.locator("div#target")), method)(re.compile("apple BANANA")) getattr(expect(page.locator("div#target")), method)(re.compile("apple banana"), ignore_case=True) # defaults to regex flag with pytest.raises(AssertionError) as excinfo: getattr(expect(page.locator("div#target")), method)(re.compile("apple banana"), timeout=300) expected_error_msg = method.replace("_", " ") assert expected_error_msg in str(excinfo.value) # overrides regex flag with pytest.raises(AssertionError) as excinfo: getattr(expect(page.locator("div#target")), method)( re.compile("apple banana", re.IGNORECASE), ignore_case=False, timeout=300, ) assert expected_error_msg in str(excinfo.value) # Array Variants getattr(expect(page.locator("div")), method)([re.compile("apple BANANA"), re.compile("orange")]) getattr(expect(page.locator("div")), method)([re.compile("apple banana"), re.compile("ORANGE")], ignore_case=True) # defaults regex flag with pytest.raises(AssertionError) as excinfo: getattr(expect(page.locator("div")), method)( [re.compile("apple banana"), re.compile("ORANGE")], timeout=300, ) assert expected_error_msg in str(excinfo.value) # overrides regex flag with pytest.raises(AssertionError) as excinfo: getattr(expect(page.locator("div")), method)( [ re.compile("apple banana", re.IGNORECASE), re.compile("ORANGE", re.IGNORECASE), ], ignore_case=False, timeout=300, ) assert expected_error_msg in str(excinfo.value) # not variant getattr(expect(page.locator("div#target")), f"not_{method}")(re.compile("apple banana")) with pytest.raises(AssertionError) as excinfo: getattr(expect(page.locator("div#target")), f"not_{method}")( re.compile("apple banana"), ignore_case=True, timeout=300, ) assert f"not {expected_error_msg}" in str(excinfo)
def test_assertions_locator_to_be_focused(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<input type=checkbox>") my_checkbox = page.locator("input") with pytest.raises(AssertionError): expect(my_checkbox).to_be_focused(timeout=100) my_checkbox.focus() expect(my_checkbox).to_be_focused()
def test_to_have_values_fails_when_not_a_select_element( page: Page, server: Server) -> None: page.set_content(""" <input type="text"> """) locator = page.locator("input") with pytest.raises(Error) as excinfo: expect(locator).to_have_values(["R", "G"], timeout=500) assert "Error: Not a select element with a multiple attribute" in str( excinfo.value)
def test_to_have_values_works_with_regex(page: Page, server: Server) -> None: page.set_content(""" <select multiple> <option value="R">Red</option> <option value="G">Green</option> <option value="B">Blue</option> </select> """) locator = page.locator("select") locator.select_option(["R", "G"]) expect(locator).to_have_values([re.compile("R"), re.compile("G")])
def test_to_have_values_follows_labels(page: Page, server: Server) -> None: page.set_content(""" <label for="colors">Pick a Color</label> <select id="colors" multiple> <option value="R">Red</option> <option value="G">Green</option> <option value="B">Blue</option> </select> """) locator = page.locator("text=Pick a Color") locator.select_option(["R", "G"]) expect(locator).to_have_values(["R", "G"])
def test_assertions_locator_to_be_disabled_enabled(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<input type=checkbox>") my_checkbox = page.locator("input") expect(my_checkbox).not_to_be_disabled() expect(my_checkbox).to_be_enabled() with pytest.raises(AssertionError): expect(my_checkbox).to_be_disabled(timeout=100) my_checkbox.evaluate("e => e.disabled = true") expect(my_checkbox).to_be_disabled() with pytest.raises(AssertionError): expect(my_checkbox).to_be_enabled(timeout=100)
def test_to_have_values_exact_match_with_text(page: Page, server: Server) -> None: page.set_content(""" <select multiple> <option value="RR">Red</option> <option value="GG">Green</option> </select> """) locator = page.locator("select") locator.select_option(["RR", "GG"]) with pytest.raises(AssertionError) as excinfo: expect(locator).to_have_values(["R", "G"], timeout=500) assert "Locator expected to have Values '['R', 'G']'" in str(excinfo.value) assert "Actual value: ['RR', 'GG']" in str(excinfo.value)
def test_to_have_values_fails_when_multiple_not_specified( page: Page, server: Server) -> None: page.set_content(""" <select> <option value="R">Red</option> <option value="G">Green</option> <option value="B">Blue</option> </select> """) locator = page.locator("select") locator.select_option(["B"]) with pytest.raises(Error) as excinfo: expect(locator).to_have_values(["R", "G"], timeout=500) assert "Error: Not a select element with a multiple attribute" in str( excinfo.value)
def test_assertions_locator_to_have_js_property(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<div></div>") page.eval_on_selector( "div", "e => e.foo = { a: 1, b: 'string', c: new Date(1627503992000) }") expect(page.locator("div")).to_have_js_property( "foo", { "a": 1, "b": "string", "c": datetime.utcfromtimestamp(1627503992000 / 1000) }, )
def test_assertions_locator_to_be_hidden_visible(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<div style='width: 50px; height: 50px;'>Something</div>") my_checkbox = page.locator("div") expect(my_checkbox).to_be_visible() with pytest.raises(AssertionError): expect(my_checkbox).to_be_hidden(timeout=100) my_checkbox.evaluate("e => e.style.display = 'none'") expect(my_checkbox).to_be_hidden() with pytest.raises(AssertionError): expect(my_checkbox).to_be_visible(timeout=100)
def test_assertions_locator_to_contain_text(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<div id=foobar>kek</div>") expect(page.locator("div#foobar")).to_contain_text("kek") expect(page.locator("div#foobar")).not_to_contain_text("bar", timeout=100) with pytest.raises(AssertionError): expect(page.locator("div#foobar")).to_contain_text("bar", timeout=100) page.set_content("<div>Text \n1</div><div>Text2</div><div>Text3</div>") expect(page.locator("div")).to_contain_text( ["ext 1", re.compile("ext3")])
def test_assertions_locator_to_have_attribute(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<div id=foobar>kek</div>") expect(page.locator("div#foobar")).to_have_attribute("id", "foobar") expect(page.locator("div#foobar")).to_have_attribute( "id", re.compile("foobar")) expect(page.locator("div#foobar")).not_to_have_attribute("id", "kek", timeout=100) with pytest.raises(AssertionError): expect(page.locator("div#foobar")).to_have_attribute("id", "koko", timeout=100)
def test_assertions_locator_to_have_id(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<div class=foobar id=kek>kek</div>") expect(page.locator("div.foobar")).to_have_id("kek") expect(page.locator("div.foobar")).not_to_have_id("top", timeout=100) with pytest.raises(AssertionError): expect(page.locator("div.foobar")).to_have_id("top", timeout=100)
def test_assertions_locator_to_be_editable(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<input></input><button disabled>Text</button>") expect(page.locator("button")).not_to_be_editable() expect(page.locator("input")).to_be_editable() with pytest.raises(AssertionError): expect(page.locator("button")).to_be_editable(timeout=100)
def test_assertions_locator_to_be_empty(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content( "<input value=text name=input1></input><input name=input2></input>") expect(page.locator("input[name=input1]")).not_to_be_empty() expect(page.locator("input[name=input2]")).to_be_empty() with pytest.raises(AssertionError): expect(page.locator("input[name=input1]")).to_be_empty(timeout=100)
def test_assertions_locator_to_have_value(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<input type=text id=foo>") my_input = page.locator("#foo") expect(my_input).to_have_value("") expect(my_input).not_to_have_value("bar", timeout=100) my_input.fill("kektus") expect(my_input).to_have_value("kektus")
def test_assertions_locator_to_have_text(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<div id=foobar>kek</div>") expect(page.locator("div#foobar")).to_have_text("kek") expect(page.locator("div#foobar")).not_to_have_text("top", timeout=100) page.set_content("<div>Text \n1</div><div>Text 2a</div>") # Should only normalize whitespace in the first item. expect(page.locator("div")).to_have_text( ["Text 1", re.compile(r"Text \d+a")])
def test_assertions_locator_to_have_css(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content( "<div class=foobar style='color: rgb(234, 74, 90);'>kek</div>") expect(page.locator("div.foobar")).to_have_css("color", "rgb(234, 74, 90)") expect(page.locator("div.foobar")).not_to_have_css("color", "rgb(42, 42, 42)", timeout=100) with pytest.raises(AssertionError): expect(page.locator("div.foobar")).to_have_css("color", "rgb(42, 42, 42)", timeout=100)
def test_ignore_case(page: Page, server: Server, method: str) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<div id=target>apple BANANA</div><div>orange</div>") getattr(expect(page.locator("div#target")), method)("apple BANANA") getattr(expect(page.locator("div#target")), method)("apple banana", ignore_case=True) # defaults false with pytest.raises(AssertionError) as excinfo: getattr(expect(page.locator("div#target")), method)( "apple banana", timeout=300, ) expected_error_msg = method.replace("_", " ") assert expected_error_msg in str(excinfo.value) # Array Variants getattr(expect(page.locator("div")), method)(["apple BANANA", "orange"]) getattr(expect(page.locator("div")), method)(["apple banana", "ORANGE"], ignore_case=True) # defaults false with pytest.raises(AssertionError) as excinfo: getattr(expect(page.locator("div")), method)( ["apple banana", "ORANGE"], timeout=300, ) assert expected_error_msg in str(excinfo.value) # not variant getattr(expect(page.locator("div#target")), f"not_{method}")("apple banana") with pytest.raises(AssertionError) as excinfo: getattr(expect(page.locator("div#target")), f"not_{method}")( "apple banana", ignore_case=True, timeout=300, ) assert f"not {expected_error_msg}" in str(excinfo)
def test_assertions_should_serialize_regexp_correctly(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) page.set_content("<div>iGnOrEcAsE</div>") expect(page.locator("div")).to_have_text( re.compile(r"ignorecase", re.IGNORECASE)) page.set_content("""<div>start some lines between end</div>""") expect(page.locator("div")).to_have_text( re.compile(r"start.*end", re.DOTALL)) page.set_content("""<div>line1 line2 line3</div>""") expect(page.locator("div")).to_have_text( re.compile(r"^line2$", re.MULTILINE))
def test_dichotomous(self): page = self.page page.goto(self.url("/")) with page.expect_navigation(): page.locator("text=Create a new BMDS analysis").click() # set main input page.locator("#analysis_name").fill("abc") page.locator("#analysis_description").fill("def") page.locator("#analysis_model_type").select_option("D") # select models page.locator("#select_all_frequentist_restricted").click(click_count=2) page.locator("#select_all_frequentist_unrestricted").click( click_count=2) page.locator("#frequentist_unrestricted-Logistic").check() page.locator("#bayesian-Logistic").check() # view data tab page.locator('a:has-text("Data")').click() page.locator('button:has-text("New")').click() page.locator("text=Load an example dataset").click() # save current settings page.locator('a:has-text("Settings")').click() page.locator("text=Save Analysis").click() if self.can_execute: # execute and wait until complete page.locator("text=Run Analysis").click() expect(page.locator("#controlPanel")).to_contain_text( "Executing, please wait...") # view output summary tables page.locator('a:has-text("Output")').click() expect(page.locator( "#frequentist-model-result tbody tr")).to_have_count(2) expect(page.locator( "#bayesian-model-result tbody tr")).to_have_count(2) # display frequentist modal page.locator("#freq-result-0").click() expect(page.locator("#info-table tbody tr")).to_have_count(3) page.locator("#close-modal").click() # display bayesian modal page.locator("#bayesian-result-0").click() expect(page.locator("#info-table tbody tr")).to_have_count(3) page.locator("#close-modal").click() # display bayesian model average modal page.locator("text=Model Average").click() expect( page.locator("#ma-result-summary tbody tr")).to_have_count(3) page.locator("#close-modal").click() page.locator("#selection_model").select_option("0") page.locator("#selection_notes").fill("selected!") page.locator("#selection_submit").click() expect(page.locator(".toast")).to_contain_text( "Model selection updated.") page.locator('a:has-text("Logic")').click() expect(page.locator("#decision-logic tbody tr")).to_have_count(4) expect(page.locator("#rule-table tbody tr")).to_have_count(22)
def test_to_have_js_property_pass_undefined(page: Page) -> None: page.set_content("<div></div>") locator = page.locator("div") expect(locator).to_have_js_property( "foo", None) # Python does not have an undefined
def test_to_have_js_property_pass_null(page: Page) -> None: page.set_content("<div></div>") page.eval_on_selector("div", "e => e.foo = null") locator = page.locator("div") expect(locator).to_have_js_property("foo", None)
def test_assertions_response_is_ok_pass_with_not(page: Page, server: Server) -> None: response = page.request.get(server.PREFIX + "/unknown") expect(response).not_to_be_ok()
def test_assertions_response_is_ok_pass(page: Page, server: Server) -> None: response = page.request.get(server.EMPTY_PAGE) expect(response).to_be_ok()
def test_assertions_page_to_have_url(page: Page, server: Server) -> None: page.goto(server.EMPTY_PAGE) expect(page).to_have_url(server.EMPTY_PAGE) expect(page).to_have_url(re.compile(r".*/empty\.html")) with pytest.raises(AssertionError): expect(page).to_have_url("nooooo", timeout=750) with pytest.raises(AssertionError): expect(page).to_have_url(re.compile("not-the-url"), timeout=750) page.evaluate(""" setTimeout(() => { window.location = window.location.origin + '/grid.html'; }, 2000); """) expect(page).to_have_url(server.PREFIX + "/grid.html") expect(page).not_to_have_url(server.EMPTY_PAGE, timeout=750) with pytest.raises(AssertionError): expect(page).not_to_have_url(re.compile(r".*/grid\.html"), timeout=750) with pytest.raises(AssertionError): expect(page).not_to_have_url(server.PREFIX + "/grid.html", timeout=750) expect(page).to_have_url(re.compile(r".*/grid\.html")) expect(page).not_to_have_url("**/empty.html", timeout=750)