页面元素定位信息
页面元素定位信息文件
[leadscloud_login]
input_user_name = xpath>//*[@id='main']/div/div[2]/div[2]/div[2]/div/form/div[1]/div/div/input
input_user_password = xpath>//*[@id='main']/div/div[2]/div[2]/div[2]/div/form/div[2]/div/div[1]/input
button_login = xpath>//*[@id='main']/div/div[2]/div[2]/div[2]/div/form/div[3]/div/button
解析定位信息获取元素对象
解析定位信息
"""
用于解析配置文件,并获取页面元素定位表达式
"""
# -*- coding: utf-8 -*-
# @Time: 2019/2/26 11:43
# @Author : Yang DaWei
# @Project : LeadsCloudAutomation
# @FileName: ParseElementLocator.py
from configparser import ConfigParser
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
class ParseConfigFile:
"""
初始化解析文件类
"""
def __init__(self, driver, page_element_locator):
self.driver = driver
self.cf = ConfigParser()
self.cf.read(page_element_locator, encoding='utf-8')
def get_items_section(self, section_name):
"""
获取配置文件中指定section下的所有option_name键值对,并以字典类型返回给调用者
注意:使用self.cf.items(sectionName)此种方法获取到
配置文件中的options内容均被转换成小写,如loginPage.frame将被转换成loginpage.frame
"""
options_dict = dict(self.cf.items(section_name))
return options_dict
def get_option_value(self, section_name, option_name):
"""
获取指定section下的指定option的值
"""
value = self.cf.get(section_name, option_name)
return value
def get_element_location(self, section_name, option_name, timeout):
"""
获取页面元素定位表达式,并以元素对象的形式返回给调用者
:param section_name:
:param option_name:
:param timeout:
:return:
"""
driver = self.driver
location = self.get_option_value(section_name, option_name)
location_type = location.split(">")[0]
location_value = location.split(">")[1]
print("读取到的定位类型为:" + location_type + "\t读取到的定位信息为:" + location_value)
try:
element = WebDriverWait(driver, timeout).until(lambda x: x.find_element(by=location_type,
value=location_value))
return element
except Exception as e:
print("定位元素超过" + str(timeout) + "秒,详细异常信息入下:")
raise e
def highlight_element(self, driver, element):
"""
调用JS,用于高亮控件
:param driver:
:param element:
:return:
"""
driver.execute_script("arguments[0].setAttribute('style', arguments[1]);", element,
"background: yellow; border:2px solid red;")
if __name__ == '__main__':
from selenium import webdriver
# from Config import VarConfig
from PageObject.Login_Page import Login_Page
# locator_file = VarConfig.login_page
driver = webdriver.Firefox()
driver.get("http://test.leadscloud.com/Front-Vue/#/login")
sleep(5)
Login_Page(driver).input_user_name().send_keys("yangdawei_10171")
# ge = ParseConfigFile(driver, locator_file)
# input_user_name = ge.get_element_location("leadscloud_login", "input_user_name", 5)
# ge.highlight_element(driver, input_user_name)
Login_Page(driver).input_user_password().send_keys("yangdawei_10166")
# input_user_password = ge.get_element_location("leadscloud_login", "input_user_password", 5)
# ge.highlight_element(driver, input_user_password)
# input_user_password.send_keys("111111")
# button_login = ge.get_element_location("leadscloud_login", "button_login", 5)
# ge.highlight_element(driver, button_login)
Login_Page(driver).login_button().click()
driver.quit()
页面元素对象封装
解析定位文件并封装成可操作对象
'''用于获取PageElementLocator.ini 中Login页面元素对象'''
# encoding = utf-8
"""
__title__ = ''
__author__ = 'davieyang'
__mtime__ = '2018/4/21'
"""
from Util.GetElements import *
from Util.ParseElementLocator import ParseConfigFile
class LoginPage(object):
def __init__(self, driver):
self.driver = driver
self.parseCF = ParseConfigFile()
self.loginOptions = self.parseCF.getItemsSection("163mail_login")
print(self.loginOptions)
def switchtoframe(self):
try:
# 从定位表达式配置文件中读取frame的定位表达式
locatorExpression = self.loginOptions["loginPage.frame".lower()].split('>')[1]
self.driver.switch_to.frame(locatorExpression)
except Exception as e:
raise e
def switchtodefaultframe(self):
try:
self.driver.switch_to.default_content()
except Exception as e:
raise e
def usernameobj(self):
try:
# 从定位表达式配置文件中读取定位用户名如输入框的定位方式和表达式
locateType, locatorExpression = self.loginOptions["loginPage.username".lower()].split(">")
# 获取登录页面的用户名输入框页面对象,返回给调用者
elementobj = get_element(self.driver, locateType, locatorExpression)
return elementobj
except Exception as e:
raise e
def passwordobj(self):
try:
# 从定位表达式配置文件中读取定位密码输入框的定位方式和表达式
locateType, locatorExpression = self.loginOptions["loginPage.password".lower()].split(">")
# 获取登录页面的密码输入框页面对象,并返回给调用者
elementoj = get_element(self.driver, locateType, locatorExpression)
return elementoj
except Exception as e:
raise e
def loginbutton(self):
try:
# 从定位表达式配置文件中读取定位登录按钮的定位方式和表达式
locateType, locatorExpression = self.loginOptions["loginPage.loginbutton".lower()].split(">")
# 获取登录页面的登录按钮页面对象,并返回给调用者
elementobj = get_element(self.driver, locateType, locatorExpression)
return elementobj
except Exception as e:
raise e
if __name__ == '__main__':
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path="F:\\automation\\webdriver\\chromedriver.exe")
driver.get("http://mail.163.com")
time.sleep(5)
login = LoginPage(driver)
login.switchtoframe()
login.usernameobj().send_keys("xxxx")
login.passwordobj().send_keys("xxxx")
login.loginbutton().click()
time.sleep(10)
login.switchtodefaultframe()
assert u"未读邮件" in driver.page_source
driver.quit()
测试代码示例
测试脚本
# -*- coding: utf-8 -*-
# @Time: 2019/3/1 13:40
# @Author : Yang DaWei
# @Project : LeadsCloudAutomation
# @FileName: test_login_page.py
import time
import unittest
from selenium import webdriver
from PageObject.Login_Page import Login_Page
class Test_Login_Page(unittest.TestCase):
driver = None
@classmethod
def setUpClass(cls):
""" set Up method """
cls.driver = webdriver.Chrome()
cls.test_page = ''
time.sleep(3)
def tearDown(self):
""" tear Down method """
def setUp(self):
"""setUP pass"""
@classmethod
def tearDownClass(cls):
""" tear Down method """
cls.driver.close()
def test_login(self):
self.driver.get("http://admin.leadscloud.com/Front-Vue/#/")
Login_Page(self.driver).input_user_name().send_keys("yangdawei_10171")
Login_Page(self.driver).input_user_password().send_keys("111111")
Login_Page(self.driver).login_button().click()
try:
self.assertTrue("仪表盘", self.driver.page_source)
print("断言成功")
except Exception as e:
raise e
time.sleep(10)