class TestSearch(unittest.TestCase): # 创建测试类 @classmethod # 类方法,只执行一次,但必须要加注解@classmethod,且名字固定为setUpClass def setUpClass(cls): pass @classmethod # 类方法,只执行一次,但必须要加注解@classmethod,且名字固定为tearDownClass def tearDownClass(cls): pass def setUp(self): # 每条用例执行测试之前都要执行此方法 self.jsonfile = '../dataconfig/cookieagent.json' self.operationjson = OperationJson(file_path=self.jsonfile) #实例化 self.cookie = self.operationjson.get_all_data() print("self.cookie:%s" % self.cookie) self.activeweb = ActiveWeb() # 实例化 self.loginurl = "https://bjw.halodigit.com:9090/nereus/agent/index" #pass def tearDown(self): # 每条用例执行测试之后都要执行此方法 self.activeweb.closeBrowse() # pass #定义搜索查找函数 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)) 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
class WebFunction: def __init__(self): self.activeweb = ActiveWeb() def assertresult(self, testresult, preresult): message = [1, 2] if testresult == preresult: self.activeweb.printgreenword() passmessage = "测试通过-描述【预期结果为:%s,实际结果为:%s,两者一致。】" % (preresult, testresult) print(passmessage) self.activeweb.printnormalword() message[0] = 'pass' message[1] = passmessage else: self.activeweb.printredword() failmessage = "测试失败-失败描述【预期结果为:%s,而实际结果为:%s,两者不一致。】" % (preresult, testresult) print(failmessage) self.activeweb.printnormalword() message[0] = 'fail' message[1] = failmessage return message def assertText(self, num, testxpath, preresult): testresult = self.activeweb.findElementByXpathAndReturnText( num, testxpath) assertresult = self.assertresult(testresult, preresult) return assertresult def assertSelectSearch(self, num, selectxpath, selectoptiontext, selectinputxpath, selectinputtext, searchbuttonxpath, searchtableresultxpath, colnum, checktext): message = [1, 2] flag = False self.activeweb.findElementByXpathAndReturnOptions( selectxpath, str(selectoptiontext)) 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(): flag = True if flag: self.activeweb.printgreenword() passmessage = "测试通过-描述【预期搜索结果中包含:%s,实际搜索结果为:%s,符合预期。】" % ( str(checktext), tabledic) print(passmessage) self.activeweb.printnormalword() message[0] = 'pass' message[1] = passmessage else: self.activeweb.printredword() failmessage = "测试失败-失败描述【预期搜索结果中包含:%s,实际搜索结果为:%s,不符合预期。】" % ( str(checktext), tabledic) print(failmessage) self.activeweb.printnormalword() message[0] = 'fail' message[1] = failmessage return message def assertInputValue(self, testxpath, testvaluename, preresult): testresult = self.activeweb.findElementByXpathAndReturnValue( testxpath, testvaluename) self.assertresult(testresult, preresult)
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