Esempio n. 1
0
 def run(self, webdriver, params):
     """
     Set the text of an input
     :param webdriver:
     :param params:
     :return:
     """
     if 'of' in params and 'to' in params:
         strategy, selector = translate_selector(params['of'], webdriver=webdriver)
         return strategy(selector).send_keys(params['to'])
     else:
         raise DysonError("Keys \"of\" and \"to\" are required")
Esempio n. 2
0
 def run(self, webdriver, params):
     """
     Get the text of a specific element (return innerText)
     :param webdriver:
     :param params:
     :return:
     """
     if 'of' in params:
         strategy, selector = translate_selector(params['of'], webdriver)
         return strategy(selector).text
     else:
         raise DysonError("Key \"of\" is required")
Esempio n. 3
0
    def run(self, webdriver, params):
        """
        Click an element on the page
        :param webdriver:
        :param params:
        :return:
        """
        if len(params.keys()) > 0:
            selector, strategy = translate_selector(params, webdriver)

            if selector and strategy:
                return selector(strategy).click()
            else:
                raise DysonError("You need to specify a valid selector to click")
        else:
            raise DysonError("You need to specify an argument to \"click\"")
Esempio n. 4
0
    def run(self, webdriver, params):
        """
        Click an element on the page
        :param webdriver:
        :param params:
        :return:
        """
        if len(params.keys()) > 0:
            selector, strategy = translate_selector(params, webdriver)

            if selector and strategy:
                return selector(strategy).click()
            else:
                raise DysonError(
                    "You need to specify a valid selector to click")
        else:
            raise DysonError("You need to specify an argument to \"click\"")
Esempio n. 5
0
    def run(self, webdriver, params):
        """
        Get an attribute of an element
        :param webdriver:
        :param params:
        :return:
        """

        if 'of' in params:
            if 'attribute' in params:
                element = params['of']
                attribute = params['attribute']

                strategy, selector = translate_selector(element, webdriver)

                return strategy(selector).get_attribute(attribute)
            else:
                raise DysonError("Key \"attribute\" required")
        else:
            raise DysonError("Key \"of\" required")
    def run(self, webdriver, params):
        """
        Unchecks an element on the page.
        ONLY applies to checkbox's
        :param webdriver:
        :param params:
        :return:
        """
        if len(params.keys()) > 0:
            selector, strategy = translate_selector(params, webdriver)

            if selector and strategy:
                element = selector(strategy)

                if element.is_selected():
                    return selector(strategy).click()
            else:
                raise DysonError("You need to specify a valid selector to uncheck")
        else:
            raise DysonError("You need to specify an argument to \"uncheck\"")
Esempio n. 7
0
    def run(self, webdriver, params):
        """
        Unchecks an element on the page.
        ONLY applies to checkbox's
        :param webdriver:
        :param params:
        :return:
        """
        if len(params.keys()) > 0:
            selector, strategy = translate_selector(params, webdriver)

            if selector and strategy:
                element = selector(strategy)

                if element.is_selected():
                    return selector(strategy).click()
            else:
                raise DysonError(
                    "You need to specify a valid selector to uncheck")
        else:
            raise DysonError("You need to specify an argument to \"uncheck\"")
Esempio n. 8
0
    def run(self, webdriver, params):
        """
        Collection of switch_to actions with selenium.
        Available actions:
        - switch_to
            - frame <selector>
            - alert
                - action
                    - dismiss
                    - accept
                    - send_keys
                    - authenticate
        :param webdriver:
        :param params:
        :return:
        """

        if isinstance(params, dict):
            if 'frame' in params:
                """
                Switch to a frame / iframe
                """
                selector, strategy = translate_selector(params['frame'], webdriver)
                return webdriver.switch_to.frame(selector(strategy))

            elif 'alert' in params:
                """
                Switch to an alert window
                """
                if 'action' in params['alert']:
                    alert_action = params['alert']['action']
                    valid_actions = frozenset(['accept', 'dismiss', 'authenticate', 'send_keys'])

                    if alert_action in valid_actions:
                        if 'accept' is alert_action:
                            return webdriver.switch_to.alert.accept()
                        elif 'dismiss' is alert_action:
                            return webdriver.switch_to.alert.dismiss()
                        elif 'get_text' is alert_action:
                            return webdriver.switch_to.alert.text
                    else:
                        raise DysonError("Invalid action \"%s\". Valid actions are %s" %
                                  (alert_action, ','.join(valid_actions)))
                elif 'username' in params['alert']:
                    username = params['alert']['username']
                    password = ""

                    if params['alert']['password']:
                        password = params['alert']['password']

                    return webdriver.switch_to.alert.authenticate(username, password)
                else:
                    return webdriver.switch_to.alert()
            elif 'window' in params:
                return webdriver.switch_to.window(params['window'])
            else:
                raise DysonError("Unsure how to switch to \"%s\". Valid options are %s" % (params, ','.join(self.ACTIONS)))

        elif isinstance(params, string_types):
            if "default_content" == params:
                return webdriver.switch_to.default_content()
            else:
                raise DysonError("Unsure how to switch to \"%s\". Valid options are %s" % (params, ','.join(self.ACTIONS)))
Esempio n. 9
0
    def run(self, webdriver, params):
        """
        Validate things
        :param webdriver:
        :param params:
            allowed validations:
                - title
                    - is
                    - is_not
                - present
                - not_present
                - text_of
                    - is
                    - is_not
                - value_of
                    - is
                    - is_not
                - is_checked
                - is_unchecked
        :return:
        """
        if params and isinstance(params, dict):
            if 'title' in params:
                """
                Validate the title_shouldbe of the page
                """
                actual_title = webdriver.title
                title_shouldbe = params['title']
                if 'is' in title_shouldbe:
                    if actual_title != title_shouldbe:
                        self.fail("Title is not \"%s\". Actual: \"%s\"" %
                                  (title_shouldbe, actual_title))

                elif 'is_not' in title_shouldbe:
                    if actual_title == title_shouldbe:
                        self.fail("Title is \"%s\" when it shouldn't be" %
                                  title_shouldbe)

            if 'present' in params:
                """
                Validate the presence of an element
                """
                strategy, selector = translate_selector(
                    params['present'], webdriver)
                return strategy(selector)

            if 'not_present' in params:
                """
                Validate the absence of an element
                """
                element = None
                strategy, selector = translate_selector(
                    params['not_present'], webdriver)
                return strategy(selector)

            if 'text_of' in params:
                """
                Validate text (or innerText) of an element
                """
                text_of = params['text_of']
                if 'element' not in text_of:
                    raise DysonError("Key \"element\" is required")

                if 'is' in text_of:
                    strategy, selector = translate_selector(
                        text_of['element'], webdriver)
                    actual_text = strategy(selector).text
                    if actual_text != text_of['is']:
                        self.fail(
                            "Text of %s is not \"%s\".  Actual: \"%s\"" %
                            (text_of['element'], text_of['is'], actual_text))
                    else:
                        return actual_text
                elif 'is_not' in text_of:
                    strategy, selector = translate_selector(
                        text_of['element'], webdriver)
                    actual_text = strategy(selector).text
                    if actual_text == text_of['is_not']:
                        self.fail("Text of %s is \"%s\" when it shouldn't be" %
                                  (text_of['element'], text_of['is_not']))
                    else:
                        return actual_text

            if 'value_of' in params:
                """
                Validate value attribute of an element
                """
                value_of = params['value_of']
                if 'element' not in value_of:
                    raise DysonError("Key \"element\" is required")

                if 'is' in value_of:
                    strategy, selector = translate_selector(
                        value_of['element'], webdriver)
                    actual_value = strategy(selector).get_attribute('value')
                    if actual_value != value_of['is']:
                        self.fail("Value of %s is not \"%s\". Actual: \"%s\"" %
                                  (value_of['element'], value_of['is'],
                                   actual_value))
                    else:
                        return actual_value
                elif 'is_not' in value_of:
                    strategy, selector = translate_selector(
                        value_of['element'], webdriver)
                    actual_value = strategy(selector).get_attribute('value')
                    if actual_value == value_of['is']:
                        self.fail(
                            "Value of %s is \"%s\" when it shouldn't be" %
                            (value_of['element'], value_of['is']))
                    else:
                        return actual_value

            if 'is_checked' in params:
                """
                Validate that a checkbox / radio button is checked
                """
                element = params['is_checked']
                strategy, selector = translate_selector(element, webdriver)
                status = strategy(selector).is_selected()
                if not status:
                    self.fail("Element %s is not checked" % element)
                else:
                    return status

            if 'is_not_checked' in params:
                """
                Validate that a checkbox / radio button is unchecked
                """
                element = params['is_not_checked']
                strategy, selector = translate_selector(element, webdriver)

                status = strategy(selector).is_selected()
                if status:
                    self.fail("Element %s is checked" % element)
                else:
                    return status

        elif isinstance(params, string_types):
            result = eval(params)
            if not to_boolean(result):
                self.fail("\"%s\" has a result of False" % params)
            return result
Esempio n. 10
0
    def run(self, webdriver, params):
        """
        Validate things
        :param webdriver:
        :param params:
            allowed validations:
                - title
                    - is
                    - is_not
                - present
                - not_present
                - text_of
                    - is
                    - is_not
                - value_of
                    - is
                    - is_not
                - is_checked
                - is_unchecked
        :return:
        """
        if params and isinstance(params, dict):
            if 'title' in params:
                """
                Validate the title_shouldbe of the page
                """
                actual_title = webdriver.title
                title_shouldbe = params['title']
                if 'is' in title_shouldbe:
                    if actual_title != title_shouldbe:
                        self.fail("Title is not \"%s\". Actual: \"%s\"" % (title_shouldbe, actual_title))

                elif 'is_not' in title_shouldbe:
                    if actual_title == title_shouldbe:
                        self.fail("Title is \"%s\" when it shouldn't be" % title_shouldbe)

            if 'present' in params:
                """
                Validate the presence of an element
                """
                strategy, selector = translate_selector(params['present'], webdriver)
                return strategy(selector)

            if 'not_present' in params:
                """
                Validate the absence of an element
                """
                element = None
                strategy, selector = translate_selector(params['not_present'], webdriver)
                return strategy(selector)

            if 'text_of' in params:
                """
                Validate text (or innerText) of an element
                """
                text_of = params['text_of']
                if 'element' not in text_of:
                    raise DysonError("Key \"element\" is required")

                if 'is' in text_of:
                    strategy, selector = translate_selector(text_of['element'], webdriver)
                    actual_text = strategy(selector).text
                    if actual_text != text_of['is']:
                        self.fail("Text of %s is not \"%s\".  Actual: \"%s\"" %
                                  (text_of['element'], text_of['is'], actual_text))
                    else:
                        return actual_text
                elif 'is_not' in text_of:
                    strategy, selector = translate_selector(text_of['element'], webdriver)
                    actual_text = strategy(selector).text
                    if actual_text == text_of['is_not']:
                        self.fail("Text of %s is \"%s\" when it shouldn't be" % (
                            text_of['element'], text_of['is_not']
                        ))
                    else:
                        return actual_text

            if 'value_of' in params:
                """
                Validate value attribute of an element
                """
                value_of = params['value_of']
                if 'element' not in value_of:
                    raise DysonError("Key \"element\" is required")

                if 'is' in value_of:
                    strategy, selector = translate_selector(value_of['element'], webdriver)
                    actual_value = strategy(selector).get_attribute('value')
                    if actual_value != value_of['is']:
                        self.fail("Value of %s is not \"%s\". Actual: \"%s\"" % (
                            value_of['element'], value_of['is'], actual_value
                        ))
                    else:
                        return actual_value
                elif 'is_not' in value_of:
                    strategy, selector = translate_selector(value_of['element'], webdriver)
                    actual_value = strategy(selector).get_attribute('value')
                    if actual_value == value_of['is']:
                        self.fail("Value of %s is \"%s\" when it shouldn't be" % (
                            value_of['element'], value_of['is']
                        ))
                    else:
                        return actual_value

            if 'is_checked' in params:
                """
                Validate that a checkbox / radio button is checked
                """
                element = params['is_checked']
                strategy, selector = translate_selector(element, webdriver)
                status = strategy(selector).is_selected()
                if not status:
                    self.fail("Element %s is not checked" % element)
                else:
                    return status

            if 'is_not_checked' in params:
                """
                Validate that a checkbox / radio button is unchecked
                """
                element = params['is_not_checked']
                strategy, selector = translate_selector(element, webdriver)

                status = strategy(selector).is_selected()
                if status:
                    self.fail("Element %s is checked" % element)
                else:
                    return status

        elif isinstance(params, string_types):
            result = eval(params)
            if not to_boolean(result):
                self.fail("\"%s\" has a result of False" % params)
            return result
    def run(self, webdriver, params):
        """
        Collection of switch_to actions with selenium.
        Available actions:
        - switch_to
            - frame <selector>
            - alert
                - action
                    - dismiss
                    - accept
                    - send_keys
                    - authenticate
        :param webdriver:
        :param params:
        :return:
        """

        if isinstance(params, dict):
            if 'frame' in params:
                """
                Switch to a frame / iframe
                """
                selector, strategy = translate_selector(params['frame'], webdriver)
                return webdriver.switch_to.frame(selector(strategy))

            elif 'alert' in params:
                """
                Switch to an alert window
                """
                if 'action' in params['alert']:
                    alert_action = params['alert']['action']
                    valid_actions = frozenset(['accept', 'dismiss', 'authenticate', 'send_keys'])

                    if alert_action in valid_actions:
                        if 'accept' is alert_action:
                            return webdriver.switch_to.alert.accept()
                        elif 'dismiss' is alert_action:
                            return webdriver.switch_to.alert.dismiss()
                        elif 'get_text' is alert_action:
                            return webdriver.switch_to.alert.text
                    else:
                        raise DysonError("Invalid action \"%s\". Valid actions are %s" %
                                  (alert_action, ','.join(valid_actions)))
                elif 'username' in params['alert']:
                    username = params['alert']['username']
                    password = ""

                    if params['alert']['password']:
                        password = params['alert']['password']

                    return webdriver.switch_to.alert.authenticate(username, password)
                else:
                    return webdriver.switch_to.alert()
            else:
                raise DysonError("Unsure how to switch to \"%s\". Valid options are %s" % (params, ','.join(self.ACTIONS)))

        elif isinstance(params, string_types):
            if "default_content" == params:
                return webdriver.switch_to.default_content()
            else:
                raise DysonError("Unsure how to switch to \"%s\". Valid options are %s" % (params, ','.join(self.ACTIONS)))