Best Python code snippet using pytractor_python
test_mixins.py
Source:test_mixins.py
...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)...
mixins.py
Source:mixins.py
...62 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_required104 def page_source(self):105 return super(WebDriverMixin, self).page_source106 @property107 @angular_wait_required108 def title(self):109 return super(WebDriverMixin, self).title110 @property111 @angular_wait_required112 def location_abs_url(self):113 return self._execute_client_script('getLocationAbsUrl',114 self._root_element, async=False)115 @angular_wait_required116 def find_elements_by_repeater(self, descriptor, using=None):117 return self._execute_client_script('findAllRepeaterRows',118 descriptor, False, using,119 async=False)120 @angular_wait_required121 def find_element(self, *args, **kwargs):122 return super(WebDriverMixin, self).find_element(*args, **kwargs)123 @angular_wait_required124 def find_elements(self, *args, **kwargs):125 return super(WebDriverMixin, self).find_elements(*args, **kwargs)126 @angular_wait_required127 def find_elements_by_binding(self, descriptor, using=None):128 elements = self._execute_client_script('findBindings', descriptor,129 False, using, async=False)130 return elements131 def find_element_by_binding(self, descriptor, using=None):132 elements = self.find_elements_by_binding(descriptor, using=using)133 if len(elements) == 0:134 raise NoSuchElementException(135 "No element found for binding descriptor"136 " '{}'".format(descriptor)137 )138 else:139 return elements[0]140 def find_element_by_exact_binding(self, descriptor, using=None):141 elements = self.find_elements_by_exact_binding(descriptor, using=using)142 if len(elements) == 0:143 raise NoSuchElementException(144 "No element found for binding descriptor"145 " '{}'".format(descriptor)146 )147 else:148 return elements[0]149 @angular_wait_required150 def find_elements_by_exact_binding(self, descriptor, using=None):151 elements = self._execute_client_script('findBindings', descriptor,152 True, using, async=False)153 return elements154 def find_element_by_model(self, descriptor, using=None):155 elements = self.find_elements_by_model(descriptor, using=using)156 if len(elements) == 0:157 raise NoSuchElementException(158 "No element found for model descriptor"159 " {}".format(descriptor)160 )161 else:162 return elements[0]163 @angular_wait_required164 def find_elements_by_model(self, descriptor, using=None):165 elements = self._execute_client_script('findByModel', descriptor,166 using, async=False)167 # Workaround for issue #10: findByModel.js returns None instead of empty168 # list if no element has been found.169 if elements is None:170 elements = []171 return elements172 def get(self, url):173 super(WebDriverMixin, self).get('about:blank')174 full_url = urljoin(str(self._base_url), str(url))175 self.execute_script(176 """177 window.name = "{}" + window.name;178 window.location.replace("{}");179 """.format(DEFER_LABEL, full_url)180 )181 wait = WebDriverWait(self, self._test_timeout)182 wait.until_not(self._location_equals, 'about:blank')183 if not self.ignore_synchronization:184 test_result = self._test_for_angular()185 angular_on_page = test_result[0]186 if not angular_on_page:187 message = test_result[1]188 raise AngularNotFoundException(189 'Angular could not be found on page: {}:'190 ' {}'.format(full_url, message)191 )192 # TODO: inject scripts here193 # return self.execute_script(194 # 'angular.resumeBootstrap(arguments[0]);'195 # )196 self.execute_script('angular.resumeBootstrap();')197 def refresh(self):198 url = self.execute_script('return window.location.href')199 self.get(url)200 @angular_wait_required201 def set_location(self, url):202 result = self._execute_client_script('setLocation', self._root_element,203 url, async=False)...
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!!