Goal: how to package a few simple methods to the Selenium our custom class, this time we write a class called browser engine class, by changing the value of a string, using the if statement to determine and control the start that browser .
Temporary supports three engines: IE, Chrome, Firefox
Exercise scenario: Create a browser_engine.py test1 file in this package, and then create a test.py file in another package to test whether the browser engine class is working properly. The browser engine class, we began to write simply, write-only launch the browser.
browser_engine.py Code:
# coding=utf-8 from selenium import webdriver class BrowserEngine(object): """ Define a class browser engine, according to the value of the control start browser_type different browsers. Here is mainly IE, Firefox, Chrome """ def __init__(self,browser_type): """ Incoming browser type :param browser_type: """ self.browser_type = browser_type def get_browser(self): """ If statements to control the start of the initialization different browsers, Chrome is the default :return: """ if self.browser_type == 'Firefox': driver = webdriver.Firefox() elif self.browser_type == 'IE': driver = webdriver.Ie() else: driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(10) return driver
test.py Code: passing a value type of engine in open_browser method, and then choose which browser to call the class.
# coding = utf-8 from selenium import webdriver import time from test1.browser_engine import BrowserEngine # Test class structure class TestBrowserEngine(object): # Open your browser def open_browser (self, browser_type): # incoming browser type parameter browserengine = BrowserEngine(browser_type) self.driver = browserengine.get_browser() tb_driver=TestBrowserEngine() tb_driver.open_browser("Chrome")
Reference article: https://blog.csdn.net/u011541946/article/details/70171401