所以下面的代码是一个从firefox上的selenium ide生成的webdriver python代码。我有一些其他代码生成但很长,我想保持简单和简短。
所以我的问题是我想为test_函数中的每一行生成一个测试用例,但是我需要它按顺序排列,我不想创建多个测试函数。所以我尝试使用yield功能(这里解释为http://nose.readthedocs.org/en/latest/writing_tests.html),但是没有通过加载Web浏览器。这有点难以解释,因为我没有下面的实际代码。
我将使用nosetests --with-xunit导出结果,然后让Jenkins处理xml。当我现在运行代码并查看xml时,它只包含输出中的一个大块文本,并且所有这些都在一个标记中,它没有分开。
有没有办法生成单独的测试用例,但仍然按顺序执行指令。我有一个解决方案是我可以发送第一个指令(driver.get(self.base_url +“/”)),然后执行第一个和第二个,然后是第一个,第二个,第三个。但我不认为这是最好的方式。
我也知道关于单元测试的断言和大部分内容,我只需要一种方法让每个Web浏览器操作都是一个测试用例但是当它完成每个案例时不关闭浏览器,我需要它来保持一个浏览器打开并通过每个动作,就像你通过firefox上的selenium的ide来播放它一样。
也许我正在查看我的问题,或编码错误。
以下是生成的代码:
# -*- coding: utf-8 -*-
from selenium import webdriver
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
import unittest, time, re
class Test(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.ca/"
self.verificationErrors = []
self.accept_next_alert = True
def test_(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("lst-ib").clear()
driver.find_element_by_id("lst-ib").send_keys("nba")
driver.find_element_by_link_text("NBA.com").click()
driver.find_element_by_link_text("Tickets").click()
driver.find_element_by_link_text("Stats").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
这是我想要它做的(使用伪代码):
# -*- coding: utf-8 -*-
from selenium import webdriver
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
import unittest, time, re
class Test(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.ca/"
self.verificationErrors = []
self.accept_next_alert = True
def test_(self):
driver = self.driver
#run the instruction below and add it to the list of tests
driver.get(self.base_url + "/")
#if the previous instruction was successful, run the one below and add it to the list
driver.find_element_by_id("lst-ib").clear()
#do the same
driver.find_element_by_id("lst-ib").send_keys("nba")
#etc, you get the idea...hopefully :)
driver.find_element_by_link_text("NBA.com").click()
driver.find_element_by_link_text("Tickets").click()
driver.find_element_by_link_text("Stats").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
如果它以我希望它运行的方式运行,输出将如下所示:
Website loaded google.ca ... ok
element found lst-ib and cleared ... ok
element found lst-ib and sent keys nba ... ok
element found nba.com and clicked ... ok
element found tickets and clicked ... ok
element found stats and clicked ... ok
----------------------------------------------------------------------
Ran 6 tests in 5.0s
目前看起来像这样:
loaded google, searched and clicked nba.com, clicked tickets and then starts... ok
----------------------------------------------------------------------
Ran 1 tests in 5.0s
xml中的只是一个大块,它看起来不太好,也没有给出好的测试反馈
感谢您的帮助:))
--------更新--------------
好像我没有充分解释这个问题
所以我想让代码做的是:
# -*- coding: utf-8 -*-
from selenium import webdriver
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
import unittest, time, re
class Test(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.google.ca/"
self.verificationErrors = []
self.accept_next_alert = True
def test_one(self):
driver = self.driver
driver.get(self.base_url + "/")
def test_two(self):
driver = self.driver
driver.find_element_by_id("lst-ib").clear()
def test_three(self):
driver = self.driver
driver.find_element_by_id("lst-ib").send_keys("nba")
def test_four(self):
driver = self.driver
driver.find_element_by_link_text("NBA.com").click()
def test_five(self):
driver = self.driver
driver.find_element_by_link_text("Tickets").click()
def test_six(self):
driver = self.driver
driver.find_element_by_link_text("Stats").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
在上面的情况下,将创建的xml文件将有6个不同的条目,但它不能完成我需要它做的事情,因为它每次启动测试时都会创建一个新的浏览器。我需要它来创建6个案例但不关闭浏览器。现在考虑一下我可能会有一些如何从测试功能中删除设置