Selenium(Python) ddt读取MySQL数据驱动
import unittest<br />from time import sleep<br /><br />from ddt import ddt, data<br />from pymysql import connect<br />from selenium import webdriver<br /><br /><br />def getMySQLTestData():<br /> # 查询数据库的方法<br /> db = connect(host="localhost",<br /> user="root",<br /> password="123456",<br /> db="world",<br /> port=3306,<br /> charset="utf8")<br /> # 打开数据库连接<br /> cur = db.cursor()<br /> # 使用cursor()方法获取操作游标<br /> sql = "SELECT `search_word`, `search_result` FROM testdata;"<br /> # sql语句<br /> cur.execute(sql)<br /> # 执行sql语句<br /> results = cur.fetchall()<br /> # 获取查询的结果<br /> db.commit()<br /> # 提交<br /> cur.close()<br /> # 关闭游标<br /> db.close()<br /> # 断开数据库连接<br /> return results<br /> # 返回一个list<br /><br /><br />@ddt<br /># ddt驱动<br />class MyTestCase(unittest.TestCase):<br /><br /> def setUp(self):<br /> self.driver = webdriver.Firefox()<br /> self.base_url = "https://www.baidu.com/"<br /> self.driver.get(self.base_url)<br /> self.driver.maximize_window()<br /> sleep(2)<br /><br /> @data(*getMySQLTestData())<br /> # 传参<br /> def test_something(self, sqlTestData):<br /> searchTerm, searchResult = sqlTestData<br /> driver = self.driver<br /> driver.find_element_by_xpath(".//*[@id='kw']").send_keys(searchTerm)<br /> driver.find_element_by_xpath(".//*[@id='su']").click()<br /> sleep(2)<br /> responseText = driver.find_element_by_xpath(".//*[@id='1']/h3/a").text<br /> self.assertEqual(responseText, searchResult)<br /><br /> def tearDown(self):<br /> self.driver.close()<br /> self.driver.quit()<br /><br />if __name__ == '__main__':<br /> unittest.main()<br /><br /><br />
相关推荐
abdstime 2020-07-29
追迷梦境 2020-06-13
AnndyR 2020-05-28
shengge0 2020-05-08
deadgrape 2020-03-06
JakobHu 2020-01-06
typhoonpython 2019-11-05
wklken的笔记 2019-10-19
wordmhg 2019-10-19
liuxiaohua 2018-03-14
Pythonandme 2019-02-22
BitTigerio 2018-02-28