@@ -13,18 +13,14 @@ def testsites(sites, test_type=None, only_test_untested_last_hours=24, order_by=
1313 * test_type=num|None to execute all available tests
1414 """
1515
16+ result = list ()
17+
1618 # TODO: implementera test_type=None
1719
1820 print ("###############################################" )
1921
2022 i = 1
2123
22- #sites = list()
23- #for row in result:
24- # site_id = row[0]
25- # website = row[1]
26- # sites.append([site_id, website])
27-
2824 print ('Webbadresser som testas:' , len (sites ))
2925
3026 for site in sites :
@@ -59,33 +55,39 @@ def testsites(sites, test_type=None, only_test_untested_last_hours=24, order_by=
5955 checkreport = str (the_test_result [1 ]).encode ('utf-8' ) # för att lösa encoding-probs
6056 jsondata = str (json_data ).encode ('utf-8' ) # --//--
6157
62- site_test = SiteTests (site_id = site_id , type_of_test = test_type , check_report = checkreport , rating = the_test_result [0 ], test_date = datetime .datetime .now (), json_check_data = jsondata )
58+ site_test = SiteTests (site_id = site_id , type_of_test = test_type , check_report = checkreport , rating = the_test_result [0 ], test_date = datetime .datetime .now (), json_check_data = jsondata ).todata ()
59+
60+ result .append (site_test )
6361
6462 the_test_result = None # 190506 för att inte skriva testresultat till sajter när testet kraschat. Måste det sättas till ''?
6563 except Exception as e :
6664 print ('FAIL!' , website , '\n ' , e )
6765 pass
6866
6967 i += 1
68+
69+ return result
7070
71- def testing (sites = list ([[ 0 , "https://webperf.se" ]]), test_type = - 1 ):
71+ def testing (sites , test_type = ALL_TESTS ):
7272 print ('### {0} ###' .format (datetime .datetime .now ().strftime ('%Y-%m-%d %H:%M:%S' )))
73+ tests = {}
7374 ##############
74- if (test_type == - 1 or test_type == GOOGLE_PAGESPEED ):
75+ if (test_type == ALL_TESTS or test_type == GOOGLE_PAGESPEED ):
7576 print ('###############################\n Kör test: 0 - Google Pagespeed' )
76- testsites (sites , test_type = GOOGLE_PAGESPEED )
77- if (test_type == - 1 or test_type == PAGE_NOT_FOUND ):
77+ tests [ 'google_pagespeed' ] = testsites (sites , test_type = GOOGLE_PAGESPEED )
78+ if (test_type == ALL_TESTS or test_type == PAGE_NOT_FOUND ):
7879 print ('###############################\n Kör test: 2 - 404-test' )
79- testsites (sites , test_type = PAGE_NOT_FOUND )
80- if (test_type == - 1 or test_type == HTML ):
80+ tests [ '404' ] = testsites (sites , test_type = PAGE_NOT_FOUND )
81+ if (test_type == ALL_TESTS or test_type == HTML ):
8182 print ('###############################\n Kör test: 6 - HTML' )
82- testsites (sites , test_type = HTML )
83- if (test_type == - 1 or test_type == CSS ):
83+ tests [ 'html' ] = testsites (sites , test_type = HTML )
84+ if (test_type == ALL_TESTS or test_type == CSS ):
8485 print ('###############################\n Kör test: 7 - CSS' )
85- testsites (sites , test_type = CSS )
86- if (test_type == - 1 or test_type == WEBBKOLL ):
86+ tests [ 'css' ] = testsites (sites , test_type = CSS )
87+ if (test_type == ALL_TESTS or test_type == WEBBKOLL ):
8788 print ('###############################\n Kör test: 20 - Webbkoll' )
88- testsites (sites , test_type = WEBBKOLL )
89+ tests ['webbkoll' ] = testsites (sites , test_type = WEBBKOLL )
90+ return tests
8991
9092def validate_test_type (test_type ):
9193 if test_type != HTML and test_type != PAGE_NOT_FOUND and test_type != CSS and test_type != WEBBKOLL and test_type != GOOGLE_PAGESPEED :
@@ -114,8 +116,9 @@ def main(argv):
114116 -o/--output <file path>\t : output file path (JSON)
115117 """
116118
117- test_type = - 1
119+ test_type = ALL_TESTS
118120 sites = list ()
121+ output_filename = ''
119122
120123 try :
121124 opts , args = getopt .getopt (argv ,"hu:t:i:o:" ,["help" ,"url" ,"test" , "input" , "output" ])
@@ -149,15 +152,15 @@ def main(argv):
149152 sites .append ([site ["id" ], site ["url" ]])
150153 pass
151154 elif opt in ("-o" , "--output" ): # output file path
152- # TODO: make it possible to store result(s) in json file
155+ output_filename = arg
153156 pass
154157
155158
156159 if (len (sites )):
157- if ( test_type != - 1 ):
158- testing ( sites , test_type = test_type )
159- else :
160- testing ( sites )
160+ siteTests = testing ( sites , test_type = test_type )
161+ if ( len ( output_filename ) > 0 ):
162+ with open ( output_filename , 'w' ) as outfile :
163+ json . dump ( siteTests , outfile )
161164 else :
162165 print (main .__doc__ )
163166
0 commit comments