selenium+python 的测试套件和分布式集成问题,请指教
本帖最后由 meibaocai 于 2015-2-3 11:04 编辑以下是测试套件code:# -*- coding: utf-8 -*-import unittest
import HTMLTestRunner
import time
import sys
from selenium import webdriver
sys.path.append("\test_case")
from test_case.public import sendreport
from test_case.public import config
from test_case importTest_Collect3
reload(sys)
sys.setdefaultencoding('utf-8')
listaa='E:\\selenium_test\\test_case'
def creatsuitel():
print 1111
testunit=unittest.TestSuite()
print 222
#discover 方法筛选出来的用例,循环添加到测试套件中
discover=unittest.defaultTestLoader.discover(listaa,pattern ='*Collect3.py',top_level_dir=None)
print 333
for test_suite in discover:
print 4444
for test_case in test_suite:
print 555
testunit.addTests(test_case)
print testunit
return testunit
#添加测试套件,即:测试集
now = time.strftime('%Y-%m-%d-%H_%M_%S',time.localtime(time.time()))
filename = 'E:\\selenium_test\\report\\'+now+'result.html'
#filename = 'E:\\selenium_test\\report\\test.html'
fp = file(filename, 'wb')
runner =HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'测试报告'+now,description=u'用例执行情况:')
for host,browser in config.getconfig().items():
try:
print host
print browser
alltestnames = creatsuitel()
driver = webdriver.Remote(
command_executor=host,
desired_capabilities={'platform': 'ANY','browserName':browser,'version': '','javascriptEnabled': True
})
runner.run(alltestnames)
print 7777
except Exception,e:
print Exception,":",e
continue
以下是case:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
from threading import Thread
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
import unittest, time, re,random
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
sys.path.append("\public")
from public import config
from public import login
from public import logout
#登陆模块(函数)
class Test_Collect(unittest.TestCase):
def setUp(self):
print 111
def test_Collect(self):
#通过host,browser 来参数化脚本
#driver=self.driver
self.maximize_window()
self.delete_all_cookies()
u"""商品详情中收藏商品、收藏夹中删除收藏商品 'browser'"""
self.get("http://www.xxxx.com")
self.implicitly_wait(5)
while config.isElementExsit(driver,"a") :
self.find_element_by_css_selector("a").click()
login.login(self=driver)
self.implicitly_wait(2)
self.find_element_by_link_text(u"BOYS男装").click()
time.sleep(2)
self.find_element_by_xpath(u"(//a)").click()
time.sleep(10)
self.switch_to_window(self.window_handles[-1])
self.maximize_window()
time.sleep(5)
#下拉滚动条的js
js="var q=document.documentElement.scrollTop=5000"
driver.execute_script(js)
#获取商品数,将其加入到goods数组中,然后随机取其中一个商品
#driver.find_element_by_xpath("/html/body/div/div/div/div/div/div['t']/div/a").click()
lt=len(self.find_elements_by_css_selector(".name>a"))
rd=random.randint(1, lt)
print rd
time.sleep(5)
self.find_elements_by_css_selector(".name>a").click()
time.sleep(2)
self.switch_to_window(self.window_handles[-1])
time.sleep(5)
self.find_element_by_css_selector("a").click()
time.sleep(5)
self.find_element_by_link_text(u"我的收藏").click()
time.sleep(5)
inputs=self.find_elements_by_tag_name('input')
n=0
for ii in inputs:
if ii.get_attribute('type')=="checkbox":
n=n+1
n=n-3
print u"当前列表文件为%d" %n
if n > 0:
print "收藏商品成功!"
elif n==0:
raise NameError('收藏商品失败!')
#勾选全选输入框,点击“删除”选项
self.find_element_by_id("checkpro").click()
self.find_element_by_id("removebut").click()
alert = self.switch_to_alert()
alert.accept()
#判断商品收藏商品是否成功
inputs=self.find_elements_by_tag_name('input')
n=0
for ii in inputs:
if ii.get_attribute('type')=="checkbox":
n=n+1
n=n-3
print u"当前列表文件为%d" %n
if n == 0:
#空列表,收藏商品列表页有3个复选框
print "删除收藏商品成功!"
elif n>0:
raise NameError('收藏商品失败!')
time.sleep(5)
logout.logout(self=self)
time.sleep(2)
self.quit()
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
以下是config.py
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
from threading import Thread
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
import unittest, time, re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def getconfig():
hublist={'http://172.16.13.136:5555/wd/hub':'firefox','http://172.16.13.136:6666/wd/hub':'chrome'}
#hublist={'http://127.0.0.1:5555/wd/hub':'firefox',}
#hublist={'http://127.0.0.1:5555/wd/hub':'internet explorer','http://127.0.0.1:6666/wd/hub':'firefox','http://172.16.13.49:5556/wd/hub':'chrome'}
#hublist={'http://127.0.0.1:5555/wd/hub':'internet explorer'}
return hublist
def isElementExsit(driver,locator):
try:
driver.find_element_by_css_selector(locator)
flag=True
except Exception,e:
print Exception,":",e
flag=False
return flag
以下是:login.py
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
#登陆模块(函数)
def login(self):
self.find_element_by_link_text("请登录").click()
time.sleep(2)
self.find_element_by_id("loginemail").clear()
self.find_element_by_id("loginemail").send_keys("111")
self.find_element_by_id("loginpassword").clear()
self.find_element_by_id("loginpassword").send_keys("1111")
self.find_element_by_class_name("btn_b_login").click()
time.sleep(5)
以下是执行结果:
C:\Python27\python.exe E:/selenium_test/all_yoho_case3.py
http://172.16.13.136:5555/wd/hub
firefox
1111
222
333
4444
555
<unittest.suite.TestSuite tests=[<Test_Collect3.Test_Collect testMethod=test_Collect>]>
E
Time Elapsed: 0:00:02.689000
7777
http://172.16.13.136:6666/wd/hub
chrome
1111
222
333
4444
555
<unittest.suite.TestSuite tests=[<Test_Collect3.Test_Collect testMethod=test_Collect>]>
E
Time Elapsed: 0:00:03.736000
7777
Process finished with exit code 0
以下是:具体用例
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
from threading import Thread
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
import unittest, time, re,random
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
sys.path.append("\public")
from public import config
from public import login
from public import logout
#登陆模块(函数)
class Test_Collect(unittest.TestCase):
def setUp(self):
print 111
def test_Collect(self):
#通过host,browser 来参数化脚本
#driver=self.driver
self.maximize_window()
self.delete_all_cookies()
u"""商品详情中收藏商品、收藏夹中删除收藏商品 'browser'"""
self.get("http://xxxxx.com")
self.implicitly_wait(5)
while config.isElementExsit(driver,"a") :
self.find_element_by_css_selector("a").click()
login.login(self=driver)
self.implicitly_wait(2)
self.find_element_by_link_text(u"BOYS男装").click()
time.sleep(2)
self.find_element_by_xpath(u"(//a)").click()
time.sleep(10)
self.switch_to_window(self.window_handles[-1])
self.maximize_window()
time.sleep(5)
#下拉滚动条的js
js="var q=document.documentElement.scrollTop=5000"
driver.execute_script(js)
#获取商品数,将其加入到goods数组中,然后随机取其中一个商品
lt=len(self.find_elements_by_css_selector(".name>a"))
rd=random.randint(1, lt)
print rd
time.sleep(5)
self.find_elements_by_css_selector(".name>a").click()
time.sleep(2)
self.switch_to_window(self.window_handles[-1])
time.sleep(5)
self.find_element_by_css_selector("a").click()
time.sleep(5)
self.find_element_by_link_text(u"我的收藏").click()
time.sleep(5)
inputs=self.find_elements_by_tag_name('input')
n=0
for ii in inputs:
if ii.get_attribute('type')=="checkbox":
n=n+1
n=n-3
print u"当前列表文件为%d" %n
if n > 0:
print "收藏商品成功!"
elif n==0:
raise NameError('收藏商品失败!')
#勾选全选输入框,点击“删除”选项
self.find_element_by_id("checkpro").click()
self.find_element_by_id("removebut").click()
alert = self.switch_to_alert()
alert.accept()
#判断商品收藏商品是否成功
inputs=self.find_elements_by_tag_name('input')
n=0
for ii in inputs:
if ii.get_attribute('type')=="checkbox":
n=n+1
n=n-3
print u"当前列表文件为%d" %n
if n == 0:
#空列表,收藏商品列表页有3个复选框
print "删除收藏商品成功!"
elif n>0:
raise NameError('收藏商品失败!')
time.sleep(5)
logout.logout(self=self)
time.sleep(2)
self.quit()
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors) # -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
from threading import Thread
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
import unittest, time, re,random
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
sys.path.append("\public")
from public import config
from public import login
from public import logout
#登陆模块(函数)
class Test_Collect(unittest.TestCase):
def setUp(self):
print 111
def test_Collect(self):
#通过host,browser 来参数化脚本
#driver=self.driver
self.maximize_window()
self.delete_all_cookies()
u"""商品详情中收藏商品、收藏夹中删除收藏商品 'browser'"""
self.get("http://www.xxxx.com")
self.implicitly_wait(5)
while config.isElementExsit(driver,"a") :
self.find_element_by_css_selector("a").click()
login.login(self=driver)
self.implicitly_wait(2)
self.find_element_by_link_text(u"BOYS男装").click()
time.sleep(2)
self.find_element_by_xpath(u"(//a)").click()
time.sleep(10)
self.switch_to_window(self.window_handles[-1])
self.maximize_window()
time.sleep(5)
#下拉滚动条的js
js="var q=document.documentElement.scrollTop=5000"
driver.execute_script(js)
#获取商品数,将其加入到goods数组中,然后随机取其中一个商品
#driver.find_element_by_xpath("/html/body/div/div/div/div/div/div['t']/div/a").click()
lt=len(self.find_elements_by_css_selector(".name>a"))
rd=random.randint(1, lt)
print rd
time.sleep(5)
self.find_elements_by_css_selector(".name>a").click()
time.sleep(2)
self.switch_to_window(self.window_handles[-1])
time.sleep(5)
self.find_element_by_css_selector("a").click()
time.sleep(5)
self.find_element_by_link_text(u"我的收藏").click()
time.sleep(5)
inputs=self.find_elements_by_tag_name('input')
n=0
for ii in inputs:
if ii.get_attribute('type')=="checkbox":
n=n+1
n=n-3
print u"当前列表文件为%d" %n
if n > 0:
print "收藏商品成功!"
elif n==0:
raise NameError('收藏商品失败!')
#勾选全选输入框,点击“删除”选项
self.find_element_by_id("checkpro").click()
self.find_element_by_id("removebut").click()
alert = self.switch_to_alert()
alert.accept()
#判断商品收藏商品是否成功
inputs=self.find_elements_by_tag_name('input')
n=0
for ii in inputs:
if ii.get_attribute('type')=="checkbox":
n=n+1
n=n-3
print u"当前列表文件为%d" %n
if n == 0:
#空列表,收藏商品列表页有3个复选框
print "删除收藏商品成功!"
elif n>0:
raise NameError('收藏商品失败!')
time.sleep(5)
logout.logout(self=self)
time.sleep(2)
self.quit()
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors) testunit=unittest.TestSuite()
print 222
#discover 方法筛选出来的用例,循环添加到测试套件中
discover=unittest.defaultTestLoader.discover(listaa,pattern ='*Collect3.py',top_level_dir=None)
print 333
for test_suite in discover:
print 4444
for test_case in test_suite:
print 555
testunit.addTests(test_case)
print testunit
return testunit
我也是写了一个类似的循环,但是很奇怪,在suite增加了case后,运行这段代码,并没有把新增加的case添加到循环里面? 这是为什么呢,大神们,求解释。。。。:'(:'( 为什么是怎么把driver传入到测试用例的 看运行结果打印结果,循环那一块根本就没有进行循环啊,是不是这一块写错了。
页:
[1]