一、模块驱动测试实例
1 把登录和退出统一封装在login类中,若把login类单独放在一个文件中,就可以给任一测试脚本调用,这里就跟测试脚本放一起
2 from selenium import webdriver
3
4 class login():
5 #登录
6 def user_login(self,driver):
7 user = dr.find_element_by_id('idInput').clear()
8 user.send_keys('username')
9 pwd = dr.find_element_by_id('pwdInput').clear()
10 pwd.send_keys('password')
11 dr.find_element_by_id('loginBtn').click()
12
13 #退出
14 def user_logout(self,driver):
15 dr.find_element_by_link_text('退出').click()
16 dr.quit()
17
18 #执行测试用例
19 dr=webdriver.Chrome()
20 dr.implicitly_wait(20)
21 dr.get('http://www.126.com')
22
23 #调用登录模块
24 login().user_login(dr)
25
26 #调用退出模块
27 login().user_logout(dr)
二、数据驱动测试实例
#1、参数化邮件登录,把user_login()方法增加user,pwd两个入参参数 #把登录和退出统一封装在login类中,若把login类单独放在一个文件中,就可以给任一测试脚本调用,这里就跟测试脚本放一起 from selenium import webdriver class login(): #登录 def user_login(self,driver,user,pwd): user = dr.find_element_by_id('idInput').clear() user.send_keys('username') pwd = dr.find_element_by_id('pwdInput').clear() pwd.send_keys('password') dr.find_element_by_id('loginBtn').click() #退出 def user_logout(self,driver): dr.find_element_by_link_text('退出').click() dr.quit() #执行测试用例 class loginTest(): def __init__(self): self.dr=webdriver.Chrome() self.dr.implicitly_wait(20) self.dr.get("http://www.126.com") #admin用户登录 def test_admin_login(self): user = 'admin' pwd = '1234' login().user_login(self.dr,user,pwd) login().user_logout(self.dr) self.dr.quit() #guest用户登录 def test_guest_login(self): user = 'guest' pwd = '12345' login().user_login(self.dr,user,pwd) login().user_logout(self.dr) self.dr.quit() if __name__ == '__main__': loginTest().test_admin_login() loginTest().test_guest_login()
'''参数化搜索关键字''' from selenium import webdriver list = ['python','zhongwen','text'] for text in list: dr = webdriver.Chrome() dr.implicitly_wait(20) dr.get('http://www.baidu.com') dr.find_element_by_id('kw').send_keys(text) dr.find_element_by_id('su').click() dr.quit()