Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Replace unittest asserts with pytest asserts #1289

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 37 additions & 40 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ def get_new_browser(self):
def test_can_open_page(self):
"""should be able to visit, get title and quit"""
self.browser.visit(EXAMPLE_APP)
self.assertEqual("Example Title", self.browser.title)
assert "Example Title" == self.browser.title

def test_can_back_on_history(self):
"""should be able to back on history"""
self.browser.visit(EXAMPLE_APP)
self.browser.visit(f"{EXAMPLE_APP}iframe")
self.browser.back()
self.assertEqual(EXAMPLE_APP, self.browser.url)
assert EXAMPLE_APP == self.browser.url

def test_can_forward_on_history(self):
"""User can forward history"""
Expand All @@ -83,50 +83,50 @@ def test_can_forward_on_history(self):
def test_should_have_html(self):
self.browser.visit(EXAMPLE_APP)
html = self.browser.html
self.assertIn("<title>Example Title</title>", html)
self.assertIn('<h1 id="firstheader">Example Header</h1>', html)
assert "<title>Example Title</title>" in html
assert '<h1 id="firstheader">Example Header</h1>' in html

def test_should_reload_a_page(self):
self.browser.visit(EXAMPLE_APP)
self.browser.reload()
self.assertEqual("Example Title", self.browser.title)
assert "Example Title" == self.browser.title

def test_should_have_url(self):
"should have access to the url"
self.assertEqual(EXAMPLE_APP, self.browser.url)
assert EXAMPLE_APP == self.browser.url

def test_accessing_attributes_of_links(self):
"should allow link's attributes retrieval"
foo = self.browser.links.find_by_text("FOO")
self.assertEqual("http://localhost:5000/foo", foo["href"])
assert "http://localhost:5000/foo" == foo["href"]

def test_accessing_attributes_of_inputs(self):
"should allow input's attributes retrieval"
button = self.browser.find_by_css('input[name="send"]')
self.assertEqual("send", button["name"])
assert "send" == button["name"]

def test_accessing_attributes_of_simple_elements(self):
"should allow simple element's attributes retrieval"
header = self.browser.find_by_css("h1")
self.assertEqual("firstheader", header["id"])
assert "firstheader" == header["id"]

def test_links_should_have_value_attribute(self):
foo = self.browser.links.find_by_href("http://localhost:5000/foo")
self.assertEqual("FOO", foo.value)
assert "FOO" == foo.value

def test_should_receive_browser_on_parent(self):
'element should contains the browser on "parent" attribute'
element = self.browser.find_by_id("firstheader")
self.assertEqual(self.browser, element.parent)
assert self.browser == element.parent

def test_redirection(self):
"""
when visiting /redirected, browser should be redirected to /redirected-location?come=get&some=true
browser.url should be updated
"""
self.browser.visit(f"{EXAMPLE_APP}redirected")
self.assertIn("I just been redirected to this location.", self.browser.html)
self.assertIn("redirect-location?come=get&some=true", self.browser.url)
assert "I just been redirected to this location." in self.browser.html
assert "redirect-location?come=get&some=true" in self.browser.url


class WebDriverTests(
Expand All @@ -135,14 +135,14 @@ class WebDriverTests(
IsElementPresentTest,
):
def test_status_code(self):
with self.assertRaises(NotImplementedError):
with pytest.raises(NotImplementedError):
self.browser.status_code

def test_can_open_page_in_new_tab(self):
"""should be able to visit url in a new tab"""
self.browser.windows.current.new_tab(EXAMPLE_APP)
self.browser.windows[1].is_current = True
self.assertEqual(EXAMPLE_APP, self.browser.url)
assert EXAMPLE_APP == self.browser.url
assert 2 == len(self.browser.windows)

self.browser.windows[0].is_current = True
Expand All @@ -151,56 +151,53 @@ def test_can_open_page_in_new_tab(self):
def test_can_execute_javascript(self):
"should be able to execute javascript"
self.browser.execute_script("$('body').empty()")
self.assertEqual("", self.browser.find_by_tag("body").value)
assert "" == self.browser.find_by_tag("body").value

def test_can_evaluate_script(self):
"should evaluate script"
self.assertEqual(8, self.browser.evaluate_script("4+4"))
assert 8 == self.browser.evaluate_script("4+4")

def test_can_see_the_text_for_an_element(self):
"should provide text for an element"
self.assertEqual(self.browser.find_by_id("simple_text").text, "my test text")
assert self.browser.find_by_id("simple_text").text == "my test text"

def test_the_text_for_an_element_strips_html_tags(self):
"should show that the text attribute strips html"
self.assertEqual(
self.browser.find_by_id("text_with_html").text,
"another bit of text",
)
assert self.browser.find_by_id("text_with_html").text == "another bit of text"

def test_can_verify_if_a_element_is_visible(self):
"should provide verify if element is visible"
self.assertTrue(self.browser.find_by_id("visible").visible)
assert self.browser.find_by_id("visible").visible

def test_can_verify_if_a_element_is_invisible(self):
"should provide verify if element is invisible"
self.assertFalse(self.browser.find_by_id("invisible").visible)
assert not self.browser.find_by_id("invisible").visible

def test_default_wait_time(self):
"should driver default wait time 2"
self.assertEqual(2, self.browser.wait_time)
assert 2 == self.browser.wait_time

def test_access_alerts_and_accept_them(self):
self.browser.visit(EXAMPLE_APP + "alert")
self.browser.find_by_tag("h1").click()
alert = self.browser.get_alert()
self.assertEqual("This is an alert example.", alert.text)
assert "This is an alert example." == alert.text
alert.accept()

def test_access_prompts_and_be_able_to_fill_then(self):
self.browser.visit(EXAMPLE_APP + "alert")
self.browser.find_by_tag("h2").click()

alert = self.browser.get_alert()
self.assertEqual("What is your name?", alert.text)
assert "What is your name?" == alert.text
alert.fill_with("Splinter")
alert.accept()

# Wait for alert
time.sleep(2.5)

response = self.browser.get_alert()
self.assertEqual("Splinter", response.text)
assert "Splinter" == response.text
response.accept()

def test_access_confirm_and_accept_and_dismiss_them(self):
Expand All @@ -209,73 +206,73 @@ def test_access_confirm_and_accept_and_dismiss_them(self):
self.browser.find_by_tag("h3").click()
alert = self.browser.get_alert()

self.assertEqual("Should I continue?", alert.text)
assert "Should I continue?" == alert.text
alert.accept()

# Wait for alert
time.sleep(2.5)

alert = self.browser.get_alert()
self.assertEqual("You say I should", alert.text)
assert "You say I should" == alert.text
alert.accept()

self.browser.find_by_tag("h3").click()
alert = self.browser.get_alert()
self.assertEqual("Should I continue?", alert.text)
assert "Should I continue?" == alert.text
alert.dismiss()

# Wait for alert
time.sleep(2.5)

alert = self.browser.get_alert()
self.assertEqual("You say I should not", alert.text)
assert "You say I should not" == alert.text
alert.accept()

def test_access_confirm_and_accept_and_dismiss_them_using_with(self):
self.browser.visit(EXAMPLE_APP + "alert")

self.browser.find_by_tag("h3").click()
with self.browser.get_alert() as alert:
self.assertEqual("Should I continue?", alert.text)
assert "Should I continue?" == alert.text
alert.accept()

# Wait for alert
time.sleep(2.5)

with self.browser.get_alert() as alert:
self.assertEqual("You say I should", alert.text)
assert "You say I should" == alert.text
alert.accept()

self.browser.find_by_tag("h3").click()
with self.browser.get_alert() as alert:
self.assertEqual("Should I continue?", alert.text)
assert "Should I continue?" == alert.text
alert.dismiss()

# Wait for alert
time.sleep(2.5)

with self.browser.get_alert() as alert:
self.assertEqual("You say I should not", alert.text)
assert "You say I should not" == alert.text
alert.accept()

def test_access_alerts_using_with(self):
"should access alerts using 'with' statement"
self.browser.visit(EXAMPLE_APP + "alert")
self.browser.find_by_tag("h1").click()
with self.browser.get_alert() as alert:
self.assertEqual("This is an alert example.", alert.text)
assert "This is an alert example." == alert.text
alert.accept()

def test_get_alert_return_none_if_no_alerts(self):
"should return None if no alerts available"
alert = self.browser.get_alert()
self.assertEqual(None, alert)
assert alert is None

def test_can_select_a_option_via_element_text(self):
"should provide a way to select a option via element"
self.assertFalse(self.browser.find_option_by_value("rj").selected)
assert not self.browser.find_option_by_value("rj").selected
self.browser.find_by_name("uf").select_by_text("Rio de Janeiro")
self.assertTrue(self.browser.find_option_by_value("rj").selected)
assert self.browser.find_option_by_value("rj").selected

def test_should_be_able_to_change_user_agent(self):
driver_name = self.browser.driver_name.lower()
Expand Down
6 changes: 3 additions & 3 deletions tests/click_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
class ClickElementsTest:
def test_click_links(self):
self.browser.links.find_by_text("FOO").click()
self.assertIn("BAR!", self.browser.html)
assert "BAR!" in self.browser.html

def test_click_element_by_css_selector(self):
self.browser.find_by_css('a[href="http://localhost:5000/foo"]').click()
self.assertIn("BAR!", self.browser.html)
assert "BAR!" in self.browser.html

def test_click_input_by_css_selector(self):
self.browser.find_by_css('input[name="send"]').click()
self.assertIn("My name is: Master Splinter", self.browser.html)
assert "My name is: Master Splinter" in self.browser.html
31 changes: 9 additions & 22 deletions tests/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,27 @@

class ElementTest:
def test_element_has_class_when_element_has_the_class_as_first_class(self):
self.assertTrue(
self.browser.find_by_css(".has-class-first").has_class("has-class-first"),
)
assert self.browser.find_by_css(".has-class-first").has_class("has-class-first")

def test_element_has_class_when_element_has_the_class_as_middle_class(self):
self.assertTrue(
self.browser.find_by_css(".has-class-middle").has_class("has-class-middle"),
)
assert self.browser.find_by_css(".has-class-middle").has_class("has-class-middle")

def test_element_has_class_when_element_has_the_class_as_end_class(self):
self.assertTrue(
self.browser.find_by_css(".has-class-end").has_class("has-class-end"),
)
assert self.browser.find_by_css(".has-class-end").has_class("has-class-end")

def test_element_has_class_when_element_doesnt_have_the_class(self):
self.assertFalse(
self.browser.find_by_css(".has-class-first").has_class("has-class"),
)
assert not self.browser.find_by_css(".has-class-first").has_class("has-class")

def test_element_outer_html(self):
self.assertEqual(
self.browser.find_by_id("html-property").outer_html,
assert self.browser.find_by_id("html-property").outer_html == (
'<div id="html-property" class="outer html classes">'
'inner <div class="inner-html">inner text</div> html test</div>',
'inner <div class="inner-html">inner text</div> html test</div>'
)

def test_element_html_with_breakline(self):
self.assertEqual(
self.browser.find_by_id("html-property-with-breakline").html,
"\\n some text here\\n",
)
assert self.browser.find_by_id("html-property-with-breakline").html == "\\n some text here\\n"

def test_element_html(self):
self.assertEqual(
self.browser.find_by_id("html-property").html,
'inner <div class="inner-html">inner text</div> html test',
assert (
self.browser.find_by_id("html-property").html == 'inner <div class="inner-html">inner text</div> html test'
)
28 changes: 14 additions & 14 deletions tests/element_does_not_exist.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
# Copyright 2012 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
import pytest

from splinter.exceptions import ElementDoesNotExist


class ElementDoestNotExistTest:
def test_element_query_should_raises_when_element_first_doest_exists(self):
with self.assertRaises(ElementDoesNotExist):
with pytest.raises(ElementDoesNotExist):
self.browser.find_by_css(".element-that-dont-exists").first

def test_element_list_raises_when_element_last_does_not_exists(self):
with self.assertRaises(ElementDoesNotExist):
with pytest.raises(ElementDoesNotExist):
self.browser.find_by_css(".element-that-dont-exists").last

def test_element_list_raises_when_element_does_not_exists(self):
with self.assertRaises(ElementDoesNotExist):
with pytest.raises(ElementDoesNotExist):
self.browser.find_by_css(".element-that-dont-exists")[2]

def test_element_list_raises_with_unicode_query(self):
with self.assertRaises(ElementDoesNotExist):
with pytest.raises(ElementDoesNotExist):
self.browser.find_by_css(".element[title=título]").last

def test_element_list_contains_right_information_and_raises_right_exception(self):
"element list contains right information about query and raises nice exception message"
with self.assertRaises(ElementDoesNotExist) as cm:
with pytest.raises(ElementDoesNotExist) as err:
element_list = self.browser.find_by_css(".element-that-dont-exists")
self.assertEqual("css", element_list.find_by)
self.assertEqual(".element-that-dont-exists", element_list.query)
assert "css" == element_list.find_by
assert ".element-that-dont-exists" == element_list.query
element_list.first

expected_message = 'No elements were found with css ".element-that-dont-exists"'

e = cm.exception
self.assertEqual(expected_message, e.args[0])
assert expected_message == err.value.args[0]

def test_element_list_raises_when_element_first_doesnt_exists_in_element_context(
self,
):
"element list raises exception with right information in element context"
with self.assertRaises(ElementDoesNotExist) as cm:
with pytest.raises(ElementDoesNotExist) as err:
element_list = self.browser.find_by_css("#inside").find_by_css(
".inner-element-that-dont-exists",
)
self.assertEqual("css", element_list.find_by)
self.assertEqual(".inner-element-that-dont-exists", element_list.query)
assert "css" == element_list.find_by
assert ".inner-element-that-dont-exists" == element_list.query
element_list.first

expected_message = 'No elements were found with css ".inner-element-that-dont-exists"'

e = cm.exception
self.assertEqual(expected_message, e.args[0])
assert expected_message == err.value.args[0]
Loading
Loading