How to use get_actual_webelements method in Selene

Best Python code snippet using selene_python

entity.py

Source:entity.py Github

copy

Full Screen

...833 )834 self.with_(Config(timeout=timeout)).should(condition)835 super().should(condition)836 return self837 def get_actual_webelements(self) -> List[WebElement]:838 warnings.warn(839 "considering to be deprecated; use collection as callable instead, like: browser.all('.foo')()",840 PendingDeprecationWarning,841 )842 return self()843 def caching(self) -> Collection:844 warnings.warn(845 "deprecated; use `cached` property instead: browser.all('#foo').cached",846 DeprecationWarning,847 )848 return self.cached849 def all_by(self, condition: Condition[[], Element]) -> Collection:850 warnings.warn(851 "deprecated; use `filtered_by` instead: browser.all('.foo').filtered_by(be.enabled)",...

Full Screen

Full Screen

elements.py

Source:elements.py Github

copy

Full Screen

...60# todo: PyCharm generates abstract methods impl before __init__ method.61# todo: Should we use this order convention? like below...62class IndexedWebElementLocator(ISeleneWebElementLocator):63 def find(self):64 # return self._collection.get_actual_webelements()[self._index]65 return wait_for(66 self._collection,67 have.size_at_least(self._index + 1),68 config.timeout,69 config.poll_during_waits)[self._index]70 @property71 def description(self):72 return "%s[%s]" % (self._collection, self._index)73 def __init__(self, index, collection):74 # type: (int, SeleneCollection) -> None75 self._index = index76 self._collection = collection77class WebdriverListWebElementLocator(ISeleneListWebElementLocator):78 def __init__(self, by, search_context):79 # type: (Tuple[By, str], ISearchContext) -> None80 self._by = by81 self._search_context = search_context82 @property83 def description(self):84 return 'all_by%s' % str(self._by)85 def find(self):86 return self._search_context.find_elements(*self._by)87class InnerListWebElementLocator(ISeleneListWebElementLocator):88 def __init__(self, by, element):89 # type: (Tuple[By, str], SeleneElement) -> None90 self._by = by91 self._element = element92 @property93 def description(self):94 return "(%s).find_all_by(%s)" % (self._element, self._by)95 def find(self):96 # return self._element.get_actual_webelement().find_elements(*self._by)97 return wait_for(self._element, be.visible, config.timeout, config.poll_during_waits) \98 .find_elements(*self._by)99class FilteredListWebElementLocator(ISeleneListWebElementLocator):100 def find(self):101 webelements = self._collection()102 filtered = [webelement103 for webelement in webelements104 if is_matched(self._condition, webelement)]105 return filtered106 @property107 def description(self):108 return "(%s).filter_by(%s)" % (self._collection, self._condition.description())109 def __init__(self, condition, collection):110 # type: (IEntityCondition, SeleneCollection) -> None111 self._condition = condition112 self._collection = collection113class SlicedListWebElementLocator(ISeleneListWebElementLocator):114 def find(self):115 # webelements = self._collection()116 webelements = wait_for(self._collection,117 have.size_at_least(self._slice.stop),118 config.timeout,119 config.poll_during_waits)120 return webelements[self._slice.start:self._slice.stop:self._slice.step]121 @property122 def description(self):123 return "(%s)[%s:%s:%s]" % (self._collection, self._slice.start, self._slice.stop, self._slice.step)124 def __init__(self, slc, collection):125 # type: (slice, SeleneCollection) -> None126 self._slice = slc127 self._collection = collection128class FoundByConditionWebElementLocator(ISeleneWebElementLocator):129 def find(self):130 for webelement in self._collection():131 if is_matched(self._condition, webelement):132 return webelement133 raise NoSuchElementException('Element was not found by: %s' % (self._condition,))134 @property135 def description(self):136 return "(%s).select_by(%s)" % (self._collection, self._condition.description())137 def __init__(self, condition, collection):138 # type: (IEntityCondition, SeleneCollection) -> None139 self._condition = condition140 self._collection = collection141def _wait_with_screenshot(webdriver, entity, condition, timeout=None, polling=None):142 if timeout is None:143 timeout = config.timeout144 if polling is None:145 polling = config.poll_during_waits146 try:147 return wait_for(entity, condition, timeout, polling)148 except TimeoutException as e:149 screenshot = helpers.take_screenshot(webdriver, )150 msg = '''{original_msg}151 screenshot: file://{screenshot}'''.format(original_msg=e.msg, screenshot=screenshot)152 raise TimeoutException(msg, e.screen, e.stacktrace)153class SeleneElement(with_metaclass(DelegatingMeta, IWebElement)):154 @property155 def __delegate__(self):156 # type: () -> IWebElement157 return self._locator.find()158 # todo: is this alias needed?159 def get_actual_webelement(self):160 # type: () -> IWebElement161 return self.__delegate__162 # todo: consider removing this method once conditions will be refactored163 # todo: (currently Condition impl depends on this method)164 def __call__(self):165 return self.__delegate__166 # todo: or... maybe better will be remove __delegate__, and just use __call_ instead... ?167 @classmethod168 def by(cls, by, webdriver, context=None):169 # type: (Tuple[str, str], IWebDriver, ISearchContext) -> SeleneElement170 if not context:171 context = webdriver172 return SeleneElement(WebDriverWebElementLocator(by, context), webdriver)173 @classmethod174 def by_css(cls, css_selector, webdriver, context=None):175 # type: (str, IWebDriver, ISearchContext) -> SeleneElement176 if not context:177 context = webdriver178 return SeleneElement.by((By.CSS_SELECTOR, css_selector), webdriver, context)179 @classmethod180 def by_css_or_by(cls, css_selector_or_by, webdriver, context=None):181 if not context:182 context = webdriver183 return SeleneElement.by(184 css_or_by_to_by(css_selector_or_by),185 webdriver,186 context)187 # todo: consider renaming webdriver to driver, because actually SeleneDriver also can be put here...188 def __init__(self, selene_locator, webdriver):189 # type: (ISeleneWebElementLocator, IWebDriver) -> None190 self._locator = selene_locator191 self._webdriver = webdriver192 self._actions_chains = ActionChains(webdriver)193 def __str__(self):194 return self._locator.description195 def _execute_on_webelement(self, command, condition=be.or_not_to_be):196 return command(_wait_with_screenshot(self._webdriver, self, condition))197 # *** Relative elements ***198 def element(self, css_selector_or_by):199 return SeleneElement(200 InnerWebElementLocator(css_or_by_to_by(css_selector_or_by), self),201 self._webdriver)202 s = element203 find = element204 # todo: consider making find a separate not-lazy method (not alias)205 # to be used in such example: s("#element").hover().find(".inner").click()206 # over: s("#element").hover().element(".inner").click()207 # todo: should then all action-commands return cached elements by default?208 # todo: this is an object, it does not find. should we switch from method to "as a property" implementation?209 def caching(self):210 return SeleneElement(CachingWebElementLocator(self), self._webdriver)211 # todo: cached or cache?212 def cached(self):213 caching = self.caching()214 return caching.should(be.in_dom)215 def all(self, css_selector_or_by):216 # return SeleneCollection.by_css_or_by(css_selector_or_by, self._webdriver, context=self)217 return SeleneCollection(218 InnerListWebElementLocator(css_or_by_to_by(css_selector_or_by), self),219 self._webdriver)220 ss = all221 elements = all222 find_all = all223 @property224 def parent_element(self):225 return self.element(by.be_parent())226 @property227 def following_sibling(self):228 return self.element(by.be_following_sibling())229 @property230 def first_child(self):231 return self.element(by.be_first_child())232 # *** Asserts (Explicit waits) ***233 def should(self, condition, timeout=None):234 if timeout is None:235 timeout = config.timeout236 # todo: implement proper cashing237 # self._found = wait_for(self, condition, timeout)238 _wait_with_screenshot(self._webdriver, self, condition, timeout)239 return self240 # todo: consider removing some aliases241 def assure(self, condition, timeout=None):242 return self.should(condition, timeout)243 def should_be(self, condition, timeout=None):244 return self.should(condition, timeout)245 def should_have(self, condition, timeout=None):246 return self.should(condition, timeout)247 def should_not(self, condition, timeout=None):248 if timeout is None:249 timeout = config.timeout250 # todo: implement proper cashing251 not_condition = not_(condition)252 _wait_with_screenshot(self._webdriver, self, not_condition, timeout)253 return self254 # todo: consider removing some aliases255 def assure_not(self, condition, timeout=None):256 return self.should_not(condition, timeout)257 def should_not_be(self, condition, timeout=None):258 return self.should_not(condition, timeout)259 def should_not_have(self, condition, timeout=None):260 return self.should_not(condition, timeout)261 # *** Additional actions ***262 def double_click(self):263 self._execute_on_webelement(264 lambda it: self._actions_chains.double_click(it).perform(),265 condition=be.visible)266 return self267 def context_click(self):268 self._execute_on_webelement(lambda it: self._actions_chains.context_click(it).perform(),269 condition=be.visible)270 return self271 def set(self, new_text_value):272 def clear_and_send_keys(webelement):273 webelement.clear()274 webelement.send_keys(new_text_value)275 self._execute_on_webelement(276 clear_and_send_keys,277 condition=be.visible)278 return self279 set_value = set280 def scroll_to(self):281 def js_scroll_to(webelement):282 location = webelement.location283 self._webdriver.execute_script("window.scrollTo({x},{y});".format(x=location['x'],284 y=location['y']))285 self._execute_on_webelement(286 js_scroll_to,287 condition=be.visible)288 return self289 def press_enter(self):290 return self.send_keys(Keys.ENTER)291 def press_escape(self):292 return self.send_keys(Keys.ESCAPE)293 def press_tab(self):294 return self.send_keys(Keys.TAB)295 def hover(self):296 self._execute_on_webelement(297 lambda it: self._actions_chains.move_to_element(it).perform(),298 condition=be.visible)299 return self300 # *** ISearchContext methods ***301 def find_elements(self, by=By.ID, value=None):302 return self._execute_on_webelement(303 lambda it: it.find_elements(by, value),304 condition=be.visible)305 # return self.__delegate__.find_elements(by, value) # todo: remove306 def find_element(self, by=By.ID, value=None):307 return self._execute_on_webelement(308 lambda it: it.find_element(by, value),309 condition=be.visible)310 # return self.__delegate__.find_element(by, value) # todo: remove311 # *** IWebElement methods ***312 @property313 def tag_name(self):314 return self._execute_on_webelement(315 lambda it: it.tag_name,316 condition=be.in_dom)317 @property318 def text(self):319 return self._execute_on_webelement(320 lambda it: it.text,321 condition=be.visible)322 def click(self):323 self._execute_on_webelement(324 lambda it: it.click(),325 condition=be.visible)326 return self # todo: think on: IWebElement#click was supposed to return None327 def submit(self):328 self._execute_on_webelement(329 lambda it: it.submit(),330 condition=be.visible)331 return self332 def clear(self):333 self._execute_on_webelement(334 lambda it: it.clear(),335 condition=be.visible)336 return self337 def get_attribute(self, name):338 return self._execute_on_webelement(339 lambda it: it.get_attribute(name),340 condition=be.in_dom)341 def is_selected(self):342 return self._execute_on_webelement(343 lambda it: it.is_selected(),344 condition=be.visible)345 def is_enabled(self):346 return self._execute_on_webelement(347 lambda it: it.is_enabled(),348 condition=be.visible)349 def send_keys(self, *value):350 self._execute_on_webelement(351 lambda it: it.send_keys(*value),352 condition=be.visible)353 return self354 # RenderedWebElement Items355 def is_displayed(self):356 return self._execute_on_webelement(357 lambda it: it.is_displayed(),358 condition=be.in_dom)359 @property360 def location_once_scrolled_into_view(self):361 return self._execute_on_webelement(362 lambda it: it.location_once_scrolled_into_view,363 condition=be.visible)364 @property365 def size(self):366 return self._execute_on_webelement(367 lambda it: it.size,368 condition=be.visible)369 def value_of_css_property(self, property_name):370 return self._execute_on_webelement(371 lambda it: it.value_of_css_property(property_name),372 condition=be.in_dom)373 @property374 def location(self):375 return self._execute_on_webelement(376 lambda it: it.location,377 condition=be.visible)378 @property379 def rect(self):380 return self._execute_on_webelement(381 lambda it: it.rect,382 condition=be.visible)383 @property384 def screenshot_as_base64(self):385 return self._execute_on_webelement(386 lambda it: it.screenshot_as_base64,387 condition=be.visible) # todo: or `be.in_dom`?388 @property389 def screenshot_as_png(self):390 return self._execute_on_webelement(391 lambda it: it.screenshot_as_png,392 condition=be.visible) # todo: or `be.in_dom`?393 def screenshot(self, filename):394 return self._execute_on_webelement(395 lambda it: it.screenshot(filename),396 condition=be.visible) # todo: or `be.in_dom`?397 @property398 def parent(self):399 return self._execute_on_webelement(400 lambda it: it.parent, # todo: should not we return here some Selene entity as search_context?401 condition=be.in_dom)402 @property403 def id(self):404 return self._execute_on_webelement(405 lambda it: it.id,406 condition=be.in_dom)407class SeleneCollection(with_metaclass(DelegatingMeta, Sequence)):408 """409 To fully match Selenium, SeleneCollection should extend collection.abc.MutableSequence.410 But that's the place where we should be more restrictive.411 It is actually the Selenium, who should use "Sequence" instead of "MutableSequence" (list)412 """413 @property414 def __delegate__(self):415 # type: () -> List[IWebElement]416 return self._locator.find()417 def get_actual_webelements(self):418 # type: () -> List[IWebElement]419 return self.__delegate__420 def __call__(self):421 # type: () -> List[IWebElement]422 return self.__delegate__423 @classmethod424 def by(cls, by, webdriver, context=None):425 # type: (Tuple[str, str], IWebDriver, ISearchContext) -> SeleneCollection426 if not context:427 context = webdriver428 return SeleneCollection(WebdriverListWebElementLocator(by, context), webdriver)429 @classmethod430 def by_css(cls, css_selector, webdriver, context=None):431 # type: (str, IWebDriver, ISearchContext) -> SeleneCollection...

Full Screen

Full Screen

conditions.py

Source:conditions.py Github

copy

Full Screen

...197 def description(self):198 return self.__class__.__name__199 def fn(self, elements):200 # type: (SeleneCollection) -> List[IWebElement]201 return self.match(elements.get_actual_webelements())202 @abstractmethod203 def match(self, webelements):204 # type: (List[IWebElement]) -> List[IWebElement]205 pass206class Texts(CollectionCondition):207 def __init__(self, *expected):208 self.expected = expected209 def match(self, webelements):210 actual = [it.text for it in webelements]211 if not (len(actual) == len(self.expected) and all(lmap(operator.contains, actual, self.expected))):212 raise ConditionMismatchException(213 expected=self.expected,214 actual=actual)215 return webelements...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selene automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful