-
Notifications
You must be signed in to change notification settings - Fork 0
/
InlineCSSFinder.py
197 lines (169 loc) · 8.58 KB
/
InlineCSSFinder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#The main script for extracting Inline CSS from list of sites. Uses Selenium webdriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
import logging
#Include Excel functions for saving data in excel
from openpyxl import Workbook,load_workbook
from openpyxl.style import Color, Fill
col1='A' #For Excel Manipulation of Cells -------------
col2='B' #For Excel Manipulation of Cells -------------
class test1(unittest.TestCase):
def setUp(self):
FORMAT = "%(message)-s"
logging.basicConfig(format=FORMAT,filename='reportInlineCSS.txt', filemode='a', level=logging.INFO)
self.logger2 = logging.getLogger("f2")#Create second logger
self.handler2 = logging.FileHandler('reportInlineCSS.txt',mode='a')#Create Filehandler
self.handler2.setLevel(logging.INFO)#Set verbosity of filehandler
self.handler_format2 = logging.Formatter("%(message)-s")#Set Formatter
self.handler2.setFormatter(self.handler_format2)#Add formatter to handler
self.logger2.addHandler(self.handler2)#Add handler to logger2
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(2)
self.base_url = "http://"
self.verificationErrors = []
self.accept_next_alert = True
def test_2(self):
driver = self.driver
def Close_popUps(current_handle):
#Close any popup windows and return to main window
for handle in self.driver.window_handles:
if handle!=current_handle:
self.driver.switch_to_window(handle)
self.driver.close()
self.driver.switch_to_window(current_handle)
#row=0 #For Excel Manipulation of Cells -------------
total_counter=0
fail_counter=0
L=list()# The list of urls that need revalidation after testing failure
NoSitesLoaded=list()
#Printing Blog to Excel-------------
#row = row+1
#_cell1 = ws.cell('%s%s'%(col1, row))
#_cell2 = ws.cell('%s%s'%(col2, row))
#_cell1.style.fill.fill_type = Fill.FILL_SOLID
#_cell1.style.fill.start_color.index = 'FF9999'
#_cell1.style.font.color.index = Color.WHITE
#_cell2.style.font.color.index = Color.WHITE
#_cell2.style.fill.fill_type = Fill.FILL_SOLID
#_cell2.style.fill.start_color.index = 'FF9999'
#_cell1.value = 'A/A'
#_cell2.value = 'FIREFOX AUTOMATION TESTING'
for url in lines:
try:
self.driver.get(url) #Go to the specified url
#main_window_ID=self.driver.current_window_handle#Save ID of main window
except:
fail_counter +=1#increase fail counter by1
print('Error with url parser! Bypassing URL: %s')% ( self.driver.current_url)
#Printing Blog to Excel-------------
NoSitesLoaded.append(self.driver.current_url)#Add URL to revalidation list
continue#return to for loop and run test for the next URL
total_counter=total_counter+1
self.logger2.propagate = False
self.logger2.info("============================================================")
logging.info("======== %d Test STARTED for url:%s ==========",total_counter,self.driver.current_url)
#Printing Blog to Excel-------------
#row = row+1
#ws.cell('%s%s'%(col1, row)).value = '***********************************************************'
#ws.cell('%s%s'%(col2, row)).value = '*********************#%s Test STARTED**************************************' % (total_counter)
#End of Printing Blog to Excel-------------
#Close any popup windows and return to main window
#Close_popUps(main_window_ID)
try:
self.assertEqual("",driver.find_element_by_xpath("//style").text)#Check if js is inserted in html page source
css_links=driver.find_elements_by_tag_name('style')
#PRINT BLOCK
#row= row+1
#ws.cell('%s%s'%(col1, row)).value = '%d' % (total_counter)
#ws.cell('%s%s'%(col2, row)).value = ' *****************#URL: %s*****************' % (self.driver.current_url)
#END OF PRINT BLOCK
for links in css_links:
linkcss_text=links.get_attribute('outerHTML')
#print(linkcss_text)
#PRINT BLOCK
#row= row+1
#_cell = ws.cell('%s%s'%(col2, row))
#_cell.style.fill.fill_type = Fill.FILL_SOLID
#_cell.style.fill.start_color.index = '99CCFF'
#ws.cell('%s%s'%(col1, row)).value = '%s%s' % (" ", " ")
#_cell.value = '#%s : CSS file'% (linkcss_text)
#print("Found jQuery")
#END OF PRINT BLOCK
logging.info("%s",linkcss_text)
continue
except:
###
#row= row+1
#ws.cell('%s%s'%(col1, row)).value = '%d' % (total_counter)
#ws.cell('%s%s'%(col2, row)).value = ' *****************#URL: %s*****************' % (self.driver.current_url)
#row= row+1
#_cell = ws.cell('%s%s'%(col2, row))
#_cell.style.fill.fill_type = Fill.FILL_SOLID
#_cell.style.fill.start_color.index = 'B2FF66'
#ws.cell('%s%s'%(col1, row)).value = '%s%s' % (" ", " ")
#_cell.value = '#Not Found any css link'
###
print("Not Found inline css in url:"+self.driver.current_url)
L.append(self.driver.current_url)#Add URL to revalidation list
fail_counter +=1
#logging.info("Not Found any css link")
#logging.info("#%d Failed to display.",fail_counter)
continue#return to for loop and run test for the next URL
self.logger2.info("===========================================================================")
self.logger2.info("===========================================================================")
percentage_fail=(fail_counter/total_counter)*100#calculate percentage failure
logging.warning("Total sites visited: %d",total_counter )#print total sites tested
logging.warning("Sites CSS not appeared: %d, percentage: %d %%", fail_counter, percentage_fail)
logging.warning("URLS needing revalidation: %s", L)#Print revalidation list
logging.warning("URLS that do not work: %s", NoSitesLoaded)#Print revalidation list
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
logging.shutdown()#Close all loggers and handlers
self.driver.quit()#Close browser
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
log_file = "reportInlineCSS.txt"
f = open(log_file, "a")
#Read testing urls from file urls.txt
lines = open("urls.txt").read().splitlines()
#Settings for Printing to Excel-------------
"""try:
wb = load_workbook(filename = r'allcss.xlsx')
ws = wb.create_sheet()
ws.title = "Firefox JS"
except:
wb = Workbook()
ws = wb.create_sheet()
ws.title = "Firefox JS"
dest_filename = r'allcss.xlsx'
"""
try:
unittest.main()
#unittest.main()
except Exception:
pass
finally:
f.close()
#wb.save(filename = dest_filename)#Save Finally to Excel-------------