Best Python code snippet using selene_python
match.py
Source:match.py
...39 expected: str,40 describing_matched_to='has text',41 compared_by_predicate_to=predicate.includes,42) -> Condition[[], Element]:43 return ElementCondition.raise_if_not_actual(44 describing_matched_to + ' ' + expected,45 query.text,46 compared_by_predicate_to(expected),47 )48def element_has_exact_text(expected: str) -> Condition[[], Element]:49 return element_has_text(expected, 'has exact text', predicate.equals)50def element_has_js_property(name: str):51 def property_value(element: Element) -> str:52 return element().get_property(name)53 def property_values(collection: Collection) -> List[str]:54 return [element.get_property(name) for element in collection()]55 raw_property_condition = ElementCondition.raise_if_not_actual(56 'has js property ' + name, property_value, predicate.is_truthy57 )58 class ConditionWithValues(ElementCondition):59 def value(self, expected: str) -> Condition[[], Element]:60 return ElementCondition.raise_if_not_actual(61 f"has js property '{name}' with value '{expected}'",62 property_value,63 predicate.equals(expected),64 )65 def value_containing(self, expected: str) -> Condition[[], Element]:66 return ElementCondition.raise_if_not_actual(67 f"has js property '{name}' with value containing '{expected}'",68 property_value,69 predicate.includes(expected),70 )71 def values(self, *expected: str) -> Condition[[], Collection]:72 return CollectionCondition.raise_if_not_actual(73 f"has js property '{name}' with values '{expected}'",74 property_values,75 predicate.equals_to_list(expected),76 )77 def values_containing(self, *expected: str) -> Condition[[], Collection]:78 return CollectionCondition.raise_if_not_actual(79 f"has js property '{name}' with values containing '{expected}'",80 property_values,81 predicate.equals_by_contains_to_list(expected),82 )83 return ConditionWithValues(84 str(raw_property_condition), raw_property_condition.call85 )86def element_has_css_property(name: str):87 def property_value(element: Element) -> str:88 return element().value_of_css_property(name)89 def property_values(collection: Collection) -> List[str]:90 return [91 element.value_of_css_property(name) for element in collection()92 ]93 raw_property_condition = ElementCondition.raise_if_not_actual(94 'has css property ' + name, property_value, predicate.is_truthy95 )96 class ConditionWithValues(ElementCondition):97 def value(self, expected: str) -> Condition[[], Element]:98 return ElementCondition.raise_if_not_actual(99 f"has css property '{name}' with value '{expected}'",100 property_value,101 predicate.equals(expected),102 )103 def value_containing(self, expected: str) -> Condition[[], Element]:104 return ElementCondition.raise_if_not_actual(105 f"has css property '{name}' with value containing '{expected}'",106 property_value,107 predicate.includes(expected),108 )109 def values(self, *expected: str) -> Condition[[], Collection]:110 return CollectionCondition.raise_if_not_actual(111 f"has css property '{name}' with values '{expected}'",112 property_values,113 predicate.equals_to_list(expected),114 )115 def values_containing(self, *expected: str) -> Condition[[], Collection]:116 return CollectionCondition.raise_if_not_actual(117 f"has css property '{name}' with values containing '{expected}'",118 property_values,119 predicate.equals_by_contains_to_list(expected),120 )121 return ConditionWithValues(122 str(raw_property_condition), raw_property_condition.call123 )124def element_has_attribute(name: str):125 def attribute_value(element: Element) -> str:126 return element().get_attribute(name)127 def attribute_values(collection: Collection) -> List[str]:128 return [element.get_attribute(name) for element in collection()]129 raw_attribute_condition = ElementCondition.raise_if_not_actual(130 'has attribute ' + name, attribute_value, predicate.is_truthy131 )132 class ConditionWithValues(ElementCondition):133 def value(134 self, expected: str, ignore_case=False135 ) -> Condition[[], Element]:136 if ignore_case:137 warnings.warn(138 'ignore_case syntax is experimental and might change in future',139 FutureWarning,140 )141 return ElementCondition.raise_if_not_actual(142 f"has attribute '{name}' with value '{expected}'",143 attribute_value,144 predicate.equals(expected, ignore_case),145 )146 def value_containing(147 self, expected: str, ignore_case=False148 ) -> Condition[[], Element]:149 if ignore_case:150 warnings.warn(151 'ignore_case syntax is experimental and might change in future',152 FutureWarning,153 )154 return ElementCondition.raise_if_not_actual(155 f"has attribute '{name}' with value containing '{expected}'",156 attribute_value,157 predicate.includes(expected, ignore_case),158 )159 def values(self, *expected: str) -> Condition[[], Collection]:160 return CollectionCondition.raise_if_not_actual(161 f"has attribute '{name}' with values '{expected}'",162 attribute_values,163 predicate.equals_to_list(expected),164 )165 def values_containing(self, *expected: str) -> Condition[[], Collection]:166 return CollectionCondition.raise_if_not_actual(167 f"has attribute '{name}' with values containing '{expected}'",168 attribute_values,169 predicate.equals_by_contains_to_list(expected),170 )171 return ConditionWithValues(172 str(raw_attribute_condition), raw_attribute_condition.call173 )174element_is_selected: Condition[[], Element] = element_has_attribute(175 'elementIsSelected'176)177def element_has_value(expected: str) -> Condition[[], Element]:178 return element_has_attribute('value').value(expected)179def element_has_value_containing(expected: str) -> Condition[[], Element]:180 return element_has_attribute('value').value_containing(expected)181def element_has_css_class(expected: str) -> Condition[[], Element]:182 def class_attribute_value(element: Element) -> str:183 return element().get_attribute('class')184 return ElementCondition.raise_if_not_actual(185 f"has css class '{expected}'",186 class_attribute_value,187 predicate.includes_word(expected),188 )189element_is_blank: Condition[[], Element] = element_has_exact_text('').and_(190 element_has_value('')191)192def element_has_tag(193 expected: str,194 describing_matched_to='has tag',195 compared_by_predicate_to=predicate.equals,196) -> Condition[[], Element]:197 return ElementCondition.raise_if_not_actual(198 f'{describing_matched_to} + {expected}',199 query.tag,200 compared_by_predicate_to(expected),201 )202def element_has_tag_containing(expected: str) -> Condition[[], Element]:203 return element_has_tag(expected, 'has tag containing', predicate.includes)204def _is_collection_empty(collection: Collection) -> bool:205 warnings.warn(206 'match.collection_is_empty or be.empty is deprecated; '207 'use more explicit and obvious have.size(0) instead',208 DeprecationWarning,209 )210 return len(collection()) == 0211collection_is_empty: Condition[[], Collection] = CollectionCondition.raise_if_not(212 'is empty', _is_collection_empty213)214def collection_has_size(215 expected: int,216 describing_matched_to='has size',217 compared_by_predicate_to=predicate.equals,218) -> Condition[[], Collection]:219 def size(collection: Collection) -> int:220 return len(collection())221 return CollectionCondition.raise_if_not_actual(222 f'{describing_matched_to} {expected}',223 size,224 compared_by_predicate_to(expected),225 )226def collection_has_size_greater_than(expected: int) -> Condition[[], Collection]:227 return collection_has_size(228 expected, 'has size greater than', predicate.is_greater_than229 )230def collection_has_size_greater_than_or_equal(231 expected: int,232) -> Condition[[], Collection]:233 return collection_has_size(234 expected,235 'has size greater than or equal',236 predicate.is_greater_than_or_equal,237 )238def collection_has_size_less_than(expected: int) -> Condition[[], Collection]:239 return collection_has_size(240 expected, 'has size less than', predicate.is_less_than241 )242def collection_has_size_less_than_or_equal(243 expected: int,244) -> Condition[[], Collection]:245 return collection_has_size(246 expected,247 'has size less than or equal',248 predicate.is_less_than_or_equal,249 )250def collection_has_texts(*expected: str) -> Condition[[], Collection]:251 def visible_texts(collection: Collection) -> List[str]:252 return [253 webelement.text254 for webelement in collection()255 if webelement.is_displayed()256 ]257 return CollectionCondition.raise_if_not_actual(258 f'has texts {expected}',259 visible_texts,260 predicate.equals_by_contains_to_list(expected),261 )262def collection_has_exact_texts(*expected: str) -> Condition[[], Collection]:263 def visible_texts(collection: Collection) -> List[str]:264 return [265 webelement.text266 for webelement in collection()267 if webelement.is_displayed()268 ]269 return CollectionCondition.raise_if_not_actual(270 f'has exact texts {expected}',271 visible_texts,272 predicate.equals_to_list(expected),273 )274def browser_has_url(275 expected: str,276 describing_matched_to='has url',277 compared_by_predicate_to=predicate.equals,278) -> Condition[[Browser], []]:279 def url(browser: Browser) -> str:280 return browser.driver.current_url281 return BrowserCondition.raise_if_not_actual(282 f"{describing_matched_to} '{expected}'",283 url,284 compared_by_predicate_to(expected),285 )286def browser_has_url_containing(expected: str) -> Condition[[], Browser]:287 return browser_has_url(expected, 'has url containing', predicate.includes)288def browser_has_title(289 expected: str,290 describing_matched_to='has title',291 compared_by_predicate_to=predicate.equals,292) -> Condition[[], Browser]:293 def title(browser: Browser) -> str:294 return browser.driver.title295 return BrowserCondition.raise_if_not_actual(296 f"{describing_matched_to} '{expected}'",297 title,298 compared_by_predicate_to(expected),299 )300def browser_has_title_containing(expected: str) -> Condition[[], Browser]:301 return browser_has_title(302 expected, 'has title containing', predicate.includes303 )304def browser_has_tabs_number(305 expected: int,306 describing_matched_to='has tabs number',307 compared_by_predicate_to=predicate.equals,308) -> Condition[[], Browser]:309 def tabs_number(browser: Browser) -> int:310 return len(browser.driver.window_handles)311 return BrowserCondition.raise_if_not_actual(312 f'{describing_matched_to} {expected}',313 tabs_number,314 compared_by_predicate_to(expected),315 )316def browser_has_tabs_number_greater_than(expected: int) -> Condition[[], Browser]:317 return browser_has_tabs_number(318 expected, 'has tabs number greater than', predicate.is_greater_than319 )320def browser_has_tabs_number_greater_than_or_equal(321 expected: int,322) -> Condition[[], Browser]:323 return browser_has_tabs_number(324 expected,325 'has tabs number greater than or equal',326 predicate.is_greater_than_or_equal,327 )328def browser_has_tabs_number_less_than(expected: int) -> Condition[[], Browser]:329 return browser_has_tabs_number(330 expected, 'has tabs number less than', predicate.is_less_than331 )332def browser_has_tabs_number_less_than_or_equal(333 expected: int,334) -> Condition[[], Browser]:335 return browser_has_tabs_number(336 expected,337 'has tabs number less than or equal',338 predicate.is_less_than_or_equal,339 )340def browser_has_js_returned(341 expected: Any, script: str, *args342) -> Condition[[], Browser]:343 def script_result(browser: Browser):344 return browser.driver.execute_script(script, *args)345 return BrowserCondition.raise_if_not_actual(346 f'has the ```{script}``` script returned {expected}',347 script_result,348 predicate.equals(expected),...
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!!