def test_URLFilter(self): u'''URL过滤 www.163.com,控制IP段,生效时间内无法访问163,可以访问百度''' baidutitle = getAssertText('baidutitle') login.loginWeb(self) # admin账号登录 self.driver.implicitly_wait(10) timePlan = timePlanPage(self.driver, self.url) # 创建时间计划 为当天 timePlan.click_sysObj() time.sleep(0.5) timePlan.click_timePlan() time.sleep(1) # 操作删除 以访已有规则 timePlan.click_selall() time.sleep(0.2) timePlan.click_delall() time.sleep(1) try: self.driver.implicitly_wait(2) timePlan.find_ok() except NoSuchElementException: try: timePlan.find_tipsshowin() time.sleep(1) except NoSuchElementException: pass else: time.sleep(1) print('时间计划列表已删除') timePlan.click_add() time.sleep(1) timePlan.input_TimeRangeName('TimePlan') timePlan.click_save() time.sleep(1) # 断言 开启提示信息是否有误 list_name = str(timePlan.getText_byXpath(timePlan.listName)) time.sleep(1) self.assertEqual(list_name, 'TimePlan', msg='时间段名 与配置的不一致') print('时间计划已添加') # 获取本机ip 默认有线地址,有线断开会显示无线 pcaddr = socket.gethostbyname(socket.getfqdn(socket.gethostname())) #配置防火墙 firewall = AccessControlPage(self.driver, self.url) firewall.click_FireWall() time.sleep(0.5) firewall.click_AccessControl() time.sleep(1) firewall.click_add() time.sleep(1) firewall.input_PolicyNames('URLfilter') firewall.click_sourceIP() time.sleep(1) firewall.click_usergroup() time.sleep(1) # 选择IP段 用自身IP firewall.click_userip() time.sleep(1) firewall.input_starip(pcaddr) firewall.input_endip(pcaddr) firewall.click_saveW1() time.sleep(1) seltime = firewall.selelement_byName('timeGrpName') time.sleep(1) Select(seltime).select_by_value('TimePlan') selFilterTypes = firewall.selelement_byName(firewall.selFilterTypes) time.sleep(1) Select(selFilterTypes).select_by_value('2') #URL过滤 firewall.input_glnrUrl('www.163.com') firewall.click_save() time.sleep(1) # 开启访问控制 checkTrafficS = firewall.getAttribute_byId( firewall.checkTrafficS, 'checktype') # checktype 0未开启,1开启 if checkTrafficS == '0': firewall.click_checkTraffic() time.sleep(2) print('访问控制策略 已添加') #断言 list_content = str(firewall.getText_byXpath(firewall.list_Content)) self.assertEqual(list_content, 'www.163.com', msg='过滤内容不为"www.163.com"') checkTrafficS = firewall.getAttribute_byId( firewall.checkTrafficS, 'checktype') #checktype 0未开启,1开启 self.assertEqual(checkTrafficS, '1', msg='访问控制 未开启') self.driver.quit() # 访问官网,使用打不开网页的 title做断言 time.sleep(5) self.dr = webdriver.Chrome() # self.dr.maximize_window() self.dr.implicitly_wait(10) self.dr.set_page_load_timeout(10) # selenium超时设置/等待时间过长自动停止 # 清除浏览器cookies cookies = self.dr.get_cookies() print(f"main: cookies = {cookies}") self.dr.delete_all_cookies() # 因为被过滤,会打不开网站,,命令执行后会一直处于命令执行中,最后报超时错误,默认超时300s # selenium.common.exceptions.TimeoutException: Message: timeout # 配合上文 set_page_load_timeout(10)设置等待超时时间,这里等待10s,判断报超时错误则正常,否则失败 time.sleep(3) # 尝试访问百度 应可以 time.sleep(1) self.dr.get('http://www.baidu.com') title2 = self.dr.title print(title2) self.assertEqual(title2, baidutitle, msg='URL过滤开启后 不能打开其他网页') print('百度正常打开 验证成功') # 尝试访问163 应不可以 try: self.dr.get('http://www.163.com/') except TimeoutException: print('163禁止 验证成功') else: raise Exception('163禁止失败') self.dr.close() logger.info('test_URLFilter passed')
def tearDown(self): nodata = getAssertText('nodata') # 关闭防火墙,删除禁止策略 login.loginWeb(self) # admin账号登录 self.driver.implicitly_wait(10) firewall = AccessControlPage(self.driver, self.url) firewall.click_FireWall() time.sleep(0.5) firewall.click_AccessControl() time.sleep(1) firewall.click_checkTraffic() time.sleep(1) checkTrafficS = firewall.getAttribute_byId( firewall.checkTrafficS, 'checktype') # checktype 0未开启,1开启 self.assertEqual(checkTrafficS, '0', msg='访问控制 未关闭') print('访问控制策略 已关闭') firewall.click_delete() time.sleep(1) firewall.click_ok() time.sleep(1) # 断言 listnodata = str(firewall.getText_byXpath(firewall.listnodata)) self.assertEqual(listnodata, nodata, msg='策略删除失败') print('策略已删除') # 删除时间计划 timePlan = timePlanPage(self.driver, self.url) timePlan.click_sysObj() time.sleep(0.5) timePlan.click_timePlan() time.sleep(1) timePlan.click_delete() time.sleep(1) timePlan.click_ok() time.sleep(1) # 断言 listtips2 = str(timePlan.getText_byXpath(timePlan.listnodata)) self.assertEqual(listtips2, nodata, msg='时间计划删除失败') print('时间计划已删除') self.driver.quit() logger.info('tearDown over') logger.info('%s' % ('=' * 50))
def test_DNSfilter(self): u'''DNS过滤 www.baidu.com,无法ping通百度,可以ping news.baidu.com和官网''' # 调用脚本修改dns为114.114.114.114,清空dns缓存 os.system('%s' % (batpath + 'changeStaticDns_flushdns.bat')) time.sleep(5) login.loginWeb(self) # admin账号登录 self.driver.implicitly_wait(10) #配置防火墙 firewall = AccessControlPage(self.driver, self.url) firewall.click_FireWall() time.sleep(0.5) firewall.click_AccessControl() time.sleep(1) firewall.click_add() time.sleep(1) firewall.input_PolicyNames('DNSfilter') selFilterTypes = firewall.selelement_byName(firewall.selFilterTypes) time.sleep(1) Select(selFilterTypes).select_by_value('4') #DNS过滤 firewall.input_glnrdns('www.baidu.com') firewall.click_save() time.sleep(1) # 开启访问控制 checkTrafficS = firewall.getAttribute_byId( firewall.checkTrafficS, 'checktype') # checktype 0未开启,1开启 if checkTrafficS == '0': firewall.click_checkTraffic() time.sleep(2) print('访问控制策略 已添加') #断言 list_content = str(firewall.getText_byXpath(firewall.list_Content)) self.assertEqual(list_content, 'www.baidu.com', msg='过滤内容不为"www.baidu.com"') checkTrafficS = firewall.getAttribute_byId( firewall.checkTrafficS, 'checktype') #checktype 0未开启,1开启 self.assertEqual(checkTrafficS, '1', msg='访问控制 未开启') self.driver.quit() # 判断联网 ,应该无法解析ping通www.baidu.com,能解析ping通访问news.baidu.com和www.utt.com.cn time.sleep(5) p = pingTestIP('www.baidu.com') if p == 'Y': raise Exception('百度 依旧可以ping通') p = pingTestIP('news.baidu.com') if p == 'N': raise Exception('news.baidu.com 无法ping通') p = pingTestIP('www.utt.com.cn') if p == 'N': raise Exception('www.utt.com.cn 无法ping通') time.sleep(1) logger.info('test_DNSfilter passed')
def test_002_filter(self): u'''ACL防火墙''' nodata = getAssertText('nodata') #创建时间计划 为当天 timePlan = timePlanPage(self.driver, self.url) timePlan.click_sysObj() time.sleep(0.5) timePlan.click_timePlan() time.sleep(1) timePlan.click_add() time.sleep(1) timePlan.input_TimeRangeName('TimePlan') timePlan.click_save() time.sleep(1) list_name = str(timePlan.getText_byXpath(timePlan.listName)) time.sleep(1) self.assertEqual(list_name, 'TimePlan', msg='时间段名 与配置的不一致') print('时间计划已添加') # 配置防火墙 #新建一条IP过滤 firewall = AccessControlPage(self.driver, self.url) firewall.click_FireWall() time.sleep(0.5) firewall.click_AccessControl() time.sleep(1) firewall.click_add() time.sleep(1) firewall.input_PolicyNames('IPfilter') firewall.click_sourceIP() time.sleep(1) firewall.click_usergroup() time.sleep(0.5) firewall.click_userip() time.sleep(0.5) firewall.click_userall() time.sleep(0.5) firewall.click_saveW1() # 弹窗中的保存 time.sleep(1) seltime = firewall.selelement_byName('timeGrpName') time.sleep(1) Select(seltime).select_by_value('TimePlan') time.sleep(0.5) selFilterTypes = firewall.selelement_byName(firewall.selFilterTypes) Select(selFilterTypes).select_by_value('1') # ip过滤 time.sleep(0.5) selProtocol = firewall.selelement_byName(firewall.selProtocol) Select(selProtocol).select_by_value('4') #4 AH time.sleep(0.3) Select(selProtocol).select_by_value('5') #all time.sleep(0.3) Select(selProtocol).select_by_value('1') #ICMP time.sleep(0.3) Select(selProtocol).select_by_value('2') #TCP time.sleep(0.3) Select(selProtocol).select_by_value('3') #UDP time.sleep(0.5) selservice = firewall.selelement_byName('cyfw') Select(selservice).select_by_value('137') time.sleep(0.5) firewall.click_save() time.sleep(1) #新建一条URL firewall.click_add() time.sleep(1) firewall.input_PolicyNames('URLfilter') firewall.click_sourceIP() time.sleep(1) firewall.click_usergroup() time.sleep(0.5) firewall.click_userip() time.sleep(0.5) firewall.click_userall() time.sleep(0.5) firewall.click_saveW1() # 弹窗中的保存 time.sleep(1) seltime = firewall.selelement_byName('timeGrpName') Select(seltime).select_by_value('TimePlan') time.sleep(0.5) selFilterTypes = firewall.selelement_byName(firewall.selFilterTypes) Select(selFilterTypes).select_by_value('4') # DNS过滤 time.sleep(1) Select(selFilterTypes).select_by_value('3') # 关键字过滤 time.sleep(1) Select(selFilterTypes).select_by_value('2') # URL过滤 time.sleep(1) firewall.input_glnrUrl('www.123.com') time.sleep(1) firewall.click_save() time.sleep(1) print('访问控制策略 已添加') # 开/关访问控制 checkTrafficS = firewall.getAttribute_byId( firewall.checkTrafficS, 'checktype') # checktype 0未开启,1开启 self.assertEqual(checkTrafficS, '0', msg='访问控制 默认未关闭') if checkTrafficS == '0': firewall.click_checkTraffic() time.sleep(2) checkTrafficS = firewall.getAttribute_byId(firewall.checkTrafficS, 'checktype') self.assertEqual(checkTrafficS, '1', msg='访问控制 开启失败') if checkTrafficS == '1': firewall.click_checkTraffic() time.sleep(3) checkTrafficS = firewall.getAttribute_byId(firewall.checkTrafficS, 'checktype') self.assertEqual(checkTrafficS, '0', msg='访问控制 关闭失败') #测试移动策略 list1_name = str(firewall.getText_byXpath(firewall.list1_name)) self.assertEqual(list1_name, 'IPfilter', msg='访问控制列表 顺序1不为 IPfilter') selmoveto_1 = firewall.selelement_byID(firewall.u_moveto_1) Select(selmoveto_1).select_by_value('URLfilter') time.sleep(0.5) firewall.click_movetosave() time.sleep(1) i = 0 while i < 30: firewall.click_AccessControl() time.sleep(1) list1_name = str(firewall.getText_byXpath(firewall.list1_name)) if list1_name == 'URLfilter': print('访问控制列表 移动验证通过') break else: time.sleep(1) i += 1 else: raise Exception(u'访问控制列表 移动出错') #删除 firewall.click_selall() time.sleep(0.5) firewall.click_delall() time.sleep(1) firewall.click_ok() time.sleep(1) listnodata = str(firewall.getText_byXpath(firewall.listnodata)) self.assertEqual(listnodata, nodata, msg='策略删除失败') print('策略已删除') # 删除时间计划 timePlan = timePlanPage(self.driver, self.url) timePlan.click_sysObj() time.sleep(0.5) timePlan.click_timePlan() time.sleep(1) timePlan.click_delete() time.sleep(1) timePlan.click_ok() time.sleep(1) # 断言 listtips2 = str(timePlan.getText_byXpath(timePlan.listnodata)) self.assertEqual(listtips2, nodata, msg='时间计划删除失败') self.driver.quit() logger.info('test_002_filter passed')
def test_IPfilter(self): u'''IP地址过滤tcp80 控制组织架构成员,生效时间内无法访问http网页''' organization_group.import_empty_template(self) # 判断组织架构是否有其他组 有则清空 organization_group.add_user(self) #将自身IP增加到组织架构中 login.loginWeb(self) # admin账号登录 self.driver.implicitly_wait(10) timePlan = timePlanPage(self.driver, self.url) # 创建时间计划 为当天 timePlan.click_sysObj() time.sleep(0.5) timePlan.click_timePlan() time.sleep(1) # 操作删除 以访已有规则 timePlan.click_selall() time.sleep(0.2) timePlan.click_delall() time.sleep(1) try: self.driver.implicitly_wait(2) timePlan.find_ok() except NoSuchElementException: try: timePlan.find_tipsshowin() time.sleep(1) except NoSuchElementException: pass else: time.sleep(1) print('时间计划列表已删除') timePlan.click_add() time.sleep(1) timePlan.input_TimeRangeName('TimePlan') timePlan.click_save() time.sleep(1) # 断言 开启提示信息是否有误 list_name = str(timePlan.getText_byXpath(timePlan.listName)) time.sleep(1) self.assertEqual(list_name, 'TimePlan', msg='时间段名 与配置的不一致') print('时间计划已添加') #配置防火墙 firewall = AccessControlPage(self.driver, self.url) firewall.click_FireWall() time.sleep(0.5) firewall.click_AccessControl() time.sleep(1) firewall.click_add() time.sleep(1) firewall.input_PolicyNames('IPfilter') firewall.click_sourceIP() time.sleep(1) firewall.click_usergroup() time.sleep(1) # 组织架构,这里选择的是ROOT 所有 firewall.click_Root() firewall.click_saveW1() time.sleep(1) seltime = firewall.selelement_byName('timeGrpName') time.sleep(1) Select(seltime).select_by_value('TimePlan') # #默认就是IP过滤、TCP 可省略 # FilterTypessel = self.driver.find_element_by_name('FilterTypes') # time.sleep(1) # Select(FilterTypessel).select_by_value('1') #IP过滤 # Protocol = self.driver.find_element_by_name('Protocol') # time.sleep(1) # Select(Protocol).select_by_value('2') #TCP selservice = firewall.selelement_byName('cyfw') time.sleep(1) Select(selservice).select_by_value('80') #80(web) firewall.click_save() time.sleep(1) firewall.click_checkTraffic() time.sleep(2) print('访问控制策略 已添加') #断言 list_dstport = str(firewall.getText_byXpath(firewall.list_dstport)) self.assertEqual(list_dstport, '80-80', msg='策略目的端口不为"80-80"') checkTrafficS = firewall.getAttribute_byId( firewall.checkTrafficS, 'checktype') #checktype 0未开启,1开启 self.assertEqual(checkTrafficS, '1', msg='访问控制 未开启') self.driver.quit() #访问官网,使用打不开网页的 title做断言 time.sleep(8) self.driver = webdriver.Chrome() # self.driver.maximize_window() self.driver.implicitly_wait(10) self.driver.get('http://' + httpWebUrl + '/') title = str(self.driver.title) # print(title) self.assertEqual(title, httpWebUrl, msg='TCP80 禁止失败') self.driver.quit() logger.info('test_IPfilter passed')
def test_Keywordfilter(self): u'''关键字过滤 “通知”,在200.200.200.134搜索通知文档不成功''' OAurl = getAssertText('OAurl') OAadmin = getAssertText('OAadmin') OApasswd = getAssertText('OApasswd') login.loginWeb(self) # admin账号登录 self.driver.implicitly_wait(10) #配置防火墙 firewall = AccessControlPage(self.driver,self.url) firewall.click_FireWall() time.sleep(0.5) firewall.click_AccessControl() time.sleep(1) firewall.click_add() time.sleep(1) firewall.input_PolicyNames('Keywordfilter') selFilterTypes = firewall.selelement_byName(firewall.selFilterTypes) time.sleep(1) Select(selFilterTypes).select_by_value('3') #关键字过滤 firewall.input_glnrKeyword('通知') firewall.click_save() time.sleep(20) # 开启访问控制 checkTrafficS = firewall.getAttribute_byId(firewall.checkTrafficS,'checktype') # checktype 0未开启,1开启 if checkTrafficS == '0': firewall.click_checkTraffic() time.sleep(2) print('访问控制策略 已添加') #断言 list_content = str(firewall.getText_byXpath(firewall.list_Content)) self.assertEqual(list_content , '通知', msg='过滤内容不为"www.163.com"') checkTrafficS=firewall.getAttribute_byId(firewall.checkTrafficS,'checktype') #checktype 0未开启,1开启 self.assertEqual(checkTrafficS,'1',msg='访问控制 未开启') self.driver.quit() # # # 访问OA time.sleep(5) self.driver = webdriver.Chrome() self.driver.set_page_load_timeout(10) # selenium超时设置/等待时间过长自动停止 配合去情形1 # self.driver.maximize_window() self.driver.implicitly_wait(10) self.driver.get(OAurl) firewall = AccessControlPage(self.driver, self.url) firewall.input_OAloginid(OAadmin) firewall.input_OApassword(OApasswd) firewall.click_OAlogin() time.sleep(2) # 切换成文档搜索 输入关键字 # # try: # self.driver.implicitly_wait(2) # firewall.click_notice() # time.sleep(2) # except NoSuchElementException: # pass firewall.click_OAsearcn() time.sleep(1) firewall.click_OAsearcnWD() time.sleep(1) firewall.input_OAsearchvalue('通知') time.sleep(1) #不同chrome 点击确定后的情况不同 # 情况1: # 因为被过滤,在点击后不会有返回结果,命令执行后会一直处于命令执行中,最后报超时错误,默认超时300s # selenium.common.exceptions.TimeoutException: Message: timeout # 配合上文 set_page_load_timeout(10)设置等待超时时间,这里等待10s,判断报超时错误则正常,否则失败 # 情况2 # 点击的命令有返回,这时候就通过 不能定位到HeaderForXtalbe状态栏确定 firewall.find_OAsearchbt() time.sleep(5) try: firewall.find_OAsearchbt() # self.driver.find_element_by_id('searchbt').click() except TimeoutException: print('关键字过滤 验证通过') else: try: firewall.find_HeaderForXtalbe() except NoSuchElementException: print('关键字过滤 验证通过2') else: raise Exception('关键字过滤失败') # 退出OA firewall.click_OAlogout() time.sleep(1) firewall.click_OAsubmit() self.driver.quit() logger.info('test_Keywordfilter passed')