python3 selenium 自动化 unittest单元测试框架之360搜索测试用例

**前面我写了百度搜索的unittest单元测试框架**

python3 selenium自动化 unittest单元测试 百度搜索例子详解
前面的文章写的比较简单,而且在实际应用中不经常用,前面的文章没有参数化。
今天说下工作中常用的参数化,函数二次封装调用的方法,这些方法很实用,废话少说,直接上代码,代码如下:

from selenium import webdriver
from time import sleep
import unittest

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

    #环境恢复
    @classmethod
    def tearDownClass(self):
        self.dr.quit()

     #打开网页
    def getUrl(self,url):
        return self.dr.get(url)

    #输入搜索信息
    def input_text(self,locator,text):
        return self.by_id(locator).send_keys(text)

    #点击搜索
    def click_btn(self,locator):
        return self.by_class(locator).click()

   #搜索后进入断言
    def assertText(self,text):
        try:
            sleep(3)
            self.assertIn(text,self.dr.title)
        except Exception as meg:
            print('断言错误')

    #by_id 定位进行封装
    def by_id(self,locator):
        return self.dr.find_element_by_id(locator)

     #by_class定位进行封装
    def by_class(self,locator):
        return self.dr.find_element_by_class_name(locator)

   #对前面的函数再次封装
    def all_actions(self,url,loc1,text,loc2):
        self.getUrl(url)

        self.dr.maximize_window()

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

    # 执行用例1,搜索shawn关键字并断言
    def test1_shawn(self):
        name = 'shawn'
        self.all_actions(self.url,'input',name,'skin-search-button')
        self.assertText(name)

     # 执行用例2,搜索jamesxie关键字并断言
    def test2_james(self):
        name = 'jamexie'
        self.all_actions(self.url, 'input', name, 'skin-search-button')
        self.assertText(name)

    # 执行用例3,搜索xiezhiming关键字并断言
    def test3_xiezhiming(self):
        name = 'xiezhiming'
        self.all_actions(self.url, 'input', name, 'skin-search-button')
        self.assertText(name)



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

猜你喜欢

转载自blog.csdn.net/xiezhiming1234/article/details/82263127