Test `find_children` using a flask server (#541)
This commit is contained in:
Родитель
bdf705716c
Коммит
c8c802aea8
|
@ -9,3 +9,4 @@ pytest==7.1.3
|
|||
setuptools==65.5.0
|
||||
wheel==0.37.1
|
||||
webdriver-manager==3.8.4
|
||||
flask==2.2.2
|
|
@ -1,10 +1,15 @@
|
|||
# encoding: utf-8
|
||||
import multiprocessing
|
||||
import unittest
|
||||
|
||||
from selenium import webdriver
|
||||
from selenium.common.exceptions import WebDriverException
|
||||
from webdriver_manager.firefox import GeckoDriverManager
|
||||
|
||||
from coverage_crawler import crawler
|
||||
from tests.example_website import website_app
|
||||
from tests.example_website.website_app import WEBSITE_TITLE
|
||||
from tests.example_website.website_app import run_server
|
||||
|
||||
|
||||
class TestCrawler(unittest.TestCase):
|
||||
|
@ -14,7 +19,7 @@ class TestCrawler(unittest.TestCase):
|
|||
self.driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
|
||||
|
||||
def tearDown(self):
|
||||
self.driver.close()
|
||||
self.driver.quit()
|
||||
|
||||
def test_close_all_windows_except_first(self):
|
||||
"""
|
||||
|
@ -28,3 +33,52 @@ class TestCrawler(unittest.TestCase):
|
|||
crawler.close_all_windows_except_first(self.driver)
|
||||
|
||||
assert (len(self.driver.window_handles) == 1), 'some windows were not closed properly.'
|
||||
|
||||
|
||||
class TestCrawlerLive(unittest.TestCase):
|
||||
SERVER_SETUP_TRIES = 10
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.server = multiprocessing.Process(target=run_server)
|
||||
cls.server.start()
|
||||
test_driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
|
||||
for try_id in range(cls.SERVER_SETUP_TRIES):
|
||||
try:
|
||||
print(f'class setup tries: {try_id}/{cls.SERVER_SETUP_TRIES}')
|
||||
test_driver.get(website_app.WEBSITE_URL)
|
||||
assert test_driver.title == WEBSITE_TITLE
|
||||
test_driver.quit()
|
||||
return
|
||||
except WebDriverException as e:
|
||||
print('got exception:', e)
|
||||
|
||||
cls.server.terminate()
|
||||
cls.server.join()
|
||||
cls.fail(cls, 'website did not start up correctly.')
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.server.terminate()
|
||||
cls.server.join()
|
||||
|
||||
def setUp(self):
|
||||
self.driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
|
||||
|
||||
def tearDown(self):
|
||||
self.driver.quit()
|
||||
|
||||
def test_find_children(self):
|
||||
"""
|
||||
when example server is open,
|
||||
verify that find_children returns the expected children.
|
||||
"""
|
||||
expected_link_text = 'i am the first link in the first div'
|
||||
|
||||
self.driver.get(website_app.WEBSITE_URL)
|
||||
assert (self.driver.title == WEBSITE_TITLE), f'incorrect driver title: {self.driver.title}'
|
||||
|
||||
children = crawler.find_children(self.driver)
|
||||
num_children = len(children)
|
||||
assert (num_children == 1), f'incorrect number of children found: {num_children}'
|
||||
assert (children[0].text == expected_link_text)
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>coverage-crawler example website</title>
|
||||
</head>
|
||||
<body>
|
||||
i am a body
|
||||
<div>i am the first div
|
||||
<p>i am the first paragraph of the first div
|
||||
</p>
|
||||
<a href="google.com">i am the first link in the first div</a>
|
||||
</div>
|
||||
|
||||
<div>i am the second div</div>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,22 @@
|
|||
# encoding: utf-8
|
||||
import flask
|
||||
from flask import render_template
|
||||
|
||||
WEBSITE_DOMAIN = 'localhost'
|
||||
WEBSITE_PORT = 8000
|
||||
WEBSITE_URL = f'{WEBSITE_DOMAIN}:{WEBSITE_PORT}'
|
||||
WEBSITE_TITLE = 'coverage-crawler example website'
|
||||
app = flask.Flask(__name__)
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
|
||||
|
||||
def run_server(debug=False):
|
||||
app.run(host=WEBSITE_DOMAIN, port=WEBSITE_PORT, debug=debug)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_server(debug=True)
|
Загрузка…
Ссылка в новой задаче