实现程序与数据相分离,主要分为3步:
1.从UI对象库文件UiObjectMap.ini文件取得测试页面需要操作的页面元素的定位方式和定位表达式
2.从ObjectMap类取得该页面元素的实例对象
3.返回给测试用例方法中,进行后续处理
---好处:可以在一定条件满足一部分不会编码的测试人员实施自动化测试。
上代码!欢迎交流~~~
UiObjectMap.ini:
[163login] emailelement=xpath>//div[@class = "_1_E34 "] usernameelement=id>unameInput userpwdelement=id>pwdInput confirmbutton=class name>_3sT09 usernameinput=id>unameInput
ObjectMap.py
#encoding=utf-8 from selenium.webdriver.support.ui import WebDriverWait import ConfigParser import os from selenium import webdriver class ObjectMap(object): def __init__(self): # 获取存放页面元素定位表达方式及定位表达式的配置文件所在绝对路径 # os.path.abspath(__file__)表示获取当前文件所在路径目录 self.uiObjMapPath = os.path.dirname(os.path.abspath(__file__))\ + "\\UiObjectMap.ini" print self.uiObjMapPath def getElementObject(self, driver, webSiteName, elementName): try: # 创建一个读取配置文件的实例 cf = ConfigParser.ConfigParser() # 将配置文件内容加载到内存 cf.read(self.uiObjMapPath) # 根据section和option获取配置文件中页面元素的定位方式及 # 定位表达式组成的字符串,并使用“>”分割 locators = cf.get(webSiteName, elementName).split(">") # 得到定位方式 locatorMethod = locators[0] # 得到定位表达式 locatorExpression = locators[1] print locatorMethod, locatorExpression # 通过显示等待方式获取页面元素 element = WebDriverWait(driver, 10).until(lambda x: \ x.find_element(locatorMethod, locatorExpression)) except Exception, e: raise e else: # 当页面元素被找到后,将该页面元素对象返回给调用者 return element
test_163_login:
# encoding=utf-8
import unittest
import time
import chardet
from selenium import webdriver
from selenium.common.exceptions import TimeoutException,NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import traceback
import time
from ObjectMap import ObjectMap
class TestLoginByChrome(unittest.TestCase):
def setUp(self):
self.obj=ObjectMap()
self.driver = webdriver.Chrome(executable_path="D:\\python\\Scripts\\chromedriver.exe")
def test_a_ke163login(self):
url = "https://ke.youdao.com/login"
# 访问搜狗首页,焦点会自动定位到搜索输入框中
self.driver.get(url)
#显式等待,判断文本内容“网易邮箱登录 是否出现在了元素中
try:
wait = WebDriverWait(self.driver,10,0.2)
#当class的属性值,中间有空格时,通过by方法会报错:Compound class names not permitted
wait.until(EC.text_to_be_present_in_element((By.CLASS_NAME,"_1_E34"),u"手机号登录"))
emailelement = self.obj.getElementObject(self.driver,"163login","emailelement")
emailelement.click()
time.sleep(5)
wait1 = WebDriverWait(self.driver, 10, 0.2)
usernameinput = self.obj.getElementObject(self.driver,"163login","usernameinput")
#wait1.until(EC.visibility_of(self.driver.find_element_by_id("unameInput")))
wait1.until(EC.visibility_of(usernameinput))
usernameelement = self.obj.getElementObject(self.driver,"163login","usernameelement")
usernameelement.clear()
usernameelement.send_keys("[email protected]")
userpwdelement = self.driver.find_element_by_id("pwdInput")
userpwdelement.clear()
userpwdelement.send_keys("1qaz2wsx")
confirmbutton=self.obj.getElementObject(self.driver,"163login","confirmbutton")
confirmbutton.click()
time.sleep(5)
#判断是否登录成功跳转回主页
title= self.driver.title
self.assertEqual(title,u"有道精品课 - 为你精选好课","error occur ")
except TimeoutException,e:
print traceback.print_exc()
except NoSuchElementException,e:
print traceback.print_exc()
except Exception,e:
print traceback.print_exc()
def tearDown(self):
# 退出IE浏览器
self.driver.quit()
if __name__ == '__main__':
unittest.main()