Selenium (Python) DDT read MySQL data driven

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.