Best Site_prism code snippet using SitePrism.within
section_spec.rb
Source:section_spec.rb
...30 end31 it 'creates objects that are a subclass of SitePrism::Section' do32 expect(page_with_sections.section_with_a_block.class.ancestors).to include(described_class)33 end34 it 'has elements from within the defined section' do35 expect(page_with_sections.section_with_a_block).to respond_to(:single_section_element)36 end37 it 'has elements from the block' do38 expect(page_with_sections.section_with_a_block).to respond_to(:block_element)39 end40 context 'when second argument is a Class' do41 subject { page_with_section.new }42 let(:page_with_section) do43 Class.new(SitePrism::Page) do44 section :section, SitePrism::Section, '.section'45 end46 end47 it { is_expected.to respond_to(:section) }48 it { is_expected.to respond_to(:has_section?) }49 it { is_expected.to respond_to(:has_no_section?) }50 it { is_expected.to respond_to(:wait_until_section_visible) }51 it { is_expected.to respond_to(:wait_until_section_invisible) }52 it { is_expected.to respond_to(:all_there?) }53 it { is_expected.to respond_to(:within) }54 end55 context 'when second argument is not a Class but a block is given' do56 subject { page_with_anonymous_section.new }57 let(:page_with_anonymous_section) do58 Class.new(SitePrism::Page) do59 section :anonymous_section, '.section' do60 element :title, 'h1'61 end62 end63 end64 it { is_expected.to respond_to(:anonymous_section) }65 it { is_expected.to respond_to(:has_anonymous_section?) }66 it { is_expected.to respond_to(:has_no_anonymous_section?) }67 it { is_expected.to respond_to(:wait_until_anonymous_section_visible) }68 it { is_expected.to respond_to(:wait_until_anonymous_section_invisible) }69 it { is_expected.to respond_to(:all_there?) }70 it { is_expected.to respond_to(:within) }71 end72 context 'when second argument is a Class and a block is given' do73 subject { page_with_anonymous_section.new }74 let(:page_with_anonymous_section) do75 Class.new(SitePrism::Page) do76 section :anonymous_section, SitePrism::Section, '.section' do77 element :title, 'h1'78 end79 end80 end81 it { is_expected.to respond_to(:anonymous_section) }82 it { is_expected.to respond_to(:has_anonymous_section?) }83 it { is_expected.to respond_to(:has_no_anonymous_section?) }84 it { is_expected.to respond_to(:wait_until_anonymous_section_visible) }85 it { is_expected.to respond_to(:wait_until_anonymous_section_invisible) }86 it { is_expected.to respond_to(:all_there?) }87 it { is_expected.to respond_to(:within) }88 end89 context 'when second argument is not a Class and no block is given' do90 let(:incorrect_section) { SitePrism::Page.section(:incorrect_section, '.section') }91 let(:message) do92 'You should provide descendant of SitePrism::Section class or/and a block as the second argument.'93 end94 it 'raises an ArgumentError' do95 expect { incorrect_section }.to raise_error(ArgumentError).with_message(message)96 end97 end98 end99 describe '.set_default search arguments' do100 let(:page) do101 Class.new(SitePrism::Page) do102 section_with_default_arguments = Class.new(SitePrism::Section) do103 set_default_search_arguments :css, '.section'104 end105 section_with_default_arguments_for_parent = Class.new(section_with_default_arguments)106 section :section_using_defaults, section_with_default_arguments107 section :section_using_defaults_from_parent, section_with_default_arguments_for_parent108 section :section_with_locator, section_with_default_arguments, '.other-section'109 sections :sections, section_with_default_arguments110 end.new111 end112 let(:default_search_arguments) { [:css, '.section'] }113 context 'when search arguments are provided during the DSL definition' do114 it 'returns the search arguments for a section' do115 expect(page).to receive(:_find).with('.other-section', { wait: 0 })116 page.section_with_locator117 end118 it 'ignores the `default_search_arguments`' do119 expect(page).not_to receive(:_find).with(*default_search_arguments, { wait: 0 })120 page.section_with_locator121 end122 end123 context 'when search arguments are not provided during the DSL definition' do124 let(:invalid_page) do125 Class.new(SitePrism::Page) do126 section :section, SitePrism::Section127 end128 end129 let(:error_message) do130 'search arguments are needed in `section` definition or alternatively use `set_default_search_arguments`'131 end132 it 'uses the default search arguments set on the section' do133 expect(page).to receive(:_find).with(*default_search_arguments, { wait: 0 })134 page.section_using_defaults135 end136 it 'uses the default_search_arguments set on the parent if none set on section' do137 expect(page).to receive(:_find).with(*default_search_arguments, { wait: 0 })138 page.section_using_defaults_from_parent139 end140 it 'raises an ArgumentError if no default_search_arguments exist in the inheritane tree' do141 expect { invalid_page }.to raise_error(ArgumentError).with_message(error_message)142 end143 end144 end145 describe '.default_search_arguments' do146 let(:base_section) do147 Class.new(SitePrism::Section) do148 set_default_search_arguments :css, 'a.b'149 end150 end151 let(:child_section) do152 Class.new(base_section) do153 set_default_search_arguments :xpath, '//h3'154 end155 end156 let(:other_child_section) do157 Class.new(base_section)158 end159 it 'is false by default' do160 expect(described_class.default_search_arguments).to be false161 end162 it 'returns the default search arguments' do163 expect(base_section.default_search_arguments).to eq([:css, 'a.b'])164 end165 context 'when both parent and child class have default_search_arguments' do166 it 'returns the child level arguments' do167 expect(child_section.default_search_arguments).to eq([:xpath, '//h3'])168 end169 end170 context 'when only parent class has default_search_arguments' do171 it 'returns the parent level arguments' do172 expect(other_child_section.default_search_arguments).to eq([:css, 'a.b'])173 end174 end175 end176 describe '.set_default_search_arguments' do177 it { expect(described_class).to respond_to(:set_default_search_arguments) }178 end179 describe '#new' do180 let(:new_page) do181 Class.new(SitePrism::Page) do182 section :new_section, SitePrism::Section, '.class-one', css: '.my-css', text: 'Hi'183 element :new_element, '.class-two'184 end185 end186 let(:page) { new_page.new }187 context 'with a block given' do188 let(:section_with_block) do189 described_class.new(SitePrism::Page.new, locator) { 1 + 1 }190 end191 it 'passes the locator to Capybara.within' do192 expect(Capybara).to receive(:within).with(locator)193 section_with_block194 end195 end196 context 'without a block given' do197 it 'does not pass the locator to Capybara.within' do198 expect(Capybara).not_to receive(:within)199 section_without_block200 end201 end202 context 'with Capybara query arguments' do203 let(:query_args) { { css: '.my-css', text: 'Hi' } }204 let(:locator_args) { '.class-one' }205 it 'passes in a hash of query arguments' do206 expect(page).to receive(:_find).with(*locator_args, { **query_args, wait: 0 })207 page.new_section208 end209 end210 context 'without Capybara query arguments' do211 let(:query_args) { {} }212 let(:locator_args) { '.class-two' }213 it 'passes in an empty hash, which is then sanitized out' do214 expect(page).to receive(:_find).with(*locator_args, { **query_args, wait: 0 })215 page.new_element216 end217 end218 end219 describe '#within' do220 it 'passes the block to Capybara#within' do221 expect(Capybara).to receive(:within).with(locator)222 section_without_block.within { :noop }223 end224 end225 describe '#visible?' do226 it 'delegates through root_element' do227 expect(locator).to receive(:visible?)228 section_without_block.visible?229 end230 end231 describe '#text' do232 it 'delegates through root_element' do233 expect(locator).to receive(:text)234 section_without_block.text235 end236 end...
element_container.rb
Source:element_container.rb
...44 create_existence_checker iframe_name, element_selector45 create_nonexistence_checker iframe_name, element_selector46 create_waiter iframe_name, element_selector47 define_method iframe_name do |&block|48 within_frame scope_selector do49 block.call iframe_page_class.new50 end51 end52 end53 def add_to_mapped_items(item)54 @mapped_items ||= []55 @mapped_items << item.to_s56 end57 def raise_if_block(obj, name, has_block)58 return unless has_block59 raise SitePrism::UnsupportedBlock, "#{obj.class}##{name} does not accept blocks, did you mean to define a (i)frame?"60 end61 private62 def build(name, *find_args)...
room.rb
Source:room.rb
...5end6class RoomCoursePicker < SitePrism::Page7 set_url "/room/choose_course/{place_id}"8 def select_course(text)9 within "#course_list" do10 click_link text11 end12 end13end14class RoomTeacherPicker < SitePrism::Page15 set_url "/room/course_log/{id}/teachers"16 element :submit, "#footer .btn-default"17 def select_teacher(text)18 within "#teachers_form" do19 label = find('label', text: text)20 label.click21 end22 end23end24class RoomStudentPicker < SitePrism::Page25 class StudentsList < SitePrism::Section26 def student_button(name)27 within root_element do28 return first('button', text: name)29 end30 end31 end32 set_url "/room/course_log/{id}/students"33 element :total_students, ".btn.btn-light.students-list-bottom-btn"34 section :students_list, StudentsList, ".students-list"35 def type_card(card_code)36 if md = /\d+/.match(card_code)37 card = md[0].to_s38 else39 raise "Invalid card #{card_code}"40 end41 card.each_char do |c|...
within
Using AI Code Generation
1 def self.element(element_name, *find_args)2 define_method(element_name) do3 find(*find_args)4 def self.element(element_name, *find_args)5 define_method(element_name) do6 find(*find_args)7 def self.element(element_name, *find_args)8 define_method(element_name) do9 find(*find_args)10 def self.element(element_name, *find_args)11 define_method(element_name) do12 find(*find_args)13 def self.element(element_name, *find_args)14 define_method(element_name) do15 find(*find_args)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!