Best Python code snippet using selene_python
custom_conditions.py
Source:custom_conditions.py
...49def test_wait_for_notification_after_reload_v1():50 browser.open(51 'https://the-internet.herokuapp.com/notification_message_rendered'52 )53 def assert_action_successful_on_reload(entity):54 browser.element('[href*=notification_message]').click()55 if not browser.element('#flash').matching(56 have.exact_text('Action successful\nÃ')57 ):58 raise AssertionError('wrong message received')59 browser.wait.for_(assert_action_successful_on_reload)60def test_wait_for_notification_after_reload_v2():61 """62 more descriptive implementation63 with custom rendering of error message on failure64 """65 browser.open(66 'https://the-internet.herokuapp.com/notification_message_rendered'67 )68 def assert_action_successful_on_reload(entity):69 browser.element('[href*=notification_message]').click()70 notification = browser.element('#flash')71 webelement = notification()72 actual = webelement.text73 expected = 'Action successful\nÃ'74 if actual != expected:75 raise AssertionError(76 f'notification message was wrong:'77 f'\texpected: {expected}'78 f'\t actual: {actual}'79 )80 browser.wait.for_(assert_action_successful_on_reload)81def test_wait_for_notification_after_reload_v3():82 """83 more descriptive implementation84 with custom rendering of error message on failure85 with condition parameter - a text of message to receive86 with a bit cleaner custom condition description in the log on failure87 (by default the condition name88 would be more low level default python fn representation)89 wrapping condition as fn into class like BrowserCondition90 â also allows to use some extra built-in feature s91 like BrowserCondition.or_, .and_, etc.92 here we also use the condition parameter - entity,93 - that should be browser94 if condition was called on a browser object95 """96 browser.open(97 'https://the-internet.herokuapp.com/notification_message_rendered'98 )99 def notification_on_reload(message: str) -> BrowserCondition:100 def fn(entity: Browser):101 entity.element('[href*=notification_message]').click()102 notification = entity.element('#flash')103 webelement = notification()104 actual = webelement.text105 expected = message106 if actual != expected:107 raise AssertionError(108 f'notification message was wrong:'109 f'\texpected: {expected}'110 f'\t actual: {actual}'111 )112 return BrowserCondition(113 f'received message {message} on reload',114 fn,115 )116 browser.wait.for_(117 notification_on_reload('Action successful\nÃ').or_(118 notification_on_reload('Action unsuccesful, please try again\nÃ')119 )120 )121def test_wait_for_notification_after_reload_v4():122 """123 with default rendering (built into selene's condition) of error message on failure124 """125 browser.open(126 'https://the-internet.herokuapp.com/notification_message_rendered'127 )128 def assert_action_successful_on_reload(entity):129 browser.element('[href*=notification_message]').click()130 have.exact_text('Action successful\nÃ')(browser.element('#flash'))131 browser.wait.for_(assert_action_successful_on_reload)132def test_wait_for_notification_after_reload_v5():133 """134 with default rendering (built into selene's condition) of error message on failure135 AND simplified syntax with lambdas136 """137 browser.open(138 'https://the-internet.herokuapp.com/notification_message_rendered'139 )140 browser.wait.for_(141 lambda _: (142 browser.element('[href*=notification_message]').click(),...
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!!