Best Selenium code snippet using Selenium.WebDriver.in_viewport
element.rb
Source:element.rb
...467 #468 def obscured?469 element_call do470 return true unless present?471 scroll.to :bottom unless in_viewport?472 execute_js(:elementObscured, self)473 end474 end475 #476 # Returns true if the top of the element is visible in the viewport.477 #478 # @return [Boolean]479 #480 def in_viewport?481 element_call { execute_js(:isElementInViewport, @element) }482 end483 #484 # Returns given style property of this element.485 #486 # @example487 # browser.button(value: "Delete").style #=> "border: 4px solid red;"488 # browser.button(value: "Delete").style("border") #=> "4px solid rgb(255, 0, 0)"489 #490 # @param [String] property491 # @return [String]492 #493 def style(property = nil)494 if property...
webdriver.rb
Source:webdriver.rb
...137 def scroll_into_view(elt)138 self.execute_script("arguments[0].scrollIntoView(true);", elt)139 # Wait for the element to appear in our viewport140 Selenium::Config.retries.times do |try|141 in_viewport = self.execute_script("142 var rect = arguments[0].getBoundingClientRect();143 return (144 rect.top >= 0 &&145 rect.left >= 0 &&146 rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&147 rect.right <= (window.innerWidth || document.documentElement.clientWidth)148 );149 ", elt)150 break if in_viewport151 sleep 0.1152 end153 # If it's still not in the viewport we're optimistically charging ahead154 # here and assuming that the calling test will fail if it really155 # matters...156 elt157 end158 alias :find_element_orig :find_element159 def find_element(*selectors)160 wait_for_ajax161 try = 0162 while true163 begin164 matched = find_elements(*selectors)...
action_builder_spec.rb
Source:action_builder_spec.rb
...197 driver.action.move_to_location(rect.x.ceil, rect.y.ceil).click.perform198 expect(element.attribute(:value)).to eq('Clicked')199 end200 end201 def in_viewport?(element)202 in_viewport = <<~IN_VIEWPORT203 for(var e=arguments[0],f=e.offsetTop,t=e.offsetLeft,o=e.offsetWidth,n=e.offsetHeight;204 e.offsetParent;)f+=(e=e.offsetParent).offsetTop,t+=e.offsetLeft;205 return f<window.pageYOffset+window.innerHeight&&t<window.pageXOffset+window.innerWidth&&f+n>206 window.pageYOffset&&t+o>window.pageXOffset207 IN_VIEWPORT208 driver.execute_script(in_viewport, element)209 end210 describe '#scroll_to', only: {browser: %i[chrome edge]} do211 it 'scrolls to element' do212 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')213 iframe = driver.find_element(tag_name: 'iframe')214 expect(in_viewport?(iframe)).to eq false215 driver.action.scroll_to(iframe).perform216 expect(in_viewport?(iframe)).to eq true217 end218 it 'scrolls to provided offset from element' do219 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')220 iframe = driver.find_element(tag_name: 'iframe')221 driver.action.scroll_to(iframe, 0, 200).perform222 driver.switch_to.frame(iframe)223 checkbox = driver.find_element(name: 'scroll_checkbox')224 expect(in_viewport?(checkbox)).to eq true225 end226 end227 describe '#scroll_by', only: {browser: %i[chrome edge]} do228 it 'scrolls by amount provided' do229 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')230 footer = driver.find_element(tag_name: 'footer')231 y = footer.rect.y232 driver.action.scroll_by(0, y).perform233 expect(in_viewport?(footer)).to eq true234 end235 end236 describe '#scroll', only: {browser: %i[chrome edge]} do237 context 'when origin is offset from viewport' do238 it 'scrolls by amount provided' do239 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame.html')240 iframe = driver.find_element(tag_name: 'iframe')241 origin = WheelActions::ScrollOrigin.viewport(10, 10)242 driver.action.scroll(0, 200, origin: origin).perform243 driver.switch_to.frame(iframe)244 checkbox = driver.find_element(name: 'scroll_checkbox')245 expect(in_viewport?(checkbox)).to eq true246 end247 it 'raises MoveTargetOutOfBoundsError when origin offset is out of viewport' do248 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')249 origin = WheelActions::ScrollOrigin.viewport(-10, -10)250 expect {251 driver.action.scroll(0, 200, origin: origin).perform252 }.to raise_error(Error::MoveTargetOutOfBoundsError)253 end254 end255 context 'when origin is offset from center of element' do256 it 'scrolls by amount provided' do257 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')258 footer = driver.find_element(tag_name: 'footer')259 origin = WheelActions::ScrollOrigin.element(footer, 10, -20)260 driver.action.scroll(0, 200, origin: origin).perform261 driver.switch_to.frame(driver.find_element(tag_name: 'iframe'))262 checkbox = driver.find_element(name: 'scroll_checkbox')263 expect(in_viewport?(checkbox)).to eq true264 end265 it 'raises MoveTargetOutOfBoundsError when origin offset is out of viewport' do266 driver.navigate.to url_for('scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html')267 footer = driver.find_element(tag_name: 'footer')268 origin = WheelActions::ScrollOrigin.element(footer, 10, 20)269 expect {270 driver.action.scroll(0, 200, origin: origin).perform271 }.to raise_error(Error::MoveTargetOutOfBoundsError)272 end273 end274 end275 end # ActionBuilder276 end # WebDriver277end # Selenium...
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!!