差異處

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

連向這個比對檢視

Both sides previous revision 前次修改
下次修改
前次修改
rf:rf:migratetoselenium2library [2016/01/23 00:01]
tony [Bridge Keyword]
rf:rf:migratetoselenium2library [2023/06/25 09:48] (目前版本)
行 2: 行 2:
 ====== Migrate SeleniumLibrary to Selenium2Library ====== ====== Migrate SeleniumLibrary to Selenium2Library ======
 ===== Introduction ===== ===== Introduction =====
-分享從SeleniumLibrary(SL1)升級到Selenium2Library(SL2)的歷程與方法。+分享從SeleniumLibrary(SL1)升級到Selenium2Library(SL2)的歷程與方法。\\ 
 +\\ 
 +Note. 某一天看到Selenium2Library從3.0開始後,已經於自身library中提供wrapper的實作,要升級的人可以嘗試看看。[[http://​robotframework.org/​Selenium2Library/​Selenium2Library.html|link]] 
 ===== Browser ===== ===== Browser =====
 由於SL1與SL2支援的瀏覽器版本有差異存在,有考慮過使用[[rf:​rf:​usecustomizedfirefox|portable的firefox]],但發現難以支援所有需要的作業系統。因此最無痛的方式,就是透過Remote Selenium,可參考[[rf:​rf:​seleniumgrid|此篇]]教學。 由於SL1與SL2支援的瀏覽器版本有差異存在,有考慮過使用[[rf:​rf:​usecustomizedfirefox|portable的firefox]],但發現難以支援所有需要的作業系統。因此最無痛的方式,就是透過Remote Selenium,可參考[[rf:​rf:​seleniumgrid|此篇]]教學。
-===== Implement A Wrapper =====+===== Implement A Wrapper ​Library ​=====
 SL1與SL2 Keyword有不少[[https://​github.com/​robotframework/​Selenium2Library/​wiki/​Differences-From-SeleniumLibrary|差異]]存在。而升級到SL2的過程,並非一蹴可幾。因此我透過一個SeleniumLibraryWrapper,讓Keyword的呼叫使用共同的介面,而要使用SL1或SL2則由各別Keyword決定。方法如下:​ SL1與SL2 Keyword有不少[[https://​github.com/​robotframework/​Selenium2Library/​wiki/​Differences-From-SeleniumLibrary|差異]]存在。而升級到SL2的過程,並非一蹴可幾。因此我透過一個SeleniumLibraryWrapper,讓Keyword的呼叫使用共同的介面,而要使用SL1或SL2則由各別Keyword決定。方法如下:​
   - Import Selenium Library: 偵測要使用的Selenium版本,並設定為Global Variable。   - Import Selenium Library: 偵測要使用的Selenium版本,並設定為Global Variable。
行 18: 行 21:
 {{:​rf:​rf:​rf_s_wrapper_click_element.png|}}\\ {{:​rf:​rf:​rf_s_wrapper_click_element.png|}}\\
 其它Keyword也是用類似的方法,至於要做到什麼程度,看你們的需求而定了。 其它Keyword也是用類似的方法,至於要做到什麼程度,看你們的需求而定了。
-===== Changes =====+===== Keyword ​Changes =====
 這部分針對Keyword行為改變做說明,這是要補充官方沒列到的:​ 這部分針對Keyword行為改變做說明,這是要補充官方沒列到的:​
 ==== 無法操作invisible的項目 ==== ==== 無法操作invisible的項目 ====
行 25: 行 28:
 在SL1中,這個Keyword是可以做text與textarea的驗證。而在SL2則分為Textfield Value Should Be與Textarea Value Should Be,因此如果原本針對textarea的驗證,必須改為Textarea Value Should Be或改用Get Value再做assert。 在SL1中,這個Keyword是可以做text與textarea的驗證。而在SL2則分為Textfield Value Should Be與Textarea Value Should Be,因此如果原本針對textarea的驗證,必須改為Textarea Value Should Be或改用Get Value再做assert。
 ==== Select From List ==== ==== Select From List ====
-在SL1中,原先可以透過index=value的方式找尋我要的項目。而在SL2後,用Select From List By Value。+在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等待節點出現後,再進行操作。\\
 +[[http://​www.seleniumhq.org/​exceptions/​stale_element_reference.jsp|more..]]
 +==== Firefox無法點擊可視範圍外的DOM element ====
 +以下是我使用會有問題的版本資訊:​
 +  * selenium: 3.8.0。
 +  * seleniumLibrary:​ 3.0.0。
 +  * selenium-server-standalone:​ 3.8.1。
 +  * geckodriver:​ 0.19.0、0.19.1。
 +  * firefox: 57.0.2、57.0.4。
 +同樣的selenium版本下,chrome沒這問題:​
 +  * chrome: 63.0.3239.84。
 +  * chromedriver:​ 2.34.522940。
 +起初解決這問題使用scrollbar控制可視範圍,但實在太麻煩,所以改使用chrome去測試。
 ===== Reference ===== ===== Reference =====
   * [[https://​github.com/​robotframework/​Selenium2Library/​wiki/​Differences-From-SeleniumLibrary|Differences From SeleniumLibrary]]   * [[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-seleniumlibrary.googlecode.com/​hg/​doc/​SeleniumLibrary.html?​r=2.9.1|SeleniumLibrary document]]
   * [[http://​robotframework.org/​Selenium2Library/​doc/​Selenium2Library.html|Selenium2Library document]]   * [[http://​robotframework.org/​Selenium2Library/​doc/​Selenium2Library.html|Selenium2Library document]]
 +
  
 =====    ===== =====    =====