博客第一篇:测试
Posted robin-liu1978
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了博客第一篇:测试相关的知识,希望对你有一定的参考价值。
#coding:utf-8
#2018/8/10
__author__ = ‘robin‘
#重写了一些selenium的查找方法,使这些方法更简单
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Key
class BasePage(object):
"""定义一个页面的基类"""
# webdriver instance
def __init__(self, browser=‘‘):
‘‘‘
初始化一个selenium测试浏览器
‘‘‘
if browser == ‘‘:
br = input(‘plesse input your browser: ‘)
if br == "firefox":
driver = webdriver.Firefox()
elif br == "chrome":
driver = webdriver.Chrome()
elif br == "ie":
driver = webdriver.Ie()
elif br == "phantomjs":
driver = webdriver.PhantomJS()
self.driver = driver
def findElement(self, element):
‘‘‘
找单个元素,并且用这种元组格式(identifier type,value)
例如: self.findElement(element)
‘‘‘
try:
type = element[0]
value = element[1]
if type == "id":
elem = self.driver.find_element_by_id(value)
elif type == "name":
elem = self.driver.find_element_by_name(value)
elif type == "class":
elem = self.driver.find_element_by_class_name(value)
elif type == "link_text":
elem = self.driver.find_element_by_link_text(value)
elif type == "xpath":
elem = self.driver.find_element_by_xpath(value)
elif type == "css":
elem = self.driver.find_element_by_css_selector(value)
else:
raise NameError("Please input correct the type!")
except Exception:
raise ValueError("No such element found" + str(element))
return elem
def findElements(self, element):
‘‘‘
获取多个元素
‘‘‘
try:
type = element[0]
value = element[1]
if type == "id":
elem = self.driver.find_elements_by_id(value)
elif type == "name":
elem = self.driver.find_elements_by_name(value)
elif type == "class":
elem = self.driver.find_elements_by_class_name(value)
elif type == "link_text":
elem = self.driver.find_elements_by_link_text(value)
elif type == "xpath":
elem = self.driver.find_elements_by_xpath(value)
elif type == "css":
elem = self.driver.find_elements_by_css_selector(value)
else:
raise NameError("Please input correct the type!")
except Exception:
raise ValueError("No such element found" + str(element))
return elem
def open(self, url):
‘‘‘
打开一个url,如果url不对会抛出一个error
‘‘‘
if url != "":
self.driver.get(url)
else:
raise ValueError("please provide a base url")
def enter(self, element):
‘‘‘
设置键盘的回车键
‘‘‘
element.send_keys(Keys.RETURN)
def click(self, element):
‘‘‘
点击元素的执行按钮
‘‘‘
element.click()
def quit(self):
‘‘‘
退出浏览器
‘‘‘
self.driver.quit()
def getAttribute(self, element, attribute):
‘‘‘
获取查找元素的属性
‘‘‘
return element.get_attribute(attribute)
def getText(self, element):
‘‘‘
获取元素的text
‘‘‘
return element.text
def getTitle(self):
‘‘‘
获取当前的title
‘‘‘
return self.driver.title
def getCurrentUrl(self):
‘‘‘
获取当前url
‘‘‘
return self.driver.current_url
def getScreenshot(self, targetpath):
‘‘‘
截取当前屏幕,并保存到目标路径
‘‘‘
self.driver.get_screenshot_as_file(targetpath)
def maximizeWindow(self):
‘‘‘
当前窗口最大化
‘‘‘
self.driver.maximize_window()
def back(self):
‘‘‘
退出当前页面
‘‘‘
self.driver.back()
def forward(self):
"""
往前进入一个页面
"""
self.driver.forward()
def getWindowSize(self):
"""
获取当前窗口的尺寸
"""
return self.driver.get_window_size()
def refresh(self):
‘‘‘
刷新当前网页
‘‘‘
self.driver.refresh()
self.driver.switch_to()
以上是关于博客第一篇:测试的主要内容,如果未能解决你的问题,请参考以下文章