差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

rf:rf:migratetoselenium2library [2017/11/26 18:49]
tony
rf:rf:migratetoselenium2library [2023/06/25 09:48]
行 1: 行 1:
-{{tag>​RobotFramework selenium}} 
-====== Migrate SeleniumLibrary to Selenium2Library ====== 
-===== Introduction ===== 
-分享從SeleniumLibrary(SL1)升級到Selenium2Library(SL2)的歷程與方法。\\ 
-\\ 
-Note. 某一天看到Selenium2Library從3.0開始後,已經於自身library中提供wrapper的實作,要升級的人可以嘗試看看。 
-===== Browser ===== 
-由於SL1與SL2支援的瀏覽器版本有差異存在,有考慮過使用[[rf:​rf:​usecustomizedfirefox|portable的firefox]],但發現難以支援所有需要的作業系統。因此最無痛的方式,就是透過Remote Selenium,可參考[[rf:​rf:​seleniumgrid|此篇]]教學。 
-===== Implement A Wrapper Library ===== 
-SL1與SL2 Keyword有不少[[https://​github.com/​robotframework/​Selenium2Library/​wiki/​Differences-From-SeleniumLibrary|差異]]存在。而升級到SL2的過程,並非一蹴可幾。因此我透過一個SeleniumLibraryWrapper,讓Keyword的呼叫使用共同的介面,而要使用SL1或SL2則由各別Keyword決定。方法如下:​ 
-  - Import Selenium Library: 偵測要使用的Selenium版本,並設定為Global Variable。 
-  - Keyword Wrapper: 透過判斷版本的結果,去決定要使用SL1或SL2的Keyword。 
-==== Import Selenium Library ==== 
-由於SL1不相容於2.9以上版本的RobotFramework,而SL2必須用2.9以上版本。因此我們可以透過判斷2.9版本以上才支援的keyword是否存在,來確認Selenium環境,接著再匯入各別版本的SeleniumLibrary。\\ 
-\\ 
-{{:​rf:​rf:​rf_s_wrapper_import_selenium_lib.png|}} 
-==== Keyword Wrapper ==== 
-我以Click Element為例。Click Element在SL1中,接收${locator}、${dont_wait}與${coordinates}三個參數,而SL2只接受${locator}。如果要將SL1轉到SL2,針對${dont_wait}可選擇略過;${coordinates}則要改用Click Element At Coordinates。由於我沒用到${coordinates},所以我僅以${dont_wait}做範例:​\\ 
-\\ 
-{{:​rf:​rf:​rf_s_wrapper_click_element.png|}}\\ 
-其它Keyword也是用類似的方法,至於要做到什麼程度,看你們的需求而定了。 
-===== Keyword Changes ===== 
-這部分針對Keyword行為改變做說明,這是要補充官方沒列到的:​ 
-==== 無法操作invisible的項目 ==== 
-SL2中,如果操作畫面上沒顯示的項目就會丟錯。如果真的要操作,可以採取的措施是改用Javascript呼叫操作。 
-==== Textfield Value Should Be ==== 
-在SL1中,這個Keyword是可以做text與textarea的驗證。而在SL2則分為Textfield Value Should Be與Textarea Value Should Be,因此如果原本針對textarea的驗證,必須改為Textarea Value Should Be或改用Get Value再做assert。 
-==== Select From List ==== 
-在SL1中,原先可以透過index=value的方式找尋我要的項目。而在SL2後,如果要根據value做選擇,要用Select From List By Value;要根據index,則有Select From List By Index;要根據顯示樣子,可以用Select From List By Label。 
  
- 
-===== 使用Remote Selenium造成的Side Effects ===== 
-原先Robot、Selenium與SUT是在同一台機器上。後來因為升級Selenium後,對於瀏覽器版本有限制;對於某些比較舊的系統,原生是不支援新版本瀏覽器,因此採用Remote Selenium solution。也發生了一些麻煩問題,像Open Browser URL無法使用localhost,或某些keyword怪怪的:​ 
-==== Choose File ==== 
-在把針對Web相關的測試動作放在遠端執行後,我就想:​ SUT上的檔案如何讓遠端機機做上傳動作呢?​ 後來經過Study,Selenium2Library針對Choose File動作,會將local檔案先傳到遠端機器,再做上傳動作。然而我發現:​ 檔案稍微大一點就會造成out of memory。我想到有幾個解法:​ 
-  - 將檔案放至中央的http server,Choose File使用http或smb路徑:​ 這我沒試過,只是想法。 
-  - 加大java heap: 我發現即使設定很大(4096 MB),問題依然可能發生。 
-  - 將檔案瘦身:​ 假如你的測試案例無法瘦身,請嘗試上面兩個方法。 
-==== Input Text ==== 
-首先是輸入特定字串,無法正常輸入。 這個可以直接看我po在GitHub的issue:​\\ 
-https://​github.com/​robotframework/​Selenium2Library/​issues/​582。\\ 
-再來是輸入檔案路徑,例如:​ 
-<​code>​ 
-Input Text | ${xpath} | \\10.134.14.212\iso\Linux\CentOS\5.4\CentOS-5.4-x86_64-bin-DVD.iso 
-</​code>​ 
-在Windows上會出現以下錯誤:​ 
-<​code>​ 
-LargeZipFile:​ Filesize would require ZIP64 extensions 
-</​code>​ 
-我想這與Input Text實作方式有關,Choose File的內涵也是透過Input Text;假設內容為一個路徑且存在,也許就會把它當上傳檔案去做相關操作了。 而針對這個問題,我是透過Execute Javascript去做workaround,透過JQuery取得element by name,將內容修改為我要的內容:​ 
-<​code>​ 
-Execute Javascript | $("​[name='​xxxx'​]"​).val('​${val}'​);​ 
-</​code>​ 
-===== 其它問題 ===== 
-==== StaleElementReferenceException:​ Message: Element is no longer attached to the DOM ==== 
-更新為SL2後,常會在不定位置出現StaleElementReferenceException。[[http://​stackoverflow.com/​questions/​5709204/​random-element-is-no-longer-attached-to-the-dom-staleelementreferenceexception|爬文]]後,別人是說某個節點還不是有效時就去操作它而導致的。因此必須先透過Wait Until相關keywords等待節點出現後,再進行操作。 
-===== Reference ===== 
-  * [[https://​github.com/​robotframework/​Selenium2Library/​wiki/​Differences-From-SeleniumLibrary|Differences From SeleniumLibrary]] 
-  * [[http://​robotframework-seleniumlibrary.googlecode.com/​hg/​doc/​SeleniumLibrary.html?​r=2.9.1|SeleniumLibrary document]] 
-  * [[http://​robotframework.org/​Selenium2Library/​doc/​Selenium2Library.html|Selenium2Library document]] 
- 
- 
-=====    ===== 
----- 
-\\ 
-~~DISQUS~~