How to use function_to_retry method in SeleniumBase

Best Python code snippet using SeleniumBase

retry_tests.py

Source:retry_tests.py Github

copy

Full Screen

...5 self.x = 06 self.expected_args = (1, 2, 3)7 self.expected_kwargs = {"foo": "bar"}8 def when_i_execute_the_retryable_function(self):9 self.result = self.function_to_retry(*self.expected_args, **self.expected_kwargs)10 def it_should_call_it_once(self):11 assert self.x == 112 def it_should_forward_the_arguments(self):13 assert self.args == self.expected_args14 def it_should_forward_the_keyword_arguments(self):15 assert self.kwargs == self.expected_kwargs16 def it_should_return_the_final_answer(self):17 assert self.result is self.x18 @retry(Exception, times=3, interval=0.001)19 def function_to_retry(self, *args, **kwargs):20 self.args = args21 self.kwargs = kwargs22 self.x += 123 return self.x24class WhenRetryingAFunctionWhichThrowsAnExceptionAndOnErrorTakesNoArguments:25 def given_a_call_counter(self):26 self.x = 027 self.expected_args = (1, 2, 3)28 self.expected_kwargs = {"foo": "bar"}29 self.args = []30 self.kwargs = []31 self.on_error_calls = 032 self.exception = ValueError()33 def when_i_execute_the_retryable_function(self):34 @retry(ValueError, times=3, interval=0.001, on_error=self.on_error)35 def function_to_retry(*args, **kwargs):36 self.args.append(args)37 self.kwargs.append(kwargs)38 self.x += 139 if self.x != 3:40 raise self.exception41 return self.x42 self.result = function_to_retry(*self.expected_args, **self.expected_kwargs)43 def it_should_keep_trying_until_the_exception_goes_away(self):44 assert self.x == 345 def it_should_forward_the_arguments_every_time(self):46 assert all(a == self.expected_args for a in self.args)47 def it_should_forward_the_keyword_arguments_every_time(self):48 assert all(k == self.expected_kwargs for k in self.kwargs)49 def it_should_return_the_final_answer(self):50 assert self.result is self.x51 def it_should_call_on_error_every_time_it_failed(self):52 assert self.on_error_calls == 253 def on_error(self):54 self.on_error_calls += 155class WhenRetryingAFunctionWhichThrowsAnExceptionAndOnErrorTakesOneArgument:56 def given_a_call_counter(self):57 self.x = 058 self.expected_args = (1, 2, 3)59 self.expected_kwargs = {"foo": "bar"}60 self.args = []61 self.kwargs = []62 self.on_error_calls = []63 self.exception = ValueError()64 def when_i_execute_the_retryable_function(self):65 @retry(ValueError, times=3, interval=0.001, on_error=self.on_error)66 def function_to_retry(*args, **kwargs):67 self.args.append(args)68 self.kwargs.append(kwargs)69 self.x += 170 if self.x != 3:71 raise self.exception72 return self.x73 self.result = function_to_retry(*self.expected_args, **self.expected_kwargs)74 def it_should_keep_trying_until_the_exception_goes_away(self):75 assert self.x == 376 def it_should_forward_the_arguments_every_time(self):77 assert all(a == self.expected_args for a in self.args)78 def it_should_forward_the_keyword_arguments_every_time(self):79 assert all(k == self.expected_kwargs for k in self.kwargs)80 def it_should_return_the_final_answer(self):81 assert self.result is self.x82 def it_should_call_on_error_every_time_it_failed(self):83 assert self.on_error_calls == [self.exception, self.exception]84 def on_error(self, ex):85 self.on_error_calls.append(ex)86class WhenRetryingAFunctionWhichThrowsAnExceptionOnErrorTakesTwoArguments:87 def given_a_call_counter(self):88 self.x = 089 self.expected_args = (1, 2, 3)90 self.expected_kwargs = {"foo": "bar"}91 self.args = []92 self.kwargs = []93 self.on_error_calls = []94 self.exception = ValueError()95 def when_i_execute_the_retryable_function(self):96 @retry(ValueError, times=3, interval=0.001, on_error=self.on_error)97 def function_to_retry(*args, **kwargs):98 self.args.append(args)99 self.kwargs.append(kwargs)100 self.x += 1101 if self.x != 3:102 raise self.exception103 return self.x104 self.result = function_to_retry(*self.expected_args, **self.expected_kwargs)105 def it_should_keep_trying_until_the_exception_goes_away(self):106 assert self.x == 3107 def it_should_forward_the_arguments_every_time(self):108 assert all(a == self.expected_args for a in self.args)109 def it_should_forward_the_keyword_arguments_every_time(self):110 assert all(k == self.expected_kwargs for k in self.kwargs)111 def it_should_return_the_final_answer(self):112 assert self.result is self.x113 def it_should_call_on_error_every_time_it_failed(self):114 assert self.on_error_calls == [(self.exception, 0), (self.exception, 1)]115 def on_error(self, ex, retry_count):116 self.on_error_calls.append((ex, retry_count))117class WhenRetryingAFunctionWhichThrowsAnExceptionTooManyTimes:118 def given_an_error_to_throw(self):119 self.x = 0120 self.expected_exception = ValueError()121 def when_i_execute_the_retryable_function(self):122 self.exception = catch(self.function_to_retry)123 def it_should_keep_trying_until_the_number_of_retries_is_exceeded(self):124 assert self.x == 3125 def it_should_bubble_the_exception_out(self):126 assert self.exception is self.expected_exception127 @retry(ValueError, times=3, interval=0.001)128 def function_to_retry(self):129 self.x += 1130 raise self.expected_exception131class WhenRetryingAFunctionWhichThrowsAnExceptionWeWereNotExpecting:132 def given_an_error_to_throw(self):133 self.x = 0134 self.expected_exception = TypeError()135 def when_i_execute_the_retryable_function(self):136 self.exception = catch(self.function_to_retry)137 def it_should_bubble_the_exception_out(self):138 assert self.exception is self.expected_exception139 def it_should_only_try_once(self):140 assert self.x == 1141 @retry(ValueError, times=3, interval=0.001)142 def function_to_retry(self, *args, **kwargs):143 self.x += 1144 raise self.expected_exception145class WhenRetryingAFunctionAndListeningForMultipleErrors:146 def given_a_call_counter(self):147 self.x = 0148 self.expected_args = (1, 2, 3)149 self.expected_kwargs = {"foo": "bar"}150 self.args = []151 self.kwargs = []152 def when_i_execute_the_retryable_function(self):153 self.result = self.function_to_retry(*self.expected_args, **self.expected_kwargs)154 def it_should_keep_trying_until_the_exception_goes_away(self):155 assert self.x == 3156 def it_should_forward_the_arguments_every_time(self):157 assert all(a == self.expected_args for a in self.args)158 def it_should_forward_the_keyword_arguments_every_time(self):159 assert all(k == self.expected_kwargs for k in self.kwargs)160 def it_should_return_the_final_answer(self):161 assert self.result is self.x162 @retry([ValueError, IndexError], times=3, interval=0.001)163 def function_to_retry(self, *args, **kwargs):164 self.args.append(args)165 self.kwargs.append(kwargs)166 self.x += 1167 if self.x == 1:168 raise ValueError169 if self.x == 2:170 raise IndexError171 return self.x172class WhenRetryingAFunctionAndListeningForMultipleErrorsButItThrowsAnExceptionWeWereNotExpecting:173 def given_an_error_to_throw(self):174 self.x = 0175 self.expected_exception = TypeError()176 def when_i_execute_the_retryable_function(self):177 self.exception = catch(self.function_to_retry)178 def it_should_bubble_the_exception_out(self):179 assert self.exception is self.expected_exception180 def it_should_only_try_once(self):181 assert self.x == 1182 @retry([ValueError, IndexError], times=3, interval=0.001)183 def function_to_retry(self, *args, **kwargs):184 self.x += 1185 raise self.expected_exception186class WhenRetryingAFunctionAtTheUseSiteAndItWorksFirstTime:187 def given_a_call_counter(self):188 self.x = 0189 self.expected_args = (1, 2, 3)190 self.expected_kwargs = {"foo": "bar"}191 def when_i_retry_the_function(self):192 self.result = retry_(self.function_to_retry, Exception, 3, 0.001, lambda e, x: None, *self.expected_args, **self.expected_kwargs)193 def it_should_call_it_once(self):194 assert self.x == 1195 def it_should_forward_the_arguments(self):196 assert self.args == self.expected_args197 def it_should_forward_the_keyword_arguments(self):198 assert self.kwargs == self.expected_kwargs199 def it_should_return_the_final_answer(self):200 assert self.result is self.x201 def function_to_retry(self, *args, **kwargs):202 self.args = args203 self.kwargs = kwargs204 self.x += 1205 return self.x206class WhenRetryingAFunctionAtTheUseSiteAndItThrowsAnException:207 def given_a_call_counter(self):208 self.x = 0209 self.expected_args = (1, 2, 3)210 self.expected_kwargs = {"foo": "bar"}211 self.args = []212 self.kwargs = []213 def when_i_retry_the_function(self):214 self.result = retry_(self.function_to_retry, ValueError, 3, 0.001, lambda e, x: None, *self.expected_args, **self.expected_kwargs)215 def it_should_keep_trying_until_the_exception_goes_away(self):216 assert self.x == 3217 def it_should_forward_the_arguments_every_time(self):218 assert all(a == self.expected_args for a in self.args)219 def it_should_forward_the_keyword_arguments_every_time(self):220 assert all(k == self.expected_kwargs for k in self.kwargs)221 def it_should_return_the_final_answer(self):222 assert self.result is self.x223 def function_to_retry(self, *args, **kwargs):224 self.args.append(args)225 self.kwargs.append(kwargs)226 self.x += 1227 if self.x != 3:228 raise ValueError229 return self.x230class WhenRetryingAFunctionAtTheUseSiteAndItThrowsAnExceptionTooManyTimes:231 def given_an_error_to_throw(self):232 self.x = 0233 self.expected_exception = ValueError()234 def when_i_retry_the_function(self):235 self.exception = catch(retry_, self.function_to_retry, ValueError, times=3, interval=0.001)236 def it_should_keep_trying_until_the_number_of_retries_is_exceeded(self):237 assert self.x == 3238 def it_should_bubble_the_exception_out(self):239 assert self.exception is self.expected_exception240 def function_to_retry(self):241 self.x += 1242 raise self.expected_exception243class WhenRetryingAFunctionAtTheUseSiteAndItThrowsAnExceptionWeWereNotExpecting:244 def given_an_error_to_throw(self):245 self.x = 0246 self.expected_exception = TypeError()247 def when_i_retry_the_function(self):248 self.exception = catch(retry_, self.function_to_retry, ValueError, times=3, interval=0.001)249 def it_should_bubble_the_exception_out(self):250 assert self.exception is self.expected_exception251 def it_should_only_try_once(self):252 assert self.x == 1253 def function_to_retry(self, *args, **kwargs):254 self.x += 1255 raise self.expected_exception256class WhenRetryingAFunctionAtTheUseSiteAndListeningForMultipleErrors:257 def given_a_call_counter(self):258 self.x = 0259 self.expected_args = (1, 2, 3)260 self.expected_kwargs = {"foo": "bar"}261 self.args = []262 self.kwargs = []263 def when_i_retry_the_function(self):264 self.result = retry_(self.function_to_retry, [ValueError, IndexError], 3, 0.001, lambda e, x: None, *self.expected_args, **self.expected_kwargs)265 def it_should_keep_trying_until_the_exception_goes_away(self):266 assert self.x == 3267 def it_should_forward_the_arguments_every_time(self):268 assert all(a == self.expected_args for a in self.args)269 def it_should_forward_the_keyword_arguments_every_time(self):270 assert all(k == self.expected_kwargs for k in self.kwargs)271 def it_should_return_the_final_answer(self):272 assert self.result is self.x273 def function_to_retry(self, *args, **kwargs):274 self.args.append(args)275 self.kwargs.append(kwargs)276 self.x += 1277 if self.x == 1:278 raise ValueError279 if self.x == 2:280 raise IndexError281 return self.x282class WhenRetryingAFunctionAtTheUseSiteAndListeningForMultipleErrorsButItThrowsAnExceptionWeWereNotExpecting:283 def given_an_error_to_throw(self):284 self.x = 0285 self.expected_exception = TypeError()286 def when_i_retry_the_function(self):287 self.exception = catch(retry_, self.function_to_retry, [ValueError, IndexError], times=3, interval=0.001)288 def it_should_bubble_the_exception_out(self):289 assert self.exception is self.expected_exception290 def it_should_only_try_once(self):291 assert self.x == 1292 def function_to_retry(self, *args, **kwargs):293 self.x += 1...

Full Screen

Full Screen

api_caller.py

Source:api_caller.py Github

copy

Full Screen

...18 :return: The return object of the given function to retry.19 """20 for attempt in range(max_retries):21 try:22 response = function_to_retry(response_timeout=response_timeout, **kwargs)23 except requests.exceptions.Timeout:24 logging.warning(f"Retrying a timed out call to url --> {self.base_api_url}{kwargs['url_extension']}")25 response_timeout *= 226 continue27 else:28 break29 else:30 raise requests.exceptions.Timeout31 return response32 def raw_get_request(self, url_extension: str, response_timeout: float = 5.0) -> requests.Response:33 response = requests.get(url=f"{self.base_api_url}{url_extension}",34 headers=self.headers,35 timeout=response_timeout)36 return response...

Full Screen

Full Screen

sat_ops.py

Source:sat_ops.py Github

copy

Full Screen

...37 status_code = 038 json_data = {}39 while status_code != requests.codes.ok:40 if arg:41 status_code, json_data = function_to_retry(arg)42 else:43 status_code, json_data = function_to_retry()44 print("status_code: {0} | json_data {1}".format(status_code, json_data))45 try_count += 146 if try_count == try_limit:47 return try_count, {"Message": "Try limit {0} reached".format(try_limit)}48 return try_count, json_data49 def fetch_sensors_list(self):50 """51 Communicate with the satellite to get the list of sensors52 """53 url = "{0}/{1}".format(satellite_url, ids_endpoint)54 try:55 r = self.http_get(url)56 except requests.exceptions.Timeout:57 return 408, "Timeout while fetching sensor list"...

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 SeleniumBase 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