Ruby-based watir-webdriver Automated Testing Solution and implementation (3), watirwebdriver
Then the Ruby-based watir-webdriver Automated Testing Solution and implementation (2)Http://www.cnblogs.com/javame/p/4259468.htmlcontinue... to write a script, you must first learn to capture elements, and then learn to capture page elements. Attribute_value
Obtains 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 "OK" in the pop-up window.
Ie. popupwin. button (: name => "OK"). click
Sikuli_image
Click image control
Ie. sikuli_image (: image => "1.png"). clickie. sikuli_image (: image =>" 1.png1_2.png "). click # You can specify multiple images for recognition.
Double_click
Double-click event
ie .sikuli_image(:image=>"1.png").double_click
Right_click
Right-click event
Exist?
Determine whether 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 is 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 => "" example .doc lick
Input box
? Ie. text_field (: name => ""). set "variable"
? Ie. text_field (: name => ""). The value of text_field is not in text, but in value!
Drop-down list
? Ie. select_list (: name => ""). select "drop-down box value"
? Ie. select_list (: name => ""). select "#1" # indicates the first item.
? Ie. select_list (: name => ""). rand_select
? Ie. select_list (: name => ""). getSelectedItems | getAllContents-> return Array
Single region
? Ie. radio (: id => "",: name => "",: index => n). set (select the current radio)
? Ie. radio (: id => "",: name => "",: index => n). clear (deselect the current radio)
Ie. div (: class => "iradio_minimal-blue checked"). radios [1]
Check box
? Ie. check_box (: id => "",: name => "",: index => n ). set (true | false) (true indicates selected, false indicates not selected)
? Ie. check_box (: id => "",: name => "",: index => n). clear (deselect the current checkbox)
Link
? Ie. link (: text => ""). click/doclick
? Ie. link (: text => ""). href (returns the link pointed to by the current link)
Cell (TD tag, you usually need to first find the upper-layer controls such as table and div)
? Ie. table (: class => "",: index => n). cell (: class => "",: index => n). text
? Ie. table (: index => n). rows row column. text (rows and columns start from 1)
? Ie. div (: class => "",: index => n). cell (: class => "",: index => n). text
Span
? Ie. table (: id => ""). span (: class => ""). text
Dialog Box
? Ie. popupwin. get_static_text (return the text of the current prompt box)
? Ie. popupwin. button (: name => "OK"). click/doclick (doclick is required for the previous button)
? Ie. file_dialog (: index => 1/2). set_file (file_path_download, true) (the pop-up window for saving files)
Image
? Ie. image (: src =>/word3a_nor.gif/). click/doclick
Back
Back
Ie. back
Forward
Forward
Ie. forward
Refresh
Refresh page
Ie. refresh
Processing frame in Watir-WebDriver is very simple, just like processing other page elements:
b.frame(:id => "content_ifr").send_keys "hello world"
File Download
The simplest and best way to process the File Download Dialog Box is to completely avoid dialog box pop-up.
You can tell the browser in the code to automatically download the file to the specified directory, and then access the directory in the test case for verification.
Firefox download_directory = "#{Dir.pwd}/downloads"download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.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, :profile => profile
You can enter 'about: config' in the address bar to view all Firefox configuration items.
If you want to know a way to work out the file types (eg. application/pdf) then you can read the following blog post for an step by step guide. if you want to know how to process specific types of files, read this blog.
Chrome download_directory = "#{Dir.pwd}/downloads"download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows? profile = Selenium::WebDriver::Chrome::Profile.newprofile['download.prompt_for_download'] = falseprofile['download.default_directory'] = download_directory b = Watir::Browser.new :chrome, :profile => profile
New browser window
When a new browser window is opened, you can use the 'use' method to process the new window.
browser.window(:title => "annoying popup").use do browser.button(:id => "close").clickend
JS pop-up box
In web applications, JavaScript dialogs are very common.
Watir-WebDriver has built-in methods to process these dialogs and can return the content displayed in the dialog box. First, load the extension:
View Code
Optional
If you have trouble using the above method, you can overwrite JavaScript functions by yourself, so that the dialog box that should have been displayed will not be displayed at the time of triggering.
# Make the alert method return null
Browser.exe cute_script ("window. alert = function (){}")
# Make prompt return a specific string to simulate user input
Browser.exe cute_script ("window. prompt = function () {return 'my name '}")
# Make the prompt method return null to simulate the user clicking the Cancel (Cancel) button
Browser.exe cute_script ("window. prompt = function () {return null }")
# Make the confirm method return true to simulate the user clicking the OK button
Browser.exe cute_script ("window. confirm = function () {return true }")
# Enable the confirm method to return false to simulate the user clicking the Cancel (Cancel) button.
Browser.exe cute_script ("window. confirm = function () {return false }")
Page Performance
Watir-WebDriver-Performance gem provides the page Performance measurement function while accessing the page. It uses W3C page Performance measurement indicators. This is a perfect solution for capturing response performance indicators. Its usage is very intuitive and simple, but currently only Chrome and IE9l browsers are supported.
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.
Screenshots
The built-in features of Watir-WebDriver are very good and useful.
browser.driver.save_screenshot 'screenshot.png'
The great thing about this is it gives you a screen shot of the entire page, not just above the fold. the best thing about the function is that it can capture the whole page, not the part shown on the screen.
If you are using Cucumber, 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
Simulate special buttons
The. send_keys method can be used to simulate special keyboard keys (such as shift). The parameter is the symbolic representation (symbolic) of the keys you need to simulate ).
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 work with the buttons:
b.element.click(:shift, :control)
The list of supported key names is as follows:
View Code Rich Text Editor
You can use Watir-WebDriver to input text to the WYSIWYG editor (which should be a Rich Text Editor) in two ways:
Locate the iFrame of the editor and use the. send_keys method (the disadvantage is that the browser must run on the front-end)
Execute javascript in the browser and set the editor value through 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