spec/element_spec.rb in page_magic-1.0.0.alpha4 vs spec/element_spec.rb in page_magic-1.0.0.alpha5

- old
+ new

@@ -1,8 +1,6 @@ describe 'Page elements' do - - before :each do Capybara.app = Class.new(Sinatra::Base) do get '/' do <<-HTML <label>enter text @@ -16,11 +14,10 @@ Capybara.current_session.visit('/') end describe 'inheriting' do - include_context :webapp it 'lets you create custom elements' do custom_element = Class.new(PageMagic::Element) do selector css: '.form' @@ -29,11 +26,10 @@ def self.name 'Form' end end - page = Class.new do include PageMagic url '/elements' section custom_element end @@ -42,14 +38,31 @@ page.visit page.form.form_link.visible?.should be_true end end - describe 'browser_element' do - it 'should raise an error if a selector has not been specified' do - expect { PageMagic::Element.new(:name, Object.new, type: :element).browser_element }.to raise_error(PageMagic::UndefinedSelectorException) + it 'should raise an error if a selector has not been specified' do + expect { PageMagic::Element.new(:name, Object.new, type: :element).browser_element }.to raise_error(PageMagic::UndefinedSelectorException) + end + + describe '#respond_to?' do + subject do + PageMagic::Element.new(:name, Object.new, type: :element, browser_element: double(element_method: '')) do + element :sub_element, css: '.sub-element' + end end + it 'checks for methods on self' do + expect(subject.respond_to?(:session)).to eq(true) + end + + it 'checks against registered elements' do + expect(subject.respond_to?(:sub_element)).to eq(true) + end + + it 'checks for the method of the browser_element' do + expect(subject.respond_to?(:element_method)).to eq(true) + end end describe '#browser_element' do let!(:browser) { double('browser') } let!(:page) do @@ -59,84 +72,82 @@ page_class.new end context 'options supplied to selector' do it 'passes them on to the cappybara finder method' do - options = {key: :value} + options = { key: :value } xpath_selector = '//input' expect(Capybara.current_session).to receive(:find).with(:xpath, xpath_selector, options) - PageMagic::Element.new(:my_input, page, type: :text_field, selector: {xpath: xpath_selector}.merge(options)).browser_element + PageMagic::Element.new(:my_input, page, type: :text_field, selector: { xpath: xpath_selector }.merge(options)).browser_element end end it 'should find by xpath' do - element = PageMagic::Element.new(:my_input, page, type: :text_field, selector: {xpath: '//input'}).browser_element + element = PageMagic::Element.new(:my_input, page, type: :text_field, selector: { xpath: '//input' }).browser_element element.value == 'filled in' end it 'should locate an element using its id' do - element = PageMagic::Element.new(:my_input, page, type: :text_field, selector: {id: 'field_id'}).browser_element + element = PageMagic::Element.new(:my_input, page, type: :text_field, selector: { id: 'field_id' }).browser_element element.value.should == 'filled in' end it 'should locate an element using its name' do - element = PageMagic::Element.new(:my_input, page, type: :text_field, selector: {name: 'field_name'}).browser_element + element = PageMagic::Element.new(:my_input, page, type: :text_field, selector: { name: 'field_name' }).browser_element element.value.should == 'filled in' end - it 'should locate an element using its label' do - element = PageMagic::Element.new(:my_link, page, type: :link, selector: {label: 'enter text'}).browser_element + element = PageMagic::Element.new(:my_link, page, type: :link, selector: { label: 'enter text' }).browser_element element[:id].should == 'field_id' end it 'should raise an exception when finding another element using its text' do - expect { PageMagic::Element.new(:my_link, page, type: :text_field, selector: {text: 'my link'}).browser_element }.to raise_error(PageMagic::UnsupportedSelectorException) + expect { PageMagic::Element.new(:my_link, page, type: :text_field, selector: { text: 'my link' }).browser_element }.to raise_error(PageMagic::UnsupportedSelectorException) end it 'should locate an element using css' do - element = PageMagic::Element.new(:my_link, page, type: :link, selector: {css: "input[name='field_name']"}).browser_element + element = PageMagic::Element.new(:my_link, page, type: :link, selector: { css: "input[name='field_name']" }).browser_element element[:id].should == 'field_id' end - it 'should return a prefetched value' do PageMagic::Element.new(:help, page, type: :link, browser_element: :prefetched_object).browser_element.should == :prefetched_object end it 'should raise errors for unsupported selectors' do - expect { PageMagic::Element.new(:my_link, page, type: :link, selector: {unsupported: ""}).browser_element }.to raise_error(PageMagic::UnsupportedSelectorException) + expect { PageMagic::Element.new(:my_link, page, type: :link, selector: { unsupported: '' }).browser_element }.to raise_error(PageMagic::UnsupportedSelectorException) end context 'text selector' do it 'should locate a link' do - element = PageMagic::Element.new(:my_link, page, type: :link, selector: {text: 'my link'}).browser_element + element = PageMagic::Element.new(:my_link, page, type: :link, selector: { text: 'my link' }).browser_element element[:id].should == 'my_link' end it 'should locate a button' do - element = PageMagic::Element.new(:my_button, page, type: :button, selector: {text: 'my button'}).browser_element + element = PageMagic::Element.new(:my_button, page, type: :button, selector: { text: 'my button' }).browser_element element[:id].should == 'my_button' end end end describe '#section?' do context 'element definitions exist' do subject do - PageMagic::Element.new(:my_link, :page, type: :link, selector: {text: 'my link'}) do + PageMagic::Element.new(:my_link, :page, type: :link, selector: { text: 'my link' }) do element :thing, text: 'text' end end it 'returns true' do expect(subject.section?).to eq(true) end end context 'method defined' do subject do - PageMagic::Element.new(:my_link, :page, type: :link, selector: {text: 'my link'}) do + PageMagic::Element.new(:my_link, :page, type: :link, selector: { text: 'my link' }) do def custom_method end end end @@ -145,11 +156,11 @@ end end context 'neither method or elements defined' do subject do - PageMagic::Element.new(:my_link, :page, type: :link, selector: {text: 'my link'}) + PageMagic::Element.new(:my_link, :page, type: :link, selector: { text: 'my link' }) end it 'returns false' do expect(subject.section?).to eq(false) end end @@ -164,21 +175,19 @@ PageMagic::Element.new(:help, page, type: :link, selector: :selector).session.should == page.session end end - context 'tests coppied in from section' do include_context :webapp before :each do @elements_page = elements_page.new @elements_page.visit end let!(:elements_page) do - Class.new do include PageMagic url '/elements' section :form_by_css do selector css: '.form' @@ -204,8 +213,6 @@ it 'can have elements' do @elements_page.form_by_css.link_in_form.visible?.should be_true end end - - end