Skip to content

Commit f68314a

Browse files
authored
Merge pull request #480 from Webperf-se/url-unavailable
Change so tests return -1 in rating if Tests are unable to connect
2 parents d01b7f4 + ab45d07 commit f68314a

25 files changed

+205
-55
lines changed

helpers/sitespeed_helper.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import json
3+
import os
34
import urllib
45
import urllib.parse
56

@@ -37,6 +38,9 @@ def get_data_from_sitespeed(filename, org_domain):
3738
if filename == '':
3839
return result
3940

41+
if not os.path.exists(filename):
42+
return result
43+
4044
# Fix for content having unallowed chars
4145
with open(filename, encoding='utf-8') as json_input_file:
4246
har_data = json.load(json_input_file)
88 Bytes
Binary file not shown.

locales/en/LC_MESSAGES/webperf-core.po

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,48 @@ msgstr "website with url: {0} has been added\n"
2121
msgid "TEXT_WEBSITE_URL_DELETED"
2222
msgstr "website with url: {0} has been deleted\n"
2323

24-
#: default.py:24
2524
msgid "TEXT_TEST_START_HEADER"
2625
msgstr "###############################################"
2726

28-
#: default.py:28
2927
msgid "TEXT_TESTING_NUMBER_OF_SITES"
3028
msgstr "Number of websites being tested {0}"
3129

32-
#: default.py:33
3330
msgid "TEXT_TESTING_SITE"
3431
msgstr "# Testing website {0}\n"
3532

3633
msgid "TEXT_WEBSITE_X_OF_Y"
3734
msgstr "Website {0} of {1}.\n"
3835

39-
#: default.py:61
4036
msgid "TEXT_SITE_RATING"
4137
msgstr "### Rating:"
4238

43-
#: default.py:63
4439
msgid "TEXT_SITE_REVIEW"
4540
msgstr "### Review:\n"
4641

47-
#: default.py:81
4842
msgid "TEXT_EXCEPTION"
4943
msgstr "Exception, someone should look at this!"
5044

45+
msgid "TEXT_SITE_UNAVAILABLE"
46+
msgstr "Error, was unable to load the page you requested."
47+
5148
msgid "TEXT_TEST_START"
5249
msgstr "Started: {0}"
5350

5451
msgid "TEXT_TEST_END"
5552
msgstr "Finished: {0}\n"
5653

57-
#: default.py:127
5854
msgid "TEXT_TEST_VALID_ARGUMENTS"
5955
msgstr "Valid arguments for option -t/--test:"
6056

61-
#: default.py:128
6257
msgid "TEXT_TEST_VALID_ARGUMENTS_GOOGLE_LIGHTHOUSE"
6358
msgstr "-t 1\t: Performance (Google Lighthouse)"
6459

65-
#: default.py:129
6660
msgid "TEXT_TEST_VALID_ARGUMENTS_GOOGLE_LIGHTHOUSE_SEO"
6761
msgstr "-t 4\t: SEO (Google Lighthouse)"
6862

69-
#: default.py:130
7063
msgid "TEXT_TEST_VALID_ARGUMENTS_GOOGLE_LIGHTHOUSE_A11Y"
7164
msgstr "-t 10\t: Accessibility (Google Lighthouse)"
7265

73-
#: default.py:132
7466
msgid "TEXT_TEST_VALID_ARGUMENTS_GOOGLE_LIGHTHOUSE_BEST_PRACTICE"
7567
msgstr "-t 5\t: Best Practice (Google Lighthouse)"
7668

@@ -80,26 +72,21 @@ msgstr "-t 17\t: Quality on frontend (Yellow Lab Tools)"
8072
msgid "TEXT_TEST_VALID_ARGUMENTS_PA11Y"
8173
msgstr "-t 18\t: Accessibility (Pa11y)"
8274

83-
#: default.py:133
8475
msgid "TEXT_TEST_VALID_ARGUMENTS_PAGE_NOT_FOUND"
8576
msgstr "-t 2\t: 404 (Page not Found)"
8677

87-
#: default.py:134
8878
msgid "TEXT_TEST_VALID_ARGUMENTS_HTML"
8979
msgstr "-t 6\t: HTML Validation"
9080

91-
#: default.py:135
9281
msgid "TEXT_TEST_VALID_ARGUMENTS_CSS"
9382
msgstr "-t 7\t: CSS Validation"
9483

9584
msgid "TEXT_TEST_VALID_ARGUMENTS_SITESPEED"
9685
msgstr "-t 15\t: Performance (Sitespeed.io)"
9786

98-
#: default.py:136
9987
msgid "TEXT_TEST_VALID_ARGUMENTS_WEBBKOLL"
10088
msgstr "-t 20\t: Integrity & Security (Webbkoll)"
10189

102-
#: default.py:137
10390
msgid "TEXT_TEST_VALID_ARGUMENTS_STANDARD_FILES"
10491
msgstr "-t 9\t: Standard files"
10592

@@ -121,7 +108,6 @@ msgstr "-t 25\t: Software (Alpha)"
121108
msgid "TEXT_TEST_VALID_ARGUMENTS_A11Y_STATEMENT"
122109
msgstr "-t 26\t: Accessibility Statement (Alfa)"
123110

124-
#: default.py:137 default.py:141 default.py:146 default.py:213
125111
msgid "TEXT_COMMAND_USAGE"
126112
msgstr "\n\tWebPerf Core\n\n\tUsage:\ndefault.py -u https://webperf.se\n\n\tOptions and arguments:\n\t-h/--help\t\t\t: Help information on how to use script\n\t-u/--url <site url>\t\t: website url to test against\n\t-t/--test <test number>\t\t: run ONE test (use ? to list available tests)\n\t-r/--review\t\t\t: show reviews in terminal\n\t-i/--input <file path>\t\t: input file path (.json/.sqlite/.sitemap/<category name>.webprf)\n\t-i/--input-skip <number>\t: number of items to skip\n\t-i/--input-take <number>\t: number of items to take\n\t-o/--output <file path>\t\t: output file path (.json/.csv/.sql/.sqlite/.md)\n\t-A/--addUrl <site url>\t\t: website url (required in compination with -i/--input)\n\t-D/--deleteUrl <site url>\t: website url (required in compination with -i/--input)\n\t-L/--language <lang code>\t: language used for output(en = default/sv)"
127113

88 Bytes
Binary file not shown.

locales/gov/LC_MESSAGES/webperf-core.po

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ msgstr "### Review:\n"
4848
msgid "TEXT_EXCEPTION"
4949
msgstr "Exception, someone should look at this!"
5050

51+
msgid "TEXT_SITE_UNAVAILABLE"
52+
msgstr "Error, was unable to load the page you requested."
53+
5154
msgid "TEXT_TEST_START"
5255
msgstr "Started: {0}"
5356

90 Bytes
Binary file not shown.

locales/sv/LC_MESSAGES/webperf-core.po

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ msgstr "### Omdöme:\n"
4242
msgid "TEXT_EXCEPTION"
4343
msgstr "Fel, någon behöver ta en titt på detta."
4444

45+
msgid "TEXT_SITE_UNAVAILABLE"
46+
msgstr "Fel, det gick inte att läsa in den begärda sidan."
47+
4548
msgid "TEXT_TEST_START"
4649
msgstr "Startad: {0}"
4750

tests/a11y_pa11y.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ def run_test(global_translation, lang_code, url):
3434

3535
use_axe = False
3636
json_result = get_pa11y_errors(url, use_axe)
37+
# If we fail to connect to website the result_dict will be None and we should end test
38+
if json_result is None:
39+
error_rating = Rating(global_translation, REVIEW_SHOW_IMPROVEMENTS_ONLY)
40+
error_rating.overall_review = global_translation('TEXT_SITE_UNAVAILABLE')
41+
return (error_rating, {'failed': True })
42+
3743
num_errors = len(json_result)
3844
if num_errors == 0:
3945
use_axe = True
@@ -42,16 +48,10 @@ def run_test(global_translation, lang_code, url):
4248

4349
num_errors = len(json_result)
4450

45-
unique_errors = set()
4651
return_dict = json_result
4752
errors = json_result
4853

49-
for error in errors:
50-
if 'message' in error:
51-
err_mess = error['message'].replace('This', 'A')
52-
error_review = f'- {err_mess}\n'
53-
unique_errors.add(error_review)
54-
54+
unique_errors = get_unique_errors(errors)
5555
num_unique_errors = len(unique_errors)
5656

5757
rating = rate_errors(global_translation,
@@ -62,6 +62,21 @@ def run_test(global_translation, lang_code, url):
6262

6363
return (rating, return_dict)
6464

65+
def get_unique_errors(errors):
66+
"""
67+
Gets unique errors from a list of many errors
68+
69+
Parameters:
70+
errors (list): The list of errors.
71+
"""
72+
unique_errors = set()
73+
for error in errors:
74+
if 'message' in error:
75+
err_mess = error['message'].replace('This', 'A')
76+
error_review = f'- {err_mess}\n'
77+
unique_errors.add(error_review)
78+
return unique_errors
79+
6580
def rate_errors(
6681
global_translation,
6782
local_translation,
@@ -183,5 +198,9 @@ def get_pa11y_errors(url, use_axe):
183198
f"--ignore color-contrast --reporter json {additional_args}{url}")
184199
with subprocess.Popen(command.split(), stdout=subprocess.PIPE) as process:
185200
output, _ = process.communicate(timeout=REQUEST_TIMEOUT * 10)
201+
202+
# If we fail to connect to website the result_dict should be None and we should end test
203+
if output is None or len(output) == 0:
204+
return None
186205
json_result = json.loads(output)
187206
return json_result

tests/a11y_statement.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,46 @@ def run_test(global_translation, lang_code, url):
6060
info = {'called_url': url}
6161
rating += rate_statement(info, global_translation, local_translation)
6262

63+
if not rating.isused():
64+
rating += get_rating_not_set_rating(url, return_dict, local_translation, global_translation)
65+
6366
print(global_translation('TEXT_TEST_END').format(
6467
datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
6568

6669
return (rating, return_dict)
6770

71+
def get_rating_not_set_rating(url, return_dict, local_translation, global_translation):
72+
"""
73+
This function checks the availability of a URL and sets the rating based on its availability.
74+
75+
Args:
76+
url (str): The URL to be checked.
77+
return_dict (dict): A dictionary to store the failure status if the URL is not available.
78+
local_translation (function): A function to translate text to local language.
79+
global_translation (function): A function to translate text to global language.
80+
81+
Returns:
82+
Rating: An instance of the Rating class with the
83+
overall review set based on the URL's availability.
84+
"""
85+
rating = Rating(global_translation, REVIEW_SHOW_IMPROVEMENTS_ONLY)
86+
url_is_available = False
87+
for _, url_content in checked_urls.items():
88+
if url_content != '' and url_content is not None:
89+
url_is_available = True
90+
break
91+
92+
if not url_is_available:
93+
rating.overall_review = global_translation('TEXT_SITE_UNAVAILABLE')
94+
return_dict['failed'] = True
95+
return rating
96+
97+
rating.set_overall(1.0, local_translation(
98+
'TEXT_REVIEW_NO_ACCESSIBILITY_STATEMENT'))
99+
rating.overall_review = local_translation('TEXT_REVIEW_CALLED_URL').format(
100+
url, rating.overall_review)
101+
return rating
102+
68103

69104
def get_digg_report_canonical():
70105
"""

tests/css_validator_w3c.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ def run_test(global_translation, lang_code, url):
3030
print(global_translation('TEXT_TEST_START').format(
3131
datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
3232

33+
rating = Rating(global_translation, REVIEW_SHOW_IMPROVEMENTS_ONLY)
34+
data = get_data_for_url(url)
35+
if data is None:
36+
rating.overall_review = global_translation('TEXT_SITE_UNAVAILABLE')
37+
return (rating, {'failed': True })
38+
39+
40+
# 2. FIND ALL INLE CSS (AND CALCULTE)
41+
# 2.1 FINS ALL <STYLE>
42+
all_link_resources = []
43+
3344
result_dict = {
3445
'has_style_elements': False,
3546
'has_style_attributes': False,
@@ -42,14 +53,6 @@ def run_test(global_translation, lang_code, url):
4253
}
4354
}
4455

45-
# We don't need extra iterations for what we are using it for
46-
data = get_data_for_url(url)
47-
# 2. FIND ALL INLE CSS (AND CALCULTE)
48-
# 2.1 FINS ALL <STYLE>
49-
all_link_resources = []
50-
51-
rating = Rating(global_translation, REVIEW_SHOW_IMPROVEMENTS_ONLY)
52-
5356
for html_entry in data['htmls']:
5457
tmp_all_link_resources, tmp_rating = handle_html_markup_entry(
5558
html_entry,

0 commit comments

Comments
 (0)