博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python学习_模块化示例
阅读量:5772 次
发布时间:2019-06-18

本文共 5593 字,大约阅读时间需要 18 分钟。

以登陆为例,示例登陆写法

#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()

 

转载于:https://www.cnblogs.com/jilu1219/p/6756105.html

你可能感兴趣的文章
Java利用httpasyncclient进行异步HTTP请求
查看>>
循环多少次? 【杭电--HDOJ-1799】 附题+具体解释
查看>>
linux系统终端命令提示符设置(PS1)记录
查看>>
C++运算符重载
查看>>
【Web】URI和URL,及URL的编码
查看>>
宿舍局域网的应用
查看>>
html代码究竟什么用途
查看>>
oracle的substr函数的用法
查看>>
QT 数据库编程四
查看>>
npm下载包时代理配置
查看>>
全球十大理论
查看>>
model.addAttribute("student",student)——渲染
查看>>
JavaWeb开发之普通图片验证码生成技术与算术表达式验证码生成技术
查看>>
python和pywin32实现窗口查找、遍历和点击
查看>>
第二章 概率图模型的基本原理
查看>>
Hadoop HDFS编程 API入门系列之路径过滤上传多个文件到HDFS(二)
查看>>
Nginx反向代理,负载均衡,redis session共享,keepalived高可用
查看>>
CentOS7 yum 安装git
查看>>
sublime text 3浅色主题
查看>>
数据结构之红黑树(三)——删除操作
查看>>