python + selenium数据驱动(参数化)

来源:

https://blog.csdn.net/adonis_lu37/article/details/79346367

读取txt文件中的数据来实现参数化

创建data.txt文件

baidu_readData.py

#-*- coding:utf-8 -*-

from selenium import webdriver
import os,time

source = open("C:\Python27\example\data.txt","r") values = source.readlines() source.close() driver = webdriver.Chrome() driver.get("http://www.baidu.com") for val in values: driver.find_element_by_id("kw").clear() driver.find_element_by_id("kw").send_keys(val) driver.find_element_by_id("su").click() time.sleep(3) driver.quit() 

登录参数化(读取txt文件)

按照上面的思路,对自动化脚本中用户名、密码进行参数化,通过Python文档我们发现读取文件的方式有:整个文件读取、逐行读取、固定字节读取。并没有找到一次读取两条数据的好方法。

创建data.txt文件

login_douban_by_txt.py

#-*- coding:utf-8 -*-

from selenium import webdriver
import os,time

source = open("C:\Python27\example\data.txt","r") values = source.readlines() source.close() for val in values: if "username" in val: un = val.split('=')[1:] elif "password" in val: pw = val.split('=')[1:] print un,pw driver = webdriver.Chrome() driver.get("http://www.douban.com") driver.find_element_by_name("form_email").clear() driver.find_element_by_name("form_email").send_keys(un) time.sleep(3) driver.find_element_by_name("form_password").clear() driver.find_element_by_name("form_password").send_keys(pw) time.sleep(3) driver.find_element_by_class_name("bn-submit").click() time.sleep(3) driver.quit() 

登录参数化(函数)

userinfo.py

def fun(un="15757119427",pw='mailu777333'): print "successful read username and password!" return un,pw

login_douban_by_function.py

from selenium import webdriver
import hello_selenium,time

un,pw=hello_selenium.fun()

print un,pw

driver = webdriver.Chrome()
driver.get("http://www.douban.com") driver.find_element_by_name("form_email").clear() driver.find_element_by_name("form_email").send_keys(un) time.sleep(3) driver.find_element_by_name("form_password").clear() driver.find_element_by_name("form_password").send_keys(pw) time.sleep(3) driver.find_element_by_class_name("bn-submit").click() time.sleep(3) driver.quit() 

表单参数化(csv)

创建userinfo.csv文件

loop_read.py

#-*- coding:utf-8 -*-

#导入csv包
import csv

#读取本地csv文件
my_file="C:\Python27\example\data.csv" data=csv.reader(file(my_file,'rb')) #循环输出每一行的内容 for user in data: print user[0] print user[1] print user[2] print user[3]

运行结果:

 

实例:

# _*_ coding:utf-8 _*_
import csv,unittest #导入unittest 模块
from time import sleep
from selenium import webdriver
from module import baidumodule
class baidu(unittest.TestCase):#创建测试用例集
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()#最大化窗口
self.driver.implicitly_wait(10)#隐式等待
self.search = baidumodule(self.driver) #将driver传给aidumodule这个类
with open("file.csv","r") as name:
self.lines = name.readlines()#以行读取整个文件
def tearDown(self):
self.search.login_out()#调用登出函数

def test_search(self):#测试用例一
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[0])
sleep(1)
title = driver.title
self.assertEqual(title,'selenium_百度搜索')#判断当前title是否等于selenium_百度搜索
sleep(2)
def test_search1(self):#测试用例二
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[1])
sleep(1)
title = driver.title
self.assertEqual(title,'selenium2_百度搜索')
sleep(2)
def test_search2(self):#测试用例三
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[2])
sleep(1)
title = driver.title
self.assertEqual(title,'selenium3_百度搜索')
sleep(2)
def test_search3(self):#测试用例四
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[3])
sleep(1)
title = driver.title
self.assertEqual(title,'webdriver_百度搜索')
sleep(2)

if __name__ == "__main__":
unittest.main()

执行结果:

....
----------------------------------------------------------------------
Ran 4 tests in 51.427s

OK

可以看到执行结果中成功返回我们的测试用例数,并成功执行退出(项目中善用sleep,这里只是做演示,sleep太多执行时间太长)
如果执行失败

self.assertEqual(title,'selenium_百度搜索2')
AssertionError: 'selenium_百度搜索' != 'selenium_百度搜索2'
Ran 4 tests in 23.518s

FAILED (failures=1)

原文:https://blog.csdn.net/zha6476003/article/details/80317181

转载于:https://www.cnblogs.com/jiaoxiaohui/p/10517922.html

你可能感兴趣的:(python + selenium数据驱动(参数化))