YAML的基础介绍 :https://www.jianshu.com/p/eaa1bf01b3a6
新建一个YAML存储数据的文件,后缀名为“.yaml”,内容如下:
案例说明:通过YAML数据来定义测试用例的参数。
案例代码:
import yaml, unittest, time
from selenium import webdriver
def read_yaml_data():
"获取yaml的数据"
yaml_data = open(r"D:\OneDrive\Code\Pycharm_Code\Test_Framework\Test_Automation\selenium3_code\DATA\data_0304.yaml",
'r', encoding='utf-8')
data = yaml.load(yaml_data)
yaml_data.close()
print(data)
return data
class Test_Login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.test_url = "https://mail.sohu.com/fe/#/login"
def tearDown(self):
self.driver.quit()
def by_xpath(self, usernameloc):
return self.driver.find_element_by_xpath(usernameloc)
def get_assert_text(self):
try:
time.sleep(2)
return self.by_xpath(
"//html[@id='ng-app']//div[@id='mailContent']//div[@class='hello ng-scope']/span[1]").text
except Exception as message:
print("报错,Error:%s" % message)
def sohu_login(self, user, pwd):
"""封装登录功能"""
self.by_xpath("//html[@id='ng-app']//div[@id='theme']//div[@class='ipt-box pos-r']/input[@type='text']").clear()
self.by_xpath(
"//html[@id='ng-app']//div[@id='theme']//div[@class='ipt-box pos-r']/input[@type='text']").send_keys(user)
self.by_xpath("//html[@id='ng-app']//div[@id='theme']//input[@type='password']").send_keys(pwd)
self.by_xpath("//html[@id='ng-app']//div[@id='theme']//input[@value='登 录']").click()
# 测试用例1
def sohu_login_test_01(self):
self.driver.get(self.test_url)
time.sleep(2)
self.sohu_login(read_yaml_data()['userNull']['username'], read_yaml_data()['userNull']['password'])
self.assertEqual(self.get_assert_text(), read_yaml_data()['userNull']['assertText'])
# 测试用例2
def sohu_login_test_02(self):
self.driver.get(self.test_url)
time.sleep(2)
self.sohu_login(read_yaml_data()['passwordNull']['username1'], read_yaml_data()['passwordNull']['password1'])
self.assertEqual(self.get_assert_text(), read_yaml_data()['passwordNull']['assertText1'])
if __name__ == "__main__":
unittest.main()