Selenium2+python automation 59 - data driven (ddt) Selenium2+python automation 48 - login method (parameterized)

foreword

When designing use cases, some use cases are only different in the input of parameter data, such as the login function, but the operation process is the same. If the use case is repeatedly written, the amount of code will increase. For test cases with multiple sets of data, the data-driven design pattern can be used. One set of data corresponds to a test case, and the test case is automatically loaded and generated.

1. Environmental preparation

1. Install the ddt module, open cmd and enter pip install ddt online installation

>>pip install ddt

 

2. Data-driven principle

1. The test data is a list type of multiple dictionaries

2. Add modification @ddt.ddt before the test class

3. Add modification @ddt.data() before case

4. After running the use case will be automatically loaded into three separate use cases

5. Test results:

Testing started at 21:51 ...
start!
{'username': 'selenium\xe7\xbe\xa4', 'psw': '232607095'}
end!
start!
{'username': 'python\xe7\xbe\xa4', 'psw': '226296743'}
end!
start!
{'username': 'appium\xe7\xbe\xa4', 'psw': '512200893'}
end!

 

Three, selenium case

1. Read data from the excel method encapsulated in the previous article as a test test Selenium2+python automation 58-read Excel data (xlrd)

2. Make some modifications on the basis of the login article written before, and test the parameters to read the data in excel Selenium2+python automation 48-login method (parameterization)

3. The code reference is as follows

# Test data
testData = data.dict_data()
print testData

@ddt.ddt
class Bolg(unittest.TestCase):
    u'''Log in to blog'''
    def setUp(self):
        self.driver = webdriver.Firefox()
        url = "https://passport.cnblogs.com/user/signin"
        self.driver.get(url)
        self.driver.implicitly_wait(30)

    def login(self, username, psw):
        u'''A login is written here method, account and password parameterized '''
        self.driver.find_element_by_id("input1").send_keys(username)
        self.driver.find_element_by_id("input2").send_keys(psw)
        self.driver.find_element_by_id("signin" ).click()
        time.sleep(3)

    def is_login_sucess(self):
        u'''Determine whether the login account name is obtained'''
        try:
            text = self.driver.find_element_by_id("lnk_current_user").text
            print text
            return True
        except:
            return False

    @ddt.data(*testData)
    def test_login(self , data):
        u'''Login case reference'''
        print ("Current test data%s"%data)
        # Call the login method
        self.login(data["username"], data["password"])
        # Judgment result
        result = self.is_login_sucess()
        self.assertTrue(result)

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339438&siteId=291194637