Python自动化测试系列[v1.0.0][数据驱动PARAMETERIZEEJSON]

安装PARAMETERIZE

C:\Users\Administrator>pip install parameterized
Collecting parameterized
  Downloading https://files.pythonhosted.org/packages/a3/bf/6ef8239028beae8298e0806b4f79c2466b1b16ca5b85dc13d631c5ea92c4/parameterized-0.7.1-py2.py3-none-any.whl
Installing collected packages: parameterized
Successfully installed parameterized-0.7.1

测试代码

# -*- coding: utf-8 -*-
# @Time: 4/27/2019 1:52 PM
# @Author : Yang DaWei
# @Project : DataDrivenTest
# @FileName: Unittest_Parameterized.py
import unittest
from selenium import webdriver
import time
from parameterized import parameterized
from selenium.common.exceptions import NoSuchElementException  # 引入NoSuchElementException异常类

class LoginTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.url = "http://mail.163.com"
        self.driver.implicitly_wait(10)

    def user_login_163(self, username, password):
        driver = self.driver
        driver.get(self.url)
        # 定义frame,他是页面中的iframe控件
        frame = self.driver.find_element_by_xpath("//*[@id='loginDiv']/iframe")
        time.sleep(1)
        try:
            self.driver.switch_to.frame(frame)  # 切换进iframe控件
            self.driver.find_element_by_name("email").send_keys(username)  # 输入用户名
            self.driver.find_element_by_name("password").send_keys(password)  # 输入密码
            self.driver.find_element_by_id("dologin").click()  # 点击登陆按钮
        except NoSuchElementException as e:
            # 将未找到页面元素的异常记录进日志
            raise e
        except Exception as e:
            raise e

    @parameterized.expand([
        ('', "davieyang", "请输入帐号"),
        ("davieyang", '', "请输入密码"),
        ("error", "error", "帐号或密码错误"),
    ])
    def test_login(self, username, password, assert_text):
        self.user_login_163(username, password)
        message = self.driver.find_element_by_id("nerror").text
        self.assertEqual(message, assert_text)

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

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

你可能感兴趣的:(Python自动化测试系列[v1.0.0][数据驱动PARAMETERIZEEJSON])