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()

  

Selenium封装的更多相关文章

  1. Selenium - 封装WebDrivers (C#)

    Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Selenium原装支持的各浏览器统一为OnDriver, 并将常用操作封装. using System ...

  2. selenium 封装

    周末无聊 在家封装一个pyselenium.可能这些封装大家都会使用,但是我还是根据我自己的习惯去选择性的去封装一些在我工作中用的,这样的话,我就不用去看selenium的api的,我可以根据我自己的 ...

  3. 【转】Selenium - 封装WebDrivers (C#)

    本文转载自:http://www.cnblogs.com/qixue/p/3977135.html Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Se ...

  4. python+selenium封装UI自动化框架

    seleinum框架 框架的思想:  解决我们测试过程中的问题:大量的重复步骤,用自动化来实现    1)配置和程序的分离    2)测试数据和程序的分离    3)不懂编程的人员可以方便使用:使用的 ...

  5. selenium 封装代码

    package pers.xeon.automate.auxt; import org.openqa.selenium.By; import org.openqa.selenium.WebElemen ...

  6. Java Selenium封装--RemoteWebDriver

    package com.selenium.driver; import java.io.File; import java.io.IOException; import java.net.URL; i ...

  7. Java Selenium封装--RemoteWebElement

    package com.liuke.selenium.driver; import java.sql.SQLException; import java.util.List; import org.j ...

  8. selenium之封装登陆操作

    # selenium 封装登录操作举例 import os, time # from selenium import webdriver class LoginPage(): '''登录模块''' d ...

  9. Python+Selenium框架设计之框架内封装基类和实现POM

    原文地址https://blog.csdn.net/u011541946/article/details/70269965 作者:Anthony_tester 来源:CSDN    博客地址https ...

随机推荐

  1. Git基础--笔记

    0.取的项目的git仓库 有两种取得 Git 项目仓库的方法.第一种是在现存的目录下,通过导入所有文件来创建新的 Git 仓库. 第二种是从已有的 Git 仓库克隆出一个新的镜像仓库来 1.在工作目录 ...

  2. MySQL,Oracle,PostgreSQL,mongoDB 通过web方式管理维护, 提高开发及运维效率

    在开发及项目运维中,对数据库的操作大家目前都是使用客户端工具进行操作,例如MySQL的客户端工具navicat,Oracle的客户端工具 PL/SQL Developer, MSSQL的客户端工具查询 ...

  3. Zookeeper + Guava loading cache 实现分布式缓存

    1. 概述 项目中,创建的活动内容存入redis,然后需要用到活动内容的地方,从redis去取,然后参与计算. 活动数据的一个特点是更新不频繁.数据量不大.因为项目部署一般是多机器.多实例,除了red ...

  4. 关于App的cpu/内存/流量 /电量的方法(GT工具)

    https://mp.weixin.qq.com/s?__biz=MzUzNTQxMzMzMg==&mid=2247484376&idx=1&sn=651e9cf22801b5 ...

  5. Java基础之PDF文件的合并

    1.首先下载一个jar包:pdfbox-app-1.7.1.jar 2.代码如下: package com; import java.io.File; import java.io.IOExcepti ...

  6. IDEA Properties中文unicode转码问题

    在IDEA中创建了properties文件,发现默认中文不会自动进行unicode转码.如下 在project settings - File Encoding,在标红的选项上打上勾,确定即可 效果图 ...

  7. ASP.NET错误处理的方式(二)

    要创建页中的全局处理程序,请创建 Page_Error 事件的处理程序.要创建应用程序范围的错误处理程序,请在 Global.asax 文件中将代码添加到 Application_Error 方法.只 ...

  8. Python学习---PyCharm的使用学习

    1.1. IDEA的使用 PyCharm2017下载 链接:https://pan.baidu.com/s/1HPR9FtVV5BCvd3uTdOetxw 密码:ok0q 激活IDEA2017(方案一 ...

  9. Visual Studio Code (vscode)编译C++

    Visual Studio Code (简称 VS Code / VSC) 是一款免费开源的现代化轻量级代码编辑器,支持几乎所有主流的开发语言的语法高亮.智能代码补全.自定义热键.括号匹配.代码片段. ...

  10. July 27th 2017 Week 30th Thursday

    A smile is the most charming part of a person forever. 微笑永远是一个人身上最好看的东西. Smile in the mirror, and yo ...