예제 #1
0
    def __init__(self):
        self.driver = None
        self.action_chains = ActionChains(self.driver)
        self.currentElement = None
        self.listOfElements = []
        self.desired_caps = {}
        self.driver_factory = DriverFactory()
        self.implicit_wait = 10

        super(Core, self)
예제 #2
0
class Core(object):
    def __init__(self):
        self.driver = None
        self.action_chains = ActionChains(self.driver)
        self.currentElement = None
        self.listOfElements = []
        self.desired_caps = {}
        self.driver_factory = DriverFactory()
        self.implicit_wait = 10

        super(Core, self)

    def set_driver(self):
        """This method sets the driver the driver -- Should only be used by go_to_url(browser) method
        """
        self.driver = self.driver_factory.set_driver()

    def set_browser_type(self, browser):
        self.driver_factory.set_browser_type(browser)

    def go_to_url(self, url):
        if self.driver is None:
            self.driver = self.driver_factory.set_driver()
        try:

            log("Navigating to page: " + url)
            self.driver.get(url)
            return True
        except Exception as e:
            fail("Could not navigate to url: " + url)
            return False

    def get_current_page_url(self):
        return self.driver.current_url

    def take_screenshot(self, name):
        try:
            self.driver.get_screenshot_as_file(name + '.png')
            return True
        except IOError as e:
            error("Unable to save screenshot: " + e)
            return False

    # Methods to select objects
    def reset_current_element(self):
        self.currentElement = None
        return True

    def select_default_object(self):
        """Sets current element as the default element(Html/body) which contains all the elements in the HTML's body"""
        try:
            debug("Selecting root element")
            self.currentElement = self.driver.find_element_by_xpath("/html/body")
            return True
        except NoSuchElementException:
            error("Unable to select html/body element")
            return False

    def select_element_by_id(self, id):
        """Select an Element by ID
        :param id: element id value
        """
        try:
            if self.currentElement is not None:
                self.currentElement.find_element_by_id(id)
            else:
                self.currentElement = self.driver.find_element_by_id(id)
            debug("Element selected by id: " + id)
            return True
        except NoSuchElementException:
            error("Element with id: " + id + " not found")
            return False

    def select_element_by_class_name(self, class_name):
        """Select an Element by class name
        :param class_name: element class value
        """
        try:
            if self.currentElement is not None:
                self.currentElement.find_element_by_class_name(class_name)
            else:
                self.currentElement = self.driver.find_element_by_class_name(class_name)
            debug("Element selected by class: " + class_name)
            return True
        except NoSuchElementException:
            error("Element with class: " + class_name + " not found")
            return False

    def select_element_by_tag_name(self, tag_name):
        """Select an Element by tag name
        :param tag_name: element tag value
        """
        try:
            if self.currentElement is not None:
                self.currentElement.find_element_by_tag_name(tag_name)
            else:
                self.currentElement = self.driver.find_element_by_tag_name(tag_name)
            debug("Element selected by tag name: " + tag_name)
            return True
        except NoSuchElementException:
            error("Element with tag name: " + tag_name + " not found")
            return False

    def select_element_by_xpath(self, xpath):
        """Select an Element by Class Name
        :param xpath:
        """
        try:
            if self.currentElement is not None:
                self.currentElement.find_element_by_xpath(xpath)
            else:
                self.currentElement = self.driver.find_element_by_xpath(xpath)
            debug("Element selected by xpath: " + xpath)
            return True
        except NoSuchElementException:
            error("Element with xpath: " + xpath + " not found")
            return False

    def select_element_by_css_selector(self, css_selector):
        """Select an Element by Class Name
        :param css_selector:
        """
        try:
            if self.currentElement is not None:
                self.currentElement.find_element_by_css_selector(css_selector)
            else:
                self.currentElement = self.driver.find_element_by_css_selector(css_selector)
            debug("Element selected by css selector: " + css_selector)
            return True
        except NoSuchElementException:
            error("Element with css selector: " + css_selector + " not found")
            return False

    def select_element_by_link_text(self, link):
        """Select an Element by link text
        :param link:
        """
        try:
            if self.currentElement is not None:
                self.currentElement.find_element_by_link_text(link)
            else:
                self.currentElement = self.driver.find_element_by_link_text(link)
            debug("Element selected by link: " + link)
            return True
        except NoSuchElementException:
            error("Element with link: " + link + " not found")
            return False

    def select_element_by_partial_link_text(self, link):
        """Select an Element by partial link text
        :param link: element partial link text
        """
        try:
            if self.currentElement is not None:
                self.currentElement.find_element_by_partial_link_text(link)
            else:
                self.currentElement = self.driver.find_element_by_partial_link_text(link)
            debug("Element selected by partial link text: " + link)
            return True
        except NoSuchElementException:
            error("Element with partial link text: " + link + " not found")
            return False

    def select_element_by_name(self, name):
        """Select an Element by Class Name
        :param name: element name value
        """
        try:
            if self.currentElement is not None:
                self.currentElement.find_element_by_name(name)
            else:
                self.currentElement = self.driver.find_element_by_name(name)
            debug("Element selected by name: " + name)
            return True
        except NoSuchElementException:
            error("Element with name: %s not found" % name)
            return False

    def select_parent_element(self):
        try:
            if self.currentElement is not None:
                self.currentElement.find_element_by_xpath("..")
            else:
                self.currentElement = self.currentElement.find_element_by_xpath("..")
            debug("Parent Element selected")
            return True
        except NoSuchElementException:
            error("Parent Element not found")
            return False

    '''
    Wait for elements to match condition
    '''

    def wait_for_condition(self, condition):
        return WebDriverWait(self.driver, self.implicit_wait).until(condition)

    def condition_element_displayed(self, locator_strategy):
        return self.wait_for_condition(expected_conditions.visibility_of_element_located(locator_strategy))

    def condition_element_invisible(self, locator_strategy):
        return self.wait_for_condition(expected_conditions.invisibility_of_element_located(locator_strategy))

    def condition_element_present(self, locator_strategy):
        return self.wait_for_condition(expected_conditions.presence_of_element_located(locator_strategy))

    def wait_for_element_to_be_displayed_by_id(self, id):
        debug("Waiting for element by id '%s' to be displayed" % id)
        try:
            self.condition_element_displayed(by_id(id))
            debug("Element found by id '%s'" % id)
            return True
        except(NoSuchElementException, TimeoutException):
            error("Unable to find element by id '%s'" % id)
            return False

    def wait_for_element_to_be_invisible_by_id(self, id):
        debug("Waiting for element by id '%s' to disappear" % id)
        try:
            self.condition_element_invisible(by_id(id))
            debug("Element by id '%s' is not displayed" % id)
            return True
        except(NoSuchElementException, TimeoutException):
            error("After waiting %s seconds, the element by id '%s' is still visible"
                  % (str(self.implicit_wait), id))
            return False

    def wait_for_element_to_be_present_by_id(self, id):
        debug("Waiting for element by id '%s' to be present" % id)
        try:
            self.condition_element_present(by_id(id))
            debug("Element present by id '%s'" % id)
            return True
        except(NoSuchElementException, TimeoutException):
            error("Unable to find element by id '%s'" % id)
            return False

    def wait_for_element_to_be_displayed_by_class_name(self, class_name):
        debug("Waiting for element by class name '%s' to be displayed" % class_name)
        try:
            WebDriverWait(self.driver, self.implicit_wait).until(expected_conditions.visibility_of_element_located(
                (By.CLASS_NAME, class_name)))
            debug("Element found by class name '%s'" % class_name)
            return True
        except(NoSuchElementException, TimeoutException):
            error("Unable to find element by class name '%s'" % class_name)
            return False

    def wait_for_element_to_be_invisible_by_class_name(self, class_name):
        debug("Waiting for element by class name '%s' to disappear" % class_name)
        try:
            WebDriverWait(self.driver, self.implicit_wait).until(expected_conditions.invisibility_of_element_located(
                (By.CLASS_NAME, class_name)))
            debug("Element by class name '%s' is not displayed" % class_name)
            return True
        except(NoSuchElementException, TimeoutException):
            error("After waiting %s seconds, the element by class name '%s' is still visible"
                  % (str(self.implicit_wait), class_name))
            return False

    def wait_for_element_to_be_present_by_class_name(self, class_name):
        debug("Waiting for element by class name '%s' to be present" % class_name)
        try:
            WebDriverWait(self.driver, self.implicit_wait).until(expected_conditions.presence_of_element_located(
                (By.CLASS_NAME, class_name)))
            debug("Element present by class name '%s'" % class_name)
            return True
        except(NoSuchElementException, TimeoutException):
            error("Unable to find element by class name '%s'" % class_name)
            return False

    def wait_for_element_to_be_displayed_by_name(self, name):
        debug("Waiting for element by name '%s' to be displayed" % name)
        try:
            WebDriverWait(self.driver, self.implicit_wait).until(expected_conditions.visibility_of_element_located(
                (By.NAME, name)))
            debug("Element found by name '%s'" % name)
            return True
        except(NoSuchElementException, TimeoutException):
            error("Unable to find element by name '%s'" % name)
            return False

    def wait_for_element_to_be_invisible_by_name(self, name):
        debug("Waiting for element by name '%s' to disappear" % name)
        try:
            WebDriverWait(self.driver, self.implicit_wait).until(expected_conditions.invisibility_of_element_located(
                (By.NAME, name)))
            debug("Element by name '%s' is not displayed" % name)
            return True
        except(NoSuchElementException, TimeoutException):
            error("After waiting %s seconds, the element by name '%s' is still visible"
                  % (str(self.implicit_wait), name))
            return False

    def wait_for_element_to_be_present_by_name(self, name):
        debug("Waiting for element by name '%s' to be present" % name)
        try:
            WebDriverWait(self.driver, self.implicit_wait).until(expected_conditions.presence_of_element_located(
                (By.NAME, name)))
            debug("Element present by name '%s'" % name)
            return True
        except(NoSuchElementException, TimeoutException):
            error("Unable to find element by name '%s'" % name)
            return False

    def wait_for_element_to_be_displayed_by_link_text(self, link_text):
        debug("Waiting for element by link text '%s' to be displayed" % link_text)
        try:
            WebDriverWait(self.driver, self.implicit_wait).until(expected_conditions.visibility_of_element_located(
                (By.LINK_TEXT, link_text)))
            debug("Element found by link text '%s'" % link_text)
            return True
        except(NoSuchElementException, TimeoutException):
            error("Unable to find element by link text '%s'" % link_text)
            return False

    def wait_for_element_to_be_invisible_by_link_text(self, link_text):
        debug("Waiting for element by link text '%s' to disappear" % link_text)
        try:
            WebDriverWait(self.driver, self.implicit_wait).until(expected_conditions.invisibility_of_element_located(
                (By.LINK_TEXT, link_text)))
            debug("Element by link text '%s' is not displayed" % link_text)
            return True
        except(NoSuchElementException, TimeoutException):
            error("After waiting %s seconds, the element by link text '%s' is still visible"
                  % (str(self.implicit_wait), link_text))
            return False

    def wait_for_element_to_be_present_by_link_text(self, link_text):
        debug("Waiting for element by link text '%s' to be present" % link_text)
        try:
            WebDriverWait(self.driver, self.implicit_wait).until(expected_conditions.presence_of_element_located(
                (By.LINK_TEXT, link_text)))
            debug("Element present by link text '%s'" % link_text)
            return True
        except(NoSuchElementException, TimeoutException):
            error("Unable to find element by link text '%s'" % link_text)
            return False
    
    

    

    # Methods to build a list of elements
    def build_element_list_by_id(self, id):
        if self.currentElement is not None:
            self.listOfElements = self.currentElement.find_elements_by_id(id)
        else:
            self.listOfElements = self.driver.find_elements_by_id(id)
        if self.get_list_size() > 0:
            debug("List built by id: " + id)
            return True
        else:
            error("No elements found by id: " + id)
            return False

    def build_element_list_by_class_name(self, class_name):
        if self.currentElement is not None:
            self.listOfElements = self.currentElement.find_elements_by_class_name(class_name)
        else:
            self.listOfElements = self.driver.find_elements_by_class_name(class_name)
        if self.get_list_size() > 0:
            debug("List built by id: " + class_name)
            return True
        else:
            error("No elements found by id: " + class_name)
            return False

    def build_element_list_by_tag_name(self, tag_name):
        if self.currentElement is not None:
            self.listOfElements = self.currentElement.find_elements_by_tag_name(tag_name)
        else:
            self.listOfElements = self.driver.find_elements_by_tag_name(tag_name)
        if self.get_list_size() > 0:
            debug("List built by tag name: " + tag_name)
            return True
        else:
            error("No elements found by tag name: " + tag_name)
            return False

    def build_element_list_by_xpath(self, xpath):
        if self.currentElement is not None:
            self.listOfElements = self.currentElement.find_elements_by_xpath(xpath)
        else:
            self.listOfElements = self.driver.find_elements_by_xpath(xpath)
        if self.get_list_size() > 0:
            debug("List built by id: " + xpath)
            return True
        else:
            error("No elements found by id: " + xpath)
            return False

    def build_element_list_by_css_selector(self, css_selector):
        if self.currentElement is not None:
            self.listOfElements = self.currentElement.find_elements_by_css_selector(css_selector)
        else:
            self.listOfElements = self.driver.find_elements_by_css_selector(css_selector)
        if self.get_list_size() > 0:
            debug("List built by id: " + css_selector)
            return True
        else:
            error("No elements found by id: " + css_selector)
            return False

    def build_element_list_by_link_text(self, link):
        if self.currentElement is not None:
            self.listOfElements = self.currentElement.find_elements_by_link_text(link)
        else:
            self.listOfElements = self.driver.find_elements_by_link_text(link)
        if self.get_list_size() > 0:
            debug("List built by id: " + link)
            return True
        else:
            error("No elements found by id: " + link)
            return False

    def build_element_list_by_partial_link_text(self, link):
        if self.currentElement is not None:
            self.listOfElements = self.currentElement.find_elements_by_partial_link_text(link)
        else:
            self.listOfElements = self.driver.find_elements_by_partial_link_text(link)
        if self.get_list_size() > 0:
            debug("List built by id: " + link)
            return True
        else:
            error("No elements found by id: " + link)
            return False

    def build_element_list_by_name(self, name):
        if self.currentElement is not None:
            self.listOfElements = self.currentElement.find_elements_by_name(name)
        else:
            self.listOfElements = self.driver.find_elements_by_name(name)
        if self.get_list_size() > 0:
            debug("List built by id: " + name)
            return True
        else:
            error("No elements found by id: " + name)
            return False

    """Methods to select a specific element from listOfElements"""

    def get_list_size(self):
        return len(self.listOfElements)

    def select_element_from_list_by_index(self, index):
        if index <= self.get_list_size():
            self.currentElement = self.listOfElements[index]
            return True
        else:
            error("Element with the given index '" + str(index) + "' does not exist")
            return False

    def select_first_element_from_list(self):
        if self.get_list_size() > 0:
            self.currentElement = self.listOfElements[0]
            debug("Selected first element from list")
            return True
        else:
            error("List is emtpy")
            return False

    def select_last_element_from_list(self):
        if len(self.listOfElements) > 0:
            self.currentElement = self.listOfElements[self.get_list_size()]
            debug("Selected last element from list")
            return True
        else:
            error("List is emtpy")
            return False

    def select_element_from_list_by_attribute_and_value(self, attribute, value):
        found = False
        if self.get_list_size() > 0:
            for element in self.listOfElements:
                if value in element.get_attribute:
                    self.currentElement = element
                    found = True
                    debug("Element selected from list by attribute and value: " + attribute + " - " + value)
                    break
            if not found:
                error("Element not found by attribute and value: " + attribute + " - " + value)
        else:
            error("List is emtpy")
        return found

    # Methods to interact with the selected Object
    def type(self, value):
        debug("Typing text on current element: " + value)
        try:
            self.currentElement.clear()
            self.currentElement.send_keys(value)
            return True
        except Exception as e:
            error(e)
            return False

    def submit(self):
        debug("Pressing the Submit button on current element")
        try:
            self.currentElement.submit()
            return True
        except Exception as e:
            error(e)
            return False

    def click(self):
        debug("Clicked on current element")
        try:
            self.currentElement.click()
            return True
        except Exception as e:
            error(e)
            return False

    def hover(self):
        debug("Hovering over current element")
        try:
            hover = ActionChains(self.driver).move_to_element(self.currentElement)
            hover.perform()
            return True
        except Exception as e:
            error(e)
            return False

    def double_click(self):
        debug("Double clicked on current element")
        action_chains = ActionChains(self.driver)
        action_chains.double_click(self.currentElement).perform()

    def get_text(self):
        return self.currentElement.text

    def close(self):
        try:
            if self.driver is not None:
                log("Closing driver: " + self.driver.name)
                self.driver.quit()
                return True
        except WebDriverException as e:
            error(e)
            return False

    def get_tag_name(self):
        return self.currentElement.tag_name

    def get_id(self):
        return self.currentElement.id

    def get_location(self):
        return self.currentElement.location

    def get_location_once_scrolled_into_view(self):
        return self.currentElement.location_once_scrolled_into_view

    def get_size(self):
        return self.currentElement.size

    def is_displayed(self):
        return self.currentElement.is_displayed()

    def is_enabled(self):
        return self.currentElement.is_enabled()

    def is_selected(self):
        return self.currentElement.is_selected()

    def get_attribute(self, attribute):
        try:
            return self.currentElement.get_attribute(attribute)
        except NoSuchAttributeException:
            error("Attribute not found: " + attribute)
            return ""

    def get_css_property(self, prop):
        return self.currentElement.value_of_css_property(prop)

    def execute_javascript(self, script):
        self.driver.execute_script(script)

    def execute_javascript_on_current_element(self, script):
        self.driver.execute_script(script, self.currentElement)