1、添加locator文件
#-*-coding:utf-8-*-
PROJECT_LOCATOR = {
"通用": {
"通用_首页_BUTTON_登录": "xpath=//a[text()='登录']",
"通用_首页_TEXT_你好": "xpath=//a[contains(text(),'你好')]",
"通用_首页_BUTTON_主题": "xpath=//a[text()='主题']",
},
"登录": {
"登录_首页_TITLE_输入账号": "xpath=//*[text()='Log in to your account.']",
"登录_首页_INPUT_用户名": "id=id_username",
"登录_首页_INPUT_密码": "id=id_password",
"登录_首页_BUTTON_登录": "name=submit",
},
"主题": {
"主题_首页_TITLE_主题": "xpath=//h1[text()='主题']",
"主题_首页_BUTTON_新建主题": "xpath=//a[text()='新建主题']",
"主题_新建_TITLE_新主题": "xpath=//h2[text()='添加一个新主题:']",
"主题_新建_INPUT_主题名称": "id=id_text",
"主题_新建_BUTTON_添加": "name=submit",
},
}
2、读取locator文件
#-*-coding:utf-8-*-
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
def get_locator_value(locator_key):
from variables import locator
locator_variables = locator.PROJECT_LOCATOR
class_name = locator_key.split("_")[0]
class_variables = locator_variables[class_name]
locator_value = class_variables[locator_key]
logging.info("Locato is {}:{}".format(locator_key, locator_value))
return locator_value
3、Selenium方法二次分装
目前Selenium2Library实际上是完全继承自SeleniumLibrary的,没有做任何修改,只是套了一层皮而已,所以我们之后直接使用SeleniumLibrary;
@keyword这装饰器比较有意思可以让我们用中文来定义关键的名字。
#-*-coding:utf-8-*-
from lib import SysLib
from SeleniumLibrary import SeleniumLibrary
from SeleniumLibrary.base import keyword
from SeleniumLibrary.keywords import (AlertKeywords,
BrowserManagementKeywords,
CookieKeywords,
ElementKeywords,
FormElementKeywords,
FrameKeywords,
JavaScriptKeywords,
RunOnFailureKeywords,
ScreenshotKeywords,
SelectElementKeywords,
TableElementKeywords,
WaitingKeywords,
WebDriverCache,
WindowKeywords)
class SeleniumLib(SeleniumLibrary):
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
def __init__(self, *args, **kwargs):
super(SeleniumLib,self).__init__(*args, **kwargs)
def find_element(self, locator, parent=None):
if "="not in locator:
locator = SysLib.get_locator_value(locator)
return super(SeleniumLib, self).find_element(locator, parent=parent)
@keyword("点击")
def click_element(self, locator):
locator = self.find_element(locator)
return ElementKeywords(self).click_element(locator=locator)
@keyword("检查元素")
def wait_until_page_contains_element(self, locator):
locator = self.find_element(locator)
return WaitingKeywords(self).wait_until_page_contains_element(locator=locator)
@keyword("检查元素不存在")
def wait_until_page_does_not_contain_element(self, locator):
locator = self.find_element(locator)
return WaitingKeywords(self).wait_until_page_does_not_contain_element(locator=locator)
@keyword("检查文本")
def wait_until_page_contains(self, text):
return WaitingKeywords(self).wait_until_page_contains(text=text)
@keyword("检查文本不存在")
def wait_until_page_does_not_contain(self, text):
return WaitingKeywords(self).wait_until_page_does_not_contain(text=text)
@keyword("输入文本")
def input_text(self, locator, text):
locator = self.find_element(locator)
return FormElementKeywords(self).input_text(locator=locator, text=text)
4、项目结构调整
对我们已经编写的项目结构中一些不合理的地方进行调整,robotframework支持使用中文关键字,为了更加便于理解我们将所有关键字改为中文。还修改了一些文件放置的位置,同学们请自行修改一下文件之间的调用关系。
5、新增测试用例
新增了一条完整的测试用例,有完整的前置条件、后置条件、测试步骤检查点,完全符合在实际项目中的使用,小伙伴们也可以在此项目的基础上自己再编写一些自动化测试用例以做练习。
5、GitHub项目链接
该项目已上传至GitHub,有兴趣的同学请自行下载:https://github.com/SanQuan007/SeleniumTest_RF