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()
Selenium封装的更多相关文章
- Selenium - 封装WebDrivers (C#)
Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Selenium原装支持的各浏览器统一为OnDriver, 并将常用操作封装. using System ...
- selenium 封装
周末无聊 在家封装一个pyselenium.可能这些封装大家都会使用,但是我还是根据我自己的习惯去选择性的去封装一些在我工作中用的,这样的话,我就不用去看selenium的api的,我可以根据我自己的 ...
- 【转】Selenium - 封装WebDrivers (C#)
本文转载自:http://www.cnblogs.com/qixue/p/3977135.html Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Se ...
- python+selenium封装UI自动化框架
seleinum框架 框架的思想: 解决我们测试过程中的问题:大量的重复步骤,用自动化来实现 1)配置和程序的分离 2)测试数据和程序的分离 3)不懂编程的人员可以方便使用:使用的 ...
- selenium 封装代码
package pers.xeon.automate.auxt; import org.openqa.selenium.By; import org.openqa.selenium.WebElemen ...
- Java Selenium封装--RemoteWebDriver
package com.selenium.driver; import java.io.File; import java.io.IOException; import java.net.URL; i ...
- Java Selenium封装--RemoteWebElement
package com.liuke.selenium.driver; import java.sql.SQLException; import java.util.List; import org.j ...
- selenium之封装登陆操作
# selenium 封装登录操作举例 import os, time # from selenium import webdriver class LoginPage(): '''登录模块''' d ...
- Python+Selenium框架设计之框架内封装基类和实现POM
原文地址https://blog.csdn.net/u011541946/article/details/70269965 作者:Anthony_tester 来源:CSDN 博客地址https ...
随机推荐
- JDK的安装与卸载
1.jdk 下载链接:http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html 2.在 ...
- C#学习笔记-原型模式
题目:编写基本的简历. 实现: 创建基本的Resume类,然后主函数通过实例化Resume来写简历即可. Resume类: class Resume { private string name; pr ...
- C++输入输出流--<iostream>详解
C++输入输出流包含在头文件<iostream>中, 流的定义如下:通过设备驱动程序与键盘.屏幕.文件.打印机等进行交互, iostream 类提供与之交互的方法.输出流:输出流的对象是字 ...
- bzoj P3884 上帝与集合的正确用法
Description 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“ ...
- js原生日历
突然发现日期对象可以进行 加减 , 利用这个特性写了一个可以说是对只要会JavaScript 的就可以写的日历:没有各种算法,只有一些逻辑相信只要懂javascript就差不多看俩眼就会的日历. & ...
- 使用SVG中的Symbol元素制作Icon【摘转】
以下为内容摘抄和转摘记录: 为什么要用svg ? SVG优势 随着高清屏幕的普及,相比使用png等位图而言,使用SVG等矢量图形是一种全新的设计方式.更重要的是相比位图而言,SVG有着无可比拟的优势. ...
- Apache 2 解析html中的php
Ubuntu下安装Apache 2无法解析html中的php Ubuntu下安装了Apache 2却无法解析html中的php ,好多说是在httpd.conf文件中修改代码,但是ubuntu中没有这 ...
- Week1——JavaEE
本科目标 首先,对我来说自己想走的方向是JavaWeb后台开发,因此JavaEE对我来说也是比较重要的,想学好这门课.进一步巩固自己现有的基础知识,完善自己的项目经验,更加熟悉开发流程.在框架方面我还 ...
- 解决Spring框架下中文乱码的问题
在使用了Spring框架下回发现很多表单交互的地方会发生乱码,而且写到数据库中也是乱码,这其实还是字符编码的问题,在我们还在用自己写的servlet的时候,直接在request和response加上字 ...
- 毕向东_Java基础视频教程第20天_IO流(5~6)
第20天-05-IO流(文件列表一) static File[] listRoots() List the available filesystem roots. String[] list() Re ...