1、環境安裝css
應客戶要求,IDE使用Eclipse插件RED,須要使用目前eclipse最新Oxygen版本才能更好兼容RED.html
1.下載並安裝Oxygenpython
2.Merketing搜索RED安裝插件web
3.下載並安裝Python 2.7.13json
4.配置環境變量,//pyhon; //python27/Scripts;windows
5.在線安裝robot frameworksession
cmd> pip install robotframeworkeclipse
6.在線安裝selenium2Library測試
cmd> pip install robotframework-selenium2libraryui
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
若是能運行成功表明環境正常
2、遇到的問題
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}