#!/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