Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e7307e619a
Fetching contributors…

Cannot retrieve contributors at this time

file 272 lines (228 sloc) 10.384 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)

describe "Button" do

  before :each do
    browser.goto(WatirSpec.url_for("forms_with_input_elements.html"))
  end

  # Exists method
  describe "#exists?" do
    it "returns true if the button exists" do
      browser.button(:id, "new_user_submit").should exist
      browser.button(:id, /new_user_submit/).should exist
      browser.button(:name, "new_user_reset").should exist
      browser.button(:name, /new_user_reset/).should exist
      browser.button(:value, "Button 2").should exist
      browser.button(:value, /Button 2/).should exist
      not_compliant_on :ie do
        browser.button(:src, "images/button.png").should exist
      end
      browser.button(:src, /button\.png/).should exist
      browser.button(:text, "Button 2").should exist
      browser.button(:text, /Button 2/).should exist
      browser.button(:class, "image").should exist
      browser.button(:class, /image/).should exist
      browser.button(:index, 0).should exist
      browser.button(:xpath, "//input[@id='new_user_submit']").should exist
      browser.button(:alt, "Create a new user").should exist
      browser.button(:alt, /Create a/).should exist
    end

    it "returns true if the button exists (how = :caption)" do
      browser.button(:caption, "Button 2").should exist
      browser.button(:caption, /Button 2/).should exist
    end


    it "returns the first button if given no args" do
      browser.button.should exist
    end

    it "returns false if the button doesn't exist" do
      browser.button(:id, "no_such_id").should_not exist
      browser.button(:id, /no_such_id/).should_not exist
      browser.button(:name, "no_such_name").should_not exist
      browser.button(:name, /no_such_name/).should_not exist
      browser.button(:value, "no_such_value").should_not exist
      browser.button(:value, /no_such_value/).should_not exist
      browser.button(:src, "no_such_src").should_not exist
      browser.button(:src, /no_such_src/).should_not exist
      browser.button(:text, "no_such_text").should_not exist
      browser.button(:text, /no_such_text/).should_not exist
      browser.button(:class, "no_such_class").should_not exist
      browser.button(:class, /no_such_class/).should_not exist
      browser.button(:index, 1337).should_not exist
      browser.button(:xpath, "//input[@id='no_such_id']").should_not exist
    end

    it "checks the tag name and type attribute when locating by xpath" do
      browser.button(:xpath, "//input[@type='text']").should_not exist
      browser.button(:xpath, "//input[@type='button']").should exist
    end

    it "raises TypeError when 'what' argument is invalid" do
      lambda { browser.button(:id, 3.14).exists? }.should raise_error(TypeError)
    end

    it "raises MissingWayOfFindingObjectException when 'how' argument is invalid" do
      lambda { browser.button(:no_such_how, 'some_value').exists? }.should raise_error(MissingWayOfFindingObjectException)
    end
  end

  # Attribute methods
  describe "#class_name" do
    it "returns the class name of the button" do
      browser.button(:name, "new_user_image").class_name.should == "image"
    end

    it "returns an empty string if the button has no class name" do
      browser.button(:name, "new_user_submit").class_name.should == ""
    end
  end

  describe "#id" do
    it "returns the id if the button exists" do
      browser.button(:index, 0).id.should == 'new_user_submit'
      browser.button(:index, 1).id.should == 'new_user_reset'
      browser.button(:index, 2).id.should == 'new_user_button'
    end

    it "raises UnknownObjectException if button does not exist" do
      lambda { browser.button(:index, 1337).id }.should raise_error(UnknownObjectException)
    end
  end

  describe "#name" do
    it "returns the name if button exists" do
      browser.button(:index, 0).name.should == 'new_user_submit'
      browser.button(:index, 1).name.should == 'new_user_reset'
      browser.button(:index, 2).name.should == 'new_user_button'
    end

    it "raises UnknownObjectException if the button does not exist" do
      lambda { browser.button(:name, "no_such_name").name }.should raise_error(UnknownObjectException)
    end
  end

  describe "#src" do
    it "returns the src attribute for the button image" do
      # varies between browsers
      browser.button(:name, "new_user_image").src.should include("images/button.png")
    end

    it "raises UnknownObjectException if the button does not exist" do
      lambda { browser.button(:name, "no_such_name").src }.should raise_error(UnknownObjectException)
    end
  end

  describe "#style" do
    not_compliant_on :ie,
                    [:webdriver, :iphone],
                    [:webdriver, :safari],
                    [:webdriver, :phantomjs] do
      it "returns the style attribute if the button exists" do
        browser.button(:id, 'delete_user_submit').style.should == "border: 4px solid red;"
      end
    end

    deviates_on :ie8 do
      it "returns the style attribute if the button exists" do
        browser.button(:id, 'delete_user_submit').style.should == "BORDER-BOTTOM: red 4px solid; BORDER-LEFT: red 4px solid; BORDER-TOP: red 4px solid; BORDER-RIGHT: red 4px solid"
      end
    end
    deviates_on :ie9 do
      it "returns the style attribute if the button exists" do
        browser.button(:id, 'delete_user_submit').style.should == "border: 4px solid red;"
      end
    end

    deviates_on [:webdriver, :iphone], [:webdriver, :safari], [:webdriver, :phantomjs] do
      it "returns the style attribute if the button exists" do
        style = browser.button(:id, 'delete_user_submit').style
        style.should include("border-top-width: 4px;")
        style.should include("border-left-style: solid;")
        style.should include("border-right-color: red;")
      end
    end

    it "returns an empty string if the element exists and the attribute doesn't exist" do
      browser.button(:id, 'new_user_submit').style.should == ""
    end

    it "raises UnknownObjectException if the button does not exist" do
      lambda { browser.button(:name, "no_such_name").style }.should raise_error(UnknownObjectException)
    end
  end

  describe "#title" do
    it "returns the title of the button" do
      browser.button(:index, 0).title.should == 'Submit the form'
    end

    it "returns an empty string for button without title" do
      browser.button(:index, 1).title.should == ''
    end
  end

  describe "#type" do
    it "returns the type if button exists" do
      browser.button(:index, 0).type.should == 'submit'
      browser.button(:index, 1).type.should == 'reset'
      browser.button(:index, 2).type.should == 'button'
    end

    it "raises UnknownObjectException if button does not exist" do
      lambda { browser.button(:name, "no_such_name").type }.should raise_error(UnknownObjectException)
    end
  end

  describe "#value" do
    it "returns the value if button exists" do
      browser.button(:index, 0).value.should == 'Submit'
      browser.button(:index, 1).value.should == 'Reset'
      browser.button(:index, 2).value.should == 'Button'
    end

    it "raises UnknownObjectException if button does not exist" do
      lambda { browser.button(:name, "no_such_name").value }.should raise_error(UnknownObjectException)
    end
  end

  describe "#text" do
    it "returns the text of the button" do
      browser.button(:index, 0).text.should == 'Submit'
      browser.button(:index, 1).text.should == 'Reset'
      browser.button(:index, 2).text.should == 'Button'
      browser.button(:index, 3).text.should == "Button 2"
    end

    it "raises UnknownObjectException if the element does not exist" do
      lambda { browser.button(:id, "no_such_id").text }.should raise_error(UnknownObjectException)
    end
  end

  describe "#respond_to?" do
    it "returns true for all attribute methods" do
      browser.button(:index, 0).should respond_to(:class_name)
      browser.button(:index, 0).should respond_to(:id)
      browser.button(:index, 0).should respond_to(:name)
      browser.button(:index, 0).should respond_to(:src)
      browser.button(:index, 0).should respond_to(:style)
      browser.button(:index, 0).should respond_to(:title)
      browser.button(:index, 0).should respond_to(:type)
      browser.button(:index, 0).should respond_to(:value)
    end
  end

  # Access methods
  describe "#enabled?" do
    it "returns true if the button is enabled" do
      browser.button(:name, 'new_user_submit').should be_enabled
    end

    it "returns false if the button is disabled" do
      browser.button(:name, 'new_user_submit_disabled').should_not be_enabled
    end

    it "raises UnknownObjectException if the button doesn't exist" do
      lambda { browser.button(:name, "no_such_name").enabled? }.should raise_error(UnknownObjectException)
    end
  end

  describe "#disabled?" do
    it "returns false when button is enabled" do
      browser.button(:name, 'new_user_submit').should_not be_disabled
    end

    it "returns true when button is disabled" do
      browser.button(:name, 'new_user_submit_disabled').should be_disabled
    end

    it "raises UnknownObjectException if button does not exist" do
      lambda { browser.button(:name, "no_such_name").disabled? }.should raise_error(UnknownObjectException)
    end
  end

  # Manipulation methods
  describe "#click" do
    it "clicks the button if it exists" do
      browser.goto(WatirSpec.url_for("forms_with_input_elements.html", :needs_server => true))
      browser.button(:id, 'delete_user_submit').click
      browser.text.should include("Semantic table")
    end

    it "fires events" do
      browser.button(:id, 'new_user_button').click
      browser.button(:id, 'new_user_button').value.should == 'new_value_set_by_onclick_event'
    end

    it "raises UnknownObjectException when clicking a button that doesn't exist" do
      lambda { browser.button(:value, "no_such_value").click }.should raise_error(UnknownObjectException)
      lambda { browser.button(:id, "no_such_id").click }.should raise_error(UnknownObjectException)
    end

    it "raises ObjectDisabledException when clicking a disabled button" do
      lambda { browser.button(:value, "Disabled").click }.should raise_error(ObjectDisabledException)
    end
  end


end
Something went wrong with that request. Please try again.