The scheme and implementation of Watir-webdriver Automation test based on Ruby (III.)

Source: Internet
Author: User
Tags goto

then the Watir-webdriver Automation test scheme and implementation based on Ruby (II.)Http://www.cnblogs.com/Javame/p/4159468.html continue ... Scripting is the first thing to learn to capture elements, and then to learn the capture of page elements. Page Element Attribute_value

Gets the properties of the current control

Value = Ie.link (:id=> ' xxx '). Attribute_value ("href")
Rand_select

Randomly select an item in the select list

Ie.select_list (:name=> "). Rand_select
Popupwin

Click on the ' OK ' button on the pop-up window

Ie.popupwin.button (:name=> "OK"). click
Sikuli_image

Click the picture control

Ie.sikuli_image (:image=> "1.png"). Clickie.sikuli_image (:image=> "1.png;2.png"). click# can specify more than one picture to identify
Double_click

Double-click event

ie. sikuli_image (:image=> "1.png"). Double_click
Right_click

Right-click event

Exist?

Determine if a user element exists

Edit = Ie.text_field (: Name, "username")                                                                        if edit.exist? ()                                      #The highlighted                                     edit.flash                                                              Ie.text_field (: Name, "password"). Set (PWD)                                                 Ie.button (: Class, " X-login-submit "). Click                             end                   End    
Text fields
Require ' watir-webdriver 'b = Watir::browser.start ' Bit.ly/watir-webdriver-demo 't = b.text_field:id = ' Entry_0 't.exists?t.set ' your name 't.value   
Select Lists–combos
Require ' watir-webdriver 'b = Watir::browser.start ' Bit.ly/watir-webdriver-demo 's = b.select_list:id = ' Entry_1 's.select ' Ruby 's.selected_options   
Radios
Require ' watir-webdriver 'b = Watir::browser.start ' Bit.ly/watir-webdriver-demo 'r = B.label (: Text = ' What's Ruby? '). Parent.radio:value = ' A gem 'r.exists?r.setr.set?  
Checkboxes
Require ' watir-webdriver 'b = Watir::browser.start ' Bit.ly/watir-webdriver-demo 'c = B.label (: Text = ' What Versions of Ruby? '). Parent.checkbox:value = ' 1.9.2 'c.exists?c.setc.set?  
Buttons
Require ' watir-webdriver 'b = Watir::browser.start ' Bit.ly/watir-webdriver-demo 'btn = B.button:value, ' Submit 'btn.exists?btn.click  
Links
Require ' watir-webdriver 'b = Watir::browser.start ' Bit.ly/watir-webdriver-demo 'l = b.link:text + ' Google Docs 'l.exists?l.click  
DIVs & Spans
Require ' watir-webdriver 'b = Watir::browser.start ' Bit.ly/watir-webdriver-demo 'd = b.div:class = = ' Ss-form-desc ss-no-ignore-whitespace 'd.exists?d.texts = B.span:class = ' ss-powered-by 's.exists?s.text 
Instance

Button

? Ie.button (:name=> "",:id=> "",:index=>n,:type=> ""). Click

? Ie.button (:name=> "",:id=> "",:index=>n,:type=> ""). DoClick

Input box

? Ie.text_field (:name=> ""). Set "Variable"

? Ie.text_field (:name=> ""). Value takes Text_field values not in text but value!

drop-down box

? Ie.select_list (:name=> ""). Select drop-down box value

? Ie.select_list (:name=> ""). Select "#1" #表示第一项内容

? Ie.select_list (:name=> ""). Rand_select

? Ie.select_list (:name=> "") .getselecteditems|getallcontents-> return to array

Radio Box

? Ie.radio (:id=> "",:name=> "",: Index=>n). Set (check current radio)

? Ie.radio (:id=> "",:name=> "",: Index=>n). Clear (uncheck current radio)

Ie.div (:class=> "Iradio_minimal-blue checked"). Radios[1]

check box

? Ie.check_box (:id=> "",:name=> "",: Index=>n). Set (True|false) (true = checked, false means unchecked)

? Ie.check_box (:id=> "",:name=> "",: Index=>n). Clear (Uncheck the current checkbox)

Link

? Ie.link (:text=> ""). Click/doclick

? Ie.link (:text=> ""). href (returns the link pointed to by the current link)

Cell (TD label, usually need to find upper controls such as table, Div, etc.)

? Ie.table (:class=> "",: index=>n). Cell (:class=> "",: index=>n). Text

? Ie.table (: index=>n). Rows column. Text (rows, columns starting from 1)

? Ie.div (:class=> "",: index=>n). Cell (:class=> "",: index=>n). Text

Span

? Ie.table (:id=> ""). Span (:class=> ""). Text

Pop-up box

? Ie.popupwin.get_static_text (returns the text of the current prompt box)

? Ie.popupwin.button (:name=> "OK"). Click/doclick (The previous click button must be DoClick)

? Ie.file_dialog (: INDEX=>1/2). Set_file (File_path_download,true) (a pop-up window to save the file)

Image

? Ie.image (: src=>/word3a_nor.gif/). Click/doclick

Back

Back off

Ie.back

Forward

Forward

Ie.forward

Refresh

Refresh Page

Ie.refresh

Working with a frame in watir-webdriver is very simple, just as with other page elements:

B.frame (: id = "CONTENT_IFR"). Send_keys "Hello World"

Download the file

The simplest and best way to process a file download dialog is to completely avoid the dialog box pop-up.

In the code, you can tell the browser to automatically download the file to the specified directory and then access the directory for validation in the test case.

Firefox download_directory = "#{dir.pwd}/downloads"download_directory.gsub! (" /"," \ ") if selenium::webdriver::P latform.windows? profile = Selenium::webdriver::firefox:: profile.newprofile[' browser.download.folderList ' = 2 # custom locationprofile[' browser.download.dir '] =  download_directoryprofile[' browser.helperApps.neverAsk.saveToDisk ' = "Text/csv,application/pdf" B = watir:: Browser.new:firefox,:p rofile =      profile

All configuration items for Firefox can be viewed by entering ' about:config ' in the Address bar.

If you want to know a, the file types (eg. application/pdf) then you can read the following blog post for a n Step by Step guide. If you want to know how to handle a particular type of file, read this post.

Chrome download_directory = "#{dir.pwd}/downloads"download_directory.gsub! (" /"," \ ") if  selenium::webdriver::P latform.windows? profile = Selenium::webdriver::chrome:: profile.newprofile[' download.prompt_for_download ' = falseprofile[' download.default_directory '] = download _directory B = watir::browser.new:chrome,:p rofile     = profile

Browser New Window

When a new browser window opens, you can use the ' using ' method to process this new window.

Browser.window (: title = "Annoying Popup"). Use do  Browser.button (: id = "Close"). Clickend 

JS Popup box

In Web apps, JavaScript dialogs are very common.

Watir-webdriver the methods for handling these dialog boxes, and can return the content displayed in the dialog box. First, load this extension:

View Code

Optional methods

If you're having trouble using the method above, you can overwrite the JavaScript functions yourself, so that the dialog that should be displayed is not displayed when the trigger is triggered.

# causes the alert method to return empty

Browser.execute_script ("Window.alert = function () {}")

# enables prompt to return a specific string that simulates the user's input

Browser.execute_script ("window.prompt = function () {return ' My Name '}")

# causes the prompt method to return null to simulate the user clicking the Cancel button

Browser.execute_script ("window.prompt = function () {return null}")

# make the Confirm method return true to simulate the user clicking the OK button

Browser.execute_script ("window.confirm = function () {return true}")

# make the Confirm method return False to impersonate the user by clicking the Cancel button

Browser.execute_script ("window.confirm = function () {return false}")

Page performance

The Watir-webdriver-performance Gem provides the ability to measure page performance while accessing a page, using Web page performance metrics. This is a perfect solution for capturing response performance metrics that are intuitive and simple to use, but currently only support Chrome and ie9l browsers.

Require ' watir-webdriver 'require ' watir-webdriver-performance 'b = watir::browser.new:chrome10. Times do  b.goto ' http://17test.info '  load_secs = b.performance.summary[:response_time]/1000 puts " Load time: #{load_secs} seconds. " End      

The statistical results are as follows:

Load time:3.701 seconds.

Screen Cutting

Watir-webdriver, the staff were very friendly and helpful.

Browser.driver.save_screenshot ' Screenshot.png '

The great thing about this is it gives you a screens shot of the entire page, not just above the fold. The best part of the feature is that it captures the entire page, not the portion of the screen that appears.

If you are using cucumber, then you can simply add the following code to the env.rb file so that you can insert it in the HTML report:

After do |scenario|   browser.driver.save_screenshot ' screenshot.png '  embed ' screenshot.png ', ' image/png 'end 

Simulating special keys

Use the. Send_keys method to simulate special keyboard keys (such as shift), whose parameters are symbolic representations of the keys you need to simulate (symbolic).

B.send_keys:enter

You can also do this:

B.element.send_keys [: Control, ' a '],: BACKSPACE

You can also modify the behavior of the click Method so that the click can be combined with the key:

B.element.click (: Shift,: Control)

The list of key keys supported is as follows:

View CodeRich Text Editor

There are two ways to enter text through Watir-webdriver to the WYSIWYG editor, which should refer to a rich Text editor:

Locate the IFRAME where the editor resides, and then use the. Send_keys method (The disadvantage is that the browser must be running in the foreground)

Execute JavaScript on the browser and set the value of the editor with the JS script

CKEditor

Require ' watir-webdriver 'b = watir::browser.new:firefoxb.goto ' http://ckeditor.com/demo 'b.execute_ Script ("ckeditor.instances[' Editor1 '].setdata (' Hello World ');" ) b.frame (: title = ' Rich text editor, Editor1, press ALT 0 for help. '). Send_keys ' Hello world again 'TinyMCE Editor require ' watir-webdriver 'b = Watir::browser.newb.goto '/http/ tinymce.moxiecode.com/tryit/full.php 'b.execute_script (' Tinymce.get (' content '). ExecCommand (' Mcesetcontent ', False, ' Hello World '); ) b.frame (: id = "CONTENT_IFR"). Send_keys ' Hello World again '        

Qa

The scheme and implementation of Watir-webdriver Automation test based on Ruby (III.)

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.