Selenium封装

import os
from  selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException


class Driver(object):
    @staticmethod
    def getDriver(option):

        if option == "gc":
            driver = webdriver.Chrome()
        elif option == "ff":
            driver = webdriver.Firefox()
        elif option == "ie":
            driver = webdriver.Ie()
        else:
            raise NameError("目前暂时只支持三剑客浏览器,option desc ==> gc:Chrome,ff:Firefox,ie:IE")
        driver.implicitly_wait(time_to_wait=20)
        driver.maximize_window()
        return driver


class EleUtil(Driver):
    driver = Driver.getDriver("gc")

    @staticmethod
    def find_element(*loc):
        return EleUtil.driver.find_element(*loc)

    @staticmethod
    def find_elements(*loc):
        return EleUtil.driver.find_element(*loc)


class Base(EleUtil):
    @staticmethod
    def openPage(url):
        return EleUtil.driver.get(url)

    @staticmethod
    def getTitle():
        return EleUtil.driver.current_url

    def wait_located_element(self, timeout, *loc_tuple):
        # 判断某个元素是否被加到了dom树里,并不代表该元素一定可见,如果定位到就返回WebElement
        try:
            ele = WebDriverWait(EleUtil.driver, timeout).until(EC.presence_of_element_located(*loc_tuple))
            if ele:
                return ele

        except TimeoutException:

            raise NoSuchElementException("No such element")

    def wait_visibility_element(self, timeout, *loc_tuple):
        try:

            ele = WebDriverWait(EleUtil.driver, timeout).until(EC.visibility_of_element_located(*loc_tuple))
            if ele:
                return ele
        except TimeoutException:
            raise NoSuchElementException("No such element")

    def waitUtilVisibilityEle(self, timeout, ele):
        try:
            element = WebDriverWait(EleUtil.driver, timeout).until(EC.visibility_of(ele))
            if element:
                return element
        except Exception:
            raise NoSuchElementException("no such element.....")

    def waitUtilVisibility_Elements(self, timeout, *loc):

        elements = WebDriverWait(EleUtil.driver, timeout).until(EC.presence_of_all_elements_located(*loc))
        return elements

    def move_to(self, ele):
        ActionChains(EleUtil.driver).move_to_element(to_element=ele).perform()

    def refresh(self):
        EleUtil.driver.refresh()

    def screen(self, img_path):
        EleUtil.driver.get_screenshot_as_file(img_path)

    def selectNewWindow(self):
        cur_handle = EleUtil.driver.current_window_handle
        for handle in EleUtil.driver.window_handles:
            if handle != cur_handle:
                EleUtil.driver.switch_to.window(handle)

 

2.框架引用uittest

import unittest
from lib.selenium_utils import *
from selenium import webdriver
import time
from time import sleep


class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print("打开浏览器")
        cls.a = time.clock()
        Base.openPage("http://www.baidu.com")

    @classmethod
    def tearDownClass(cls):
        Base.driver.quit()
        b = time.clock() - cls.a
        print("关闭浏览器", b)

    def test_method1(self):
        Base.find_element(By.ID, "kw").send_keys("selenium2 python")
        Base.find_element(By.ID, "su").click()
        print(Base.getTitle())

    def test_method2(self):
        Base.openPage("https://www.cnblogs.com/Rita-LJ/p/8079094.html")
        print(Base().getTitle())


if __name__ == "__main__":
    unittest.main()  # 程序入口





  

封装模式二:

class Base(object):
    def __init__(self,driver):

         self.driver=driver

    def find_element(self,*loc):
        return self.driver.find_element(*loc)


    def find_elements(self,*loc):
        return self.driver.find_elements(*loc)

    def openPage(self,url):
        self.driver.get(url)

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

框架引用

 

from selenium import webdriver
from lib.tett import Base
import time
from selenium.webdriver.common.by import By
import unittest

class Test(unittest.TestCase):

    def setUp(self):
        print("打开浏览器")
        self.a=time.clock()
        self.driver=webdriver.Chrome()
        self.driver.maximize_window()
        Base(self.driver).openPage("http://www.baidu.com")
        print(Base(self.driver).getTitle())
    def tearDown(self):
        self.driver.close()
        b=time.clock()-self.a
        print("close浏览器",b)
    def test_1(self):
        Base(self.driver).openPage("http://www.vip.com")
        print(Base(self.driver).getTitle())
    def test_2(self):
        Base(self.driver).find_element(By.ID,"kw").send_keys("selenium2 python")
        Base(self.driver).find_element(By.ID,"su").click()
        print(Base(self.driver).getTitle())
if __name__ == "__main__":
    unittest.main()

  

 

posted @ 2019-03-23 22:08  不带R的墨菲特  阅读(1322)  评论(0编辑  收藏  举报