spec/watirspec/filefield_spec.rb in watir-webdriver-0.0.7 vs spec/watirspec/filefield_spec.rb in watir-webdriver-0.0.8

- old
+ new

@@ -17,10 +17,14 @@ browser.file_field(:class, /portrait/).should exist browser.file_field(:index, 0).should exist browser.file_field(:xpath, "//input[@id='new_user_portrait']").should exist end + it "returns the first file field if given no args" do + browser.file_field.should exist + end + it "returns false if the file field doesn't exist" do browser.file_field(:id, 'no_such_id').should_not exist browser.file_field(:id, /no_such_id/).should_not exist browser.file_field(:name, 'no_such_name').should_not exist browser.file_field(:name, /no_such_name/).should_not exist @@ -105,12 +109,43 @@ browser.goto("#{WatirSpec.host}/forms_with_input_elements.html") path = File.expand_path(__FILE__) browser.file_field(:name, "new_user_portrait").set path - browser.file_field(:name, "new_user_portrait").value.should == path - messages.first.should include(File.basename(path)) # only some browser will return the full path + browser.file_field(:name, "new_user_portrait").value.should include(File.basename(path)) # only some browser will return the full path + messages.first.should include(File.basename(path)) browser.button(:name, "new_user_submit").click + end + + it "raises an error if the file does not exist" do + lambda { + browser.file_field.set('/tmp/unlikely-to-exist') + }.should raise_error(Errno::ENOENT) + end + end + + + describe "#value=" do + it "is able to set a file path in the field and click the upload button and fire the onchange event" do + browser.goto("#{WatirSpec.host}/forms_with_input_elements.html") + + path = File.expand_path(__FILE__) + + browser.file_field(:name, "new_user_portrait").value = path + browser.file_field(:name, "new_user_portrait").value.should include(File.basename(path)) # only some browser will return the full path + messages.first.should include(File.basename(path)) + browser.button(:name, "new_user_submit").click + end + + it "does not raise an error if the file does not exist" do + browser.file_field.value = '/tmp/unlikely-to-exist' + browser.file_field.value.should == '/tmp/unlikely-to-exist' + end + + it "does not alter its argument" do + value = '/foo/bar' + browser.file_field.value = value + value.should == '/foo/bar' end end end