码迷,mamicode.com
首页 > 编程语言 > 详细

【Python + Selenium3】之DDT数据驱动

时间:2019-11-05 17:14:59      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:ssm   python   sse   代码   stc   cto   pre   down   nbsp   

完整代码:

import unittest
from time import sleep
from selenium import webdriver
from ddt import ddt,data,file_data,unpack
from selenium.webdriver.common.by import By


@ddt
class TestBaiduSearch(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.url = "https://baidu.com"


    def baidu_search(self,keys):
        self.driver.get(self.url)
        self.driver.find_element(By.CSS_SELECTOR,"#kw").send_keys(keys)
        self.driver.find_element(By.CSS_SELECTOR,"#su").click()
        sleep(5)


    # 参数化例1
    @data(["case1","selenium3"],["case2","python3"],["case3","百度"])
    @unpack
    # @unittest.skip
    def test_baiduSearch01(self,case,data_key):
        print("第一组测试用例:"+case)
        self.baidu_search(data_key)
        self.assertEqual(self.driver.title,data_key + "_百度搜索",msg="标题不正确!")




    # 参数化例2:json
    @file_data("./data/ddt_data_file.data")
    def test_baiduSearch02(self,keys):
        print("第二组测试用例:",keys)
        self.baidu_search(keys)
        self.assertEqual(self.driver.title, keys + "_百度搜索", msg="标题不正确!")


    # 参数化例3:yaml
    @file_data("./data/ddt_data_file.yaml")
    @unpack
    def test_baiduSearch03(self,**kwargs):
        keys = kwargs[data1][1][keys]
        print("第三组测试用例:",keys)
        self.baidu_search(keys)
        self.assertEqual(self.driver.title, keys + "_百度搜索", msg="标题不正确!")


    @classmethod
    def tearDownClass(cls):
        print("用例结束!")
        cls.driver.quit()


if __name__ == __main__:
    unittest.main(verbosity=2)

json文件:ddt_data_file.data

{
  "case1":{"keys":"11"},
  "case2":{"keys":"12"},
  "case3":{"keys":"13"}
}

 

yaml文件:ddt_data_file.yaml

用例1:
  data1:
    - keys: "yaml01"
    - keys: "yaml02"
  data2:
    - keys: "yaml03"
    - keys: "yaml04"
用例2:
  data1:
    - keys: "yaml05"
    - keys: "yaml06"
  data2:
    - keys: "yaml07"
    - keys: "yaml08"
用例3:
  data1:
    - keys: "yaml09"
    - keys: "yaml10"
  data2:
    - keys: "yaml11"
    - keys: "yaml12"

yaml转换数组:

list = 
[{
‘case1‘:
{ ‘data1‘: [ { ‘keys‘: ‘yaml01‘ }, { ‘keys‘: ‘yaml02‘ } ],
‘data2‘: [ { ‘keys‘: ‘yaml03‘ }, { ‘keys‘: ‘yaml04‘ } ] },
‘case2‘:
{ ‘data1‘: [ { ‘keys‘: ‘yaml05‘ }, { ‘keys‘: ‘yaml06‘ } ],
‘data2‘: [ { ‘keys‘: ‘yaml07‘ }, { ‘keys‘: ‘yaml08‘ } ] },
‘case3‘:
{ ‘data1‘: [ { ‘keys‘: ‘yaml09‘ }, { ‘keys‘: ‘yaml10‘ } ],
‘data2‘: [ { ‘keys‘: ‘yaml11‘ }, { ‘keys‘: ‘yaml12‘ } ] }
}]

 

【Python + Selenium3】之DDT数据驱动

标签:ssm   python   sse   代码   stc   cto   pre   down   nbsp   

原文地址:https://www.cnblogs.com/Owen-ET/p/11799335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!