def _region_from_element(element, screenshot): # type: (AnyWebElement, EyesWebDriverScreenshot) -> Region location = element.location if screenshot: # Element's coordinates are context relative, so we need to convert them first. adjusted_location = screenshot.location_in_screenshot( Point.from_(location), CoordinatesType.CONTEXT_RELATIVE) else: adjusted_location = Point.from_(location) region = Region.from_(adjusted_location, element.size) return region
def _ensure_element_visible(self, element): position_provider = None if self._target_element and not self.driver.is_mobile_app: original_fc = self.driver.frame_chain.clone() eyes_element = EyesWebElement(element, self.driver) element_bounds = eyes_element.bounds current_frame_offset = original_fc.current_frame_offset element_bounds = element_bounds.offset(current_frame_offset) viewport_bounds = self._get_viewport_scroll_bounds() logger.info("viewport_bounds: {}; element_bounds: {}".format( viewport_bounds, element_bounds)) if not viewport_bounds.contains(element_bounds): self._ensure_frame_visible() element_location = Point.from_(element.location) if len(original_fc ) > 0 and element is not original_fc.peek.reference: fc = original_fc self.driver.switch_to.frames(original_fc) scroll_root_element = eyes_selenium_utils.curr_frame_scroll_root_element( self.driver, self._scroll_root_element) else: fc = self.driver.frame_chain.clone() scroll_root_element = self.scroll_root_element position_provider = self._element_position_provider_from( scroll_root_element) state = position_provider.get_state() position_provider.set_position(element_location) yield position_provider if self._target_element and position_provider and not self.driver.is_mobile_app: self.driver.switch_to.frames(fc) position_provider.restore_state(state)
def get_region(): rect = check_settings.values.target_region if rect is None: if self.driver.is_mobile_platform: bounds = self._target_element.rect else: bounds = self._target_element.bounding_client_rect region = Region( bounds["x"], bounds["y"], bounds["width"], bounds["height"], coordinates_type=CoordinatesType.CONTEXT_RELATIVE, ) else: s = self._target_element.size_and_borders.size b = self._target_element.size_and_borders.borders p = Point.from_(self._target_element.location) p = p.offset(b["left"], b["top"]) x = p.x + rect.left y = p.y + rect.top w = min(p.x + s["width"], rect.right) - x h = min(p.y + s["height"], rect.bottom) - y region = Region(x, y, w, h, CoordinatesType.CONTEXT_RELATIVE) return region
def _ensure_element_visible(self, element): if self._target_element is None: # No element? we must be checking the window. return None if self.driver.is_mobile_platform: logger.debug( "NATIVE context identified, skipping 'ensure element visible'") return None original_fc = self.driver.frame_chain.clone() switch_to = self.driver.switch_to eyes_element = EyesWebElement(element, self.driver) element_bounds = eyes_element.bounds current_frame_offset = original_fc.current_frame_offset element_bounds.offset(current_frame_offset.x, current_frame_offset.y) viewport_bounds = self._get_viewport_scroll_bounds() logger.info("viewport_bounds: {}; element_bounds: {}".format( viewport_bounds, element_bounds)) if not element_bounds.contains(viewport_bounds): self._ensure_frame_visible() element_location = Point.from_(element.location) if len(original_fc ) > 0 and element is not original_fc.peek.reference: switch_to.frames(original_fc) scroll_root_element = self.driver.find_element_by_tag_name( "html") else: scroll_root_element = self.scroll_root_element position_provider = self._element_position_provider_from( scroll_root_element) position_provider.set_position(element_location)
def location(self): # type: () -> Point loc = Point.from_( super(MobileSafariElementAdapter, self).location) # scroll into view at this point curr_pos = eyes_selenium_utils.get_current_position( self._eyes_driver, eyes_selenium_utils.scroll_root_element_from(self._eyes_driver), ) return loc + curr_pos
def _ensure_element_visible(self, element): position_provider = fc = None if element and not self.driver.is_mobile_app: original_fc = self.driver.frame_chain.clone() element_bounds = element.bounds current_frame_offset = original_fc.current_frame_offset element_bounds = element_bounds.offset(current_frame_offset) viewport_bounds = self._get_viewport_scroll_bounds() logger.info( "viewport_bounds: {}; element_bounds: {}".format( viewport_bounds, element_bounds ) ) if not viewport_bounds.contains(element_bounds): self._ensure_frame_visible() element_location = Point.from_(element.location) if len(original_fc) > 0 and element is not original_fc.peek.reference: fc = original_fc self.driver.switch_to.frames(original_fc) scroll_root_element = ( eyes_selenium_utils.curr_frame_scroll_root_element( self.driver, self._scroll_root_element ) ) # This might happen when scroll root element is calculated in the # beginning of SeleniumEyes.check when there is a frame in # driver's frame_chain but it gets popped out and becomes check target elif not self.scroll_root_element.is_attached_to_page: fc = self.driver.frame_chain.clone() scroll_root_element = self.driver.find_element_by_tag_name("html") else: fc = self.driver.frame_chain.clone() scroll_root_element = self.scroll_root_element position_provider = self._element_position_provider_from( scroll_root_element ) state = position_provider.get_state() position_provider.set_position(element_location) yield position_provider if element and position_provider and fc and not self.driver.is_mobile_app: self.driver.switch_to.frames(fc) position_provider.restore_state(state)