改进这段代码import time from time import sleep from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import Select import csv import unittest import ddt driver = webdriver.Chrome() driver.maximize_window() driver.get("http://10.2.39.8/ams/front/login.do?gotourl=http%3A%2F%2F10.2.39.8%2Fams%2Ffront%2Fasset%2Fasset_list.do") driver.implicitly_wait(5) driver.find_element_by_xpath('//*[@id="fmedit"]/div[1]/label[2]/input').click() driver.find_element_by_xpath('//*[@id="taskId"]').send_keys('23') driver.find_element_by_xpath('//*[@id="loginName"]').send_keys('20210205') driver.find_element_by_xpath('//*[@id="password"]').send_keys('20210205') driver.find_element_by_xpath('//*[@id="fmedit"]/div[7]/button').click() driver.find_element_by_xpath('//*[@id="leftmenu_asset_brand"]/a').click() driver.find_element_by_xpath('//*[@id="content"]/div[2]/div/div[1]/button').click() driver.find_element_by_id("title").send_keys("username") driver.find_element_by_id("code").send_keys("password") driver.find_element_by_css_selector('#submitButton').click()使用csv文件参数化username和password
时间: 2023-10-02 20:08:27 浏览: 161
可以使用ddt库来实现csv文件参数化,代码如下:
```
import time
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
import csv
import unittest
import ddt
@ddt.ddt
class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.get("http://10.2.39.8/ams/front/login.do?gotourl=http%3A%2F%2F10.2.39.8%2Fams%2Ffront%2Fasset%2Fasset_list.do")
self.driver.implicitly_wait(5)
def tearDown(self):
self.driver.quit()
@ddt.file_data('test_data.csv')
def test_login(self, username, password):
self.driver.find_element_by_xpath('//*[@id="fmedit"]/div[1]/label[2]/input').click()
self.driver.find_element_by_id("taskId").send_keys('23')
self.driver.find_element_by_id("loginName").send_keys(username)
self.driver.find_element_by_id("password").send_keys(password)
self.driver.find_element_by_css_selector('#fmedit > div:nth-child(7) > button').click()
self.assertIn('登录成功', self.driver.page_source)
```
其中,test_data.csv文件的格式为:
```
username,password
20210205,20210205
20210305,20210305
20210405,20210405
```
这样,就可以通过ddt库和csv文件实现对用户名和密码的参数化,从而实现自动化测试。
阅读全文