Watch out the outdated articles on the internet.
當心網上那些過時的文章
Selenium 2.0 is completely different from Selenium 1.x. Selenium 2.0 is also called the selenium webdriver. So always add the keyword webdriver when googling for answers to your selenium related questions.
Selenium 2.0與Selenium 1.x有非常大的差別。Selenium 2.0 通常也別叫做 selenium webdriver。所以在你google selenium 相關的問題的時候,都請加上 webdriver 這個關鍵字。
Implement the web UI in a modular way so it's more selenium testable.
用模塊化的方式來實現Web的UI會提高用Selenium測試的可測性
Modularize your view logic so that you only update the part of DOM that is needed to change when your models change. If you tend to re-create a bigger part of the DOM than necessary, it's not only a waste but also could introduce risk to your functional tests written in Selenium.
模塊化你的View邏輯,這樣做的好處是,當你要更改你的Models時,你只需要更新部分的DOM即可。如果你嘗試重新創建一個比需要更大的DOM,這不但是一種浪費,而且會引入使你的Selenium 功能測試腳本出現不能正常工作風險。
Reduce unnecessary dependency on DOM structure, make element locating logic as simple as possible.
減少對DOM結構不必要的依賴,元素定位邏輯的越簡單越好
When you need to locate an element, try not rely on the DOM structure too much - for example, using code logic to locate element is the most risky one. The best approach is probably to always use a scoped CSS selector with 1 or 2 levels of IDs, And if you can locate it in one selector, don't do it in two. For example
請盡量不要使用DOM的結構來定位頁面元素,例如,用代碼邏輯來定位元素是風險最大的。定位元素最好的方式是用1 到2級的CSS selector,如果一個selector 能定位,就不要用兩個, 例如
label = driver.find_element("#info-panel #name-label")
is more robust than
就比以下的方法健壯
panel = driver.find_element("#info-panel")
label = panel.find_element("#name-label")
Do wait in selenium the smart way.
恰當地使用Wait
Don't use implicit wait blindly. Implicit wait makes sense when you use find_element to find one element. But when you try to locate multiple elements by driver.find_elements, the driver will always wait the whole timeout period if implicit wait is set. That might not be what you always want. I usually write my own safe find_element method. Here is an example in the base class of my page objects:
不要盲目地使用隱式等待。當你使用find_element的時候去找某一個元素時,使用隱式等待是合適的,但是當你嘗試用 driver.find_elements 去定位多個元素時,如果設置了隱式等待,那么drivers 總是會等待整個超時周期。這往往不是你想要的效果。我通常會自己去寫一個安全的 find_element 方法。以下例子展示了我頁面對象的基類
def s selector
wait_until { @driver.find_element css: selector }
end
def wait_until(&block)
wait = Selenium::WebDriver::Wait.new(timeout: 10, interval: INTERVAL)
wait.until &block
end
So that I can write the following code in my page object
定義后,我就能這樣寫代碼
def submit_order
s('button#submit').click
end
The short method name "s" is inspired by jQuery. Here it will keep polling the DOM for 10 seconds until it finds the button with id "submit". It's like implicit wait but only for finding one element. When you really need to wait for multiple elements, you can use an explicit wait, which, to me, makes more sense than a hidden implicit one.
短方法 s 的靈感是來源于jQuery。 它會輪詢DOM 10秒,直到它在找到一個id 為'submit'的button 為止。當只找一個元素時,用隱式等待是恰當的,當你真的需要定位多個對象的時候,你應該使用顯示等待,對我來說,這樣有意義。
譯者注:關于顯式等待和隱式等待的區別,請看下面討論及文檔,有助于理解我在翻譯什么... : (
http://groups.google.com/group/selenium-users/browse_thread/thread/fd0cb59b953f5e94/123380cef05d7bdb?lnk=raot
http://seleniumhq.org/docs/04_webdriver_advanced.html
Set the initial browser window size when using Chromedriver.
當在使用Chromedirver時,設定瀏覽器窗口初始化大小
Ruby code:
profile = Selenium::WebDriver::Chrome::Profile.new
profile['browser.window_placement.top'] = 0
profile['browser.window_placement.left'] = 0
profile['browser.window_placement.right'] = 1024
profile['browser.window_placement.bottom'] = 768
driver = Selenium::WebDriver.for :chrome, profile: profile
This works in both Windows and OSX (will try Linux and update here)
Bad news for Java, C# and Python coders though, it seems that as of now setting chrome preference is not supported in the java version of Webdrive. Your best chance could be creating a ChromeProfile class based on the exiting FirefoxProfile class.
原文轉自:http://www.anti-gravitydesign.com