Import UnitTest
From time import sleep
From DDT import DDT, data
From Pymysql Import Connect
From selenium import Webdriver
Def getmysqltestdata ():
# ways to query the database
db = connect (host= "localhost",
User= "Root",
Password= "123456",
Db= "World",
port=3306,
charset= "UTF8")
# Open Database connection
cur = db.cursor ()
# get an action cursor using the cursor () method
sql = "Select ' Search_word ', ' Search_result ' from TestData;"
# SQL statements
Cur.execute (SQL)
# Execute SQL statement
Results = Cur.fetchall ()
# Get the results of a query
Db.commit ()
# submit
Cur.close ()
# Close Cursors
Db.close ()
# Disconnecting database connections
return results
# Returns a list
@ddt
# DDT Drive
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 ())
# Pass the argument
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 read MySQL data driven