EzDevInfo.com

watir

Web application testing in Ruby Watir.com | Web Application Testing in Ruby watir, pronounced water, is an open-source (bsd) family of ruby libraries for automating web browsers. it allows you to write tests that are easy to read and maintain. it is simple and flexible. watir drives browsers the same way people do. it clicks links, fills in forms, presses buttons. watir also checks results, such as…

watir-webdriver wait for page load

Using watir-webdriver, how do i wait for a page to load after i click a link?

At the moment i am doing:

sleep n

But this is not ideal as the page response varies so much.

Is there a method to test whether the page is ready or whether there is a certain element in the page. I understand in the normal watir gem there is Watir::Waiter.wait_until or something similar, but I cannot see this in the webdriver version.


Source: (StackOverflow)

How can I get Browser.text.include? to be case insensitive?

It's as simple as that:

How can I get Browser.text.include?, or Ruby in general, to be case insensitive for that specified command?


Source: (StackOverflow)

Advertisements

How do you save IRB inputs to a .rb file?

Might sound like a newbie question (and it is since I am new to Ruby and IRB) but is there a way to save the commands you did in IRB to file? I am playing with WATIR and would love to save all my inputs to file instead of copying and pasting each.

Thanks for the help!


Source: (StackOverflow)

Setting browser window size in Watir-webdriver

How can you specify the size of the browser window opened when you call the following with watir-webdriver?

browser = Watir::Browser.new(:firefox)

Source: (StackOverflow)

watir gem install error due to mini_magick file w/ special characters

I have been trying to install the watir gem for ruby 2. I followed the watir books install instructions exactly but once the watir install get to the mini_magick gem it gives this error

ERROR: While executing gem ... (Errno::EINVAL) Invalid argument - C:/Ruby200/lib/ruby/gems/2.0.0/gems/mini_magick-3.6.0/tes t/files/special! "chars'.gif

Anyone know how to get around this? I noticed that nokogiri was acting kind of weird being unable to convert from ASCII to ITF-8. Maybe this has something to do with it. Thanks y'all


Source: (StackOverflow)

What are the limitations of mechanize? and what is the difference(s) between mechanize and watir

I am using mechanize to scrap some web pages.

  • I need to know what are mechanize limitations? What mechanize can not do?
  • Can it execute javascripts embedded in the web page?
  • Can I use it to call javascript functions? I don't think it can. I think Watir can.
  • What are the differences between it and watir?

Source: (StackOverflow)

How to find out which JavaScript events fired?

I have a select list:

<select id="filter">
  <option value="Open" selected="selected">Open</option>
  <option value="Closed">Closed</option>
</select>

When I select Closed the page reloads. In this case it shows closed tickets (instead of opened). It works fine when I do it manually.

The problem is that the page does not reload when I select Closed with Watir:

browser.select_list(:id => "filter").select "Closed"

That usually means that some JavaScript event is not fired. I can fire events with Watir:

browser.select_list(:id => "filter").fire_event "onclick"

but I need to know which event to fire.

Is there a way to find out which events are defined for an element?


Source: (StackOverflow)

Watir Webdriver counting number of items in a UL list

I've done a few searches and I'm unable to find a suitable answer. Basically I have an unordered list which can be of a varying length. I want to iterate through the list, do some other things and then come back and select the next item on the list. I can do this fine when I define the amount of times my loop should iterate as I know the amount of items in the list.

However I don't want to define this for each test, I want to grab the number of items in the list and then pop that into a variable that I can use to exit the loop and do the next thing I want.

The HTML is like so:

<ul id="PageContent_cat">
  <li class="sel">
    <a target="_self" rel='nofollow' href="/searchlocation.aspx?c=S1">S1</a>
  </li>
  <li>
    <a target="_self" rel='nofollow' href="/searchlocation.aspx?c=S2">S2</a>
  </li>
  <li>
    <a target="_self" rel='nofollow' href="/searchlocation.aspx?c=S3">S3</a>
  </li>
  <li>
    <a target="_self" rel='nofollow' href="/searchlocation.aspx?c=S4">S4</a>
  </li>
  <li>
    <a target="_self" rel='nofollow' href="/searchlocation.aspx?c=S5">S5</a>
  </li>
  <li>
    <a target="_self" rel='nofollow' href="/searchlocation.aspx?c=S6">S6</a>
  </li>
  <li>
    <a target="_self" rel='nofollow' href="/searchlocation.aspx?c=S7">S7</a>
  </li>
</ul>

So I can see there are 7 items in the list. Apparently in watir I could have used something the following:

arr= ie.select_list(:name,'lr').getAllContents.to_a

But not with webdriver.

I thought I could maybe use 'lis' but I just get a Hex result:

$bob = browser.ul(:id => "PageContent_cat").lis puts $bob

Thanks,

Paul


Source: (StackOverflow)

How to handle tinyMCE when automating with watir-webdriver?

I'm evaluating Watir-webdriver, to decide if i can switch to using it for my browser tests (from Watir mostly) and one of the key things would be the ability to interact with TinyMCE WYSIWYG editors, as a number of the applications I work with use TinyMCE. I've managed to get the following solution working -

@browser = Watir::Browser.new(:firefox)
@browser.goto("http://tinymce.moxiecode.com/tryit/full.php")
autoit = WIN32OLE.new('AutoITX3.Control')
autoit.WinActivate('TinyMCE - TinyMCE - Full featured example')
@browser.frame(:index, 0).body.click
autoit.Send("^a") # CTRL + a to select all
autoit.Send("{DEL}")
autoit.Send("Some new text")

The drawback of this approach, is that by using autoit, I remain dependent on Windows and the ability to run tests cross-platform is one of the attractions of webdriver.

I noticed some webdriver specific solutions such as the following from this thread:

String tinyMCEFrame = "TextEntryFrameName" // Replace as necessary
this.getDriver().switchTo().frame(tinyMCEFrame);
String entryText = "Testing entry\r\n";
this.getDriver().findElement(By.id("tinymce")).sendKeys(entryText);
//Replace ID as necessary
this.getDriver().switchTo().window(this.getDriver().getWindowHandle());
try {
  Thread.sleep(3000);
} catch (InterruptedException e) {

  e.printStackTrace();
}

this.getDriver().findElement(By.partialLinkText("Done")).click(); 

Which looks like it might work cross-platform but I don't know if the same functionality can be accessed from within Watir-webdriver. My question is, is there a way to write, delete and submit into TinyMCE using watir-webdriver, which will not enforce a dependency on a specific supported browser or operating system?


Source: (StackOverflow)

Locating a LI element using :text using watir-webdriver

I am trying to locate and click on a jQuery menu element, the menu is defined as multiple UL elements containing a number of LI elements.

Using Firefox 3.6.17 on Mac 10.5, in standard WATIR I've used;

browser.li(:text,"Options...").click

or

browser.div(:id,"Attributes-menu").li(:text,"Copy").click

to click on the menu item but using watir-webdriver (0.2.3) it is reporting the LI element cannot be found. Although I can find the containing DIV and an instance of LI by using :class.

I've attached an example of a menu HTML below, can anyone suggest a reliable method of locating the LI item?

<div class="ws-menu-container ws-context-menu ws-context-menu-hidden" style="top: 16px; left: 214px; " id="Attributes-menu">
<ul class="ws-context-menu"><li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Copy&nbsp;&nbsp;&nbsp;</li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Add...&nbsp;&nbsp;&nbsp;</li>
<li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Remove...&nbsp;&nbsp;&nbsp;</li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Clear Translation Flag&nbsp;&nbsp;&nbsp;</li>
<li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Copy from Master Language...&nbsp;&nbsp;&nbsp;</li>
<li class="ws-context-menu-disable">&nbsp;&nbsp;&nbsp;Push to Child Languages...&nbsp;&nbsp;&nbsp;</li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu">&nbsp;&nbsp;&nbsp;Options...&nbsp;&nbsp;&nbsp;</li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu">&nbsp;&nbsp;&nbsp;Refresh&nbsp;&nbsp;&nbsp;</li></ul>
<hr class="ws-context-menu-separator">
<ul class="ws-context-menu">
<li class="ws-context-menu">&nbsp;&nbsp;&nbsp;Help&nbsp;&nbsp;&nbsp;</li></ul></div>

Source: (StackOverflow)

Ruby - require 'watir-webdriver' - generates a LoadError no such file... Why?

I am new to Ruby and would really appreciate some help understanding what is going on here.
Summary:

Gem install watir-webdriver
Installs fine
start irb
require "watir-webdriver"
... LoadError: no such file to load --watir-webdriver

Surely this should respond

=> true

Why is it not finding the gem? Or what am I doing wrong?

Console

I'm on win7, Railsinstaller (Ruby 1.8.7).


Source: (StackOverflow)

Accessing an element with no attributes in Watir

Using Watir, is there a way to access an element without attributes?

For example:

<span>Text</span>

I'd like to avoid using xpath, but if that's the only way it's cool.


Source: (StackOverflow)

How to upload a file with watir and IE?

I am writing a watir script to test an upload form.

But the script does not automatically choose the file that is to be uploaded from my harddrive.

Instead IE stops with the file chooser dialog open. As soon as I manually select the to be uploaded file in the dialog and click ok, watir continues as desired. I wonder why it stops.

This is my watir script:

require 'test/unit'
require 'watir'

# runs on win3k, IE 6.0.3790; ruby 1.8.6, watir 

class EpcHomePage < Test::Unit::TestCase

  def test_upload
    ie = @browser
    htmlfile = "C:\\testing\\upload.html"
    uploadfile = "C:\\testing\\upload.html"
    ie.goto(htmlfile)
    ie.file_field(:name,"file1").set(uploadfile)
    assert_equal uploadfile, ie.file_field(:name,"file1").value
    ie.button(:name, 'upload').click
   end

  def setup
    @browser = Watir::IE.new
  end

  def teardown
    @browser.close
  end
end

I got the code from this page: http://wiki.openqa.org/display/WTR/File+Uploads

This is the form:

<html><body>
  <form name="form1" enctype="multipart/form-data" method="post" action="upload.html">
    <input type="file" name="file1">
    <input type="submit" name="upload" value="ok">
  </form>
</body></html>

I have found this manual http://svn.openqa.org/svn/watir/trunk/watir/unittests/filefield_test.rb also. I am using IE 6 and also IE 7for the testing.

Edit: I have uploaded my simple example here (3 files that live in c:\testing\ on my machines, just start the cmd file):

http://dl.dropbox.com/u/1508092/testing.rar

It fails on 3 different machines (all windows 2003, 2x IE 6 and 1 x IE 7). I have also changed the sleep time in the script c:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.5\lib\watir\input_elements.rb from 1 second to 5 seconds, like suggested by Ċ½eljko Filipin in his answer:

    def set(path_to_file)
      assert_exists
      require 'watir/windowhelper'
      WindowHelper.check_autoit_installed
      begin
        Thread.new do
          sleep 5 # it takes some time for popup to appear
          system %{ruby -e '
          ...

This is where it stops (please note that I did manually navigate to the directory in the file dialog once. From that point on IE always shows the open dialog with this directory, but that does not mean that the script selected the directory. I think it means that IE always shows the last directory where it left):

this is where it stops

Edit:

I found that that the ole32 code looks for the english title:

POPUP_TITLES = ['Choose file', 'Choose File to Upload']

I installed IE 7 english version now. Still no success. But I think it has something to do with the localization, because input_elements.rb searches the window titles. I wonder why it still fails now. This is the code from input_elements.rb:

  class FileField < InputElement
    INPUT_TYPES = ["file"]
    POPUP_TITLES = ['Choose file', 'Choose File to Upload']

    # set the file location in the Choose file dialog in a new process
    # will raise a Watir Exception if AutoIt is not correctly installed
    def set(path_to_file)
      assert_exists
      require 'watir/windowhelper'
      WindowHelper.check_autoit_installed
      begin
        Thread.new do
          sleep 2 # it takes some time for popup to appear
          system %{ruby -e '
              require "win32ole"
              @autoit = WIN32OLE.new("AutoItX3.Control")
              time    = Time.now
              while (Time.now - time) < 15 # the loop will wait up to 15 seconds for popup to appear
                #{POPUP_TITLES.inspect}.each do |popup_title|
                  next unless @autoit.WinWait(popup_title, "", 1) == 1
                  @autoit.ControlSetText(popup_title, "", "Edit1", #{path_to_file.inspect})
                  @autoit.ControlSend(popup_title, "", "Button2", "{ENTER}")
                  exit
                end # each
              end # while
          '}
        end.join(1)
      rescue
        raise Watir::Exception::WatirException, "Problem accessing Choose file dialog"
      end
      click
    end
  end

The text "Choose file" now appears in the title of my new IE. Anything else that should be localized or changed here? I updated the screenshot to the english version.


Source: (StackOverflow)

Cucumber and Watir suddenly stopped working

I have been using Cucumber all day and even though I did not change any files, it just stopped working.

It gives me an error that reads:

Unable to either launch or connect to Chrome. Please check that ChromeDriver is up-to-date. Using Chrome binary at: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome (Selenium::WebDriver::Error::UnknownError)

Any idea, what is going on?


Source: (StackOverflow)

Make headless browser stop loading page

I am using the watir-webdriver ruby gem. It starts the browser (Chrome) and begins to load a page. The page is loading too slowly, and watir-webdriver raises a timeout error. How can I make the browser stop loading the page?

require 'watir-webdriver'

client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 10
@browser = Watir::Browser.new :chrome, :http_client => client

sites = [
  "http://google.com/",
  "http://yahoo.com/",
  "http://www.nst.com.my/", # => This is the SLOW site
  "http://drupal.org/",
  "http://www.msn.com/",
  "http://stackoverflow.com/"
]

sites.each do |url|

  begin
    @browser.goto(url)
    puts "Success #{url}"
  rescue
    puts "Timeout #{url}"
  end

end

########## Execution result ########## 

# Success http://google.com/
# Success http://yahoo.com/
# Timeout http://www.nst.com.my/
# Timeout http://drupal.org/
# Timeout http://www.msn.com/
# Timeout http://stackoverflow.com/

########## Expected result ########## 

# Success http://google.com/
# Success http://yahoo.com/
# Timeout http://www.nst.com.my/
# Success http://drupal.org/
# Success http://www.msn.com/
# Success http://stackoverflow.com/

It looks like the browser doesn't respond to any other commands before it finishes loading the page. How can I force the browser to throw away the page it is loading and execute the next command?

UPDATED

I have found an interesting capability flag loadAsync http://src.chromium.org/svn/trunk/src/chrome/test/webdriver/webdriver_capabilities_parser.cc Maybe it can be useful for solving this problem? I don't understand yet how to make watir (webdriver) to set this when starting the chromedriver. This flag was introduced here http://codereview.chromium.org/7582005/


Source: (StackOverflow)