Skip to content

Commit

Permalink
Standardise LXML driver tests (#1292)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler authored Jun 10, 2024
1 parent fb0b64e commit 8d828f7
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 379 deletions.
5 changes: 5 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def get_new_browser(self):
driver_name = self.browser.driver_name.lower()
return get_browser(driver_name)

def test_should_support_with_statement(self):
browser = self.get_new_browser()
with browser as b:
assert b is not None

def test_can_open_page(self):
"""should be able to visit, get title and quit"""
self.browser.visit(EXAMPLE_APP)
Expand Down
128 changes: 128 additions & 0 deletions tests/lxml_drivers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import os

import pytest

from .fake_webapp import EXAMPLE_APP


class LxmlDriverTests:
def test_cant_switch_to_frame(self):
"""lxml-based drivers should not be able to switch to frames"""
with pytest.raises(NotImplementedError) as err:
self.browser.get_iframe("frame_123")
self.fail()

assert f"{self.browser.driver_name.lower()} doesn't support frames." == err.value.args[0]

def test_attach_file(self):
"""should provide a way to change file field value"""
file_path = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
"mockfile.txt",
)
self.browser.attach_file("file", file_path)
self.browser.find_by_name("upload").click()

html = self.browser.html
assert "text/plain" in html
with open(file_path) as f:
assert f.read() in html

def test_forward_to_none_page(self):
"""lxml-based drivers should not fail when trying to forward to none"""
browser = self.get_new_browser()
browser.visit(EXAMPLE_APP)
browser.forward()
assert EXAMPLE_APP == browser.url
browser.quit()

def test_can_clear_password_field_content(self):
"""lxml-based drivers should not be able to clear"""
with pytest.raises(NotImplementedError):
self.browser.find_by_name("password").first.clear()

def test_can_clear_tel_field_content(self):
"""lxml-based drivers should not be able to clear"""
with pytest.raises(NotImplementedError):
self.browser.find_by_name("telephone").first.clear()

def test_can_clear_text_field_content(self):
"""lxml-based drivers should not be able to clear"""
with pytest.raises(NotImplementedError):
self.browser.find_by_name("query").first.clear()

def test_can_clear_textarea_content(self):
"""lxml-based drivers should not be able to clear"""
with pytest.raises(NotImplementedError):
self.browser.find_by_name("description").first.clear()

def test_can_clear_search_content(self):
"""lxml-based drivers should not be able to clear"""
with pytest.raises(NotImplementedError):
self.browser.find_by_name("search_keyword").first.clear()

def test_can_clear_url_content(self):
"""lxml-based drivers should not be able to clear"""
with pytest.raises(NotImplementedError):
self.browser.find_by_name("url_input").first.clear()

def test_simple_type(self):
"""
lxml-based drivers won't support type method
because it doesn't interact with JavaScript
"""
with pytest.raises(NotImplementedError):
self.browser.type("query", "with type method")

def test_simple_type_on_element(self):
"""
lxml-based drivers won't support type method
because it doesn't interact with JavaScript
"""
with pytest.raises(NotImplementedError):
self.browser.find_by_name("query").type("with type method")

def test_slowly_typing(self):
"""
lxml-based drivers won't support type method
because it doesn't interact with JavaScript
"""
with pytest.raises(NotImplementedError):
self.browser.type("query", "with type method", slowly=True)

def test_slowly_typing_on_element(self):
"""
lxml-based drivers won't support type method
on element because it doesn't interac with JavaScript
"""
with pytest.raises(NotImplementedError):
query = self.browser.find_by_name("query")
query.type("with type method", slowly=True)

def test_cant_mouseover(self):
"""lxml-based drivers should not be able to put the mouse over the element"""
with pytest.raises(NotImplementedError):
self.browser.find_by_css("#visible").mouse_over()

def test_cant_mouseout(self):
"""lxml-based drivers should not be able to mouse out of an element"""
with pytest.raises(NotImplementedError):
self.browser.find_by_css("#visible").mouse_out()

def test_finding_all_links_by_non_ascii_text(self):
"""lxml-based drivers should find links by non ascii text"""
non_ascii_encodings = {
"pangram_pl": "Jeżu klątw, spłódź Finom część gry hańb!",
"pangram_ja": "天 地 星 空",
"pangram_ru": "В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!", # NOQA RUF001
"pangram_eo": "Laŭ Ludoviko Zamenhof bongustas freŝa ĉeĥa manĝaĵo kun spicoj.",
}
for key, text in non_ascii_encodings.items():
link = self.browser.links.find_by_text(text)
assert key == link["id"]

def test_links_with_nested_tags_xpath(self):
links = self.browser.find_by_xpath('//a/span[text()="first bar"]/..')
assert len(links) == 1, 'Found more than one link with a span with text "BAR ONE". %s' % [
item.outer_html for item in links
]
116 changes: 3 additions & 113 deletions tests/test_djangoclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .base import BaseBrowserTests
from .base import get_browser
from .fake_webapp import EXAMPLE_APP
from splinter import Browser
from .lxml_drivers import LxmlDriverTests


sys.path.append("tests/fake_django")
Expand All @@ -21,7 +21,7 @@
django.setup()


class TestDjangoClientDriver(BaseBrowserTests):
class TestDjangoClientDriver(LxmlDriverTests, BaseBrowserTests):
@pytest.fixture(autouse=True, scope="class")
def setup_browser(self, request):
request.cls.browser = get_browser("django")
Expand All @@ -31,116 +31,6 @@ def setup_browser(self, request):
def visit_example_app(self):
self.browser.visit(EXAMPLE_APP)

def test_should_support_with_statement(self):
with Browser("django") as internet:
assert internet is not None

def test_attach_file(self):
"should provide a way to change file field value"
file_path = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
"mockfile.txt",
)
self.browser.attach_file("file", file_path)
self.browser.find_by_name("upload").click()

html = self.browser.html
assert "text/plain" in html
with open(file_path) as f:
assert f.read() in html

def test_forward_to_none_page(self):
"should not fail when trying to forward to none"
browser = Browser("django")
browser.visit(EXAMPLE_APP)
browser.forward()
assert EXAMPLE_APP == browser.url
browser.quit()

def test_can_clear_password_field_content(self):
"django should not be able to clear"
with pytest.raises(NotImplementedError):
self.browser.find_by_name("password").first.clear()

def test_can_clear_tel_field_content(self):
"django should not be able to clear"
with pytest.raises(NotImplementedError):
self.browser.find_by_name("telephone").first.clear()

def test_can_clear_text_field_content(self):
"django should not be able to clear"
with pytest.raises(NotImplementedError):
self.browser.find_by_name("query").first.clear()

def test_cant_switch_to_frame(self):
"django driver should not be able to switch to frames"
with pytest.raises(NotImplementedError) as err:
self.browser.get_iframe("frame_123")
self.fail()

assert "django doesn't support frames." == err.value.args[0]

def test_simple_type(self):
"""
django won't support type method
because it doesn't interact with JavaScript
"""
with pytest.raises(NotImplementedError):
self.browser.type("query", "with type method")

def test_simple_type_on_element(self):
"""
django won't support type method
because it doesn't interact with JavaScript
"""
with pytest.raises(NotImplementedError):
self.browser.find_by_name("query").type("with type method")

def test_slowly_typing(self):
"""
django won't support type method
because it doesn't interact with JavaScript
"""
with pytest.raises(NotImplementedError):
self.browser.type("query", "with type method", slowly=True)

def test_slowly_typing_on_element(self):
"""
django won't support type method
on element because it doesn't interac with JavaScript
"""
with pytest.raises(NotImplementedError):
query = self.browser.find_by_name("query")
query.type("with type method", slowly=True)

def test_cant_mouseover(self):
"django should not be able to put the mouse over the element"
with pytest.raises(NotImplementedError):
self.browser.find_by_css("#visible").mouse_over()

def test_cant_mouseout(self):
"django should not be able to mouse out of an element"
with pytest.raises(NotImplementedError):
self.browser.find_by_css("#visible").mouse_out()

def test_links_with_nested_tags_xpath(self):
links = self.browser.find_by_xpath('//a/span[text()="first bar"]/..')
assert len(links) == 1, 'Found more than one link with a span with text "BAR ONE". %s' % [
item.outer_html for item in links
]

def test_finding_all_links_by_non_ascii_text(self):
"should find links by non ascii text"
non_ascii_encodings = {
"pangram_pl": "Jeżu klątw, spłódź Finom część gry hańb!",
"pangram_ja": "天 地 星 空",
"pangram_ru": "В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!", # NOQA RUF001
"pangram_eo": "Laŭ Ludoviko Zamenhof bongustas freŝa ĉeĥa manĝaĵo kun spicoj.",
}
for key, text in non_ascii_encodings.items():
link = self.browser.links.find_by_text(text)
assert key == link["id"]

def test_cookies_extra_parameters(self):
"""Cookie can be created with extra parameters."""
timestamp = int(time.time() + 120)
Expand All @@ -159,7 +49,7 @@ def setup_browser(self, request):
request.cls.browser = get_browser("django", custom_headers=custom_headers)
request.addfinalizer(request.cls.browser.quit)

def test_create_a_phantomjs_with_custom_headers(self):
def test_create_a_browser_with_custom_headers(self):
self.browser.visit(EXAMPLE_APP + "headers")
assert self.browser.is_text_present("X-Splinter-Customheaders-1: Hello")

Expand Down
Loading

0 comments on commit 8d828f7

Please sign in to comment.