From d8b9f0fd780574720b6a43ba250b93c9d526735a Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Mon, 29 May 2023 17:44:38 +0200 Subject: [PATCH] Test improvement - Also test that custom request headers works with Playwright/Browserless (#1607) --- .github/workflows/test-only.yml | 5 +++++ changedetectionio/importer.py | 3 ++- changedetectionio/templates/edit.html | 2 ++ changedetectionio/tests/test_request.py | 24 +++++++++++++++++++----- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test-only.yml b/.github/workflows/test-only.yml index e87e925a..0881b8d7 100644 --- a/.github/workflows/test-only.yml +++ b/.github/workflows/test-only.yml @@ -55,6 +55,11 @@ jobs: # Playwright/Browserless fetch docker run --rm -e "PLAYWRIGHT_DRIVER_URL=ws://browserless:3000" --network changedet-network test-changedetectionio bash -c 'cd changedetectionio;pytest tests/fetchers/test_content.py && pytest tests/test_errorhandling.py && pytest tests/visualselector/test_fetch_data.py' + # Settings headers playwright tests - Call back in from Browserless, check headers + docker run --name "changedet" --hostname changedet --rm -e "FLASK_SERVER_NAME=changedet" -e "PLAYWRIGHT_DRIVER_URL=ws://browserless:3000?dumpio=true" --network changedet-network test-changedetectionio bash -c 'cd changedetectionio; pytest --live-server-host=0.0.0.0 --live-server-port=5004 tests/test_request.py' + docker run --name "changedet" --hostname changedet --rm -e "FLASK_SERVER_NAME=changedet" -e "WEBDRIVER_URL=http://selenium:4444/wd/hub" --network changedet-network test-changedetectionio bash -c 'cd changedetectionio; pytest --live-server-host=0.0.0.0 --live-server-port=5004 tests/test_request.py' + docker run --name "changedet" --hostname changedet --rm -e "FLASK_SERVER_NAME=changedet" -e "USE_EXPERIMENTAL_PUPPETEER_FETCH=yes" -e "PLAYWRIGHT_DRIVER_URL=ws://browserless:3000?dumpio=true" --network changedet-network test-changedetectionio bash -c 'cd changedetectionio; pytest --live-server-host=0.0.0.0 --live-server-port=5004 tests/test_request.py' + # restock detection via playwright - added name=changedet here so that playwright/browserless can connect to it docker run --rm --name "changedet" -e "FLASK_SERVER_NAME=changedet" -e "PLAYWRIGHT_DRIVER_URL=ws://browserless:3000" --network changedet-network test-changedetectionio bash -c 'cd changedetectionio;pytest --live-server-port=5004 --live-server-host=0.0.0.0 tests/restock/test_restock.py' diff --git a/changedetectionio/importer.py b/changedetectionio/importer.py index d49706a1..c39b7a68 100644 --- a/changedetectionio/importer.py +++ b/changedetectionio/importer.py @@ -52,7 +52,8 @@ class import_url_list(Importer): # Flask wtform validators wont work with basic auth, use validators package # Up to 5000 per batch so we dont flood the server - if len(url) and validators.url(url.replace('source:', '')) and good < 5000: + # @todo validators.url failed on local hostnames (such as referring to ourself when using browserless) + if len(url) and 'http' in url.lower() and good < 5000: extras = None if processor: extras = {'processor': processor} diff --git a/changedetectionio/templates/edit.html b/changedetectionio/templates/edit.html index 40b1101f..cfcc827b 100644 --- a/changedetectionio/templates/edit.html +++ b/changedetectionio/templates/edit.html @@ -159,6 +159,8 @@ User-Agent: wonderbra 1.0") }} {% else %} Headers can be also read from a file in your data-directory Read more here {% endif %} +
+ (Not supported by Selenium browser) diff --git a/changedetectionio/tests/test_request.py b/changedetectionio/tests/test_request.py index 06e518e1..51cc3970 100644 --- a/changedetectionio/tests/test_request.py +++ b/changedetectionio/tests/test_request.py @@ -10,8 +10,12 @@ def test_setup(live_server): # Hard to just add more live server URLs when one test is already running (I think) # So we add our test here (was in a different file) def test_headers_in_request(client, live_server): + #live_server_setup(live_server) # Add our URL to the import page test_url = url_for('test_headers', _external=True) + if os.getenv('PLAYWRIGHT_DRIVER_URL'): + # Because its no longer calling back to localhost but from browserless, set in test-only.yml + test_url = test_url.replace('localhost', 'changedet') # Add the test URL twice, we will check res = client.post( @@ -30,7 +34,7 @@ def test_headers_in_request(client, live_server): ) assert b"1 Imported" in res.data - time.sleep(3) + wait_for_all_checks(client) cookie_header = '_ga=GA1.2.1022228332; cookie-preferences=analytics:accepted;' @@ -40,7 +44,7 @@ def test_headers_in_request(client, live_server): data={ "url": test_url, "tag": "", - "fetch_backend": "html_requests", + "fetch_backend": 'html_webdriver' if os.getenv('PLAYWRIGHT_DRIVER_URL') else 'html_requests', "headers": "xxx:ooo\ncool:yeah\r\ncookie:"+cookie_header}, follow_redirects=True ) @@ -48,7 +52,7 @@ def test_headers_in_request(client, live_server): # Give the thread time to pick up the first version - time.sleep(5) + wait_for_all_checks(client) # The service should echo back the request headers res = client.get( @@ -64,7 +68,7 @@ def test_headers_in_request(client, live_server): from html import escape assert escape(cookie_header).encode('utf-8') in res.data - time.sleep(5) + wait_for_all_checks(client) # Re #137 - Examine the JSON index file, it should have only one set of headers entered watches_with_headers = 0 @@ -80,6 +84,9 @@ def test_headers_in_request(client, live_server): def test_body_in_request(client, live_server): # Add our URL to the import page test_url = url_for('test_body', _external=True) + if os.getenv('PLAYWRIGHT_DRIVER_URL'): + # Because its no longer calling back to localhost but from browserless, set in test-only.yml + test_url = test_url.replace('localhost', 'cdio') res = client.post( url_for("import_page"), @@ -168,6 +175,9 @@ def test_body_in_request(client, live_server): def test_method_in_request(client, live_server): # Add our URL to the import page test_url = url_for('test_method', _external=True) + if os.getenv('PLAYWRIGHT_DRIVER_URL'): + # Because its no longer calling back to localhost but from browserless, set in test-only.yml + test_url = test_url.replace('localhost', 'cdio') # Add the test URL twice, we will check res = client.post( @@ -242,7 +252,11 @@ def test_headers_textfile_in_request(client, live_server): #live_server_setup(live_server) # Add our URL to the import page test_url = url_for('test_headers', _external=True) + if os.getenv('PLAYWRIGHT_DRIVER_URL'): + # Because its no longer calling back to localhost but from browserless, set in test-only.yml + test_url = test_url.replace('localhost', 'cdio') + print ("TEST URL IS ",test_url) # Add the test URL twice, we will check res = client.post( url_for("import_page"), @@ -260,7 +274,7 @@ def test_headers_textfile_in_request(client, live_server): data={ "url": test_url, "tag": "testtag", - "fetch_backend": "html_requests", + "fetch_backend": 'html_webdriver' if os.getenv('PLAYWRIGHT_DRIVER_URL') else 'html_requests', "headers": "xxx:ooo\ncool:yeah\r\n"}, follow_redirects=True )