2012-07-21 16:44:10 -04:00
|
|
|
Capybara::SpecHelper.spec "node" do
|
|
|
|
before do
|
|
|
|
@session.visit('/with_html')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should act like a session object" do
|
|
|
|
@session.visit('/form')
|
|
|
|
@form = @session.find(:css, '#get-form')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@form).to have_field('Middle Name')
|
|
|
|
expect(@form).to have_no_field('Languages')
|
2012-07-21 16:44:10 -04:00
|
|
|
@form.fill_in('Middle Name', :with => 'Monkey')
|
|
|
|
@form.click_button('med')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(extract_results(@session)['middle_name']).to eq('Monkey')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should scope CSS selectors" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, '#second')).to have_no_css('h1')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#parent" do
|
|
|
|
it "should have a reference to its parent if there is one" do
|
|
|
|
@node = @session.find(:css, '#first')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@node.parent).to eq(@node.session.document)
|
|
|
|
expect(@node.find(:css, '#foo').parent).to eq(@node)
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "#text" do
|
|
|
|
it "should extract node texts" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.all('//a')[0].text).to eq('labore')
|
|
|
|
expect(@session.all('//a')[1].text).to eq('ullamco')
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should return document text on /html selector" do
|
|
|
|
@session.visit('/with_simple_html')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.all('/html')[0].text).to eq('Bar')
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "#[]" do
|
|
|
|
it "should extract node attributes" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.all('//a')[0][:class]).to eq('simple')
|
|
|
|
expect(@session.all('//a')[1][:id]).to eq('foo')
|
|
|
|
expect(@session.all('//input')[0][:type]).to eq('text')
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should extract boolean node attributes" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//input[@id="checked_field"]')[:checked]).to be_truthy
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "#value" do
|
|
|
|
it "should allow retrieval of the value" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//textarea[@id="normal"]').value).to eq('banana')
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should not swallow extra newlines in textarea" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//textarea[@id="additional_newline"]').value).to eq("\nbanana")
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2013-03-26 11:19:37 -04:00
|
|
|
|
2013-04-25 21:09:34 -04:00
|
|
|
it "should not swallow leading newlines for set content in textarea" do
|
|
|
|
@session.find('//textarea[@id="normal"]').set("\nbanana")
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//textarea[@id="normal"]').value).to eq("\nbanana")
|
2013-04-25 21:09:34 -04:00
|
|
|
end
|
2012-12-14 19:06:50 -05:00
|
|
|
|
2013-03-27 09:54:16 -04:00
|
|
|
it "return any HTML content in textarea" do
|
2013-03-26 11:19:37 -04:00
|
|
|
@session.find('//textarea[1]').set("some <em>html</em> here")
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//textarea[1]').value).to eq("some <em>html</em> here")
|
2013-03-26 11:19:37 -04:00
|
|
|
end
|
2013-10-28 18:58:04 -04:00
|
|
|
|
|
|
|
it "defaults to 'on' for checkbox" do
|
|
|
|
@session.visit('/form')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//input[@id="valueless_checkbox"]').value).to eq('on')
|
2013-10-28 18:58:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "defaults to 'on' for radio buttons" do
|
|
|
|
@session.visit('/form')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//input[@id="valueless_radio"]').value).to eq('on')
|
2013-10-28 18:58:04 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "#set" do
|
|
|
|
it "should allow assignment of field value" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.first('//input').value).to eq('monkey')
|
2012-07-21 16:44:10 -04:00
|
|
|
@session.first('//input').set('gorilla')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.first('//input').value).to eq('gorilla')
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
2012-11-16 17:07:50 -05:00
|
|
|
|
|
|
|
it "should fill the field even if the caret was not at the end", :requires => [:js] do
|
|
|
|
@session.execute_script("var el = document.getElementById('test_field'); el.focus(); el.setSelectionRange(0, 0);")
|
|
|
|
@session.first('//input').set('')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.first('//input').value).to eq('')
|
2012-11-16 17:07:50 -05:00
|
|
|
end
|
2013-03-26 11:26:24 -04:00
|
|
|
|
2013-03-09 15:27:53 -05:00
|
|
|
it "should not set if the text field is readonly" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.first('//input[@readonly]').value).to eq('should not change')
|
2013-03-09 15:27:53 -05:00
|
|
|
@session.first('//input[@readonly]').set('changed')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.first('//input[@readonly]').value).to eq('should not change')
|
2013-03-09 15:27:53 -05:00
|
|
|
end
|
2013-03-26 11:26:24 -04:00
|
|
|
|
2013-03-09 15:27:53 -05:00
|
|
|
it "should not set if the textarea is readonly" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.first('//textarea[@readonly]').value).to eq('textarea should not change')
|
2013-03-09 15:27:53 -05:00
|
|
|
@session.first('//textarea[@readonly]').set('changed')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.first('//textarea[@readonly]').value).to eq('textarea should not change')
|
2013-03-26 11:26:24 -04:00
|
|
|
end
|
2012-12-14 19:06:50 -05:00
|
|
|
|
|
|
|
it 'should allow me to change the contents of a contenteditable element', :requires => [:js] do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.find(:css,'#existing_content_editable').set('WYSIWYG')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css,'#existing_content_editable').text).to eq('WYSIWYG')
|
2012-12-14 19:06:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow me to set the contents of a contenteditable element', :requires => [:js] do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.find(:css,'#blank_content_editable').set('WYSIWYG')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css,'#blank_content_editable').text).to eq('WYSIWYG')
|
2012-12-14 19:06:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow me to change the contents of a contenteditable elements child', :requires => [:js] do
|
|
|
|
pending "Selenium doesn't like editing nested contents"
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.find(:css,'#existing_content_editable_child').set('WYSIWYG')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css,'#existing_content_editable_child').text).to eq('WYSIWYG')
|
2012-12-14 19:06:50 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "#tag_name" do
|
|
|
|
it "should extract node tag name" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.all('//a')[0].tag_name).to eq('a')
|
|
|
|
expect(@session.all('//a')[1].tag_name).to eq('a')
|
|
|
|
expect(@session.all('//p')[1].tag_name).to eq('p')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2013-01-29 04:35:23 -05:00
|
|
|
describe "#disabled?" do
|
|
|
|
it "should extract disabled node" do
|
|
|
|
@session.visit('/form')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//input[@id="customer_name"]')).to be_disabled
|
|
|
|
expect(@session.find('//input[@id="customer_email"]')).not_to be_disabled
|
2013-01-29 04:35:23 -05:00
|
|
|
end
|
2013-03-26 11:26:24 -04:00
|
|
|
|
2013-03-15 15:10:15 -04:00
|
|
|
it "should see disabled options as disabled" do
|
|
|
|
@session.visit('/form')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//select[@id="form_title"]/option[1]')).not_to be_disabled
|
|
|
|
expect(@session.find('//select[@id="form_title"]/option[@disabled]')).to be_disabled
|
2013-03-15 15:10:15 -04:00
|
|
|
end
|
2013-03-26 11:26:24 -04:00
|
|
|
|
2013-03-15 15:10:15 -04:00
|
|
|
it "should see enabled options in disabled select as disabled" do
|
|
|
|
@session.visit('/form')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//select[@id="form_disabled_select"]/option')).to be_disabled
|
|
|
|
expect(@session.find('//select[@id="form_title"]/option[1]')).not_to be_disabled
|
2013-03-15 15:10:15 -04:00
|
|
|
end
|
2013-01-29 04:35:23 -05:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "#visible?" do
|
|
|
|
it "should extract node visibility" do
|
2013-02-15 16:09:40 -05:00
|
|
|
Capybara.ignore_hidden_elements = false
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.first('//a')).to be_visible
|
2012-07-21 16:44:10 -04:00
|
|
|
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//div[@id="hidden"]')).not_to be_visible
|
|
|
|
expect(@session.find('//div[@id="hidden_via_ancestor"]')).not_to be_visible
|
|
|
|
expect(@session.find('//div[@id="hidden_attr"]')).not_to be_visible
|
|
|
|
expect(@session.find('//a[@id="hidden_attr_via_ancestor"]')).not_to be_visible
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "#checked?" do
|
|
|
|
it "should extract node checked state" do
|
|
|
|
@session.visit('/form')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//input[@id="gender_female"]')).to be_checked
|
|
|
|
expect(@session.find('//input[@id="gender_male"]')).not_to be_checked
|
|
|
|
expect(@session.first('//h1')).not_to be_checked
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "#selected?" do
|
|
|
|
it "should extract node selected state" do
|
|
|
|
@session.visit('/form')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//option[@value="en"]')).to be_selected
|
|
|
|
expect(@session.find('//option[@value="sv"]')).not_to be_selected
|
|
|
|
expect(@session.first('//h1')).not_to be_selected
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "#==" do
|
|
|
|
it "preserve object identity" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//h1') == @session.find('//h1')).to be true
|
|
|
|
expect(@session.find('//h1') === @session.find('//h1')).to be true
|
|
|
|
expect(@session.find('//h1').eql? @session.find('//h1')).to be false
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-11-27 02:49:51 -05:00
|
|
|
|
|
|
|
it "returns false for unrelated object" do
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//h1') == "Not Capybara::Node::Base").to be false
|
2012-11-27 02:49:51 -05:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe "#trigger", :requires => [:js, :trigger] do
|
|
|
|
it "should allow triggering of custom JS events" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.find(:css, '#with_focus_event').trigger(:focus)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session).to have_css('#focus_event_triggered')
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe '#drag_to', :requires => [:js, :drag] do
|
|
|
|
it "should drag and drop an object" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
element = @session.find('//div[@id="drag"]')
|
|
|
|
target = @session.find('//div[@id="drop"]')
|
|
|
|
element.drag_to(target)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find('//div[contains(., "Dropped!")]')).not_to be_nil
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2013-03-26 11:26:24 -04:00
|
|
|
|
|
|
|
describe '#hover', :requires => [:hover] do
|
2013-02-25 12:56:23 -05:00
|
|
|
it "should allow hovering on an element" do
|
|
|
|
@session.visit('/with_hover')
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css,'.hidden_until_hover', :visible => false)).not_to be_visible
|
2013-02-25 12:56:23 -05:00
|
|
|
@session.find(:css,'.wrapper').hover
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, '.hidden_until_hover', :visible => false)).to be_visible
|
2013-02-25 12:56:23 -05:00
|
|
|
end
|
|
|
|
end
|
2013-05-10 12:55:17 -04:00
|
|
|
|
|
|
|
describe '#double_click', :requires => [:js] do
|
|
|
|
it "should double click an element" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.find(:css, '#click-test').double_click
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, '#has-been-double-clicked')).to be
|
2013-05-10 12:55:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#right_click', :requires => [:js] do
|
|
|
|
it "should double click an element" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
@session.find(:css, '#click-test').right_click
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@session.find(:css, '#has-been-right-clicked')).to be
|
2013-05-10 12:55:17 -04:00
|
|
|
end
|
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
describe '#reload', :requires => [:js] do
|
|
|
|
context "without automatic reload" do
|
|
|
|
before { Capybara.automatic_reload = false }
|
|
|
|
it "should reload the current context of the node" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
node = @session.find(:css, '#reload-me')
|
|
|
|
@session.click_link('Reload!')
|
|
|
|
sleep(0.3)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(node.reload.text).to eq('has been reloaded')
|
|
|
|
expect(node.text).to eq('has been reloaded')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should reload a parent node" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
node = @session.find(:css, '#reload-me').find(:css, 'em')
|
|
|
|
@session.click_link('Reload!')
|
|
|
|
sleep(0.3)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(node.reload.text).to eq('has been reloaded')
|
|
|
|
expect(node.text).to eq('has been reloaded')
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
it "should not automatically reload" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
node = @session.find(:css, '#reload-me')
|
|
|
|
@session.click_link('Reload!')
|
|
|
|
sleep(0.3)
|
2012-10-30 09:26:19 -04:00
|
|
|
expect { node.text.to == 'has been reloaded' }.to raise_error
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
after { Capybara.automatic_reload = true }
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
|
|
|
|
2012-07-21 16:44:10 -04:00
|
|
|
context "with automatic reload" do
|
|
|
|
it "should reload the current context of the node automatically" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
node = @session.find(:css, '#reload-me')
|
|
|
|
@session.click_link('Reload!')
|
|
|
|
sleep(0.3)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(node.text).to eq('has been reloaded')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should reload a parent node automatically" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
node = @session.find(:css, '#reload-me').find(:css, 'em')
|
|
|
|
@session.click_link('Reload!')
|
|
|
|
sleep(0.3)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(node.text).to eq('has been reloaded')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should reload a node automatically when using find" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
node = @session.find(:css, '#reload-me')
|
|
|
|
@session.click_link('Reload!')
|
|
|
|
sleep(0.3)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(node.find(:css, 'a').text).to eq('has been reloaded')
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not reload nodes which haven't been found" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
node = @session.all(:css, '#the-list li')[1]
|
|
|
|
@session.click_link('Fetch new list!')
|
|
|
|
sleep(0.3)
|
2012-10-30 09:26:19 -04:00
|
|
|
expect { node.text.to == 'Foo' }.to raise_error
|
|
|
|
expect { node.text.to == 'Bar' }.to raise_error
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-10-30 08:42:06 -04:00
|
|
|
|
|
|
|
it "should reload nodes with options" do
|
|
|
|
@session.visit('/with_js')
|
|
|
|
node = @session.find(:css, 'em', :text => "reloaded")
|
|
|
|
@session.click_link('Reload!')
|
|
|
|
sleep(1)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(node.text).to eq('has been reloaded')
|
2012-10-30 08:42:06 -04:00
|
|
|
end
|
2012-07-21 16:44:10 -04:00
|
|
|
end
|
2012-07-13 06:59:57 -04:00
|
|
|
end
|
|
|
|
end
|