大神绕道而行,自我小白的笔记,仅此
一、创建文件夹,创建xxx.ini文件用来存放界面的定位元素,用 [界面_element]-->界面, 来划分界面界面元素,维护方便。定位元素的格式: user=name>username
user:定位元素的名称,自己起。name:定位元素的方法。> :分隔符。username:元素name的值
[login_element] #------->界面 username=name>j_username login=id>imgLogin loginerror=xpath>/html/body/div/div[2]/div/div[1]/div[3]/div[1]/div[1]/div/div/div[6]/div/span[1] [yewu_element] zhankaicaidan=class_name>z-west-exp [jiemian_element] caidan=xpath>//div[@class='z-borderlayout-icon z-west-exp'] [qiyun_login] jihau=xpath>//div[contains(text(),'我的计划')]
二、创建工具文件夹,创建读取xxx.ini文件的方法代码如下:调用时:输入参数('界面','定位元素的名称'),返回结果:定位元素的方法>元素的值,如:
aa=ReadyuansuIni()
a=aa.get_ini('login_element','username')
print(a)
结果:
name>j_username
代码如下:
import configparser class ReadyuansuIni: def read_mulu(self): read_yuansuini = configparser.ConfigParser() read_yuansuini.read('文件路径\xxx.ini',encoding="utf-8-sig") # 读取目录,可以用os return read_yuansuini def get_ini(self,byx,key): if byx=='login_element': return self.read_mulu().get(byx,key) elif byx=='yewu_element': return self.read_mulu().get(byx,key) elif byx=='jiemian_element': return self.read_mulu().get(byx, key) elif byx=='qiyun_login': return self.read_mulu().get(byx, key)
三、在工具文件夹中再创建定方法的py文件,封装读取元素用什么定位方法定位,
from 路劲 import ReadyuansuIni class Getby: def __init__(self,driver): self.driver=driver def get_element(self,bxy,key): read_ini=ReadyuansuIni() read_neirong=read_ini.get_ini(bxy,key) fangshi = read_neirong.split('>')[0] dingwei = read_neirong.split('>')[1] if fangshi == 'id': return self.driver.find_element_by_id(dingwei) elif fangshi == 'name': return self.driver.find_element_by_name(dingwei) elif fangshi == 'class': return self.driver.find_element_by_class(dingwei) elif fangshi == 'xpath': return self.driver.find_element_by_xpath(dingwei) elif fangshi == 'class_name': return self.driver.find_elements_by_class_name(dingwei) '''这点是有区别的,driver.find_elements 复数,注意'''
四、建立文件用来获取所有界面元素信息
from 路劲 import Getby class Login_page:#获取登录界面的所有元素信息 def __init__(self,driver): self.fangfa = Getby(driver) def get_username_element(self): return self.fangfa.get_element('login_element', 'username') def get_password_element(self): return self.fangfa.get_element('login_element', 'password') def get_login_element(self): return self.fangfa.get_element('login_element', 'login')
五、建立文件来操作界面元素
#coding=utf-8 from 路劲 import Login_page class LoginHandle:#操作页面的元素 def __init__(self,driver): self.login_page = Login_page(driver) def send_username(self,user): self.login_page.get_username_element().send_keys(user) def send_password(self, password): self.login_page.get_password_element().send_keys(password) def click_login(self): self.login_page.get_login_element().click() def zhankai_caidan(self): self.login_page.get_zhankai_elements()[0].click()
六、建立文件来组织业务
#coding=utf-8 from yemian_caozuocang_handle.login_handle import LoginHandleclass LoginBusiness: def __init__(self,driver): self.login_handle = LoginHandle(driver) self.driver=driver def login_pass(self,user,password): self.login_handle.send_username(user) self.login_handle.send_password(password) self.login_handle.click_login()
七、case层
import unittest,time from selenium import webdriver import HTMLTestRunner from yewucang_business.login_business import LoginBusiness from log.get_log import log log = log() get_log = log.get_log() class Tsetlogin(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(20) self.driver.get('网址') get_log.error('This is Chrome') self.aa = LoginBusiness(self.driver) def test01_pass(self): self.aa.login_pass('admin','admin123') time.sleep(0.5) title = self.driver.title if title!='网页title': case_name = self._testMethodName jietu_path = '路经 + '.png' self.driver.save_screenshot(jietu_path) # 截图 self.assertEqual(title, '网页title') def tearDown(self): self.driver.quit() if __name__ == '__main__': zhixing_case = unittest.TestSuite() zhixing_case.addTest(Tsetlogin('test01_pass')) baogao = "路经\report.html" fp = open(baogao, "wb") HTMLTestRunner.HTMLTestRunner(stream=fp,title="报告title",description=u"说明吧").run(zhixing_case)
这里我只举例说明这样就成功了,用例可以加,还有断言,相对而言try比较好点。这里面的异常处理没有加。