Selenium(Python) ddt读取MySQL数据驱动

BitTigerio 2018-03-01

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 />
Selenium(Python) ddt读取MySQL数据驱动
 

相关推荐

deadgrape / 0评论 2020-05-25
从零学习前端开发 / 0评论 2018-03-01