Best Python code snippet using pytractor_python
test_mixins.py
Source:test_mixins.py
...37 mock_kwarg = MagicMock()38 result = self.wrapped_function(mock_arg, kwarg=mock_kwarg)39 # result should be the result of the wrapped function40 self.assertIs(result, mock_check_function.return_value)41 # wait_for_angular() should have been called42 mock_wait_for_angular.assert_called_once_with()43 # the check function should have been called44 mock_check_function.assert_called_once_with(mock_arg,45 kwarg=mock_kwarg)46class WebDriverMixinConstructorTest(unittest.TestCase):47 class ConstructorTester(object):48 """Class for checking calls to __init__"""49 def __init__(self, *args, **kwargs):50 self.constructor_called(*args, **kwargs)51 def constructor_called(self, *args, **kwargs):52 """53 This function will be called by __init__. We can hook onto this54 to check for __init__ calls.55 """56 pass57 class TestDriver(WebDriverMixin, ConstructorTester):58 pass59 def test_constructor(self):60 base_url = 'BASEURL'61 root_element = 'ROOTEL'62 test_timeout = 'TESTTIMEOUT'63 script_timeout = 'SCRIPTTIMEOUT'64 with patch.object(65 self.ConstructorTester, 'constructor_called'66 ) as mock_constructor_called, patch.object(67 self.TestDriver, 'set_script_timeout', create=True68 ) as mock_set_script_timeout:69 instance = self.TestDriver(base_url, root_element,70 test_timeout=test_timeout,71 script_timeout=script_timeout)72 # verify that properties have been set73 self.assertIs(instance._base_url, base_url)74 self.assertIs(instance._root_element, root_element)75 self.assertIs(instance._test_timeout, test_timeout)76 # verify that the super class' constructor has been called77 mock_constructor_called.assert_called_once_with()78 # verify that set_script_timeout has been called79 mock_set_script_timeout.assert_called_once_with(script_timeout)80class WebDriverMixinTest(unittest.TestCase):81 def setUp(self):82 set_script_timeout_patcher = patch.object(83 WebDriverMixin, 'set_script_timeout', create=True84 )85 self.mock_set_script_timeout = set_script_timeout_patcher.start()86 self.addCleanup(set_script_timeout_patcher.stop)87 self.mock_root_element = MagicMock()88 self.instance = WebDriverMixin('http://localhost',89 self.mock_root_element)90 @patch('pytractor.mixins.resource_string')91 def verify__execute_client_script_call(self, async, mock_resource_string):92 with patch.multiple(93 self.instance,94 execute_async_script=DEFAULT, execute_script=DEFAULT,95 create=True96 ) as mock_execute:97 (mock_execute_script,98 mock_execute_async_script) = [mock_execute.get(func_name)99 for func_name in100 ('execute_script',101 'execute_async_script')]102 mock_arg = MagicMock()103 result = self.instance._execute_client_script('SCRIPT', mock_arg,104 async=async)105 # the script was read correctly with resource_string()106 mock_resource_string.assert_called_once_with(107 'pytractor.mixins',108 '{}/{}.js'.format(CLIENT_SCRIPTS_DIR, 'SCRIPT')109 )110 # execute_async_script or execute_script were called (but not both)111 script_content = mock_resource_string.return_value.decode()112 if async:113 mock_execute_async_script.assert_called_once_with(script_content,114 mock_arg)115 self.assertEqual(len(mock_execute_script.mock_calls), 0)116 # the result is the one from execute_async_script()117 self.assertIs(result, mock_execute_async_script.return_value)118 else:119 mock_execute_script.assert_called_once_with(script_content,120 mock_arg)121 self.assertEqual(len(mock_execute_async_script.mock_calls), 0)122 # the result is the one from execute_script()123 self.assertIs(result, mock_execute_script.return_value)124 def test__execute_client_script_async(self):125 self.verify__execute_client_script_call(True)126 def test__execute_client_script_sync(self):127 self.verify__execute_client_script_call(False)128 def verify_function_executes_script_with(self, func_to_call,129 script_name, *script_args,130 **script_kwargs):131 with patch.object(132 self.instance, '_execute_client_script'133 ) as mock_execute_client_script:134 result = func_to_call()135 mock_execute_client_script.assert_called_once_with(136 script_name,137 *script_args, **script_kwargs138 )139 self.assertIs(result, mock_execute_client_script.return_value)140 def test_wait_for_angular(self):141 self.verify_function_executes_script_with(142 self.instance.wait_for_angular,143 'waitForAngular', self.mock_root_element, async=True144 )145 def test_wait_for_angular_does_not_call_script_if_ignore_synchronization(146 self147 ):148 """wait_for_angular() must not call the waitForAngular script, if149 ignore_synchronization is set to True."""150 self.instance.ignore_synchronization = True151 with patch.object(152 self.instance, '_execute_client_script'153 ) as mock_execute_client_script:154 self.instance.wait_for_angular()155 self.assertEqual(mock_execute_client_script.call_count, 0)156 def test__test_for_angular(self):157 self.instance._test_timeout = 5000158 self.verify_function_executes_script_with(159 self.instance._test_for_angular,160 'testForAngular', self.instance._test_timeout / 1000161 )162 def test__location_equals(self):163 with patch.object(164 self.instance, 'execute_script', create=True165 ) as mock_execute_script:166 mock_location = MagicMock()167 mock_execute_script.return_value = MagicMock(__eq__=MagicMock())168 result = self.instance._location_equals(mock_location)...
mixins.py
Source:mixins.py
...42 Command.CLEAR_ELEMENT43]44def angular_wait_required(wrapped):45 @wraps(wrapped)46 def wait_for_angular(driver, *args, **kwargs):47 driver.wait_for_angular()48 return wrapped(driver, *args, **kwargs)49 return wait_for_angular50class WebDriverMixin(object):51 """A mixin for Selenium Webdrivers."""52 ignore_synchronization = False53 """If True, pytractor will not attempt to synchronize with the page before54 performing actions. This can be harmful because pytractor will not wait55 until $timeouts and $http calls have been processed, which can cause56 tests to become flaky. This should be used only when necessary, such as57 when a page continuously polls an API using $timeout.58 """ # docstring adapted from protractor.js59 def __init__(self, base_url='', root_element='body', script_timeout=10,60 test_timeout=10, *args, **kwargs):61 self._base_url = base_url62 self._root_element = root_element63 self._test_timeout = test_timeout64 super(WebDriverMixin, self).__init__(*args, **kwargs)65 self.set_script_timeout(script_timeout)66 def _execute_client_script(self, script_name, *args, **kwargs):67 async = kwargs.pop('async', True)68 file_name = '{}.js'.format(script_name)69 js_script = resource_string(__name__,70 '{}/{}'.format(CLIENT_SCRIPTS_DIR,71 file_name))72 if js_script:73 js_script = js_script.decode('UTF-8')74 if async:75 result = self.execute_async_script(js_script, *args)76 else:77 result = self.execute_script(js_script, *args)78 return result79 def wait_for_angular(self):80 if self.ignore_synchronization:81 return82 else:83 return self._execute_client_script('waitForAngular',84 self._root_element,85 async=True)86 def execute(self, driver_command, params=None):87 # We also get called from WebElement methods/properties.88 if driver_command in COMMANDS_NEEDING_WAIT:89 self.wait_for_angular()90 return super(WebDriverMixin, self).execute(driver_command,91 params=params)92 def _test_for_angular(self):93 return self._execute_client_script('testForAngular',94 floor(self._test_timeout / 1000))95 def _location_equals(self, location):96 result = self.execute_script('return window.location.href')97 return result == location98 @property99 @angular_wait_required100 def current_url(self):101 return super(WebDriverMixin, self).current_url102 @property103 @angular_wait_required...
special_steps.py
Source:special_steps.py
...6def step_impl(context, sleep_time):7 time.sleep(int(sleep_time))8@step(r"I wait for angular")9def step_impl(context):...
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!!