1. 程式人生 > >python+selenium2輕量級框架設計-07常用類

python+selenium2輕量級框架設計-07常用類

一般常用的方法會寫在這個裡面,包括重寫了元素的定位,方法是參考Anthony_tester的寫法

import time,os
from selenium.common.exceptions import NoSuchElementException
from framework.logger import Logger
logger = Logger(logger='BasePage').getlog()
class BasePage(object):
    """
    定義一個頁面基類,讓所有頁面都繼承這個類,封裝一些常用的頁面操作方法到這個類
    """
    def __init__(self,driver):
        self.driver = driver
    def quit_browser(self):
        self.driver.quit()
    def wait(self,seconds):
        self.driver.implicitly_wait(seconds)
        logger.info('隱式等待%s秒'%seconds)
    def forward(self):
        self.driver.forward()
    def back(self):
        self.driver.back()
    #關閉當前頁面視窗
    def close_browser(self):
        try:
            self.driver.close()
            logger.info('關閉頁面成功')
        except NameError as e:
            logger.error('關閉當前頁面失敗:%s'%e)
    #截圖
    def get_windows_img(self):
        file_path = os.path.dirname(os.getcwd())+'/framework_demo/screenshots/'
        rq = time.strftime('%Y-%m-%d_%H.%M.%S')
        filename = file_path+rq+'.jpg'
        try:
            self.driver.get_screenshot_as_file(filename)
            logger.info('截圖成功儲存在screenshots目錄下')
        except NameError as e:
            logger.error('截圖失敗:%s'%e)
    #定位元素
    def find_element(self,selector):
        """
        這個地方為什麼是根據=>來切割字串,請看頁面裡定位元素的方法
        submit_btn = "id=>su"
        login_lnk = "xpath => //*[@id='u1']/a[7]"  # 百度首頁登入連結定位
        如果採用等號,結果很多xpath表示式中包含一個=,這樣會造成切割不準確,影響元素定位
        """
        element = ''
        if '=>' not in selector:
            return self.driver.find_element_by_id(selector)
        #切割selector
        selector_by = selector.split('=>')[0]
        selector_value = selector.split('=>')[1]
        if selector_by =='id':
            try:
                element = self.driver.find_element_by_id(selector_value)
                logger.info('成功定位到元素')
            except NoSuchElementException as e:
                logger.error('定位失敗:%s'%e)
                #截圖
                self.get_windows_img()
        elif selector_by == 'xpath':
            try:
                element = self.driver.find_element_by_xpath(selector_value)
                logger.info('成功定位到元素')
            except NoSuchElementException as e:
                logger.error('定位失敗:%s'%e)
                #截圖
                self.get_windows_img()
        #其他異常處理用的不多故沒寫,可補齊
        elif selector_by =='name':
            element = self.driver.find_element_by_name(selector_value)
        elif selector_by =='class_name':
            element = self.driver.find_element_by_class_name(selector_value)
        elif selector_by =='link_text':
            element = self.driver.find_element_by_link_text(selector_value)
        elif selector_by =='partial_link_text':
            element = self.driver.find_element_by_partial_link_text(selector_value)
        elif selector_by =='tag_name':
            element = self.driver.find_element_by_tag_name(selector_value)
        elif selector_by =='selector_selector':
            element = self.driver.find_element_by_selector_selector(selector_value)
        else:
            #引發異常
            raise NameError('請檢查元素是否有效、可見')
        return element

    #清空
    def clear(self,selector):
        el = self.find_element(selector)
        try:
            el.clear()
            logger.info('清空')
        except NameError as e:
            logger.error('清空失敗%s'%e)
            self.get_windows_img()

    #輸入
    def type(self,selector,text):
        el = self.find_element(selector)
        el.clear()
        try:
            el.send_keys(text)
            logger.info('成功輸入:%s'%text)
        except NameError as e:
            logger.error('輸入失敗:%s'%e)
            self.get_windows_img()
    #點選
    def click(self,selector):
        el = self.find_element(selector)
        try:
            el.click()
            logger.info('點選元素')
        except NameError as e:
            logger.error('點選元素失敗%s'%e)
    #獲取頁面標題
    def get_page_title(self):
        title = self.driver.title
        return title
        logger.info('當前頁面標題是%s'%title)