def getScreenShot(driver): logger = GenLog(logger='执行截图方法').loggen() timeFormat = time.strftime("%Y%m%d%H%m", time.localtime(time.time())) screenShotName = os.path.dirname(os.path.abspath('.')) driver.get_screenshot_as_file(screenShotName + "\Pictures\\" + timeFormat + '.png') logger.info("有执行截图方法") # timeFormat=time.strftime("%Y%m%d%H%m",time.localtime(time.time())) # screenShotName = os.path.dirname(os.path.abspath('.')) # timeFormat=time.strftime("%Y%m%d%H%m",time.localtime(time.time())) # print(timeFormat)
class Search(baseOperator): def __init__(self): self.logger = GenLog(logger='搜索页面').loggen() def search(self): self.logger.info('执行search方法') self.keyword = (By.ID,"keyword") self.send_keys(self.keyword, "软件测试") self.buttom = (By.NAME,"imageField") self.click(self.buttom) self.result = (By.LINK_TEXT,"智能相机") self.click(self.result) self.productName = (By.CLASS_NAME,"goods_style_name") self.actualValue = self.find_element(self.productName).text if self.actualValue == '智能相机': self.logger.info('查找成功') getScreenShot(baseOperator, "d:\\") self.logger.info('已经截图保留测试结果') else: self.logger.info('查找失败') self.quit_test() # keyword = (By.ID,"keyword") # # submit = (By.NAME,"imageField") # # obj.send_keys(keyword,"智能相机") # # obj.click(submit) # Search().search()
class SearchPage(BasePage): searchtext = (By.ID, "keyword") searchbutton = (By.NAME, "imageField") logger = GenLog(logger='搜索页面').loggen() def input_search(self,searchstr): self.logger.info('执行input_search方法') self.find_element(*self.searchtext).send_keys(searchstr) def submit_search(self): self.logger.info('执行submit_search方法') self.find_element(*self.searchbutton).click() def search_result(self,searchmes): self.click = (By.LINK_TEXT,searchmes) self.productName = (By.CLASS_NAME, "goods_style_name") self.find_element(*self.click).click() self.actualValue = self.find_element(*self.productName).text if self.actualValue == searchmes: self.logger.info('查找成功') self.logger.info('截图保留测试结果') self.screenShot() else: self.logger.info('查找失败') self.quit_test() # AA=SearchPage("ecshop") # AA.input_search("智能相机") # AA.submit_search() # AA.search_result("智能相机")
class LoginPage(BasePage): logger = GenLog(logger='登录页面').loggen() loginPage = (By.LINK_TEXT, "请登录") username = (By.NAME, "username") password = (By.NAME, "password") loginbuttom = (By.NAME, "submit") user = (By.XPATH, '//*[@id="ECS_MEMBERZONE"]/font/font') def login_page(self): self.find_element(*self.loginPage).click() def input_login(self, username, password): self.logger.info('执行input_login方法') self.send_keys(self.username, username) self.send_keys(self.password, password) def login_submit(self): self.logger.info('执行login_submit方法') self.find_element(*self.loginbuttom).click() def login_result(self, username): self.logger.info('执行login_result方法') self.result = self.find_element(*self.user).text if self.result == username: self.logger.info('登录成功') self.logger.info('截图保留测试结果') self.screenShot() else: self.logger.info('登录失败') self.quit_test()
class baseOperator(object): logger = GenLog(logger='基准页面').loggen() def __init__(self): self.startBrowser() def find_element(self,*loc): self.logger.info('执行重写的find_element方法') try: WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc)) return self.driver.find_element(*loc) except AttributeError: self.logger.info('找不到目标元素') def send_keys(self,loc,value): self.logger.info('执行重写的send_keys方法') try: # loc=getattr("_%s" %loc) self.find_element(*loc).send_keys(value) except AttributeError: self.logger.info('无法输入keyword') def click(self,loc): self.logger.info('执行重写的click方法') try: self.find_element(*loc).click() except AttributeError: self.logger.info('点击按钮找不到') def quit_test(self): self.logger.info('执行重写的quit方法') time.sleep(3) self.driver.quit()
class RegisterPage(BasePage): logger = GenLog(logger='注册页面').loggen() loc = (By.LINK_TEXT, "免费注册") username = (By.ID, "username") email = (By.ID, "email") password = (By.ID, "password1") confirm_password = (By.ID, "conform_password") submit = (By.NAME, "Submit") def register(self, username, eamil, password): self.logger.info('执行register方法') self.find_element(*self.loc).click() self.send_keys(self.username, username) self.send_keys(self.email, eamil) self.send_keys(self.password, password) self.send_keys(self.confirm_password, password) self.find_element(*self.submit).click() def register_result(self, result): self.loginname = (By.XPATH, '//*[@id="ECS_MEMBERZONE"]/font/font') self.mes = self.find_element(*self.loginname).text if self.mes == result: self.logger.info('注册成功') self.logger.info('截图保留测试结果') self.screenShot() else: self.logger.info('注册失败') self.quit_test()
def webToursLogin(username, password): logger = GenLog(logger='测试用例操作').loggen() logger.info('读取测试用例配置文件') # username=readConfig('Config','Data','username') # password=readConfig('Config','Data','password') driver = startBrowser() driver.switch_to_frame('body') driver.switch_to_frame('navbar') driver.find_element_by_name("username").send_keys(username) driver.find_element_by_name("password").send_keys(password) driver.find_element_by_name("login").click() time.sleep(3) driver.quit() logger = GenLog(logger='退出测试操作').loggen() logger.info('退出地址')
def startBrowser(configSection): logger = GenLog(logger='浏览器操作').loggen() logger.info('读取浏览器配置文件') browserType = readConfig('Config',configSection,'browserName') browserFlag=True logger.info('正在判断浏览器类型') if browserType=='FireFox': driver=webdriver.Firefox() elif browserType=='Ie': driver=webdriver.Ie() elif browserType=='Chrome': driver=webdriver.Chrome() else: print("浏览器类型错误!") browserFlag=False if browserFlag : logger.info('当前浏览器是:%s' %browserType) url=readConfig('Config',configSection,'url') logger.info('打开测试地址:%s' %url) driver.get(url) driver.maximize_window() time.sleep(3) return driver
def readCase(excelFile, sheetName): logger = GenLog(logger='测试用例操作').loggen() if os.path.exists(excelFile): logger.info("解析%s文件" % excelFile) ef = xlrd.open_workbook(excelFile) suitCase = ef.sheet_by_name(sheetName) caseAmount = suitCase.nrows if caseAmount > 0: #读取第一行的数据 testData = suitCase.row_values(0) return testData else: logger.info("解析文件不存在")
def __init__(self,configSection): logger = GenLog(logger='基准页面').loggen() self.driver = startBrowser(configSection)
def __init__(self): self.logger = GenLog(logger='搜索页面').loggen()
def readSuit(excelFile,sheetName): logger = GenLog(logger='测试用例集操作').loggen() if os.path.exists(excelFile): logger.info("解析%s文件" %excelFile) ef=xlrd.open_workbook(excelFile) suitCase=ef.sheet_by_name(sheetName) taskAmount=suitCase.nrows-1 logger.info("一共有%s个测试任务" %taskAmount) for i in range(1,taskAmount+1): taskName=suitCase.cell(i,1).value taskFlag = suitCase.cell(i,2).value # logger.info("测试任务名称是:%s,测试状态是:%s" %(taskName,taskFlag)) logger.info("判断测试任务:") if taskName=='search': logger.info("当前任务是:%s" % taskName) if taskFlag=='Y': baidu().search("软件测试") else: logger.info("当前任务标记是:%s ,所以不用执行" % taskFlag) if taskName=='linkTest': logger.info("当前任务是:%s" % taskName) if taskFlag=='Y': value=readCase("C:\\Users\\lihai\Desktop\\test.xlsx", "suitcase") baidu().clicLinTest(value) else: logger.info("当前任务标记是:%s ,所以不用执行" % taskFlag) else: logger.info("解析文件不存在")
def __init__(self): self.logger = GenLog(logger='对百度进行操作').loggen() self.logger.info("执行__init__方法") self.driver = startBrowser()
class baidu(): def __init__(self): self.logger = GenLog(logger='对百度进行操作').loggen() self.logger.info("执行__init__方法") self.driver = startBrowser() def search(self, message): self.logger.info("执行search方法") self.driver.find_element_by_id('kw').send_keys(message) self.driver.find_element_by_id('su').click() getScreenShot(self.driver, "d:\\") self.logger.info("截图完成") time.sleep(2) self.driver.quit() self.logger.info("退出浏览器") def clicLinTest(self, linkValue): self.logger.info("执行clicLinTest方法") self.length = linkValue.__len__() self.i = random.randint(1, self.length - 1) self.driver.find_element_by_link_text(linkValue[self.i]).click() getScreenShot(self.driver, "d:\\") self.logger.info("截图完成") time.sleep(2) self.driver.quit() self.logger.info("退出浏览器") # testDate=readCase("C:\\Users\\Administrator\\Desktop\\linkTest.xlsx","Sheet1") # baidu().clicLinTest(testDate) # driver = startBrowser() # driver.find_element_by_id('kw').send_keys("软件测试") # driver.find_element_by_id('su').click() # time.sleep(2) # driver.quit()