class GetCookie(): def __init__(self,outjsonfile=None,outloginurl=None,outloginaccountxpath=None, outloginaccounttext=None,outloginppasswordxpath=None,outloginpasswordtext=None, outloginbuttonxpath=None): if outjsonfile==None: self.jsonfile = '../dataconfig/cookie.json' else: self.jsonfile = outjsonfile if outloginurl==None: self.loginurl ="https://bjw.halodigit.com:9090/nereus/manager/index#/login" else: self.loginurl = outloginurl if outloginaccountxpath==None: self.loginaccountxpath = "/html/body/div[1]/div[2]/form/div/div[1]/input" else: self.loginaccountxpath = outloginaccountxpath if outloginaccounttext==None: self.loginaccount = "*****@*****.**" else: self.loginaccount = outloginaccounttext if outloginppasswordxpath==None: self.loginppasswordxpath = "/html/body/div[1]/div[2]/form/div/div[2]/input" else: self.loginppasswordxpath = outloginppasswordxpath if outloginpasswordtext==None: self.loginpassword = "******" else: self.loginpassword = outloginpasswordtext if outloginbuttonxpath==None: self.loginbuttonxpath = "/html/body/div[1]/div[2]/form/div/a[1]/span" else: self.loginbuttonxpath = outloginbuttonxpath self.operationjson = OperationJson(file_path=self.jsonfile) #实例化 self.activeweb = ActiveWeb() # 实例化 def getCookie(self): # 登录 self.activeweb.getUrl(self.loginurl) # 打开网址 self.activeweb.findElementByXpathAndInput(self.loginaccountxpath,self.loginaccount) self.activeweb.findElementByXpathAndInput(self.loginppasswordxpath,self.loginpassword) self.activeweb.findElementByXpathAndClick(self.loginbuttonxpath) self.activeweb.delayTime(3) # 获取cookie cookie = self.activeweb.getCookies() self.activeweb.closeBrowse() return cookie def writerCookieToJson(self): self.cookie = self.getCookie() self.operationjson.write_data(self.cookie) print("\ncookie信息‘%s’已经写入‘%s’文件里。\n" % (self.cookie,self.jsonfile))
class LoginManager: def __init__(self): self.activeweb = ActiveWeb() # 实例化 self.loginurl = "https://bjw.halodigit.com:9090/nereus/manager/index#/login" self.loginaccountxpath = "/html/body/div[1]/div[2]/form/div/div[1]/input" self.loginaccount = "*****@*****.**" self.loginppasswordxpath = "/html/body/div[1]/div[2]/form/div/div[2]/input" self.loginpassword = "******" self.loginbuttonxpath = "/html/body/div[1]/div[2]/form/div/a[1]/span" self.cookie = self.getcookie() def getcookie(self): # 登录 self.activeweb.getUrl(self.loginurl) # 打开网址 self.activeweb.findElementByXpathAndInput(self.loginaccountxpath, self.loginaccount) self.activeweb.findElementByXpathAndInput(self.loginppasswordxpath, self.loginpassword) self.activeweb.findElementByXpathAndClick(self.loginbuttonxpath) self.activeweb.delayTime(3) # 获取cookie cookie = self.activeweb.getCookies() self.activeweb.closeBrowse() return cookie def writercookie(self, url): self.activeweb.writerCookies(self.cookie, self.loginurl, url)
class TestSearch(unittest.TestCase): # 创建测试类 @classmethod # 类方法,只执行一次,但必须要加注解@classmethod,且名字固定为setUpClass def setUpClass(cls): pass @classmethod # 类方法,只执行一次,但必须要加注解@classmethod,且名字固定为tearDownClass def tearDownClass(cls): pass def setUp(self): # 每条用例执行测试之前都要执行此方法 self.cookie = self.getcookie() self.activeweb = ActiveWeb() # 实例化 self.loginurl = "https://bjw.halodigit.com:9090/nereus/manager/index#/login" # self.loginaccountxpath = "/html/body/div[1]/div[2]/form/div/div[1]/input" # self.loginaccount = "*****@*****.**" # self.loginppasswordxpath = "/html/body/div[1]/div[2]/form/div/div[2]/input" # self.loginpassword = "******" # self.loginbuttonxpath = "/html/body/div[1]/div[2]/form/div/a[1]/span" #pass def tearDown(self): # 每条用例执行测试之后都要执行此方法 self.activeweb.closeBrowse() # pass #获取cookie def getcookie(self): # 登录 self.activeweb = ActiveWeb() # 实例化 self.loginurl = "https://bjw.halodigit.com:9090/nereus/manager/index#/login" self.loginaccountxpath = "/html/body/div[1]/div[2]/form/div/div[1]/input" self.loginaccount = "*****@*****.**" self.loginppasswordxpath = "/html/body/div[1]/div[2]/form/div/div[2]/input" self.loginpassword = "******" self.loginbuttonxpath = "/html/body/div[1]/div[2]/form/div/a[1]/span" self.activeweb.getUrl(self.loginurl) # 打开网址 self.activeweb.findElementByXpathAndInput(self.loginaccountxpath, self.loginaccount) self.activeweb.findElementByXpathAndInput(self.loginppasswordxpath, self.loginpassword) self.activeweb.findElementByXpathAndClick(self.loginbuttonxpath) self.activeweb.delayTime(3) # 获取cookie cookie = self.activeweb.getCookies() self.activeweb.closeBrowse() return cookie #定义搜索查找函数 def definesearch(self, num, is_cookie, url, selectxpath, selectoptiontext, selectinputxpath, selectinputtext, searchbuttonxpath, searchtableresultxpath, colnum, checktext): if is_cookie: self.activeweb.writerCookies(self.cookie, self.loginurl, url) else: self.activeweb.getUrl(url) self.activeweb.findElementByXpathAndReturnOptions( selectxpath, str(selectoptiontext)) if selectinputxpath == None: print("不执行输入文本内容") else: self.activeweb.findElementByXpathAndInput(selectinputxpath, str(selectinputtext)) self.activeweb.findElementByXpathAndClick(searchbuttonxpath) self.activeweb.delayTime(5) tabledic = self.activeweb.findElementByXpathAndReturnTableNum( num, searchtableresultxpath) for value in tabledic.values(): if str(checktext).lower() in value[int(colnum)].lower(): self.assertTrue(True) else: self.assertTrue(False) @staticmethod #根据不同的参数生成测试用例 def getTestFunc(num, is_cookie, url, selectxpath, selectoptiontext, selectinputxpath, selectinputtext, searchbuttonxpath, searchtableresultxpath, colnum, checktext): def func(self): self.definesearch(num, is_cookie, url, selectxpath, selectoptiontext, selectinputxpath, selectinputtext, searchbuttonxpath, searchtableresultxpath, colnum, checktext) return func