def switch_to_frame(self, frame_number, assert_it=True):
        """Switch to a frame
        :param frame_number: frame number to switch to
        :param assert_it: whether to assert switching to frame or not
        :return: None
        """
        try:
            self.context.driver.switch_to.frame(frame_number)
            self.context.logger.info("Successfully switched frame")
        except Exception as e:
            self.context.logger.info(
                "Frame not loaded yet! Waiting for another 10 seconds for frame to load..."
            )

            from time import sleep
            sleep(10)

            try:
                self.context.driver.switch_to.frame(frame_number)
                self.context.logger.info(
                    "Successfully switched to frame numbered '" +
                    str(frame_number) + "'")
            except Exception as e:
                self.context.logger.error("Unable to locate frame numbered '" +
                                          str(frame_number) +
                                          "' Error: %s" % e,
                                          exc_info=True)
                if assert_it:
                    Assert.assert_fail("Unable to locate frame numbered '" +
                                       str(frame_number) + "'")
    def type(self, locator, text, replacement=None):
        """Type text in locator
        :param locator: locator in which to type
        :param text: text to type
        :param replacement: if locator contains dynamic part, i.e. '$value',
        it will be replaced by replacement variable
        :return: None
        """
        if replacement:
            locator = locator.replace('$value', replacement)

        try:

            _element = self.fetch_element(locator)
            _element.clear()
            _element.send_keys(text)
            self.context.logger.info("Typed text '" + text + "' on element '" +
                                     locator + "'")
        except Exception as e:
            self.context.logger.error("Unable to type text '" + text +
                                      "' on element '" + locator +
                                      "'. Error: %s" % e,
                                      exc_info=True)
            Assert.assert_fail("Unable to type text '" + text +
                               "' on element '" + locator + "'")
    def select_by_visible_text(self,
                               locator,
                               option_text,
                               replacement=None,
                               retry_by_browser_refresh=False):
        """Select an option by visible option text
        :param locator: locator of select element
        :param replacement: if locator contains dynamic part, i.e. '$value',
        it will be replaced by replacement variable
        :param option_text: option text by which to select the option
        :param retry_by_browser_refresh: if set to True, when webdriver is not able to find any element,
        it will refresh the browser and try to find the element again
        :return: None
        """
        if replacement:
            locator = locator.replace('$value', replacement)

        try:
            select = Select(self.fetch_element(locator))
            select.select_by_visible_text(option_text)

            self.context.logger.info("Selected element '" + locator +
                                     "' by visible text '" + option_text + "'")
        except Exception as e:
            self.context.logger.error("Unable to select option '" +
                                      option_text + "'. Error: %s" % e,
                                      exc_info=True)
            Assert.assert_fail("Unable to select option '" + option_text + "'")
 def switch_to_default_content(self, assert_it=True):
     """Switch to parent window
     :return: None
     """
     try:
         self.context.driver.switch_to.default_content()
         self.context.logger.info("Successfully switched to default frame")
     except Exception as e:
         self.context.logger.error(
             "Unable to switch to default content! Error: %s" % e,
             exc_info=True)
         if assert_it:
             Assert.assert_fail("Unable to switch to default content!")
 def execute_java_script(self, script, element=None):
     """Execute raw java script statements
     :param script: java script to execute
     :param element: webdriver element on which to execute the java script
     :return: None
     """
     try:
         if element:
             return self.context.driver.execute_script(script, element)
         else:
             return self.context.driver.execute_script(script)
     except Exception as e:
         self.context.logger.error("Unable to execute java script '" +
                                   script + "'. Error: %s" % e,
                                   exc_info=True)
         Assert.assert_fail("Unable to execute java script '" + script +
                            "'")
    def submit(self, locator, replacement=None):
        """Submit a form
        :param locator: input submit button
        :param replacement: if locator contains dynamic part, i.e. '$value',
        it will be replaced by replacement variable
        :return: None
        """
        if replacement:
            locator = locator.replace('$value', replacement)

        try:
            _element = self.fetch_element(locator)
            _element.submit()
            self.context.logger.info("Submitted form'")
        except Exception as e:
            self.context.logger.error("Unable to submit form! Error: %s" % e,
                                      exc_info=True)
            Assert.assert_fail("Unable to submit form!")
    def hover(self, locator, replacement=None):
        """Mouse over on element
        :param locator: element locator
        :param replacement: if locator contains dynamic part, i.e. '$value',
        it will be replaced by replacement variable
        :return: None
        """
        if replacement is not None:
            locator = locator.replace('$value', replacement)

        try:
            element = self.fetch_element(locator)

            mouse_hover = ActionChains(
                self.context.driver).move_to_element(element)
            mouse_hover.perform()
        except Exception as e:
            self.context.logger.error("Unable to hover on locator '" +
                                      locator + "'. Error: %s" % e,
                                      exc_info=True)
            Assert.assert_fail("Unable to hover on locator '" + locator + "'")
    def uncheck(self, locator, replacement=None):
        """Uncheck element
        :param locator: element locator
        :param replacement: if locator contains dynamic part, i.e. '$value',
        it will be replaced by replacement variable
        :return: None
        """
        if replacement is not None:
            locator = locator.replace('$value', replacement)

        try:
            element = self.fetch_element(locator)
            if element.is_selected():
                element.click()
                self.context.logger.info(
                    "Unchecked checkbox having element '" + locator + "'")
        except Exception as e:
            self.context.logger.error("Unable to uncheck locator '" + locator +
                                      "'. Error: %s" % e,
                                      exc_info=True)
            Assert.assert_fail("Unable to uncheck locator '" + locator + "'")
    def press_key(self, locator, key, replacement=None):
        """Press keyboard key in locator
        :param locator: locator in which to type
        :param key: key to press
        :param replacement: this should replace the dynamic part in locator
        it will refresh the browser and try to find the element again
        :return: None
        """
        if replacement:
            locator = locator.replace('$value', replacement)

        try:
            self.fetch_element(locator).send_keys(key)
            self.context.logger.info("Pressed key '" + key + "' on element '" +
                                     locator + "'")
        except Exception as e:
            self.context.logger.error("Unable to press key '" + key +
                                      "' on element '" + locator +
                                      "'. Error: %s" % e,
                                      exc_info=True)
            Assert.assert_fail("Unable to press key '" + key +
                               "' on element '" + locator + "'")
    def click(self, locator, replacement=None, click_using_java_script=False):
        """Click on element
        :param locator: locator on which to click
        :param replacement: if locator contains dynamic part, i.e. '$value',
        it will be replaced by replacement variable
        :param click_using_java_script: whether to click using java script
        :return: None
        """
        if replacement:
            locator = locator.replace('$value', replacement)
        if click_using_java_script:
            _ele = self.fetch_element(locator)
            self.execute_java_script("arguments[0].click();", _ele)
            self.context.logger.info("Clicked on element '" + locator +
                                     "' using java script")
        else:
            try:
                strategy = locator.split(",")[0].strip()
                actual_locator = locator.replace(strategy + ",", "")

                timeout = int(
                    self.context.config['env']['element_fetch_timeout'])

                WebDriverWait(self.context.driver, timeout).until(
                    ec.element_to_be_clickable(
                        (getattr(By, strategy), actual_locator)))

                _ele = self.fetch_element(locator)

                if click_using_java_script:
                    self.execute_java_script("arguments[0].click();", _ele)
                else:
                    _ele.click()

                    self.context.logger.info("Clicked on element '" + locator +
                                             "'")

            except Exception as e:

                if 'safari' not in self.context.browser:
                    self.context.logger.info(
                        "Unable to click on element '" + locator +
                        "'. Trying to click using Action Chains")

                    try:
                        element = self.fetch_element(locator)

                        actions = ActionChains(self.context.driver)
                        actions.move_to_element(element)
                        actions.click(element)
                        actions.perform()

                        self.context.logger.info(
                            "Action Chains - Clicked on element '" + locator +
                            "'")
                    except Exception as e:
                        self.context.logger.error(
                            "Unable to click on element '" + locator +
                            "'. Error: %s" % e,
                            exc_info=True)
                        Assert.assert_fail("Unable to click on element '" +
                                           locator + "'")