Based on Python DDT, selenium data-driven test Example 1

Source: Internet
Author: User

#!/usr/bin/python #-*-Coding:utf-8-*-"Created on 2015-04-27@author:beyondzhou@name:logintest.py" ' Import Unittestfrom DDT import DDT, data, Unpackfrom Selenium import webdriverimport osimport timefrom selenium.common.exception S import nosuchelementexceptionfrom selenium.webdriver.common.by import By@ddtclass logintest (unittest. TestCase): @classmethod def setUp (CLS): # Get The driver of chrome chromedriver = "C:\Program File S\google\chrome\application\chromedriver.exe "os.environ[" webdriver.chrome.driver "] = Chromedriver cls.brows ER = webdriver. Chrome (chromedriver) cls.browser.get (' http://10.10.51.150 ') time.sleep (1) @data (("admin", "pica8", "confi Guration "), (" admin "," pica9 "," Invalid username or password ")) @unpack def test_login_in (self, username, password, Expected_value): # Test Login in Self.browser.find_element_by_xpath ("//input[@placeholder = ' Use Rname ']). Send_keys (username) sElf.browser.find_element_by_xpath ("//input[@placeholder = ' Password ']"). Send_keys (Password) self.browser.find_ Element_by_tag_name ("button"). Click () time.sleep (1) self.asserttrue (self.is_content_present ("%s"% Expected_ Value) def is_element_present (self, how, what): "" "Utility method to check presence of an Elem ENT on page:p arams how:by locator type:p arams what:locator value "" "Try:self . Browser.find_element (By=how, value=what) except Nosuchelementexception, _: Return False return T        Rue def is_content_present (self, What): "" "Utility method to check presence of a element on page :p arams what:content value "" "Try:self.browser.find_element (By=by.xpath, value="//*[cont Ains (Text (), '%s ')] "% what) except Nosuchelementexception, _: Return False return True @c Lassmethod def tearDown (CLS): # define when you need Cls.browser.quit () if __name__ = = ' __main__ ': Unittest.main (verbosity=2) 


Result:

Test_login_in_1___admin____pica8____configuration__ (__main__. Logintest) ... oktest_login_in_2___admin____pica9____invalid_username_or_password__ (__main__. logintest) ... ok----------------------------------------------------------------------Ran 2 tests in 17.688sOK


Based on Python DDT, selenium data-driven test Example 1

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.