python browser engine封装

# -*- coding:utf-8 -*-

import configparser
from selenium import webdriver
from webdriver_manager.opera import OperaDriverManager
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import IEDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from Common.LoggerBase import Logger
from pathlib import Path

import os
# os.environ['WDM_LOG_LEVEL'] = '0'
# #If you face error related to github credentials, you need to place github token: (*)
os.system("export GH_TOKEN = 'asdasdasdasd'")

Logger = Logger()

class BrowserEngine(object):

    # chrome_driver_path = Path(Path.cwd()) / 'tools/chromedriver.exe'
    # ie_driver_path = Path.joinpath(Path.cwd(), 'tools/IEDriverServer.exe')
    # firefox_driver_path = Path(Path.cwd()) / 'tools/firefox.exe'

    def __init__(self, driver=None):
        self.driver = driver

    # read the browser type from config.ini file, return the driver
    # def open_browser(self, driver):
    #     config = configparser.ConfigParser()
    #     file_path = Path.joinpath(Path.cwd(), 'config/config.ini')
    #     #config.read(file_path)
    #     config.read(file_path, encoding='UTF-8') #如果代码有中文注释,用这个,不然报解码错误
    #
    #     browser = config.get("browserType", "browserName")
    #     Logger.info("You had select %s browser." % browser)
    #     url = config.get("testServer", "URL")
    #     Logger.info("The test server url is: %s" % url)
    #
    #     if browser == "Firefox":
    #         driver = webdriver.Firefox(str(self.firefox_driver_path))
    #         Logger.info("Starting firefox browser.")
    #     elif browser == "Chrome":
    #         driver = webdriver.Chrome(str(self.chrome_driver_path))
    #         Logger.info("Starting Chrome browser.")
    #     elif browser == "IE":
    #         driver = webdriver.Ie(str(self.ie_driver_path))
    #         Logger.info("Starting IE browser.")
    #
    #     driver.get(url)
    #     Logger.info("Open url: %s" % url)
    #     driver.maximize_window()
    #     Logger.info("Maximize the current window.")
    #     driver.implicitly_wait(10)
    #     Logger.info("Set implicitly wait 10 seconds.")
    #     return driver


    def open_browser(self, driver=None):
        config = configparser.ConfigParser()
        file_path = Path.joinpath(Path.cwd().parent, 'config/config.ini')
        # 如果代码有中文注释,用这个,不然报解码错误  encoding='UTF-8'
        config.read(file_path, encoding='UTF-8')
        browser = config.get("browserType", "browserName")
        Logger.info("You had select %s browser." % browser)

        url = config.get("testServer", "URL")
        Logger.info("The test server url is: %s" % url)

        if browser == "Firefox":
            #Driver cache by default is valid for 1 day. You are able to change this value using constructor parameter
            driver = webdriver.Firefox(executable_path=GeckoDriverManager(log_level=0).install())
            Logger.info("Starting firefox browser.")

        elif browser == "Chrome":
            # driver = webdriver.Chrome(ChromeDriverManager(log_level=0).install())
            driver = webdriver.Chrome(ChromeDriverManager(cache_valid_range=2, log_level=0).install())
            Logger.info("Starting Chrome browser.")

        elif browser == "Ie":
            driver = webdriver.Ie(IEDriverManager(log_level=0).install())
            Logger.info("Starting IE browser.")

        elif browser == "Edge":
            driver = webdriver.Edge(EdgeChromiumDriverManager(log_level=0).install())
            Logger.info("Starting Edge browser.")

        elif browser == "Opera":
            driver = webdriver.Opera(executable_path=OperaDriverManager(log_level=0).install())
            Logger.info('Starting Opera browser.')

        # elif browser == "Opera":
        #     '''
        #     If the Opera browser is installed in a location other
        #     than C:/Program Files or C:/Program Files (x86) on windows
        #     and /usr/bin/opera for all unix variants and mac, then use the below code
        #     '''
        #     options = webdriver.ChromeOptions()
        #     options.add_argument('allow-elevated-browser')
        #     options.binary_location = "C:\\Users\\USERNAME\\FOLDERLOCATION\\Opera\\VERSION\\opera.exe"
        #     driver = webdriver.Opera(executable_path=OperaDriverManager(log_level=0).install(), options=options)

        driver.get(url)
        Logger.info("Open url: %s" % url)
        driver.maximize_window()
        Logger.info("Maximize the current window.")
        driver.implicitly_wait(10)
        Logger.info("Set implicitly wait 10 seconds.")
        return driver

    def quit_browser(self):
        Logger.info("Now, Close and quit the browser.")
        self.driver.quit()
posted @ 2020-09-11 20:53  蜗牛·哥  阅读(378)  评论(0编辑  收藏  举报