From 1cadb3c142541f15594786a15f14404c85e5a3d3 Mon Sep 17 00:00:00 2001 From: Joo Date: Wed, 25 Oct 2017 15:25:16 +0800 Subject: [PATCH] test_selenium use YAML --- Tests/test_Selenium.py | 15 +++++++++------ Util/commonTool/configUtil.py | 6 ++++-- Util/seleniumTool/loginPageUtil.py | 25 +++++++++++++++---------- config/selenium.yml | 17 +++++++++++++++++ 4 files changed, 45 insertions(+), 18 deletions(-) create mode 100644 config/selenium.yml diff --git a/Tests/test_Selenium.py b/Tests/test_Selenium.py index ef21f1c..57e1be2 100644 --- a/Tests/test_Selenium.py +++ b/Tests/test_Selenium.py @@ -10,6 +10,8 @@ from selenium import webdriver import time +selenium_yml = '/config/selenium.yml' + class test_login(object): def __init__(self): @@ -17,14 +19,15 @@ def __init__(self): @classmethod def setUpClass(cls): + site = ConfigUtil.get('site', path=selenium_yml) cls.driver = webdriver.Chrome() cls.driver.implicitly_wait(30) - cls.url = "http://mail.126.com" - cls.suffix = '@126.com' - cls.user = "tafffy" - cls.passwd = "lovesoo1314" - cls.title = u'网易' - cls.tips = u'帐号或密码错误' + cls.url = site['url'] + cls.title = site['title'] + cls.tips = site['tips'] + cls.suffix = site['suffix'] + cls.user = site['user'] + cls.passwd = site['passwd'] @classmethod def tearDownClass(cls): diff --git a/Util/commonTool/configUtil.py b/Util/commonTool/configUtil.py index e1f06f9..70ccfa1 100644 --- a/Util/commonTool/configUtil.py +++ b/Util/commonTool/configUtil.py @@ -30,9 +30,11 @@ def get(cls, section, option='', path='/config/test.yml'): filepath = ROOT + path config = yaml.load(file(filepath, 'r')) if option: - return str(config[section][option]) + result = config[section][option] else: - return str(config[section]) + result = config[section] + + return str(result) if isinstance(result, (str, unicode, int)) else result @classmethod def getint(cls, section, option='', path='/config/test.yml'): diff --git a/Util/seleniumTool/loginPageUtil.py b/Util/seleniumTool/loginPageUtil.py index eb772ab..9ba0c27 100644 --- a/Util/seleniumTool/loginPageUtil.py +++ b/Util/seleniumTool/loginPageUtil.py @@ -6,18 +6,24 @@ from selenium.webdriver.common.by import By from basePageUtil import basePage +from Util import * + +selenium_yml = '/config/selenium.yml' class loginPage(basePage): # 继承basePage类 + + loc = ConfigUtil.get('loc', path=selenium_yml) + # 定位器,通过元素属性定位元素对象 - username_loc = (By.NAME, 'email') - password_loc = (By.NAME, 'password') - submit_loc = (By.ID, 'dologin') - span_loc = (By.ID, 'nerror') - dynpw_loc = (By.ID, 'lbDynPw') - userid_loc = (By.ID, 'spnUid') - logout_loc = (By.ID, '_mail_component_41_41') + username_loc = eval(loc['username_loc']) + password_loc = eval(loc['password_loc']) + submit_loc = eval(loc['submit_loc']) + span_loc = eval(loc['span_loc']) + dynpw_loc = eval(loc['dynpw_loc']) + userid_loc = eval(loc['userid_loc']) + logout_loc = eval(loc['logout_loc']) # 操作 # 通过继承覆盖(Overriding)方法:如果子类和父类的方法名相同,优先用子类自己的方法。 @@ -28,11 +34,11 @@ def open(self): # 输入用户名:调用send_keys对象,输入用户名 def input_username(self, username): - self.send_keys(self.username_loc,username,clear_first=True) + self.send_keys(self.username_loc, username, clear_first=True) # 输入密码:调用send_keys对象,输入密码 def input_password(self, password): - self.send_keys(self.password_loc,password,clear_first=True) + self.send_keys(self.password_loc, password, clear_first=True) # 点击登录:调用send_keys对象,点击登录 def click_submit(self): @@ -60,4 +66,3 @@ def show_userid(self): # 登陆退出:调用send_keys对象,点击退出 def click_logout(self): self.find_element(self.logout_loc).click() - diff --git a/config/selenium.yml b/config/selenium.yml new file mode 100644 index 0000000..9c91119 --- /dev/null +++ b/config/selenium.yml @@ -0,0 +1,17 @@ +--- +site: + url: http://mail.126.com + title: 网易 + tips: 帐号或密码错误 + suffix: "@126.com" + user: tafffy + passwd: lovesoo1314 + +loc: + username_loc: (By.NAME, 'email') + password_loc: (By.NAME, 'password') + submit_loc: (By.ID, 'dologin') + span_loc: (By.ID, 'nerror') + dynpw_loc: (By.ID, 'lbDynPw') + userid_loc: (By.ID, 'spnUid') + logout_loc: (By.ID, '_mail_component_41_41') \ No newline at end of file