以登陆为例,示例登陆写法
#1.流水写法,最基本写法from selenium import webdriverdriver = webdriver.Firefox()driver.get("https://test.xxx.com/login.jhtml")driver.find_element_by_id("u_username").send_keys("jilu1219")driver.find_element_by_id("u_password").send_keys("xxxxxx")driver.find_element_by_id("u_submit").click()
#2.把登陆抽成方法,仍在同一个文档里头
from selenium import webdriverdef login(openUrl,username,password): driver.get(openUrl) driver.find_element_by_id("u_username").send_keys(username) driver.find_element_by_id("u_password").send_keys(password) driver.find_element_by_id("u_submit").click() driver = webdriver.Firefox() login("https://test.xxx.com/login.jhtml","jilu1219","xxxxxx")
#3.把登陆抽成方法login.py,与实际登陆代码test0424001.py放在同一个目录下
1 #login.py 代码 2 3 #coding=utf-8 4 5 from selenium import webdriver 6 7 def login(openUrl,username,password): 8 driver = webdriver.Firefox() 9 driver.get(openUrl)10 driver.find_element_by_id("u_username").send_keys(username)11 driver.find_element_by_id("u_password").send_keys(password)12 driver.find_element_by_id("u_submit").click()13 14 15 #################################16 #实际登陆代码,保存为test0424001.py17 from selenium import webdriver18 import login19 20 22 driver = webdriver.Firefox()23 24 login.login("https://test.xxxx.com/login.jhtml","jilu1219","xxxxxx")25
#4.把登陆抽成一个方法login.py,与实际登陆代码test0424001.py放在不同目录下
现目录如下:
D:\Desktop\testPython\test0424001.py
D:\Desktop\testPython\public\login.py
D:\Desktop\testPython\public\__init__.py
#当把login.py 抽到public目录下时,需在此目录下新建__init__.py 空文件
#login.py 代码 ,未变#coding=utf-8from selenium import webdriverdef login(openUrl,username,password): driver = webdriver.Firefox() driver.get(openUrl) driver.find_element_by_id("u_username").send_keys(username) driver.find_element_by_id("u_password").send_keys(password) driver.find_element_by_id("u_submit").click()
#test0424001.py 代码#coding=utf-8from selenium import webdriverimport syssys.path.append("\public") #注意需扩展path路径from public import logindriver = webdriver.Firefox()login.login("https://test.xxxx.com/login.jhtml","jilu1219","xxxxxx")
#5.在以上的基础上把登陆网址、用户名、密码参数化,把数据保存在一个.csv文件中
现目录如下:
D:\Desktop\testPython\test0424004.py
D:\Desktop\testPython\public\login.py
D:\Desktop\testPython\public\__init__.py
D:\Desktop\testPython\public\loginData.py
D:\Desktop\testPython\data\loginData.csv
#loginData.py 代码#coding=utf-8import csvdef loginData(): my_file = "D:\\Desktop\\testPython\\data\\loginData.csv" datas=csv.reader(file(my_file,'rb')) for data in datas: openUrl = data[0] username = data[1] password = data[2] return openUrl,username,password;
#login.py 代码#coding=utf-8from selenium import webdriverimport timedef login(openUrl,username,password): driver = webdriver.Firefox() driver.get(openUrl) driver.find_element_by_id("u_username").send_keys(username) driver.find_element_by_id("u_password").send_keys(password) driver.find_element_by_id("u_submit").click()
#test0424004.py 代码, 主要测试用例文件#coding=utf-8from selenium import webdriverimport syssys.path.append("\public") #注意需扩展path路径from public import loginfrom public import loginDataimport csvdriver = webdriver.Firefox()openUrl,username,password=loginData.loginData() #注意login.login(openUrl,username,password)
#6.若导入数据有多行需参数化,则在#5的基础上需做些 小调整。需调整loginData.py 及test0424004.py 代码,其余保持不变。login.py保持不变。
#loginData.py daima#coding=utf-8import csvdef loginData(): my_file = "D:\\pythonTest\\data\loginData.csv" datas = csv.reader(file(my_file,'rb')) return datas #注意
#test0424004.py代码#coding=utf-8from selenium import webdriverimport timeimport syssys.path.append("\public")from public import loginfrom public import loginDataimport csvdriver = webdriver.Firefox()#注意通过循环取出datas=loginData.loginData()for data in datas: openUrl = data[0] username = data[1] password = data[2] login.login(openUrl,username,password)
#7.若导入的csv有表头,则#6不适用,需再调整。需调整test0424004.py。login.py 不变。loginData.py不要了。
#test0424004.py代码调整 import timeimport syssys.path.append("\public")from public import loginimport csvdriver = webdriver.Firefox()#读取csv文件,文档有表头with open('D:\pythonTest\data\loginData02.csv','rb') as csvfile: reader = csv.DictReader(csvfile) urls = [] usernames = [] passwords= [] for row in reader: if row['if_excute'] != 'if_excute': url = row['url'] username = row['username'] password = row['password'] urls.append(url) usernames.append(username) passwords.append(password) #print urls #print passwords #print usernamesi = 0while i
########## 下面的例子与上面的例子无关,全部推到重来。 #############
#7.引入unittest框架 流水写法
#coding=utf-8from selenium import webdriverimport time,unittestclass Aipaike_pc(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.base_url="https://test.xxxx.com/login.jhtml" self.verificationErrors=[] self.accept_next_alert=True #登录用例,全部用例必须以test_命名 def test_login(self): driver=self.driver driver.get(self.base_url) driver.find_element_by_id("u_username").send_keys("jilu1219") driver.find_element_by_id("u_password").send_keys("xxxxxx") driver.find_element_by_id("u_submit").click() time.sleep(5) driver.close() def tearDown(self): self.driver.quit() self.assertEqual([],self.verificationErrors)if __name__ == "__main__": unittest.main()