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. Lucene原理之概念

    概念: 数据分两种: 1.结构化数据:指具有固定格式或有限长度的数据,如数据库,元数据等. 2.非结构化数据:指不定长或无固定格式的数据,如邮件,word文档等.(半结构化数据:如XML,HTML等, ...

  2. java设计模式-----7、装饰模式

    首先,什么是装饰者模式呢??? 装饰( Decorator )模式又叫做包装模式.通过一种对客户端透明的方式来扩展对象的功能,是继承关系的一个替换方案.他是23种设计模式之一,英文叫Decorator ...

  3. springboot开篇 (一)简单邮件发送

    上篇终结篇为spring 发送邮件,这次将使用springboot 发送邮件,同时本篇将作为springboot入门篇. 新建一个工程..工程目录结构如下,此次使用idea进行开发.对于一个长期使用e ...

  4. 百度翻译cs文件英文注释

    原由:本人英语烂,没办法看不懂国外的代码注释!只能借助其他手段来助我一臂之力了. 虽然翻译内容不是很准确,但好过什么都看不懂的强. 对吧?! 代码有点乱有用的园友自个整理一下吧! 最近没时间所以翻译后 ...

  5. Sql语句常用关键字

    --语 句 功 能--数据操作SELECT --从数据库表中检索数据行和列INSERT --向数据库表添加新数据行DELETE --从数据库表中删除数据行UPDATE --更新数据库表中的数据 --数 ...

  6. CentOS配置multipath

    可以通过2种方式查看HBA的WWN信息: 1. 查看sys文件系统 查看HBA卡型号:[root@localhost ~]# lspci  | grep -i fibre13:00.0 Fibre C ...

  7. 在linux中给你的应用做压力测试

    在linux中给你的应用做压力测试 作者: 立地 邮箱: jarvin_g@126.com QQ: 511363759 一.webbench 1.在Ubuntu中安装webbench —支持get,h ...

  8. Week3——Session

    Session 一.Session是会话技术的一种.会话技术分为Cookie和Session.Cookie是数据存储在客户端本地,减少服务器端的存储的压力,安全性不好,客户端可以清除cookie: S ...

  9. Python用户交互-密码不可见

    输入密码时若让用户不可见,可以使用getpass模块中的getpass方法 # 输入密码时若想要不可见,使用getpass模块中getpass方法即可 import getpass pwd=getpa ...

  10. 剑指offer相关问题

    1. 变态跳台阶 Fib(n) = Fib(n-1)+Fib(n-2)+Fib(n-3)+..........+Fib(n-n)         =Fib(0)+Fib(1)+Fib(2)+..... ...