通过设置csv文件的意义:从csv文件中读取参数,例如登录的用户名和密码,如果修改了密码,不需要改动脚本,只需要改动csv文件的值就可以了。
在执行目录下建一个.csv的文件
再建一个.py的文件,代码如下
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import *
import unittest
import time
import csv
dates = csv.reader(open("D:\\Pychrom project\\cvs\\login.csv",'r'))
list = []
for date in dates:
if date == 0:
pass
else:
list.append(date)
print(list)
print(list[0][0])
print(list[1][0])
print(list[2][0])
class TestLogin(unittest.TestCase):
""""test login case"""
def test_login(self):
"""test login with cell phone number"""
print("test login.")
self.driver.find_element_by_xpath("//div[@class='hd-fBtn-user']").click()
phone = self.driver.find_element_by_xpath("//span[@class='eico eico-phone']")
ActionChains(self.driver).click(phone).perform()
self.driver.find_element_by_xpath("//input[@placeholder='手机号或邮箱']").clear()
self.driver.find_element_by_xpath("//input[@placeholder='手机号或邮箱']").send_keys(list[0][0])
self.driver.find_element_by_xpath("//input[@placeholder='密码']").clear()
self.driver.find_element_by_xpath("//input[@placeholder='密码']").send_keys(list[1][0])
self.driver.find_element_by_xpath("//input[@class='ping gl-btnLog login_mail_btn']").click()
time.sleep(10)
#登录断言
"""""use the assertEqual function"""
self.assertEqual(login_name, list[2][0], msg="login failed")
if __name__ == '__main__':
unittest.main()
分析
三个地方用到了参数化
1.输入邮箱
2.输入密码
3.断言里的判断