360 search test case of unit test framework of Python 3 selenium automation

**I wrote the unit test framework of Baidu search**

Python 3 selenium automation unit test Baidu search example details
The previous article is relatively simple, and it is not often used in practical applications. The previous article is not parameterized.
Today, I'll talk about the commonly used parameterization and the method of function secondary encapsulation and call. These methods are very practical, with less nonsense and direct code. The code is as follows:

from selenium import webdriver
from time import sleep
import unittest

class Test(unittest.TestCase):
    #Environment preset
    @classmethod
    def setUpClass(self):
        self.dr = webdriver.Chrome()
        self.url = 'https://www.so.com/'
        self.dr.implicitly_wait(6)

    #Environmental restoration
    @classmethod
    def tearDownClass(self):
        self.dr.quit()

     #Open web page
    def getUrl(self,url):
        return self.dr.get(url)

    #Enter search information
    def input_text(self,locator,text):
        return self.by_id(locator).send_keys(text)

    #Click search
    def click_btn(self,locator):
        return self.by_class(locator).click()

   #Enter assertion after search
    def assertText(self,text):
        try:
            sleep(3)
            self.assertIn(text,self.dr.title)
        except Exception as meg:
            print('assertion failure')

    #By ID positioning for encapsulation
    def by_id(self,locator):
        return self.dr.find_element_by_id(locator)

     #By class positioning for encapsulation
    def by_class(self,locator):
        return self.dr.find_element_by_class_name(locator)

   #Encapsulate the previous function again
    def all_actions(self,url,loc1,text,loc2):
        self.getUrl(url)

        self.dr.maximize_window()

        self.input_text(loc1,text)
        self.click_btn(loc2)

    # Execute case 1, search shawn keyword and assert
    def test1_shawn(self):
        name = 'shawn'
        self.all_actions(self.url,'input',name,'skin-search-button')
        self.assertText(name)

     # Execute case 2, search for the jamesxie keyword and assert
    def test2_james(self):
        name = 'jamexie'
        self.all_actions(self.url, 'input', name, 'skin-search-button')
        self.assertText(name)

    # Execute case 3, search for the xiezhiming keyword and assert
    def test3_xiezhiming(self):
        name = 'xiezhiming'
        self.all_actions(self.url, 'input', name, 'skin-search-button')
        self.assertText(name)



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

Keywords: Selenium Python less

Added by cptn_future on Tue, 31 Dec 2019 19:48:51 +0200