Selenium(Python) ddt读取MySQL数据驱动
Posted 此生不换Yang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium(Python) ddt读取MySQL数据驱动相关的知识,希望对你有一定的参考价值。
import unittest
from time import sleep
from ddt import ddt, data
from pymysql import connect
from selenium import webdriver
def getMySQLTestData():
# 查询数据库的方法
db = connect(host="localhost",
user="root",
password="123456",
db="world",
port=3306,
charset="utf8")
# 打开数据库连接
cur = db.cursor()
# 使用cursor()方法获取操作游标
sql = "SELECT `search_word`, `search_result` FROM testdata;"
# sql语句
cur.execute(sql)
# 执行sql语句
results = cur.fetchall()
# 获取查询的结果
db.commit()
# 提交
cur.close()
# 关闭游标
db.close()
# 断开数据库连接
return results
# 返回一个元祖
@ddt
# ddt驱动
class MyTestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.base_url = "https://www.baidu.com/"
self.driver.get(self.base_url)
self.driver.maximize_window()
sleep(2)
@data(*getMySQLTestData())
# 传参
def test_something(self, sqlTestData):
searchTerm, searchResult = sqlTestData
driver = self.driver
driver.find_element_by_xpath(".//*[@id=\'kw\']").send_keys(searchTerm)
driver.find_element_by_xpath(".//*[@id=\'su\']").click()
sleep(2)
responseText = driver.find_element_by_xpath(".//*[@id=\'1\']/h3/a").text
self.assertEqual(responseText, searchResult)
def tearDown(self):
self.driver.close()
self.driver.quit()
if __name__ == \'__main__\':
unittest.main()
以上是关于Selenium(Python) ddt读取MySQL数据驱动的主要内容,如果未能解决你的问题,请参考以下文章