Selenium-webdriver Simple Tutorial

Source: Internet
Author: User
Keywords selenium webdriver selenium webdriver tutorial keyword driven framework in selenium webdriver tutorial

Selenium/webdriver can be installed by Selenium-webdriver.gem package in Ruby Environment


Gem install selenium-webdriver

supports languages and versions with Ruby 1.8.7~1.9.2,jrbuy and rubinius

Selenium-webdriver contains selenium-client, while reading, be aware that their two namespaces are in two different APIs:

1.selenium::webdriver-webdrver API

2. Selenium::client-selenium RC Api 


webdrver API is inherited from Selenium RC API, so it is not necessary to spend a lot of time in the Selenium RC API,  we can directly from Selenium::

Webdriver starts and revolves around two big classes: Seleniu::webdriver:driver and Selenium::webdriver::element, which are the portals of the entire Webdriver API.  

API Example: 

A simple example: 

Require "selenium-webdriver"  

driver = selenium::webdriver.for:firefox

driver.navigate.to " http://www.google.com.hk "

element = driver.find_element (: name, ' Q ') 

Element.send_keys" Hello Webdriver " 

Element.submit 

puts Driver.title 


driver.quit 


driver Example:

# application Javascript

puts driver.execute_script (" Return window.location.pathname ")

# using Ruby and JS to get elements 

element = Driver.execut_script (" Return document.body ") 


Driver.execut_script ("Return Arguments[0].tagname", Element) #=> "BODY"

# wait for some special element to appear 

wait = Selenium:: Webdriver::wait.new (: timeout=>10) # SeconDs

wait.until{driver.find_element (: ID, "foo")}

# Note: Wait at new, you can set three values: timeout (default 5 seconds),: message (Default nil),: Interval ( Default 0.5)


# select Frame

driver.switch_to.frame "Some-frame" # Name or Id

driver.switch_to.frame driver.find_element (: ID, ' Some-frame ') # frame

# Note: Switch_to method can not only select frame, but also handle window,alert,comfirmation and other Windows


# Select Back to main window

driver.swith_ to.default_content


element Example:

# Gets the element attribute

class_name = Element.attr

ibute ("class")


# Determines whether the element is displayed

element.displayed ?

# gets the relative coordinate position of the element on the page

element.location

element.location.x

element.location.y

# scrolls the element to the location where the video can be displayed. Return the relative coordinates of the element element.location_once_scrolled_into_view

# get the width and height of the element


element.size

# in the element input empty, see Selenium:: Webdriver::keys input value

Element.send_keys:space

element.send_keys "Tet",: Arrow_left, "s" #=> "test", first enter Tet, and then enter the left ARROW key, Enter S

element.send_keys [: Control, ' a '], "1" #=> "1", enter Crtl+a first, and then enter 1

# to get the element text

element.text

More advanced usage (see Actionbuilder)

Driver.action.key_down (: Shift). Click (. Double_click (second_element). KEY_UP (: Shift ). DraG_and_drop (element,third_element) .perform



 How to start the Chrome browser

 1. Download Chromedriver and run,

 2. Start Chromedriver = selenium::webdriver.for:remote,:url=> "http://localhost:9515" driver.get "/HTTP/ WWW.GOOGLE.COM.HK "other operations.  

 If this feels more troublesome, you can load the downloaded Chromedriver.exe path into the environment variable, you can use

 the driver = selenium::webdriver.for:chrome


 Chrome Personalization Boot

profile = selenium::webdriver::chrome::P rofile.new

profile[' download.prompt_for_download ') = false

profile[' download.default_directory ' = "d:/download"

driver = selenium::webdriver.for:chrome,:p rofile= >profile

remote application Romtewebdriver can control the browser to run on different machines, download Selenium-server-standlone-x.xx.x.

jarjava-jar Selenium-server-standalone.jar ' Start service

driver = selenium::webdriver.for:remote

By default, the service can be started running on localhost : 4444, and open Firefox. If you want to connect to a service on another machine, you can use: URL option


Driver = selenium::webdriver.for:remote,:url=> "Http://remoteserver:44444/wd/hub"

This machine does not add/wd/hub, but the remote must add

start other browsers, with: desired_capabilities option

Driver = Selenium::webdriver.for:remote, Example of the:d esired_capabilities=>:chrome

selenium::webdriver::remote::capabilities

require " Selenium-webdriver "

Include selenium caps = webdriver::remote::capabilities.htmlunit:javascript_enabled=> Truedriver = WebDriver.for:remote,:d esired_capalibities=>caps

to modify the port number of the remote service, only Firefox is currently supported

include selenium

caps = webdriver::remote::capabilities.firefox

(:p roxy=>webdriver::P roxy.new ( :http=> "myproxyaddress:8888")) 

Driver = WebDriver.for:remote,:d esired_capalibities=>caps

If it is a remotely remote service

include selenium client = webdriver::remote::http::D efault.new

 client.proxy = proxy.new:http=> "proxy.org:8888"

 Driver = WebDriver.for:remote,:http_client=>client


 Firefox

 plus extension plug-in when using Firefox, often use Firebug to view, start Firefox with firebug

include selenium

 profile = Webdriver::firefox:: Profile.new

profile.add_extension "Path/firebug.xpi"  driver = Web

Driver.for:firefox,:p rofile=>profile


 using a profile that already exists

Using a profile template that already exists, you can save it with Firefox-profilemanger (this command appears useless on FF8 and further validates)  driver = Selenium:: WebDriver.for:firefox,:p rofile=> "My_existing_profile"

if you want to use the default profile, you can:p rofile=> "default"

Alternatively, you can use the profile instance to work with existing and custom profiles. This method cannot modify a profile that already exists and can only be used under webdriver

default_profile = Selenium::webdriver::firdfox::P rofile.from_name " Default "

default_profile.native_events = True

driver = Selenium::webdriver.for:firefox,:p rofile=>default_ profile

Export Firefox profile see the "Firefox 8 export profiles"

firefox Personalization


 port number

 Selenium::webdriver::firefox:: profile.new

profile[' browser.download.dir ' = ' d:\download '

profile[' browser.download.folderlist '] = 2

profile[' Browser.helperApps.neverAsk.saveToDisk '] = "application/pdf"

  driver = Selenium::webdriver.for:firefox,: profile=>profile

  when booting with remote driver, Firefox can still be personalized

  profile = Selenium::webdriver::firfox:: profile.new

profile[' foo.bar ' = True

 capabilities = Selenium::webdriver::remote:: Capabilities.firefox:firefox_profile=>profile

driver = selenium::webdriver.fox:remote,:d esired_capabilities= >capabilities


set Firefox path

 when Firfox is not installed in the default path, set the installation path for Firefox

 Selenium::webdriver::firefox.path = "/path/to /firefox "# Firefox installation path

 driver = selenium::webdriver.for:firefox

   Local event local event under window is activated by default and can be set off: Profile = Selenium::webdriver::firefox::P rofile.newprofile.native_events = False # Close  driver = selenium::webdriver.for: Firefox,:p rofile=>profile  theoretically Linux supports local events, profile.native_events = true # Open


  The Opera

opera is also initiated using remote methods. Before you begin, download the Selenium-server-standalone-x.xx.x.jar and create a new environment variable SELENIUM_SERVER_JAR to the local system.

 Window:selenium_server_jar=. \server-standalone.jar

 liunx:export selenium_server_jar=path_to/server-standalone.jar

   Can be easily started with selenium::web

driver opera driver = selenium::web

driver.for:operadriver.navigate.to "http// Www.baidu.com "


 Timeouts

  implicit wait

 webdriver can set an implicit wait, so when you apply a #find_element anchor element, you wait for the element to appear until the NosuThe advent of Chelementerror.  driver = selenium::webdriver.for:firefoxdriver.manager.timeouts.implicit_wait = 3 # wait 3 seconds   Display waits can be instantiated with the wait class for some waiting conditions  

wait = Selenium::webdriver::wait.new (: timeout=>3) 

wait.until {driver.find_elenium (: Id=> "foo"). Displayed?}


  Internal Timeout

 Webdriver uses a large number of HTTP drivers (Jsonwireprotocol). Net::http is used as a standard library for Ruby, with a default timeout of 60 seconds. If you start a page with Webdriver#get that takes more than 60 seconds to load, you'll see Net::http's timeouterror error, and you can modify the timeout to change the default timeout length before starting the browser.

  client = selenium::webdriver::remote::http::D efault.new

client.titmeout = 120 # Set to 120 seconds

 Driver = Selenium:: WebDriver.for:temote,: Http_client=>client

 js pop-up box

 gets JS alert,prompt and comfirm pop-up windows are all with switch_to

   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 is looking for '

   a.dismiss

else

  a.accept

end

   with curb or your own HTTP client

HTTP communication is used internally by default Net::http, if you have installed curb gem you can choose to do so

  require "selenium/webdriver/remot/http/curb"

 include selenium  client = Web

driver::remote::http::curb.new

driver = WebDriver.for:remote,: http_client=>client


Selenium-webdriver Simple Tutorial


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.