原文链接:http://kailuowang.blogspot.com/2012/05/some-selenium-tips.html
Watch out the outdated articles on theinternet.
当心网上那些过时的文章
Selenium 2.0 is completely different from Selenium1.x. Selenium 2.0 is also called the selenium webdriver. So alwaysadd the keyword webdriver when googling for answers to yourselenium related questions.
Selenium2.0与Selenium1.x有非常大的差别。Selenium2.0 通常也别叫做seleniumwebdriver。所以在你google selenium 相关的问题的时候,都请加上 webdriver这个关键字。
Implement the web UI in a modular way so it's moreselenium testable.
用模块化的方式来实现Web的UI会提高用Selenium测试的可测性
Modularize your view logic so that you only updatethe 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 yourfunctional 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 relyon the DOM structure too much - for example, using code logic tolocate element is the most risky one. The best approach is probablyto always use a scoped CSS selector with 1 or 2 levels of IDs, Andif you can locate it in one selector, don't do it in two. Forexample
请尽量不要使用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 waitmakes sense when you use find_element to find one element. But whenyou try to locate multiple elements by driver.find_elements, thedriver will always wait the whole timeout period if implicit waitis set. That might not be what you always want. I usually write myown safe find_element method. Here is an example in the base classof my page objects:
不要盲目地使用隐式等待。当你使用find_element的时候去找某一个元素时,使用隐式等待是合适的,但是当你尝试用driver.find_elements 去定位多个元素时,如果设置了隐式等待,那么drivers总是会等待整个超时周期。这往往不是你想要的效果。我通常会自己去写一个安全的find_element方法。以下例子展示了我页面对象的基类
def s selector
wait_until { @driver.find_elementcss: 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 pageobject
定义后,我就能这样写代码
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 thebutton with id "submit". It's like implicit wait but only forfinding one element. When you really need to wait for multipleelements, you can use an explicit wait, which, to me, makes moresense than a hidden implicit one.
短方法 s 的灵感是来源于jQuery。 它会轮询DOM10秒,直到它在找到一个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 usingChromedriver.
当在使用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 Linuxand update here)
Bad news for Java, C# and Python coders though, it seems that as ofnow setting chrome preference is not supported in the java versionof Webdrive. Your best chance could be creating a ChromeProfileclass based on the exiting FirefoxProfile class.
以上的代码在windows 和 OSX上都工作正常。坏消息是,对于Java , C# 和Python 的coder来说,看上去现在Java版本的webdriver还不支持设定chrome 的属性。 你最好是基于现有的FirefoxProfile类来创建一个ChromeProfile 类。
Scroll to a button before clickingit.
滚动到相应的button后才点击
Clicking buttons sometimes randomly fail. It couldbe caused by the fact that the button is out of the view area atthe moment you command selenium to click it. One way to address itis to always scroll to it before clicking it.
点击button有时候会无故失败,这可能是由于selenium执行点击命令的时候,这个button在视图之外。所以解决这个问题的其中一个方法是,滚动到相应的button后才点击。
http://kailuowang.blogspot.com/2012/05/some-selenium-tips.html