封装util,firefoxutil
#-*- coding:utf-8 -*-
#导入包
from enum import Enum
from selenium import webdriver
import time
#导入休眠包
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
class Firefoxutil(object):
#打开浏览器的方法
def foxutil_start(self,url):
self.driver=webdriver.Chrome(executable_path="D:\google\Chrome\Application\chromedriver.exe")
self.driver.maximize_window()
self.driver.get(url)
self.TimeSleep(ENUMS.TWO_TIME)
#关闭浏览器
def foxutil_close(self):
self.driver.quit()
#关闭当前浏览器
def foxutil_current(self):
self.driver.close()
#隐式休眠
def TimeImplay(self,number):
self.driver.implicitly_wait(number)
def TimeSleep(self,number):
time.sleep(number)
#ID查找控件
def FindID(self,ID):
ids=(By.ID,ID)
WebDriverWait(self.driver,ENUMS.TWENTY_TIME,ENUMS.ONE_HALF).until(EC.presence_of_element_located(ids))
return self.driver.find_element_by_id(ID)
#点击事件
def ClickID(self,ID):
self.FindID(ID).click()
def SendKeysID(self,ID,message):
self.FindID(ID).clear()
self.FindID(ID).send_keys(message)
# 查找控件方式 8 种
def FindCss(self, css):
# 使用显示休眠,一共休眠20秒,每隔0.5秒休眠一次,
csses = (By.CSS_SELECTOR, css)
# 休眠
WebDriverWait(self.driver, ENUMS.TWENTY_TIME, ENUMS.ONE_HALF).until(
EC.presence_of_element_located(csses))
return self.driver.find_element_by_css_selector(css)
# 根据id设置点击事件
def ClickCss(self, css):
self.FindCss(css).click()
# 输入内容
def SendkeysCss(self, css, message):
self.FindCss(css).send_keys(message)
#getTltie
def getTitle(self):
return self.driver.title
def getClassText(self,cls):
return self.FindClass(cls).text
#断言Tltie
def AssertTitle(self,self1,expect):
self1.assertEqual(self.getTitle(),expect)
# ID查找控件
def FindClass(self, cls):
clas = (By.CLASS_NAME, cls)
WebDriverWait(self.driver, ENUMS.TWENTY_TIME, ENUMS.ONE_HALF).until(EC.presence_of_element_located(clas))
return self.driver.find_element_by_class_name(cls)
# 点击事件
def ClickClass(self, cls):
self.FindClass(cls).click()
def SendKeysClass(self, cls, message):
self.FindClass(cls).clear()
self.FindClass(cls).send_keys(message)
def AssertClass(self,self1,expect,cls):
self1.assertEqual(self.getClassText(cls),expect)
def FindXpaths(self,xpath):
xpaths=(By.XPATH,xpath)
WebDriverWait(self.driver,ENUMS.TWENTY_TIME,ENUMS.ONE_HALF).until(EC.presence_of_element_located(xpaths))
return self.driver.find_elements_by_xpath(xpath)
#查询一组的
def FindClasses(self,cls):
clss=(By.CLASS_NAME,cls)
WebDriverWait(self.driver,ENUMS.TWENTY_TIME,ENUMS.ONE_HALF).until(EC.presence_of_element_located(clss))
return self.driver.find_elements_by_class_name(cls)
def ClickClasses(self,cls,index):
self.FindClasses(cls)[index].click()
# Link查找控件
# Link查找控件
def FindLink(self, link):
links = (By.LINK_TEXT, link)
WebDriverWait(self.driver, ENUMS.TWENTY_TIME, ENUMS.ONE_HALF).until(
EC.presence_of_element_located(links))
return self.driver.find_element_by_link_text(link)
# 点击事件
def ClickLink(self, link):
self.FindLink(link).click()
# 输入内容
def SendKeysLink(self, link, message):
# 查找控件.首先清空数据,同时再输入内容
self.FindLink(link).clear()
self.FindLink(link).send_keys(message)
# 获取控件的内容
def getLinkText(self, link):
self.FindLink(link).text
def isDispalyLink(self, link):
self.FindLink(link).is_displayed()
def isSelectLink(self, link):
self.FindLink(link).is_selected()
def isEnableLink(self, link):
self.FindLink(link).is_enabled()
#切换窗口的方法
def switch_to_window(self,current):
allwindow=self.driver.window_handles
for window in allwindow:
if window!=current:
self.driver.switch_to_window(window)
pass
def ClackWindowClasses(self,cls,index):
#获取当前窗口
current=self.driver.current_window_handle
self.ClickClasses(cls,index)
self.TimeSleep(ENUMS.FIVE_TIME)
self.switch_to_window(current)
def switch_to_view_js_Class(self,cls):
#执行js
self.driver.execute_script("arguments[0].scrollIntoView();",self.FindClass(cls))
#断言
def AssertEquilTitle(self,self1,expect):
self.TimeSleep(ENUMS.FIVE_TIME)
self.message=self.getTitle()
self1.assertEqual(self.message,expect)
# 切换到frame ID
def seitch_to_id_frame(self, ID):
# 切换到frame里面
#self.driver.switch_to.frame(self.FindID(ID))
self.driver.switch_to.frame(self.FindID(ID))
# 注意 pass没有任何意义,只是在写代码的时候怕报错,用来占位的
pass
# 查找控件方式 8 种
def FindXpath(self, xpath):
# 使用显示休眠,一共休眠20秒,每隔0.5秒休眠一次,
xpaths = (By.XPATH, xpath)
# 休眠
WebDriverWait(self.driver, ENUMS.TWENTY_TIME, ENUMS.ONE_HALF).until(
EC.presence_of_element_located(xpaths))
return self.driver.find_element_by_xpath(xpath)
# 查找控件方式8种
def FindLink(self, link):
# 使用显示休眠,一共休眠20秒,每隔0.5秒休眠一次,
links = (By.LINK_TEXT, link)
# 休眠
WebDriverWait(self.driver, ENUMS.TWENTY_TIME, ENUMS.ONE_HALF).until(EC.presence_of_element_located(links))
return self.driver.find_element_by_link_text(link)
# 根据id设置点击事件
def ClickLink(self, link):
self.FindLink(link).click()
# 输入内容
def SendkeysLink(self, linl, message):
self.FindLink(linl).send_keys(message)
# 设置点击事件,以及切换窗口的封装
# 获取当前窗口
def getCurrent_Window(self):
return self.driver.current_window_handle
def ClickLink_Window(self, link):
# 获取当前窗口
current_window = self.getCurrent_Window()
# 进行点击
self.ClickLink(link)
# 休眠五秒
self.TimeSleep(ENUMS.FIVE_TIME)
# 切换窗口
self.switch_to_window(current_window)
# 切换窗口的方法
def switch_to_window(self, current):
# 获取所有的窗口
allwindows = self.driver.window_handles
# 使用for循环进行切换
for window in allwindows:
if window != current:
self.driver.switch_to.window(window)
def FindLinks(self, link):
# 使用显示休眠,一共休眠20秒,每隔0.5秒休眠一次,
links = (By.LINK_TEXT, link)
# 休眠
WebDriverWait(self.driver, ENUMS.TWENTY_TIME, ENUMS.ONE_HALF).until(EC.presence_of_element_located(links))
return self.driver.find_elements_by_link_text(link)
# 点击下标
def ClickLinks(self, link, index):
self.FindLinks(link)[index].click()
# 切换到原声的方法
def switch_to_content(self):
# 切换到本地
self.driver.switch_to.default_content()
class ENUMS(Enum):
# 两秒
TWO_TIME = 2
# 五秒
FIVE_TIME = 5
# 10 秒
TEN_TIME = 10
# 20 秒
TWENTY_TIME = 20
# 0.5 秒
ONE_HALF = 0.5
//指定的网址urluril
#-*- coding:utf-8 -*-
class URL(object):
JD_LOGIN="https://passport.jd.com/new/login.aspx?ReturnUrl=https:/"
JD_DETRIMENT="https://www.jd.com/"
//loginc
#-*- coding:utf-8 -*-
from util import firefoxutil
class LoginControl(object):
def __init__(self,firefox):
#self.firefox=firefoxutil.Firefoxutil()
self.firefox=firefox
def LoginSendkeyUwPw(self,username,password):
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
self.firefox.ClickClass("login-tab-r")
self.firefox.SendKeysID("loginname",username)
self.firefox.SendKeysID("nloginpwd",password)
self.firefox.ClickClass("login-btn")
pass
loginm
#-*- coding:utf-8 -*-
#导入csv包
import csv
#导入os包
import os
class LoginData(object):
def getData(self):
#指定读写路径
current_file=os.getcwd()
files=os.path.dirname(current_file)+"/login/loginm/1509S.csv"
#打开文件
filename=open(files,'rb')
#通过csv读取出来
data=csv.reader(filename)
return data
def getSocks(self):
#指定读写路径
current_file=os.getcwd()
files=os.path.dirname(current_file)+"/login/loginm/1509E1.csv"
filename=open(files,'rb')
data=csv.reader(filename)
return data
//测试用例的样板
袜子,袜子 - 商品搜索 - 京东,加入购物车,商品已成功加入购物车,我的购物车 - 京东商城,去结算,账户登录,13521169273,geili2016.,订单结算页 -京东商城,京东支付-请选择支付方式,我的订单,我的京东--我的订单,取消订单
//测试用例类
#-*- coding:utf-8 -*-
#导入工具类
from util import firefoxutil,urluril
#导入单元测试
import unittest
from login.logincount import loginc
from login.loginm import login_data
#定义类
class Login(unittest.TestCase):
#实例化工具类
@classmethod
def setUpClass(self):
self.firefox =firefoxutil.Firefoxutil()
self.URL=urluril.URL()
self.login=loginc.LoginControl(self.firefox)
self.loginm=login_data.LoginData()
#打开指定网页
def setUp(self):
self.firefox.foxutil_start(self.URL.JD_DETRIMENT)
#关闭网页
def tearDown(self):
self.firefox.foxutil_close()
# #执行测试用例的方法
# def test_uw_pw(self):
# for index in self.loginm.getData():
# self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
# self.login.LoginSendkeyUwPw(index[0].decode("UTF-8"),index[1].decode("UTF-8"))
# self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
# if int(index[3].decode("UTF-8"))==0:
# self.firefox.AssertClass(self,index[2].decode("UTF-8"),"msg-error")
# else:
# self.firefox.AssertTitle(self,index[2].decode("UTF-8"))
#
# pass
def test_socks(self):
#循环
for index in self.loginm.getSocks():
#休眠
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
# 输入袜子
self.firefox.SendKeysID("key",index[0].decode("UTF-8"))
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
#点击查找按钮
self.firefox.ClickClass("button")
self.firefox.TimeSleep(firefoxutil.ENUMS.FIVE_TIME)
#断言
# self.firefox.AssertTitle(self,index[1].decode("UTF-8"))
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
# 使用js定位
self.firefox.switch_to_view_js_Class("m-list")
# 通过xpath获取第一个的内容
ems=self.firefox.FindXpaths("//div[@class='gl-i-wrap']/div[4]/a/em")[0].text
print ems
self.firefox.TimeSleep(firefoxutil.ENUMS.FIVE_TIME)
# 通过xpath获取第一个的内容
self.firefox.ClackWindowClasses("p-img",0)
#使用断言
#self.firefox.AssertEquilTitle(self,index[2].decode("UTF-8"))
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
# 进行定位 通过class
self.firefox.switch_to_view_js_Class("crumb-wrap")
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
# 通过css点击 到购物车结算按钮
#self.firefox.ClickCss("a#InitCartUrl",index[2].decode("UTF-8"))
self.firefox.ClickLink(index[2].decode("UTF-8"))
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
# 进行断言
self.firefox.AssertEquilTitle(self,index[3].decode("UTF-8"))
# 点击 去结算按钮 通过 ID查找
self.firefox.ClickID("GotoShoppingCart")
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
#self.firefox.AssertEquilTitle(self,u"我的购物车 - 京东商城")
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
# 点击 去结算按钮 通过 css进行点击
self.firefox.ClickCss("a.submit-btn")
self.firefox.TimeSleep(firefoxutil.ENUMS.FIVE_TIME)
# 切换 frame 进行登陆
self.firefox.seitch_to_id_frame("dialogIframe")
#休眠
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
#登陆账号密码
self.login.LoginSendkeyUwPw(index[7].decode("UTF-8"),index[8].decode("UTF-8"))
# 设置休眠 使用强制等待 因为网络加载需要时间 休眠五秒
self.firefox.TimeSleep(firefoxutil.ENUMS.FIVE_TIME)
# 进行断言,确保进入订单页面
self.firefox.AssertEquilTitle(self, index[9].decode("UTF-8"))
self.firefox.ClickClass("checkout-submit")
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
self.firefox.AssertEquilTitle(self,index[10].decode("UTF-8"))
self.firefox.TimeSleep(firefoxutil.ENUMS.TWO_TIME)
self.firefox.ClickLink("我的订单")
self.firefox.TimeSleep(firefoxutil.ENUMS.FIVE_TIME)
# 查询一组取消订单 点击第一个需要取消的订单
self.firefox.ClickLinks("取消订单", 0)
# 设置休眠时间
self.firefox.TimeSleep(firefoxutil.ENUMS.FIVE_TIME)
# 切换到frame里面
self.firefox.seitch_to_id_frame("dialogIframe")
# 查询一组,点击下标为5的
self.firefox.ClickClasses("reasonBody", 5)
# 点击提交按钮
self.firefox.ClickLink("提交")
# 设置休眠
self.firefox.TimeSleep(firefoxutil.ENUMS.FIVE_TIME)
# 切出第一个frame,为切进入第二个frame做准备
self.firefox.switch_to_content()
# 切换 frame ,断言订单是不是取消成功
self.firefox.seitch_to_id_frame("dialogIframe")
# 获取内容进行断言
self.message = self.firefox.FindXpath("//div[@class = 'cancel-progress']/h3").text
# 进行断言
self.assertEqual(self.message, u'取消申请已提交,系统正在审核')
# 切换本地内容,去查找控件
self.firefox.switch_to_content()
# 点击 x按钮,将内容✘掉
self.firefox.ClickClass("ui-dialog-close")
pass