Example #1
0
class UiComponentTest(unittest.TestCase):
    """
    Unit test for UiComponent class.
    """
    def __init__(self, methodName='runTest'):
        super(UiComponentTest, self).__init__(methodName)
        self.driver = MockedWebDriver()

    def test_component_can_be_created_from_web_element(self):
        component = UIComponent(self.driver, 'a component', [By.ID, 'theId'])
        a_web_element = WebElement(None, '')
        ##
        component.from_web_element(a_web_element)
        ##
        assert_that(
            component.locate(), equal_to(a_web_element),
            "fromWebElement should store a reference to passed WebElement")

    def test_raise_error_when_trying_to_construct_from_wrong_type(self):
        def construct_from_foo():
            driver = MockedWebDriver()
            return UIComponent(driver, 'a component').from_web_element(Foo())

        assert_that(construct_from_foo, raises(TypeError),
                    "passing unexpected type should causes exception")

    def test_web_element_is_stored_with_caching(self):
        component = UIComponent(self.driver, 'a component', [By.ID, 'theid'])
        self.driver.set_dom_element([By.ID, 'theid'])
        element_first_time = component.cache().locate()
        ##
        self.driver.reset_dom_elements()
        self.driver.set_dom_element([By.ID, 'theid'])
        element_second_time = component.locate()
        ##
        assert_that(
            element_first_time, equal_to(element_second_time),
            "when cached web_element() should return always the same WebElement object"
        )

    def test_web_element_is_evaluated_every_time_without_caching(self):
        component = UIComponent(self.driver, 'a component', [By.ID, 'theid'])
        self.driver.set_dom_element([By.ID, 'theid'])
        element_first_time = component.locate()
        ##
        self.driver.reset_dom_elements()
        self.driver.set_dom_element([By.ID, 'theid'])
        element_second_time = component.locate()
        ##
        assert_that(
            element_first_time, is_not(equal_to(element_second_time)),
            "when cached web_element() should return always the same WebElement object"
        )

    def test_component_is_found_when_has_all_traits(self):
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        ##
        component.add_trait(lambda: True, 'always true')
        ##
        assert_that(component.is_found(), equal_to(True),
                    "component should be found when all traits are present")

    def test_component_is_not_found_when_trait_raises_an_exception(self):
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        ##
        component.add_trait(raise_exception, 'always true')
        ##
        assert_that(
            component.is_found(), equal_to(False),
            "component should not be found when evaluating traits throws an exception"
        )

    def test_is_present(self):
        self.driver.set_dom_element([By.ID, 'an_id'])
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])

        component.is_present()

        assert_that(self.driver.has_fulfilled_expectations(), equal_to(True),
                    "component should be able to locate element it is scope")

    def test_component_can_be_clicked(self):
        self.driver.set_dom_element([By.ID, 'an_id'])

        self.driver.set_expected_command(
            Command.CLICK_ELEMENT, {
                'sessionId': self.driver.session_id,
                'id': self.driver.get_id_for_stored_element([By.ID, 'an_id'])
            })

        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        ##
        component.click()
        ##
        assert_that(self.driver.has_fulfilled_expectations(), equal_to(True),
                    "clicking on component should result in click command")

    def test_component_can_be_hovered(self):
        self.driver.set_dom_element([By.ID, 'an_id'])
        self.driver.set_expected_command(
            Command.MOVE_TO, {
                'sessionId': self.driver.session_id,
                'element': self.driver.get_id_for_stored_element(
                    [By.ID, 'an_id'])
            })
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        ##
        component.hover()
        ##
        assert_that(self.driver.has_fulfilled_expectations(), equal_to(True),
                    "clicking on component should result in hoover command")

    def test_has_element(self):
        self.driver.set_dom_element([By.ID, 'an_id'])
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        self.driver.set_dom_element([By.XPATH, './/option'],
                                    parent_id=[By.ID, 'dropdownlocator'],
                                    children=1,
                                    return_values=[])

        self.driver.set_expected_command(
            Command.FIND_CHILD_ELEMENTS, {
                'using': By.ID,
                'value': self.driver.get_id_for_stored_element(
                    [By.ID, 'an_id']),
                'id': 'id',
                "sessionId": self.driver.session_id
            })
        ##
        component.has_element([By.ID, 'another_id'])
        ##
        assert_that(self.driver.has_fulfilled_expectations(), equal_to(True),
                    "component should be able to locate element it is scope")

    def test_can_cache_element(self):
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        component.cache()
        cached_element = MockedWebElement(self.driver, 'another_id')
        ##
        component._cache_web_element(cached_element)
        ##
        assert_that(component.locate(), equal_to(cached_element))
Example #2
0
class TableTest(unittest.TestCase):
    """
    Unit test for Table class.
    """

    def __init__(self, methodName='runTest'):
        super(TableTest, self).__init__(methodName)
        self.driver = MockedWebDriver()

    def setUp(self):
        self.driver.reset_dom_elements()

    def test_get_items_with_language(self):
        self.driver.set_dom_element([By.ID, 'table'])
        self.driver.set_dom_element([By.XPATH, './/tr'], parent_id=[By.ID, 'table'], children=4,
                                    return_values=[{'text': 'first'}, {'text': 'second'}, {'text': 'third'},
                                                   {'text': 'fourth'}])
        self.driver.set_expected_command(Command.GET_ELEMENT_TEXT, {'sessionId': self.driver.session_id,
                                                                    'id': self.driver.get_id_for_stored_element(
                                                                        [By.XPATH, './/tr'], 1)})
        self.driver.set_expected_command(Command.GET_ELEMENT_TEXT, {'sessionId': self.driver.session_id,
                                                                    'id': self.driver.get_id_for_stored_element(
                                                                        [By.XPATH, './/tr'], 2)})
        self.driver.set_expected_command(Command.GET_ELEMENT_TEXT, {'sessionId': self.driver.session_id,
                                                                    'id': self.driver.get_id_for_stored_element(
                                                                        [By.XPATH, './/tr'], 3)})
        self.driver.set_expected_command(Command.GET_ELEMENT_TEXT, {'sessionId': self.driver.session_id,
                                                                    'id': self.driver.get_id_for_stored_element(
                                                                        [By.XPATH, './/tr'], 4)})

        #
        items = Table(self.driver, 'table', [By.XPATH, './/tr'], ItemWithLanguage, 'item', [By.ID, 'table']).get_items()
        labels = [i.get_text() for i in items]

        assert_that(labels, equal_to(['first', 'second', 'third', 'fourth']),
                    "It should retrieve labels from stored elements")
        assert_that(self.driver.has_fulfilled_expectations(), equal_to(True),
                    "exercising get_items should result in calling Command.GET_ELEMENT_TEXT a number of times.")

    def test_detects_wrong_item_class(self):
        assert_that(calling(Table).with_args(self.driver, 'table', [By.ID, './/tr'], NonUiComponentItem, 'item',
                                             [By.ID, 'table']), raises(TypeError))

    def test_get_items_without_language(self):
        self.driver.set_dom_element([By.ID, 'table'])
        self.driver.set_dom_element([By.XPATH, './/tr'], parent_id=[By.ID, 'table'], children=4,
                                    return_values=[{'text': 'first'}, {'text': 'second'}, {'text': 'third'},
                                                   {'text': 'fourth'}])
        self.driver.set_expected_command(Command.GET_ELEMENT_TEXT, {'sessionId': self.driver.session_id,
                                                                    'id': self.driver.get_id_for_stored_element(
                                                                        [By.XPATH, './/tr'], 1)})
        self.driver.set_expected_command(Command.GET_ELEMENT_TEXT, {'sessionId': self.driver.session_id,
                                                                    'id': self.driver.get_id_for_stored_element(
                                                                        [By.XPATH, './/tr'], 2)})
        self.driver.set_expected_command(Command.GET_ELEMENT_TEXT, {'sessionId': self.driver.session_id,
                                                                    'id': self.driver.get_id_for_stored_element(
                                                                        [By.XPATH, './/tr'], 3)})
        self.driver.set_expected_command(Command.GET_ELEMENT_TEXT, {'sessionId': self.driver.session_id,
                                                                    'id': self.driver.get_id_for_stored_element(
                                                                        [By.XPATH, './/tr'], 4)})
        #
        items = Table(self.driver, 'table', [By.XPATH, './/tr'], Item, 'item', [By.ID, 'table']).get_items()
        #
        labels = [i.get_text() for i in items]

        assert_that(labels, equal_to(['first', 'second', 'third', 'fourth']),
                    "It should retrieve labels from stored elements")
        assert_that(self.driver.has_fulfilled_expectations(), equal_to(True),
                    "exercising get_items should result in calling Command.GET_ELEMENT_TEXT a number of times.")
Example #3
0
class UiComponentTest(unittest.TestCase):
    """
    Unit test for UiComponent class.
    """

    def __init__(self, methodName='runTest'):
        super(UiComponentTest, self).__init__(methodName)
        self.driver = MockedWebDriver()

    def test_component_can_be_created_from_web_element(self):
        component = UIComponent(self.driver, 'a component', [By.ID, 'theId'])
        a_web_element = WebElement(None, '')
        ##
        component.from_web_element(a_web_element)
        ##
        assert_that(component.locate(), equal_to(a_web_element),
                    "fromWebElement should store a reference to passed WebElement")

    def test_raise_error_when_trying_to_construct_from_wrong_type(self):
        def construct_from_foo():
            driver = MockedWebDriver()
            return UIComponent(driver, 'a component').from_web_element(Foo())
        assert_that(construct_from_foo, raises(TypeError), "passing unexpected type should causes exception")

    def test_web_element_is_stored_with_caching(self):
        component = UIComponent(self.driver, 'a component', [By.ID, 'theid'])
        self.driver.set_dom_element([By.ID, 'theid'])
        element_first_time = component.cache().locate()
        ##
        self.driver.reset_dom_elements()
        self.driver.set_dom_element([By.ID, 'theid'])
        element_second_time = component.locate()
        ##
        assert_that(element_first_time, equal_to(element_second_time),
                    "when cached web_element() should return always the same WebElement object")

    def test_web_element_is_evaluated_every_time_without_caching(self):
        component = UIComponent(self.driver, 'a component', [By.ID, 'theid'])
        self.driver.set_dom_element([By.ID, 'theid'])
        element_first_time = component.locate()
        ##
        self.driver.reset_dom_elements()
        self.driver.set_dom_element([By.ID, 'theid'])
        element_second_time = component.locate()
        ##
        assert_that(element_first_time, is_not(equal_to(element_second_time)),
                    "when cached web_element() should return always the same WebElement object")

    def test_component_is_found_when_has_all_traits(self):
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        ##
        component.add_trait(lambda: True, 'always true')
        ##
        assert_that(component.is_found(), equal_to(True), "component should be found when all traits are present")

    def test_component_is_not_found_when_trait_raises_an_exception(self):
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        ##
        component.add_trait(raise_exception, 'always true')
        ##
        assert_that(component.is_found(), equal_to(False),
                    "component should not be found when evaluating traits throws an exception")

    def test_is_present(self):
        self.driver.set_dom_element([By.ID, 'an_id'])
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])

        component.is_present()

        assert_that(self.driver.has_fulfilled_expectations(), equal_to(True),
                    "component should be able to locate element it is scope")

    def test_component_can_be_clicked(self):
        self.driver.set_dom_element([By.ID, 'an_id'])

        self.driver.set_expected_command(Command.CLICK_ELEMENT, {'sessionId': self.driver.session_id,
                                         'id': self.driver.get_id_for_stored_element([By.ID, 'an_id'])})

        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        ##
        component.click()
        ##
        assert_that(self.driver.has_fulfilled_expectations(), equal_to(True),
                    "clicking on component should result in click command")

    def test_component_can_be_hovered(self):
        self.driver.set_dom_element([By.ID, 'an_id'])
        self.driver.set_expected_command(Command.MOVE_TO, {'sessionId': self.driver.session_id,
                                                           'element': self.driver.get_id_for_stored_element(
                                                               [By.ID, 'an_id'])})
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        ##
        component.hover()
        ##
        assert_that(self.driver.has_fulfilled_expectations(), equal_to(True),
                    "clicking on component should result in hoover command")

    def test_has_element(self):
        self.driver.set_dom_element([By.ID, 'an_id'])
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        self.driver.set_dom_element([By.XPATH, './/option'], parent_id=[By.ID, 'dropdownlocator'], children=1,
                                    return_values=[])

        self.driver.set_expected_command(Command.FIND_CHILD_ELEMENTS, {'using': By.ID, 'value': self.driver.
                                         get_id_for_stored_element([By.ID, 'an_id']),
                                         'id': 'id', "sessionId": self.driver.session_id})
        ##
        component.has_element([By.ID, 'another_id'])
        ##
        assert_that(self.driver.has_fulfilled_expectations(), equal_to(True),
                    "component should be able to locate element it is scope")

    def test_can_cache_element(self):
        component = UIComponent(self.driver, 'a_component', [By.ID, 'an_id'])
        component.cache()
        cached_element = MockedWebElement(self.driver, 'another_id')
        ##
        component._cache_web_element(cached_element)
        ##
        assert_that(component.locate(), equal_to(cached_element))
Example #4
0
class TableTest(unittest.TestCase):
    """
    Unit test for Table class.
    """
    def __init__(self, methodName='runTest'):
        super(TableTest, self).__init__(methodName)
        self.driver = MockedWebDriver()

    def setUp(self):
        self.driver.reset_dom_elements()

    def test_get_items_with_language(self):
        self.driver.set_dom_element([By.ID, 'table'])
        self.driver.set_dom_element([By.XPATH, './/tr'],
                                    parent_id=[By.ID, 'table'],
                                    children=4,
                                    return_values=[{
                                        'text': 'first'
                                    }, {
                                        'text': 'second'
                                    }, {
                                        'text': 'third'
                                    }, {
                                        'text': 'fourth'
                                    }])
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId': self.driver.session_id,
                'id': self.driver.get_id_for_stored_element(
                    [By.XPATH, './/tr'], 1)
            })
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId': self.driver.session_id,
                'id': self.driver.get_id_for_stored_element(
                    [By.XPATH, './/tr'], 2)
            })
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId': self.driver.session_id,
                'id': self.driver.get_id_for_stored_element(
                    [By.XPATH, './/tr'], 3)
            })
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId': self.driver.session_id,
                'id': self.driver.get_id_for_stored_element(
                    [By.XPATH, './/tr'], 4)
            })

        #
        items = Table(self.driver, 'table', [By.XPATH, './/tr'],
                      ItemWithLanguage, 'item', [By.ID, 'table']).get_items()
        labels = [i.get_text() for i in items]

        assert_that(labels, equal_to(['first', 'second', 'third', 'fourth']),
                    "It should retrieve labels from stored elements")
        assert_that(
            self.driver.has_fulfilled_expectations(), equal_to(True),
            "exercising get_items should result in calling Command.GET_ELEMENT_TEXT a number of times."
        )

    def test_detects_wrong_item_class(self):
        assert_that(
            calling(Table).with_args(self.driver, 'table', [By.ID, './/tr'],
                                     NonUiComponentItem, 'item',
                                     [By.ID, 'table']), raises(TypeError))

    def test_get_items_without_language(self):
        self.driver.set_dom_element([By.ID, 'table'])
        self.driver.set_dom_element([By.XPATH, './/tr'],
                                    parent_id=[By.ID, 'table'],
                                    children=4,
                                    return_values=[{
                                        'text': 'first'
                                    }, {
                                        'text': 'second'
                                    }, {
                                        'text': 'third'
                                    }, {
                                        'text': 'fourth'
                                    }])
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId': self.driver.session_id,
                'id': self.driver.get_id_for_stored_element(
                    [By.XPATH, './/tr'], 1)
            })
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId': self.driver.session_id,
                'id': self.driver.get_id_for_stored_element(
                    [By.XPATH, './/tr'], 2)
            })
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId': self.driver.session_id,
                'id': self.driver.get_id_for_stored_element(
                    [By.XPATH, './/tr'], 3)
            })
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId': self.driver.session_id,
                'id': self.driver.get_id_for_stored_element(
                    [By.XPATH, './/tr'], 4)
            })
        #
        items = Table(self.driver, 'table', [By.XPATH, './/tr'], Item, 'item',
                      [By.ID, 'table']).get_items()
        #
        labels = [i.get_text() for i in items]

        assert_that(labels, equal_to(['first', 'second', 'third', 'fourth']),
                    "It should retrieve labels from stored elements")
        assert_that(
            self.driver.has_fulfilled_expectations(), equal_to(True),
            "exercising get_items should result in calling Command.GET_ELEMENT_TEXT a number of times."
        )
Example #5
0
class DropdownTest(unittest.TestCase):
    """
    Unit test for Dropdown class.
    """
    def __init__(self, methodName='runTest'):
        super(DropdownTest, self).__init__(methodName)
        self.driver = MockedWebDriver()

    def setUp(self):
        self.driver.reset_dom_elements()

    def test_dropdown_executes_select_option_by_name(self):
        self.driver.set_dom_elements([[By.ID, 'dropdownlocator'],
                                      [By.XPATH,
                                       "./option[text()='thename']"]])
        self.driver.set_expected_command(
            Command.CLICK_ELEMENT, {
                'sessionId':
                self.driver.session_id,
                'id':
                self.driver.get_id_for_stored_element(
                    [By.ID, 'dropdownlocator'])
            })
        self.driver.set_expected_command(
            Command.CLICK_ELEMENT, {
                'sessionId':
                self.driver.session_id,
                'id':
                self.driver.get_id_for_stored_element(
                    [By.XPATH, "./option[text()='thename']"])
            })
        #
        Dropdown(self.driver, 'dropdown',
                 [By.ID, 'dropdownlocator']).select_option_by_name('thename')
        assert_that(
            self.driver.has_fulfilled_expectations(), equal_to(True),
            "fetching option labels should result in executing Comman.CLICK and Command.FIND_CHILD_ELEMENTS"
        )

    def test_dropdown_executes_select_option_by_value(self):
        self.driver.set_dom_elements(
            [[By.ID, 'dropdownlocator'],
             [By.XPATH, "./option[@value='anoption']"]])

        self.driver.set_expected_command(
            Command.CLICK_ELEMENT, {
                'sessionId':
                self.driver.session_id,
                'id':
                self.driver.get_id_for_stored_element(
                    [By.ID, 'dropdownlocator'])
            })

        self.driver.set_expected_command(
            Command.CLICK_ELEMENT, {
                'sessionId':
                self.driver.session_id,
                'id':
                self.driver.get_id_for_stored_element(
                    [By.XPATH, "./option[@value='anoption']"])
            })
        #
        Dropdown(self.driver, 'dropdown',
                 [By.ID, 'dropdownlocator']).select_option_by_value('anoption')
        #
        assert_that(
            self.driver.has_fulfilled_expectations(), equal_to(True),
            "fetching option labels should result in executing Comman.CLICK and Command.FIND_CHILD_ELEMENTS"
        )

    def test_dropdown_executes_get_options_label(self):
        self.driver.set_dom_element([By.ID, 'dropdownlocator'])
        self.driver.set_dom_element([By.XPATH, './/option'],
                                    parent_id=[By.ID, 'dropdownlocator'],
                                    children=4,
                                    return_values=[{
                                        'text': 'first'
                                    }, {
                                        'text': 'second'
                                    }, {
                                        'text': 'third'
                                    }, {
                                        'text': 'fourth'
                                    }])
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId':
                self.driver.session_id,
                'id':
                self.driver.get_id_for_stored_element([By.XPATH, './/option'],
                                                      1)
            })
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId':
                self.driver.session_id,
                'id':
                self.driver.get_id_for_stored_element([By.XPATH, './/option'],
                                                      2)
            })
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId':
                self.driver.session_id,
                'id':
                self.driver.get_id_for_stored_element([By.XPATH, './/option'],
                                                      3)
            })
        self.driver.set_expected_command(
            Command.GET_ELEMENT_TEXT, {
                'sessionId':
                self.driver.session_id,
                'id':
                self.driver.get_id_for_stored_element([By.XPATH, './/option'],
                                                      4)
            })
        #
        labels = Dropdown(self.driver, 'dropdown',
                          [By.ID, 'dropdownlocator']).get_option_labels()
        #
        assert_that(labels, equal_to(['first', 'second', 'third', 'fourth']),
                    "It should retrieve labels from stored elements")