def test_sort_by_multiple_nested_properties(self): objects = [ get_mock_object(globalRect=get_global_rect(x=x, y=y)) for x, y in zip(X_COORDS, Y_COORDS) ] sorted_objects = sort_by_keys( objects, ['globalRect.x', 'globalRect.y'] ) self.assertEqual(len(sorted_objects), len(objects)) self.assertEqual( self._get_child_property_from_object_list( sorted_objects, child='globalRect', prop='x' ), sorted(X_COORDS) ) self.assertEqual( self._get_child_property_from_object_list( sorted_objects, child='globalRect', prop='y' ), sorted(Y_COORDS) )
def test_sort_by_single_property(self): objects = [get_mock_object(y=y) for y in Y_COORDS] sorted_objects = sort_by_keys(objects, ['y']) self.assertEqual(len(sorted_objects), len(objects)) self.assertEqual( self._get_root_property_from_object_list(sorted_objects, 'y'), sorted(Y_COORDS) )
def test_sort_three_levels_nested_property(self): objects = [ get_mock_object( fake_property=get_global_rect( y=get_global_rect(y=y) ) ) for y in Y_COORDS ] sorted_objects = sort_by_keys(objects, ['fake_property.y.y']) self.assertEqual(len(sorted_objects), len(objects)) sorted_ys = [i.fake_property.y.y for i in sorted_objects] self.assertEqual(sorted_ys, sorted(Y_COORDS))
def select_many(self, type_name='*', ap_result_sort_keys=None, **kwargs): """Get a list of nodes from the introspection tree, with type equal to *type_name* and (optionally) matching the keyword filters present in *kwargs*. You must specify either *type_name*, keyword filters or both. This method searches recursively from the instance this method is called on. Calling :meth:`select_many` on the application (root) proxy object will search the entire tree. Calling :meth:`select_many` on an object in the tree will only search it's descendants. Example Usage:: app.select_many('QPushButton', enabled=True) # returns a list of QPushButtons that are enabled. As mentioned above, this method searches the object tree recursively:: file_menu = app.select_one('QMenu', title='File') file_menu.select_many('QAction') # returns a list of QAction objects who appear below file_menu in # the object tree. .. warning:: The order in which objects are returned is not guaranteed. It is bad practise to write tests that depend on the order in which this method returns objects. (see :ref:`object_ordering` for more information). If you want to ensure a certain count of results retrieved from this method, use :meth:`wait_select_many` or if you only want to get one item, use :meth:`select_single` instead. :param type_name: Either a string naming the type you want, or a class of the appropriate type (the latter case is for overridden emulator classes). :param ap_result_sort_keys: list of object properties to sort the query result with (sort key priority starts with element 0 as highest priority and then descends down the list). :raises ValueError: if neither *type_name* or keyword filters are provided. .. seealso:: Tutorial Section :ref:`custom_proxy_classes` """ instances = self._select_many(type_name, **kwargs) return sort_by_keys(instances, ap_result_sort_keys)
def test_sort_by_single_nested_property(self): objects = [ get_mock_object(globalRect=get_global_rect(y=y)) for y in Y_COORDS ] sorted_objects = sort_by_keys(objects, ['globalRect.y']) self.assertEqual(len(sorted_objects), len(objects)) self.assertEqual( self._get_child_property_from_object_list( sorted_objects, child='globalRect', prop='y' ), sorted(Y_COORDS) )
def test_sort_by_multiple_properties(self): objects = [ get_mock_object(x=x, y=y) for x, y in zip(X_COORDS, Y_COORDS) ] sorted_objects = sort_by_keys(objects, ['x', 'y']) self.assertEqual(len(sorted_objects), len(objects)) self.assertEqual( self._get_root_property_from_object_list(sorted_objects, 'x'), sorted(X_COORDS) ) self.assertEqual( self._get_root_property_from_object_list(sorted_objects, 'y'), sorted(Y_COORDS) )
def test_returns_unchanged_if_one_object(self): obj = [get_mock_object()] output = sort_by_keys(obj, ['x']) self.assertEqual(output, obj)
def wait_select_many(self, type_name='*', ap_query_timeout=10, ap_result_count=1, ap_result_sort_keys=None, **kwargs): """Get a list of nodes from the introspection tree, with type equal to *type_name* and (optionally) matching the keyword filters present in *kwargs*. This method is identical to the :meth:`select_many` method, except that this method will poll the application under test for *ap_query_timeout* seconds in the event that the search result count is not greater than or equal to *ap_result_count*. You must specify either *type_name*, keyword filters or both. This method searches recursively from the instance this method is called on. Calling :meth:`wait_select_many` on the application (root) proxy object will search the entire tree. Calling :meth:`wait_select_many` on an object in the tree will only search it's descendants. Example Usage:: app.wait_select_many( 'QPushButton', ap_query_timeout=5, ap_result_count=2, enabled=True ) # returns at least 2 QPushButtons that are enabled, within # 5 seconds. .. warning:: The order in which objects are returned is not guaranteed. It is bad practise to write tests that depend on the order in which this method returns objects. (see :ref:`object_ordering` for more information). :param type_name: Either a string naming the type you want, or a class of the appropriate type (the latter case is for overridden emulator classes). :param ap_query_timeout: Time in seconds to wait for search criteria to match. :param ap_result_count: Minimum number of results to return. :param ap_result_sort_keys: list of object properties to sort the query result with (sort key priority starts with element 0 as highest priority and then descends down the list). :raises ValueError: if neither *type_name* or keyword filters are provided. Also raises, if search result count does not match the number specified by *ap_result_count* within *ap_query_timeout* seconds. .. seealso:: Tutorial Section :ref:`custom_proxy_classes` """ exception_message = 'Failed to find the requested number of elements.' if ap_query_timeout <= 0: instances = self._select_many(type_name, **kwargs) if len(instances) < ap_result_count: raise ValueError(exception_message) return sort_by_keys(instances, ap_result_sort_keys) for i in range(ap_query_timeout): instances = self._select_many(type_name, **kwargs) if len(instances) >= ap_result_count: return sort_by_keys(instances, ap_result_sort_keys) sleep(1) raise ValueError(exception_message)