python自动化web测试-这个代码像个鬼一样

Onida ·
更新时间:2024-11-10
· 572 次阅读

500行如清纯少女般的代码 #!/usr/bin/env python # _*_ coding:utf-8 _*_ from selenium import webdriver import time import unittest import threading #from selenium.webdriver.common.by import By class MyTestCase_V01(unittest.TestCase): def setUp(self): print("setUp") def tearDown(self): print("tearDown") # 先定位主筐体的位置,让后通过相对位置来定位表格中的元素 main_xpath = '//main[@class="el-main main"]' url = "http://192.168.3.200/#/login" Chrome_path = "F:\GITHUB_PRO\consoleTest\consoleTest\\tools\chromedriver.exe" inputuser_box = "//input[@placeholder='请输入用户名']" inputpasswd_box = "//input[@type='password']" login_submit_btn = "//*[@id='app']/div/div/form/button"#登录按钮 #主控重启 reboot_mcband = "//*[@id='app']/section/header/div[2]/button[1]" # 重启系统 comfirm = "/html/body/div[3]/div/div[3]/button[2]/span"#确认 #功放设置 band_1 = "//div[text()='1']"#列表中第1块基带板 setpar_band = "//span[text()='参数设置']" UePMax = "//*[@id='app']/section/section/main/div/div[2]/div[3]/div[4]/div/div[2]/form/div[13]/div[2]/div/div/div/div/input"#输出功率 save_par = "//span[text()='保存参数']" comfirm_button = "//button[@class='el-button el-button--default el-button--small el-button--primary ']" jz_manage = "//span[text()='基站管理']" band_manage = "//span[text()='核心板管理']" pa_manage = "//span[text()='功放管理']" reboot_band = "//span[text()='软重启']" comfirm_soft = "/html/body/div[4]/div/div[3]/button[2]/span" power_outage_restart = "//span[text()='断电重启']" pa_on = "//span[text()='开启所有功放']" pa_off = "//span[text()='关闭所有功放']" refresh = "//i[@class='el-icon-refresh']" # B40 B40_table_pa_xpath = "//*[@id='app']/section/section/main/div/div[2]/div[3]/div[2]/div[3]/table/tbody/tr[1]/td[9]" B40_table_pa_css = "#app > section > section > main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr:nth-child(1) > td.el-table_1_column_9 > div > div > span" # 读取B1功放 B1_table_pa_xpath = "//*[@id='app']/section/section/main/div/div[2]/div[3]/div[2]/div[3]/table/tbody/tr[2]/td[9]" B1_table_pa_css = "#app > section > section > main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr:nth-child(2) > td.el-table_1_column_9 > div > div > span" # 读取gsm功放 GSM_table_pa_xpath = "//*[@id='app']/section/section/main/div/div[2]/div[3]/div[2]/div[3]/table/tbody/tr[3]/td[9]" GSM_table_pa_css = "#app > section > section > main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr:nth-child(3) > td.el-table_1_column_9 > div > div > span" #核心板: OS:online status ;WS: work status; #FDD-56 OS_FDD_56_xpath_iframe = '//*[@id="app"]/section/section/main/div/div[2]/div[4]/div[2]/div[3]/table/tbody/tr[1]/td[7]/div/span' OS_FDD_56_css_iframe = '' OS_FDD_56_xpath = '//*[@id="app"]/section/section/main/div/div[2]/div[4]/div[2]/div[3]/table/tbody/tr[1]/td[7]' OS_FDD_56_css = '#app > section > section > main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr:nth-child(1) > td.el-table_6_column_105 > div > span' WS_FDD_56_xpath = '//*[@id="app"]/section/section/main/div/div[2]/div[4]/div[2]/div[3]/table/tbody/tr[1]/td[8]' WS_FDD_56_css = '#app > section > section > main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr:nth-child(1) > td.el-table_6_column_106 > div > div > span' #TDD-55 OS_TDD_55_xpath = '//*[@id="app"]/section/section/main/div/div[2]/div[4]/div[2]/div[3]/table/tbody/tr[2]/td[7]' OS_TDD_55_css = '#app > section > section > main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr.el-table__row.expanded > td.el-table_6_column_105 > div > span' WS_TDD_55_xpath = '//*[@id="app"]/section/section/main/div/div[2]/div[4]/div[2]/div[3]/table/tbody/tr[2]/td[8]' WS_TDD_55_css = '#app > section > section > main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr.el-table__row.expanded > td.el-table_6_column_106 > div > div > span' #OnlineStatus OS_GSM_KB_xpath = '//*[@id="app"]/section/section/main/div/div[2]/div[4]/div[2]/div[3]/table/tbody/tr[4]/td[7]' OS_GSM_KB_css = '#app > section > section > main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr:nth-child(4) > td.el-table_6_column_105 > div > span' WS_GSM_KB_xpath = '//*[@id="app"]/section/section/main/div/div[2]/div[4]/div[2]/div[3]/table/tbody/tr[4]/td[8]' WS_GSM_KB_css = '#app > section > section > main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr:nth-child(4) > td.el-table_6_column_106 > div > div > span' ################应用接口上########################## ''' 2、主控板重启测试;(每次重启的间隔时间为30min); 测试用例模式:A为前提条件,用例使用AB,AC两个方式来执行 主线程:调用操作线程后,延时到结束,时间xxx分钟,大约执行三次循; 操作线程: 使用两个测试用例,分包测试AB和AC 标准: A、点击重启主控板后,返回主控重启成功的字段; B、5秒后登陆控制台(输入映射的IP),登陆失败; C、1min钟后登陆控制台(输入映射的IP),登陆成功,能查询到主控版本信息; 上述三点均满足,则判断此次主控板重启成功; ''' #主控板重启 def MCB_Reboot(self,url): print("打开浏览器,登录") val = self.login(url) # 主控重启 self.driver.find_element_by_xpath(self.reboot_mcband).click() time.sleep(3) #确定重启按钮 self.driver.find_element_by_xpath(self.comfirm).click() time.sleep(3) print("关闭浏览器") self.driver.quit() time.sleep(5) # 点击重启系统后,5s内需要登录失败才是正常的,登录成功表示重启失败了 loginStatus = self.login(url) if loginStatus == 0: print("重启主控失败") self.driver.quit() else: print("重启主控成功") self.driver.quit() print("等待一分钟后登录控制台") time.sleep(60) loginStatus = self.login(url) if loginStatus == 0: print("重启主控成功") self.driver.quit() else: print("重启主控失败") print("退出浏览器,等待五分钟后执行功放开关测试") self.driver.quit() ''' 功放开关测试 前置条件:设置后各参数,功放的输出功率设置为40dbm; 使用单测试用例 执行时间:打开功放开关界面, 主线程:调用操作线程后,延时到结束,时间18分钟,大约执行三次循; 操作线程: 关闭操作,刷新,读取,判断 (定时5秒后进行开启功放操作) 开启功放,刷新,读取,判断。(定时5分钟进行关闭功放操作) ''' def PA_Reboot(self,url): # 功放开关测试 self.login(url) #点击基站管理 self.driver.find_element_by_xpath(self.jz_manage).click() time.sleep(1) #点击核心板管理 self.driver.find_element_by_xpath(self.band_manage).click() time.sleep(1) ############设置默认输出功率为40dbm################### # 点击第一块板子 self.click_band_1() # 点击参数设置 self.click_setpar_band() # 设置输出功率为40dbm self.type_UePMax("40") # 保存 self.click_save_par() time.sleep(1) self.click_save_par_confirm() time.sleep(1) # 设置默认输出功率为40dbm完成,直接单击功放管理 pa_manage self.driver.find_element_by_xpath(self.pa_manage).click() time.sleep(1) # 关闭功放,3秒后,点击刷新,读取功放输出功率为-30dbm #关闭功放 self.driver.find_element_by_xpath(self.pa_off).click() time.sleep(3) #刷新 self.driver.find_element_by_xpath(self.refresh).click() time.sleep(3) #定位主页 locater = self.driver.find_element_by_xpath(self.main_xpath) time.sleep(1) #读取表内容,第一个功放值 cssStr1 = 'main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr:nth-child(1) > td.el-table_1_column_9 > div > div > span' val = locater.find_element_by_css_selector(cssStr1).text print(val) if val == -999: print("读取功放失败") elif val > -50 and val 0: print("功放关闭失败") pa_status = 0 #读取第二个功放值 cssStr2 = 'main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr:nth-child(1) > td.el-table_1_column_11 > div > div > span' val = locater.find_element_by_css_selector(cssStr2).text print(val) if val == -999: print("读取功放失败") elif val > -50 and val 0: print("功放关闭失败") time.sleep(2) #点击开启功放,3秒后,点击刷新,读取功放输出功率为(38-40)dbm self.driver.find_element_by_xpath(self.pa_on).click() time.sleep(3) self.driver.find_element_by_xpath(self.refresh).click() time.sleep(5) # 读取功放 # 读取表内容,第一个功放值 cssStr1 = 'main > div > div.el-tabs__content > div.box-card > div.el-table.el-table--fit.el-table--border.el-table--scrollable-x.el-table--enable-row-hover.el-table--enable-row-transition.el-table--mini > div.el-table__body-wrapper.is-scrolling-left > table > tbody > tr:nth-child(1) > td.el-table_1_column_9 > div > div > span' val = locater.find_element_by_css_selector(cssStr1).text print(val) if val == -999: print("读取功放失败") elif val > -50 and val 0: print("功放关闭失败") pa_status = 0 # 读取第二个功放值 val = locater.find_element_by_css_selector(cssStr2).text print(val) if val == -999: print("读取功放失败") elif val > -50 and val 0: print("功放关闭失败") time.sleep(5) print("退出浏览器,等待五分钟后执行核心板重启测试") self.driver.quit() return pa_status #核心板(基带板)软启动 def KB_Soft_Reboot(self,url): self.login(url) # 点击基站管理 jz_manage self.driver.find_element_by_xpath(self.jz_manage).click() time.sleep(1) #点击核心板管理 self.driver.find_element_by_xpath(self.band_manage).click() time.sleep(1) #点击第1块基带板 self.click_band_1() time.sleep(1) # 点击软重启 self.driver.find_element_by_xpath(self.reboot_band).click() time.sleep(1) #点击软重启确认 self.driver.find_element_by_xpath(self.reboot_band).click() first_xpath = "//*[@id='app']/section/section/main" first_css = "" #判断该键值是否为离线 time.sleep(1) for i in {1,2,3,4}: if i == 1: time.sleep(5) #基带板离线,功放为关闭 elif i == 2: time.sleep(20) # 基带板离线,功放为关闭 elif i == 3: time.sleep(60) # 基带板在线,功放为关闭 elif i == 4: time.sleep(3*60) # 基带板工作状态,功放为开启 retStr = self.read_table_text_by_locater(first_xpath,first_css) if "离线" in retStr: print("离线状态") elif "在线" in retStr: print("在线状态") elif "工作" in retStr: print("工作状态") #查看启动状态 time.sleep(5) self.driver.quit() # 核心板断电启动 IPS:Interuption of Power Supply band_2 = "xpath=>//div[text()='2']" # 列表中第2块基带板 # 核心板(基带板)断电重启 def KB_IPS_Reboot(self,url): self.login(url) #点击基站管理 jz_manage #点击核心板管理 #断电重启 # 点击基站管理 jz_manage self.driver.find_element_by_xpath(self.jz_manage).click() time.sleep(1) # 点击核心板管理 self.driver.find_element_by_xpath(self.band_manage).click() self.driver.quit() # 点击第1块基带板 self.click_band_1() time.sleep(1) #点击断电重启 self.driver.find_element_by_xpath(self.power_outage_restart).click() time.sleep(1) first_xpath = "//*[@id='app']/section/section/main" first_css = "//main/div/div[2]/div[3]/div[2]/div[3]/table/tbody/tr[1]/td[7]/div/span" #在控制台查询基带板状态为离线状态,功放处于关闭状态; # 判断该键值是否为离线 for i in {1,2,3,4}: if i == 1: time.sleep(5) #基带板离线,功放为关闭 elif i == 2: time.sleep(20) # 基带板离线,功放为关闭 elif i == 3: time.sleep(60) # 基带板在线,功放为关闭 elif i == 4: time.sleep(3*60) # 基带板工作状态,功放为开启 retStr = self.read_table_text_by_locater(first_xpath, first_css) if "离线" in retStr: print("离线状态") elif "在线" in retStr: print("在线状态") elif "工作" in retStr: print("工作状态") self.driver.quit() ################应用接口下########################## ################底层接口上########################## # 读取表格数据 def read_table_text_by_locater(self, xpathStr, cssStr): try: locater = self.driver.find_element_by_xpath(xpathStr) data = locater.find_element_by_css_selector(cssStr).text print("data: %s" % data) return data except Exception as err: print(err) return -999 def login(self,url): try: # 打开浏览器 self.driver = webdriver.Chrome(self.Chrome_path) # 每次发送请求的时候,每间隔0.5s检查一遍是否更新 time.sleep(3) self.driver.get(self.url) self.driver.maximize_window() print("开始登录操作") self.driver.find_element_by_xpath(self.inputuser_box).send_keys("admin") self.driver.find_element_by_xpath(self.inputpasswd_box).send_keys("123456") self.driver.find_element_by_xpath(self.login_submit_btn).click() time.sleep(2) print("点击了登录按钮") except: print("登录异常") return -1 else: print("登录成功") return 0 # 点击第一块基带板 def click_band_1(self): self.driver.find_element_by_xpath(self.band_1).click() time.sleep(1) #点击参数设置 def click_setpar_band(self): #self.clicks(self.setpar_band) self.driver.find_element_by_xpath(self.setpar_band).click() time.sleep(1) #清空并输入输出功率(dBm) clear() send_keys def type_UePMax(self,text): self.driver.find_element_by_xpath(self.UePMax).clear() self.driver.find_element_by_xpath(self.UePMax).send_keys(text) #保存参数 def click_save_par(self): self.driver.find_element_by_xpath(self.save_par).click() # 保存参数成功,点击确定按钮 def click_save_par_confirm(self): self.driver.find_element_by_xpath(self.comfirm_button).click() ##############底层接口下############################ ###############测试用例################################# ''' 主控重启 等待5分钟 功放开关 等待五分钟 核心板软启动 等待五分钟 核心板断电重启 等待15分钟 回到第一步,主控重启 定时器线程为24小时 ''' runCounter = 0 def thread_timer(self,ip,gThStatus): url = ip +"/#/login" if gThStatus == 0: #主控板重启测试 self.MCB_Reboot(url) time.sleep(5*60) th = threading.Thread(target=self.thread_timer, args=(ip, 1)) # 创建一个线程 th.start() elif gThStatus == 1: self.PA_Reboot(url) time.sleep(5 * 60) th = threading.Thread(target=self.thread_timer, args=(ip, 2)) # 创建一个线程 th.start() elif gThStatus == 2: self.KB_Soft_Reboot(url) time.sleep(5 * 60) th = threading.Thread(target=self.thread_timer, args=(ip, 3)) # 创建一个线程 th.start() elif gThStatus == 3: self.KB_IPS_Reboot(url) time.sleep(15 * 60) th = threading.Thread(target=self.thread_timer, args=(ip, 0)) # 创建一个线程 th.start() ####如果运行了48次后,停止 self.runCounter += 1 if self.runCounter >= 48: return 0 else: self.gThStatus = 0 def test_App(self): # 定义一个线程池 threads = [] IP = (['192.168.3.200'], ['192.168.3.201'], ['192.168.3.202']) for i in range(len(IP)): print('IP:%s' % IP[i]) thd1 = threading.Thread(target=self.thread_timer, args=(IP[i], 0)) # 创建一个线程 threads.append(thd1) for th in threads: th.start() # start()---启动线程活动 for th in threads: th.join() #等待线程结束 if __name__=='__main__': unittest.main()
作者:炎芯随笔



Web web测试 Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号