Best Python code snippet using selene_python
condition.py
Source:condition.py
...11 for condition in conditions:12 condition.call(entity)13 return cls(' and '.join(map(str, conditions)), fn)14 @classmethod15 def by_or(cls, *conditions):16 def fn(entity):17 errors: List[Exception] = []18 for condition in conditions:19 try:20 condition.call(entity)21 return22 except Exception as e:23 errors.append(e)24 raise AssertionError('; '.join(map(str, errors)))25 return cls(' or '.join(map(str, conditions)), fn)26 @classmethod27 def as_not(28 cls, condition: Condition[E], description: str = None29 ) -> Condition[E]:30 condition_words = str(condition).split(' ')31 is_or_have = condition_words[0]32 name = ' '.join(condition_words[1:])33 no_or_not = 'not' if is_or_have == 'is' else 'no'34 new_description = description or f'{is_or_have} {no_or_not} {name}'35 def fn(entity):36 try:37 condition.call(entity)38 except Exception:39 return40 raise ConditionNotMatchedError()41 return cls(new_description, fn)42 @classmethod43 def raise_if_not(44 cls, description: str, predicate: Predicate[E]45 ) -> Condition[E]:46 def fn(entity: E) -> None:47 if not predicate(entity):48 raise ConditionNotMatchedError()49 return cls(description, fn)50 @classmethod51 def raise_if_not_actual(52 cls, description: str, query: Lambda[E, R], predicate: Predicate[R]53 ) -> Condition[E]:54 def fn(entity: E) -> None:55 query_to_str = str(query)56 result = (57 query.__name__58 if query_to_str.startswith('<function')59 else query_to_str60 )61 actual = query(entity)62 if not predicate(actual):63 raise AssertionError(f'actual {result}: {actual}')64 return cls(description, fn)65 def __init__(self, description: str, fn: Lambda[E, None]):66 self._description = description67 self._fn = fn68 def call(self, entity: E) -> None:69 self._fn(entity)70 @property71 def predicate(self) -> Lambda[E, bool]:72 def fn(entity):73 try:74 self.call(entity)75 return True76 except Exception as e:77 return False78 return fn79 @property80 def not_(self) -> Condition[E]:81 return self.__class__.as_not(self)82 def __call__(self, *args, **kwargs):83 return self._fn(*args, **kwargs)84 def __str__(self):85 return self._description86 def and_(self, condition: Condition[E]) -> Condition[E]:87 return Condition.by_and(self, condition)88 def or_(self, condition: Condition[E]) -> Condition[E]:89 return Condition.by_or(self, condition)90def not_(condition_to_be_inverted: Condition):...
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!!