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.