Commonlib目录存放通用模块(我们封装的selenium模块)
创建Business目录 ,根据业务创建测试功能模块
创建Testcase目录存放测试用例
Commonlib目录下创建通用模块Commonlib.py
from selenium import webdriver
import time
class CommonUtil():
# 初始化方法
def init(self):
# 创建浏览器对象
self.driver = webdriver.Chrome()
# 设置隐式等待
self.driver.implicitly_wait(5)
# 设置浏览器最大化
self.driver.maximize_window()
# 请求指定站点
def open_url(self,url):
self.driver.get(url)
time.sleep(3)
# 判断定位方法并调用相关方法
def locateElement(self,locate_type,value):
el = None
if locate_type == 'id':
el = self.driver.find_element_by_id(value)
elif locate_type == 'name':
el = self.driver.find_element_by_name(value)
elif locate_type == 'class':
el = self.driver.find_element_by_class_name(value)
elif locate_type == 'text':
el = self.driver.find_element_by_link_text(value)
elif locate_type == 'xpath':
el = self.driver.find_element_by_xpath(value)
elif locate_type == 'css':
el = self.driver.find_element_by_css_selector(value)
# 如果el不为None,则返回
if el is not None:
return el
# 指定对某一元素的点击操作
def click(self,locate_type,value):
#调用定位方法进行元素定位
el = self.locateElement(locate_type,value)
#执行点击操作
el.click()
time.sleep(1)
# 对指定的元素进行数据输入
def input_data(self,locate_type,value,data):
#调用定位方法进行元素定位
el = self.locateElement(locate_type,value)
#执行输入操作
el.send_keys(data)
# 获取指定元素的文本内容
def get_text(self, locate_type, value):
# 调用定位方法进行元素定位
el = self.locateElement(locate_type, value)
return el.text
# 获取指定元素的属性值
def get_attr(self, locate_type, value, data):
# 调用定位方法进行元素定位
el = self.locateElement(locate_type, value)
return el.get_attribute(data)
#收尾清理方法
def delete(self):
time.sleep(3)
self.driver.quit()
if __name__ == '__main__':
pass
Business目录创建测试功能模块toShop.py
#导入selenium封装类
from webTest.tpShopTestProject.Commonlib.Commonlib import CommonUtil
import unittest
import HTMLTestRunner
from webTest.tpShopTestProject.Testcase.test import yongLi
class TpShop(yongLi,CommonUtil,unittest.TestCase):
def setUp(self):
CommonUtil.init(self)
CommonUtil.open_url(self,'http://www.testingedu.com.cn:8000/home/User/login.html')
def test_login(self):
self.login("13800138006","123456","1234")
def test_By_Card(self):
self.by_card()
def tearDown(self):
CommonUtil.delete(self)
# self.driver.quit()
if __name__ == '__main__':
testShop = unittest.TestSuite()
testShop.addTest(TpShop("test_login"))
testShop.addTest(TpShop("test_By_Card"))
file = "e:/tpShop.html"
fle = open(file, "wb")
runner = HTMLTestRunner.HTMLTestRunner(
stream=fle,
title="TP商城测试报告",
description="用例执行情况"
)
runner.run(testShop)
Testcase目录创建测试功能模块test.py
from webTest.tpShopTestProject.Commonlib.Commonlib import CommonUtil
class yongLi(CommonUtil):
#登录
def login(self, username, password, yanzhen):
# 输入用户名
self.input_data("xpath", "//*[@id='username']", username)
# 输入密码
self.input_data("xpath", "//*[@id='password']", password)
# 输入验证码
self.input_data("xpath", "//*[@id='verify_code']", yanzhen)
# 登录
self.click("xpath", "/html/body/div[2]/div/div[2]/div/form/div/div[6]/a")
#将添加到购物车
def by_card(self):
# 输入用户名
self.input_data("xpath", "//*[@id='username']", "13800138006")
# 输入密码
self.input_data("xpath", "//*[@id='password']", "123456")
# 输入验证码
self.input_data("xpath", "//*[@id='verify_code']", "1234")
# 登录
self.click("xpath", "/html/body/div[2]/div/div[2]/div/form/div/div[6]/a")
# 进入首页
self.click("text", "首页")
# 选择商品
self.click("css",
"body > div.floor.floor1.w1224 > div.floor-main > div.floor-goods-list > a:nth-child(1) > div.goods-pic > img")
# 添加到购物车
self.click("xpath", "//*[@id='join_cart']")
运行toShop.py