python 頁面基類 Page封裝 →driver層的封裝(最底層的封裝Page)

# coding=utf-8
import time
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from Common.LoggerBase import Logger                    # 本身封裝的logger類
from pathlib import *
'''--create a logger instance--'''
Logger = Logger()

class BasePage(object):
    """
        定義一個頁面基類,讓全部頁面都繼承這個類,封裝一些經常使用的頁面操做方法到這個類
    """

    '''--初始化--'''
    def __init__(self, driver):
        self.driver = driver
        self.timeout = 10
        self.poll_frequency = 0.5

    '''--定義open方法--'''
    def open(self, url):
        self.driver.maximize_window()
        self.driver.get(url)

    '''--quit browser and end testing--'''
    def quit_browser(self):
        self.driver.quit()

    '''--瀏覽器前進操做--'''
    def forward(self):
        self.driver.forward()
        Logger.info("Click forward on current page.")

    '''--瀏覽器後退操做--'''
    def back(self):
        self.driver.back()
        Logger.info("Click back on current page.")

    '''--隱式等待--'''
    def wait(self, seconds):
        self.driver.implicitly_wait(seconds)
        Logger.info("wait for %d seconds." % seconds)

    '''--點擊關閉當前窗口--'''
    def close(self):
        try:
            self.driver.close()
            Logger.info("Closing and quit the browser.")
        except NameError as e:
            Logger.error("Failed to quit the browser with %s" % e)

    '''--保存圖片--'''
    def get_windows_img(self):
        """
            在這裏咱們把file_path這個參數寫死,直接保存到咱們項目根目錄的一個文件夾.\Screenshots下
        """
        file_path = Path(Path.cwd().parent) / 'Screenshots'
        rq = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
        screen_name = Path.joinpath(file_path, rq + '.png')
        try:
            self.driver.get_screenshot_as_file(str(screen_name))
            Logger.info("Had take screenshot and save to folder : /Screenshots")
        except NameError as e:
            Logger.error("Failed to take screenshot! %s" % e)
            self.get_windows_img()

    '''--重寫定位元素方法--'''
    def find_element(self, selector):
        """
             這個地方爲何是根據=>來切割字符串,請看頁面裏定位元素的方法
             submit_btn = "id=>su"
             login_lnk = "xpath => //*[@id='u1']/a[7]"  # 百度首頁登陸連接定位
             若是採用等號,結果不少xpath表達式中包含一個=,這樣會形成切割不許確,影響元素定位
            :param selector:
            :return: element
        """
        element = ''
        if '=>' not in selector:
            Logger.info("Not found '=>', default is ID")
            return self.driver.find_element_by_id(selector)
        selector_by = selector.split('=>')[0]
        selector_value = selector.split('=>')[1]

        if selector_by == "i" or selector_by == 'id':
            try:
                element = self.driver.find_element_by_id(selector_value)
                Logger.info("Had find the element \" %s \" successful "
                            "by %s via value: %s " % (element.text, selector_by, selector_value))
            except NoSuchElementException as e:
                Logger.error("NoSuchElementException: %s" % e)
                self.get_windows_img()  # take screenshot

        elif selector_by == "n" or selector_by == 'name':
            try:
                element = self.driver.find_element_by_name(selector_value)
                Logger.info("Had find the element \" %s \" successful "
                            "by %s via value: %s " % (element.text, selector_by, selector_value))
            except NoSuchElementException as e:
                Logger.error("NoSuchElementException: %s" % e)
                self.get_windows_img()

        elif selector_by == "c" or selector_by == 'class_name':
            try:
                element = self.driver.find_element_by_class_name(selector_value)
                Logger.info("Had find the element \" %s \" successful "
                            "by %s via value: %s " % (element.text, selector_by, selector_value))
            except NoSuchElementException as e:
                Logger.error("NoSuchElementException: %s" % e)

        elif selector_by == "l" or selector_by == 'link_text':
            try:
                element = self.driver.find_element_by_link_text(selector_value)
                Logger.info("Had find the element \" %s \" successful "
                            "by %s via value: %s " % (element.text, selector_by, selector_value))
            except NoSuchElementException as e:
                Logger.error("NoSuchElementException: %s" % e)

        elif selector_by == "p" or selector_by == 'partial_link_text':
            try:
                element = self.driver.find_element_by_partial_link_text(selector_value)
                Logger.info("Had find the element \" %s \" successful "
                            "by %s via value: %s " % (element.text, selector_by, selector_value))
            except NoSuchElementException as e:
                Logger.error("NoSuchElementException: %s" % e)

        elif selector_by == "t" or selector_by == 'tag_name':
            try:
                element = self.driver.find_element_by_tag_name(selector_value)
                Logger.info("Had find the element \" %s \" successful "
                            "by %s via value: %s " % (element.text, selector_by, selector_value))
            except NoSuchElementException as e:
                Logger.error("NoSuchElementException: %s" % e)

        elif selector_by == "x" or selector_by == 'xpath':
            try:
                element = self.driver.find_element_by_xpath(selector_value)
                Logger.info("Had find the element \" %s \" successful "
                            "by %s via value: %s " % (element.text, selector_by, selector_value))
            except NoSuchElementException as e:
                Logger.error("NoSuchElementException: %s" % e)
                self.get_windows_img()

        elif selector_by == "s" or selector_by == 'selector_selector':
            try:
                element = self.driver.find_element_by_css_selector(selector_value)
                Logger.info("Had find the element \" %s \" successful "
                            "by %s via value: %s " % (element.text, selector_by, selector_value))
            except NoSuchElementException as e:
                Logger.error("NoSuchElementException: %s" % e)

        else:
            raise NameError("Please enter a valid type of targeting elements.")

        return element

    '''--輸入--'''
    def input_text(self, selector, text):
        el = self.find_element(selector)
        try:
            el.send_keys(text)
            Logger.info("Had type \" %s \" in inputBox" % text)
        except NameError as e:
            Logger.error("Failed to type in input box with %s" % e)
            self.get_windows_img()

    '''--清除文本框--'''
    def clear(self, selector):
        el = self.find_element(selector)
        try:
            el.clear()
            Logger.info('Clear text in input box before typing')
        except NameError as e:
            Logger.error("Failed to clear in input box with %s" % e)
            self.get_windows_img()

    '''--點擊元素--'''
    def click(self, selector):
        el = self.find_element(selector)
        try:
            el.click()
            Logger.info("The element \" %s \" was clicked." % el)
        except NameError as e:
            Logger.error("Failed to click the element with %s" % e)

    '''--獲取網頁標題--'''
    def get_page_title(self):
        Logger.info("Current page title is %s" % self.driver.title)
        return self.driver.title

    def send_key(self, locator, value):
        """

        @param locator: 定位器
        @param value: value
        """
        elem = self.find_element(locator)
        try:
            elem.send_keys(value)
            Logger.info("元素對象輸入值成功,值爲:{}".format(value))
        except Exception as e:
            Logger.error("元素對象輸入值失敗,錯誤信息爲:{}".format(e))

    def get_text(self, locator):
        """
        @param locator:定位器
        @return:元素文本值
        """
        elem_text = None
        elem = self.find_element(locator)
        try:
            elem_text = elem.text
        except Exception as e:
            Logger.error("元素text獲取失敗,錯誤信息爲:{}".format(e))
            Logger.info("元素text值:{}".format(elem_text))
        return elem_text

    '''--重寫跳轉switch_to_frame方法'''
    def switch_to_frame(self, selector):
        """
        :param selector: 傳入定位器參數
        :return:
        """
        elem = self.find_element(selector)
        try:
            self.driver.switch_to.frame()
            Logger.info("frame切換成功")
        except Exception as e:
            Logger.error("frame切換失敗,錯誤信息爲:{}".format(e))

    '''--切回父級frame--'''
    def turn_back_frame(self):
        self.driver.switch_to.default_content()

    def switch_to_handle(self, index):
        """
            切換窗口句柄
        """
        # 獲取當前全部窗口句柄
        global handles
        try:
            handles = self.driver.window_handles
            Logger.info("獲取當前全部窗口句柄成功,句柄對象列表爲:{}".format(handles))
        except Exception as e:
            Logger.error("獲取當前全部窗口句柄失敗,錯誤信息爲:{}".format(e))
        # 切換到新窗口句柄
        try:
            self.driver.switch_to.window(handles[index])
            Logger.info("切換新窗口句柄成功,切換窗口的索引index爲:{}".format(index))
        except Exception as e:
            Logger.error("切換新窗口句柄失敗,錯誤信息爲:{}".format(e))

    """--多選、單選元素獲取方法--"""
    def select_by_text(self, selectByText):
        if selectByText is not "":
            select_by_text = self.find_element("xpath=>//*[text()=\"%s\"]" % selectByText)
            select_by_text.click()
        else:
            print("Not find the selectByText, get the default value!")
            pass

    def move_mouse_to_element(self, locator):
        """
        移動鼠標到某個元素上面
        @param locator:
        @return:
        """
        elem = self.find_element(locator)
        action = ActionChains(self.driver)
        action.move_to_element(elem).perform()

    def clear_input_value(self, locator):
        """
        清除輸入框中的內容
        @param locator:
        @return:
        """
        elem = self.find_element(locator)
        elem.send_keys(Keys.CONTROL, "a")
        elem.send_keys(Keys.DELETE)

    def get_value(self, locator):
        """
        獲取輸入框的value
        @param locator:
        @return:
        """
        elem = self.find_element(locator)
        return elem.get_attribute("value")

    def double_click_elem(self, locator):
        """
        雙擊元素
        @param locator:
        @return:
        """
        elem = self.find_element(locator)
        ActionChains(self.driver).double_click(elem).perform()

    def elem_is_display(self, locator):
        """
        判斷元素在頁面是否顯示,顯示返回True,不顯示返回false
        @param locator:
        @return:
        """
        elem = self.find_element(locator)
        return elem.is_displayed()

    def elem_is_selected(self, locator):
        """
        判斷元素是否被選中,用於多選框,若是多選框被選中狀態,返回True,不然返回False
        @param locator:
        @return:
        """
        elem = self.find_element(locator)
        return elem.is_selected()

    def elem_is_enable(self, locator):
        """
        判斷頁面元素是否可用
        @param locator:
        @return:
        """
        elem = self.find_element(locator)
        Logger.info("按鈕的點擊狀態,是否可點擊:{}".format(elem.is_enabled()))
        return elem.is_enabled()

    def choose_select_by_value(self, locator, value):
        """
        根據內置屬性value值,選擇下拉輸入框
        @param locator:
        @param value:
        @return:
        """
        elem = self.find_element(locator)
        Select(elem).select_by_value(value)

    def choose_select_by_index(self, locator, index):
        """
        根據索引選擇下拉框
        @param locator:
        @param index:
        @return:
        """
        elem = self.find_element(locator)
        Select(elem).select_by_index(index)

    def choose_select_by_visible_value(self, locator, value):
        """
        根據下拉選項的文本值選擇下拉框
        @param locator:
        @param value:
        @return:
        """
        elem = self.find_element(locator)
        Select(elem).select_by_visible_text(value)

    def elem_object_click(self, elem):
        """
        元素點擊,傳入參數爲元素對象
        @param elem:
        @return:
        """
        elem.click()

    def find_elements(self, selector):
        """
        :param selector: 傳入定位器參數locator => 標識切割
        :return: 返回元素對象列表
        """
        Logger.info("輸出定位器信息:{}".format(selector))

        element = ''
        if '=>' not in selector:
            Logger.info("Not found '=>', default is ID")
            return self.driver.find_element_by_id(selector)
        selector_by = selector.split('=>')[0]
        selector_value = selector.split('=>')[1]

        if selector_by is None and selector_value is None:
            Logger.error('find_elements:selector_by and selector_value is empty:,Please pass the correct value)')
        else:
            Logger.info("find_elements:正在定位元素信息:定位方式->%s,value值->%s" % (selector_by, selector_value))
            try:
                time.sleep(1)
                elems = WebDriverWait(self.driver, self.timeout, self.poll_frequency).until(
                    lambda x: x.find_elements(*selector))
                Logger.info("元素對象爲:{}".format(elems))
                return elems
            except Exception as e:
                Logger.error("定位不到元素,錯誤信息爲:{}".format(e))
                return False

    def choose_elem_by_visible_value(self, locator, value):
        """
        根據一組元素對象中某一個元素對象的文本值肯定,哪個元素對象
        @param locator:
        @param value:
        @return: 單個元素對象
        """
        elems = self.find_elements(locator)
        for item in elems:
            if self.elem_object_get_text(item) == value:
                return item

    def elem_object_get_text(self, elem):
        """
        元素對象獲取text值,傳入元素對象
        @param elem:
        @return:
        """
        elem_text = elem.text
        return elem_text

    def get_text_by_elements(self, locator, index):
        """
        @param locator: 定位器
        @return: 返回定位對象組的第一個元素的值
        """
        global elem_text
        elem = self.find_elements(locator)
        try:
            elem_text = elem[index].text
            Logger.info("獲取元素組對象,索引位置{}的值成功,值爲:{}".format(index, elem_text))
        except Exception as e:
            Logger.error("獲取元素組對象,索引位置{}的值失敗,失敗信息爲: {}".format(e, e))
        return elem_text

    '''--靜態方法--'''
    @staticmethod
    def sleep(seconds):
        time.sleep(seconds)
        Logger.info("Sleep for %d seconds" % seconds)
相關文章
相關標籤/搜索