`
lihbobo
  • 浏览: 67098 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Selenium2.0 Ruby

 
阅读更多
#summary Ruby bindings

= Introduction =

The Ruby bindings for Selenium/!WebDriver are available as the [http://rubygems.org/gems/selenium-webdriver selenium-webdriver] gem. There are many other Selenium gems out there, but this is the only official, maintained gem. If you're looking for a slightly higher level API built on the same technology, you may want to check out [http://watirwebdriver.com/ watir-webdriver].

The bindings support Ruby 1.8.7 through 1.9.2, JRuby and Rubinius. 

  * [http://selenium.googlecode.com/svn/trunk/docs/api/rb/index.html API docs]
  * [http://selenium.googlecode.com/svn/trunk/rb/CHANGES Changelog]

The gem also includes the older selenium-client gem for use with Selenium RC. When reading the docs, keep in mind that these two namespaces refer to different APIs:

  * `Selenium::WebDriver` - the !WebDriver API
  * `Selenium::Client` - Selenium RC API (previously released as the selenium-client gem)

The WebDriver API is the successor to the RC API. For people who don't have a significant investment in the legacy API, we recommend starting directly with `Selenium::WebDriver`, and focusing on the two main classes, `Selenium::WebDriver::Driver` and `Selenium::WebDriver::Element`. This is the entry point to the whole !WebDriver API.
The rest of this document deals with `Selenium::WebDriver` exclusively.

= API Example =

The bindings provide a slightly rubified version of the !WebDriver API:

{{{
require "selenium-webdriver"

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://google.com"

element = driver.find_element(:name, 'q')
element.send_keys "Hello WebDriver!"
element.submit

puts driver.title

driver.quit
}}}

Driver examples:

{{{
# execute arbitrary javascript
puts driver.execute_script("return window.location.pathname")

# pass elements between Ruby and JavaScript
element = driver.execute_script("return document.body")
driver.execute_script("return arguments[0].tagName", element) #=> "BODY"

# wait for a specific element to show up
wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
wait.until { driver.find_element(:id => "foo") }

# switch to a frame
driver.switch_to.frame "some-frame" # name or id
driver.switch_to.frame driver.find_element(:id, 'some-frame') # frame element

# switch back to the main document
driver.switch_to.default_content
}}}

Element examples:

{{{
# get an attribute
class_name = element.attribute("class")

# is the element visible on the page?
element.displayed?

# click the element
element.click

# get the element location
element.location

# scroll the element into view, then return its location
element.location_once_scrolled_into_view

# get the width and height of an element
element.size

# press space on an element - see Selenium::WebDriver::Keys for possible values
element.send_keys :space

# get the text of an element
element.text
}}}

Advanced user interactions (see [http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/ActionBuilder.html ActionBuilder]):

{{{
driver.action.key_down(:shift).
              click(element).
              double_click(second_element).
              key_up(:shift).
              drag_and_drop(element, third_element).
              perform
}}}


== IE ==

Make sure that _Internet Options_ 鈫� _Security_ has the same _Protected Mode_ setting (on or off, it doesn't matter as long as it is the same value) for all zones.

== Chrome ==

=== Command line switches ===

For a list of switches, see [http://codesearch.google.com/codesearch#OAMlx_jo-ck/src/chrome/common/chrome_switches.cc&exact_package=chromium chrome_switches.cc]:

{{{
driver = Selenium::WebDriver.for :chrome, :switches => %w[--ignore-certificate-errors --disable-popup-blocking --disable-translate]
}}}

=== Tweaking profile preferences ===

For a list of prefs, see [http://codesearch.google.com/codesearch#OAMlx_jo-ck/src/chrome/common/pref_names.cc&exact_package=chromium pref_names.cc].

{{{
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = "/path/to/dir"

driver = Selenium::WebDriver.for :chrome, :profile => profile
}}}

See also ChromeDriver.

== Remote ==

The RemoteWebDriver makes it easy to control a browser running on another machine. Download the jar (from [http://code.google.com/p/selenium/downloads/list Downloads]) and launch the server:

{{{java -jar selenium-server-standalone.jar}}}

Then connect to it from Ruby

{{{
driver = Selenium::WebDriver.for(:remote)
}}}

By default, this connects to the server running on localhost:4444 and opens Firefox. To connect to another machine, use the `:url` option:

{{{
driver = Selenium::WebDriver.for(:remote, :url => "http://myserver:4444/wd/hub")
}}}

To launch another browser, use the :desired_capabilities option:

{{{
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => :chrome)
}}}

You can also pass an instance of `Selenium::WebDriver::Remote::Capabilities`, e.g.:

{{{
caps = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => caps)
}}}

You may want to set the proxy settings of the remote browser (this currently only works for Firefox):

{{{
caps = Selenium::WebDriver::Remote::Capabilities.firefox(:proxy => WebDriver::Proxy.new(:http => "myproxyaddress:8080"))
driver = Selenium::WebDriver.for(:remote, :desired_capabilities => caps)
}}}

Or if you have a proxy in front of the remote server:

{{{
client = Selenium::WebDriver::Remote::Http::Default.new
client.proxy = Selenium::Proxy.new(:http => "proxy.org:8080")

driver = Selenium::WebDriver.for(:remote, :http_client => client)
}}}

See [http://code.google.com/p/selenium/source/browse/trunk/rb/lib/selenium/webdriver/common/proxy.rb `Selenium::WebDriver::Proxy`] for more options.

For the remote Firefox driver you can configure the profile, see the section [#Tweaking_Firefox_preferences Tweaking Firefox preferences].

== Firefox ==

The FirefoxDriver lets you configure the profile used.

=== Adding an extension ===

It's often useful to have Firebug available in the Firefox instance launched by !WebDriver:

{{{
profile = Selenium::WebDriver::Firefox::Profile.new
profile.add_extension("/path/to/firebug.xpi")

driver = Selenium::WebDriver.for :firefox, :profile => profile
}}}

=== Using an existing profile ===

You can use an existing profile as a template for the WebDriver profile by passing the profile name (see `firefox -ProfileManager` to set up custom profiles.)

{{{
driver = Selenium::WebDriver.for(:firefox, :profile => "my-existing-profile")
}}}

If you want to use your default profile, pass {{{:profile => "default"}}}

You can also get a Profile instance for an existing profile and tweak its preferences. This does not modify the existing profile, only the one used by !WebDriver.

{{{
default_profile = Selenium::WebDriver::Firefox::Profile.from_name "default"
default_profile.native_events = true

driver = Selenium::WebDriver.for(:firefox, :profile => default_profile)
}}}

=== Tweaking Firefox preferences ===

Use a proxy:

{{{
profile = Selenium::WebDriver::Firefox::Profile.new
proxy = Selenium::WebDriver::Proxy.new(:http => "proxy.org:8080")
profile.proxy = proxy

driver = Selenium::WebDriver.for :firefox, :profile => profile
}}}

Automatically download files to a given folder:

{{{
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = "/tmp/webdriver-downloads"
profile['browser.download.folderList'] = 2
profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"

driver = Selenium::WebDriver.for :firefox, :profile => profile
}}}

If you are using the remote driver you can still configure the Firefox profile:

{{{
profile = Selenium::WebDriver::Firefox::Profile.new
profile['foo.bar'] = true
 
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox(:firefox_profile => profile)
driver = Selenium::WebDriver.for :remote, :desired_capabilities => capabilities
}}}

For a list of possible preferences, see [http://preferential.mozdev.org/preferences.html this page].

=== Custom Firefox path ===

If your Firefox executable is in a non-standard location:

{{{
Selenium::WebDriver::Firefox.path = "/path/to/firefox"
driver = Selenium::WebDriver.for :firefox
}}}

=== SSL Certificates ===

The Firefox driver ignores invalid SSL certificates by default. If this is not the behaviour you want, you can do:

{{{
profile = Selenium::WebDriver::Firefox::Profile.new
profile.secure_ssl = true

driver = Selenium::WebDriver.for :firefox, :profile => profile
}}}

There is an edge case where the default SSL certificate check will not work correctly. WebDriver assumes that the certificate is untrusted whenever there's a problem, which means a certificate from a trusted issuer but with a hostname mismatch (e.g. a production certificate in a test environment) will not be correctly ovverriden.  See UntrustedSSLCertificates for more on why this is. To work around it, tell the Firefox driver to not assume the issuer is untrusted:

{{{
profile = Selenium::WebDriver::Firefox::Profile.new
profile.assume_untrusted_certificate_issuer = false
driver = Selenium::WebDriver.for :firefox, :profile => profile
}}}

Not that Profile#secure_ssl remains set to the default value of true in the above example.



=== Native events ===

Native events are enabled by default on Windows. To turn them off:

{{{
profile = Selenium::WebDriver::Firefox::Profile.new
profile.native_events = false

driver = Selenium::WebDriver.for(:firefox, :profile => profile)
}}}

Experimental support for native events is available on Linux. Set {{{profile.native_events = true}}} to turn this on.

== Opera ==

The OperaDriver is always run as a RemoteWebDriver server which the Ruby bindings connect to.

To get started, first [http://code.google.com/p/selenium/downloads/list download] the _selenium-server-standalone_ jar and set the `SELENIUM_SERVER_JAR` environmental variable to point to its location:

`export SELENIUM_SERVER_JAR=/path/to/server-standalone.jar`

Then you can simply create a new instance of `Selenium::WebDriver` with the `:opera` option:

{{{
driver = Selenium::WebDriver.forpera
driver.navigate.to 'http://opera.com/'
}}}

== Timeouts ==

=== Implicit waits ===

!WebDriver lets you configure implicit waits, so that a call to `#find_element` will wait for a specified amount of time before raising a `NoSuchElementError`:

{{{
  driver = Selenium::WebDriver.for :firefox
  driver.manage.timeouts.implicit_wait = 3 # seconds
}}}

=== Explicit waits ===

Use the Wait class to explicitly wait for some condition:

{{{
  wait = Selenium::WebDriver::Wait.new(:timeout => 3)
  wait.until { driver.find_element(:id => "cheese").displayed? }
}}}

=== Internal timeouts ===

Internally, !WebDriver uses HTTP to communicate with a lot of the drivers (the JsonWireProtocol). By default, `Net::HTTP` from Ruby's standard library is used, which has a default timeout of 60 seconds. If you call `Driver#get` on a page that takes more than 60 seconds to load, you'll see a `TimeoutError` raised from `Net::HTTP`. You can configure this timeout (before launching a browser) by doing:

{{{
  client = Selenium::WebDriver::Remote::Http::Default.new
  client.timeout = 120 # seconds
  driver = Selenium::WebDriver.for(:remote, :http_client => client)
}}}


== !JavaScript dialogs  ==

You can use webdriver to handle Javascript `alert()`, `prompt()` and `confirm()` dialogs.
The API for all three is the same.

Note: At this time alert handling is only available in Firefox and IE (or in those browsers through the remote server), and only alerts that are generated post onload can be captured.

{{{
require "selenium-webdriver"

driver = Selenium::WebDriver.for :firefox
driver.navigate.to "http://mysite.com/page_with_alert.html"

driver.find_element(:name, 'element_with_alert_javascript').click
a = driver.switch_to.alert
if a.text == 'A value you are looking for'
  a.dismiss
else
  a.accept
end

}}}

== Using Curb or your own HTTP client ==

For internal HTTP communication, `Net::HTTP` is used by default. If you e.g. have the [https://rubygems.org/gems/curb Curb gem] installed, you can switch to it by doing:

{{{
require 'selenium/webdriver/remote/http/curb'

client = Selenium::WebDriver::Remote::Http::Curb.new
driver = Selenium::WebDriver.for(:firefox, :http_client => client)
}}}

If you have the [https://github.com/drbrain/net-http-persistent net-http-persistent gem] installed, you can (as of 0.1.3) similarly use "selenium/webdriver/remote/http
分享到:
评论

相关推荐

    Selenium-server-2.0

    Selenium支持多种浏览器,包括Firefox、Chrome、Internet Explorer以及Safari等,并且兼容多种编程语言,如Java、Python、C#、Ruby等。 Selenium-server-2.0是Selenium的一个里程碑,因为它引入了重要的改进和新...

    selenium2.0使用

    Selenium 2.0,也被称为WebDriver,是一种自动化测试工具,用于模拟真实用户在Web浏览器中的交互。它支持多种编程语言,包括C#、Java、Python和Ruby等,本篇将主要探讨在Java环境下的使用。 **1. WebDriver简介** ...

    Selenium-Webdriver系列教程

    Selenium-Webdriver系列教程旨在引导读者理解和掌握Selenium 2.0,即WebDriver的核心概念、常用方法和高级功能。Selenium 1.x虽然已被弃用,但由于丰富的资源和历史积累,仍然被许多人广泛使用。然而,随着Selenium ...

    selenium2 webdriver中文文档完整

    2. 安装 JDK:确保安装了 JDK,我使用的是 Java,但 Selenium WebDriver 也支持其他语言,如 Ruby、Python、C# 等。 3. 安装 Eclipse。 4. 安装 Selenium WebDriver:下载 Selenium RC 2.21.0,解压下载的 Selenium ...

    selenium-2.0-dev-9138.tar.gz

    1. **Selenium框架**:Selenium是一个开源的自动化测试框架,支持多种编程语言,包括Python、Java、C#、Ruby等。它的核心组件包括WebDriver、Selenium IDE和Selenium RC。 2. **WebDriver**:这是Selenium的一个...

    Selenium-Webdriver系列教程.docx

    本文主要介绍 Selenium 2.0 的一些基本知识、常用方法和高级扩展,所有代码和示例均由 Ruby 编写,主要是 ruby binding。首先,需要确保 Ruby 语言在开发环境上正确安装。 Selenium 2.0 和 WebDriver 之间的关系是...

    selenium-java-2.53.1.rar

    Selenium 2.0主要的特性就是与WebDriver API的集成。WebDriver旨在提供一个更简单更简洁的编程接口以及解决一些Selenium-RC API的限制。Selenium-Webdriver更好的支持页面本身不重新加载而页面的元素改变的动态...

    [全]selenium自动化测试与前端技术简介.docx

    - Selenium 支持多种编程语言,如 Java、Python、Ruby、PHP、C# 和 JavaScript,这使得开发人员可以根据自己的熟悉度选择合适的语言编写测试脚本。 5. 分布式测试: - Selenium Grid 允许在分布式环境中运行测试...

    基于Selenium的Python网络爬虫的实现.pdf

    Selenium有两个主要版本,即Selenium 1.0和Selenium 2.0。Selenium 2.0引入了WebDriver,因此可以将其视为Selenium 1.0与WebDriver的结合。Selenium 2.0是目前的最新版本,支持的操作更为广泛和高级。在Python中导入...

    selenium 用户指南

    本用户指南是针对 Selenium 2.0 的修订版,旨在帮助用户深入理解和有效利用 Selenium 进行 Web 应用程序测试。 Selenium 2.0(也称为 WebDriver)是一个开放源码的测试框架,它允许通过编程方式控制浏览器,执行一...

    针对Ruby的Selenium WebDriver安装指南

    在Ruby环境中,Selenium WebDriver提供了方便的Ruby绑定(ruby binding),使得Ruby程序员可以轻松地利用这个强大的工具。 首先,让我们深入理解Selenium 2.0与WebDriver的关系。Selenium 2.0实际上就是WebDriver的...

    selenium for python

    Selenium 2.0引入了WebDriver,它是Selenium RC的升级版,直接通过浏览器的原生API控制浏览器,提供更高效和精确的控制。WebDriver针对每个浏览器有不同的实现,可以直接与浏览器通信,而不依赖于JavaScript。同时,...

    selenium+junit+hudson自动化测试初探

    Selenium 2.0(也称为Selenium WebDriver)整合了WebDriver,以解决Selenium RC的局限性,同时保留了Selenium的优点。WebDriver通过原生浏览器支持或浏览器扩展直接控制浏览器,而Selenium RC则通过JavaScript在...

    selenium--WEB自动化测试

    在压缩包`selenium-2.0rc2`中,可能包含了Selenium 2.0 Release Candidate 2版本的相关文件和文档,这可以作为一个起点,帮助初学者了解和学习Selenium的基本使用和功能。随着Selenium的不断更新,目前已有更高级的...

    selenium2初学者快速入门 java版

    Selenium 2.0 (WebDriver)** - Selenium 2引入了WebDriver概念,通过直接与浏览器通信来克服Selenium 1中的局限性。 - WebDriver为每个浏览器提供了一个专用的驱动程序,能够更好地模拟用户交互。 - 解决了...

    基于Selenium的软件自动化测试分析.pdf

    Selenium WebDriver是Selenium 2.0版本引入的一个重要功能,它提供了一个编程接口,支持Web应用程序的自动化测试,大大增强了Selenium的兼容性。WebDriver采用的架构允许测试工具直接调用浏览器的原生方法,支持多种...

Global site tag (gtag.js) - Google Analytics