Robot学习笔记
一、环境安装
应客户要求,IDE使用Eclipse插件RED,需要使用目前eclipse最新Oxygen版本才能更好兼容RED.
1.下载并安装Oxygen
2.Merketing搜索RED安装插件
3.下载并安装Python 2.7.13
4.配置环境变量,//pyhon; //python27/Scripts;
5.在线安装robot framework
cmd> pip install robotframework
6.在线安装selenium2Library
cmd> pip install robotframework-selenium2library
7.使用命令查看在线安装是否正常完成
robot --version
pip list
pip ist下能看到robot framework 和 selenium2library为正常安装
8.将IE driver放到path环境变量目录下,可以放到 c:/windows/ 或者刚配置的 python 根目录下
9.在eclipse下new robot projecr > new test suite
*** Settings *** Documentation description Library String Library Selenium2Library*** Variables *** ${var} Hello world! *** Test Cases *** Sample Test Case1 [Documentation] test case Open Browser http://www.baidu.com ie Input Text id=kw test Click Button id=su
如果能运行成功代表环境正常
二、遇到的问题
1. 参数中有中文报错
*** Settings *** Documentation description Library String Library Selenium2Library *** Variables *** ${var} Hello world! *** Test Cases *** Sample Test Case1 [Documentation] test case Open Browser http://www.baidu.com ie Input Text id=kw test Click Button id=su
${value} Get Title
Should Be Equal ${value} test_百度搜索
解决方案:
1.更改eclipse编码 Windows > Preferance > General > Workspace > Text file Encoding > utf-8
2.更改robot源码中的编码
找到Python安装目录下的\Lib\site-packages\robot\utils\unic.py文件
引入json库:import json
将下面的代码插入,不更改其他部分:
if isinstance(item, (list, dict, tuple)): try: item = json.dumps(item, ensure_ascii=False, encoding='cp936') except UnicodeDecodeError: try: item = json.dumps(item, ensure_ascii=False, encoding='cp936') except: pass except: pass
更改后的代码为:
from pprint import PrettyPrinter from .platform import IRONPYTHON, JYTHON, PY2 from .robottypes import is_bytes, is_unicode import json if PY2: def unic(item): if isinstance(item, unicode): return item if isinstance(item, (bytes, bytearray)): try: return item.decode('ASCII') except UnicodeError: return u''.join(chr(b) if b < 128 else '\\x%x' % b for b in bytearray(item)) if isinstance(item, (list, dict, tuple)): try: item = json.dumps(item, ensure_ascii=False, encoding='cp936') except UnicodeDecodeError: try: item = json.dumps(item, ensure_ascii=False, encoding='cp936') except: pass except: pass try: try: return unicode(item) except UnicodeError: return unic(str(item)) except: return _unrepresentable_object(item)
2.Robot 在定位元素的时候参数很多都写的是locator,但是这个Documentation 中locator 很灵活,可以是str类型的locator,也可以是webElement。并且str的locator是否书写前缀也是可选的。这里建议写前缀,因为在不写前缀的时候默认是按照default方式去查找element,效率会降低。
同时需要注意的是Get Element Attribute这个关键字的参数为attribute_locator,也就是locator@attr的形式。此处只能使用str类型的locator。如果是使用WebElement@attr进行拼接会把WebElement强制转化为str,并与@attr拼接(selenium.webdriver.remote.webelement.WebElement (session="4dff1f83afb281c77302e0c50d9fce82", element="0.5725955511954164-1")@id)
3. 在实践过程中发现如果locator使用css会在某些情况下找元素失败,查看了底层代码后发现find element方法中的parse prefix 方法判断逻辑为如果以//开头则认为prefix 为xpath,否则以=作为separator对locator进行分割,等号前为prefix,等号后为locator。
但是在css的语法中其实也能够包含=,,所以此时如果css类型的locator含有等号且不包含前缀css=那么就会报错为*** is not correct prefix.
我觉得此处应该算是selenium2library库的一个defec
4. 在测试过程中封装了一个在主页close all popups 的关键字,提取出了主页中的popup 页面的close button的locator为 css=div[style*=display:block] a.close, 当页面有多个popup时循环遍历并点击所有的关闭按钮。
${popCloseXpath_Loc} xpath=//div[contains(@style,"display:block")]//div[@class="pop_footer"]//a[@class="close"] Clear Home Popup @{popCloses}= Get WebElements ${popClose_Loc} ${popLen} Get Length ${popCloses} :FOR ${index} IN RANGE 0 ${popLen} \ Run Keyword And Ignore Error Click Element ${popClose_Loc}
${popCloseXpath_Loc} xpath=//div[contains(@style,"display:block")]//div[@class="pop_footer"]//a[@class="close"]
Clear Home Popup @{popCloses}= Get WebElements ${popClose_Loc} ${popLen} Get Length ${popCloses} :FOR ${index} IN RANGE 0 ${popLen} \ Run Keyword And Ignore Error Assign Id And Click Element By JS ${popClose_Loc}
#Assign Id And Click Element By J
Assign Id And Click Element By JS
[Arguments] ${element}
Wait Until Page Contains Element ${element}
${element} Get Sub Locator ${element} #去掉locator中prefix部分(xpath=)
Assign "${element}" Id As boundry
Execute Javascript $("#${elementId}")[0].click();
Run Keyword And Ignore Error Remove "${element}" Id boundry
#Assign Id
Assign "${element}" Id As boundry
[Documentation] Assign Id to element if it not have one
${id} Get Element Attribute ${element}@id
${id} Set Variable If '${id}'!='${EMPTY}' ${id}
... '${id}'=='${EMPTY}' boundary
Run Keyword If '${id}'=='boundary' Assign ID to Element ${element} ${id}
Set Global Variable ${elementId} ${id}
#Remove Id
Remove "${element}" Id boundry
[Documentation] Remove Id if it's boundary
Return From Keyword If '${elementId}'=='${EMPTY}' Log To Console ID제거(Skip)
Run Keyword If '${elementId}'=='boundary' Assign ID to Element ${element} ${EMPTY}
${popClose_Loc} css=div[style*="display:block"] div.pop_footer a.close Clear Home Popup [Documentation] Use Click Element By JS ... can close all popup which match the locator Click Element By JS ${popClose_Loc} Click Element By JS [Arguments] ${locator} Wait Until Page Contains Element ${locator} ${locator} Get Sub Locator ${locator} Execute Javascript $('${locator}').click(); Get Sub Locator [Arguments] ${locator} ${prefix} ${subLocator} String.Split String ${locator} = 1 [return] ${sublocator}