适用于 Hudson / jenkins 的验证的 Selenium 语法

发布于 2024-12-08 11:23:37 字数 1691 浏览 2 评论 0原文

我已经阅读了各种教程和 Stack Overflow 帖子,并了解 Selenium 可以输出 XML 测试结果,Hudson 可以以 HTML 格式读取/报告它们。

我不明白的是 Python 中使用的语法,使结果看起来像这样: Testcase_LoginPage.VerifyButton1Present 失败

Testcase_LoginPage.VerifyButton2Present 通过

目前,当我深入 Hudson 中的结果时,它们不会按照我上面描述的那样以有用的方式进行格式化,而且它将报告它只运行了一个测试,即使它运行了多个断言测试:

Traceback(最近一次调用最后): 文件“D:\ Temp \ 1TestingApps \ Selenium \ Scripts \ SampleScripts \ SamCodeSample \ test \ SOreports.py”,第22行,在tearDown中 self.assertEqual([], self.verificationErrors) AssertionError: 列表不同: [] != ['注册按钮问题2']

第二个列表包含 1 个附加元素。 第一个额外元素 0: 注册按钮问题 2

  • []
  • ['注册按钮问题 2']

在 13.610 秒内进行 1 次测试

失败(错误 = 1)

正在生成 XML 报告...

代码如下。预先感谢您的帮助!

从硒进口硒 导入unittest,xmlrunner,os,重新

类演示(unittest.TestCase):

def setUp(self):
    self.verificationErrors = []
    self.selenium = selenium("localhost", 4444, "*chrome", "https://workflowy.com/")
    self.selenium.start()

def test_hh(self):
    sel = self.selenium
    sel.open("/accounts/register/")
    try: self.assertEqual("Sign Up FAIL", "Sign Up FAIL","Sign Up button issue1")
    except AssertionError, e: self.verificationErrors.append(str(e))
    try: self.assertEqual("Sign Up FAIL", "Sign Up FAIL1","Sign Up button issue2")
    except AssertionError, e: self.verificationErrors.append(str(e))

def tearDown(self):
    self.selenium.stop()
    self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
#have to format the code this way as SO is complaining about 'bad indent'
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))

I've gone through various tutorials and Stack Overflow posts and understand that Selenium can output XML test results in a way that Hudson can read/report them in HTML format.

What I don't understand is the syntax to use in Python, to get the results to look something like:
Testcase_LoginPage.VerifyButton1Present fail

Testcase_LoginPage.VerifyButton2Present pass

Currently, when I drill down the results in Hudson, they won't be formatted in a useful way as I described above, and also it will report that it only ran ONE test, even though it ran multiple assert Tests:

Traceback (most recent call last):
File "D:\Temp\1TestingApps\Selenium\Scripts\SampleScripts\SamCodeSample\test\SOreports.py", line 22, in tearDown
self.assertEqual([], self.verificationErrors)
AssertionError: Lists differ: [] != ['Sign Up button issue2']

Second list contains 1 additional elements.
First extra element 0:
Sign Up button issue2

  • []
  • ['Sign Up button issue2']

Ran 1 test in 13.610s

FAILED (errors=1)

Generating XML reports...

Code is below. Thanks in advance for the help!

from selenium import selenium
import unittest, xmlrunner, os, re

class Demo(unittest.TestCase):

def setUp(self):
    self.verificationErrors = []
    self.selenium = selenium("localhost", 4444, "*chrome", "https://workflowy.com/")
    self.selenium.start()

def test_hh(self):
    sel = self.selenium
    sel.open("/accounts/register/")
    try: self.assertEqual("Sign Up FAIL", "Sign Up FAIL","Sign Up button issue1")
    except AssertionError, e: self.verificationErrors.append(str(e))
    try: self.assertEqual("Sign Up FAIL", "Sign Up FAIL1","Sign Up button issue2")
    except AssertionError, e: self.verificationErrors.append(str(e))

def tearDown(self):
    self.selenium.stop()
    self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
#have to format the code this way as SO is complaining about 'bad indent'
    unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆夏时光 2024-12-15 11:23:37

您只定义了一项测试,因此它只能报告一项测试。一个测试是一种测试方法,而不是断言语句。您可以在一个测试中进行多个断言,因为您可能需要断言多个结果才能确认成功的测试结果。

因此,获得所需输出的第一步是将第二个断言放入第二个测试方法中,然后您应该看到两个测试结果。

You have only one test defined, so it can only report one test. One test is a test method, not an assert statement. You can have several asserts in one single test, as you may need to assert several results in order to confirm a successful test result.

So first step to your desired output would be to put your second assert into a second test method, then you should see two test results.

萌化 2024-12-15 11:23:37

我终于弄清楚了如何以符合我的需要的有用格式报告验证和断言。问题是,当简单地将 Selenium IDE 录制的脚本导出到 Python RC 文件时,测试的默认结构缺少我需要的很多细节。

我更改了什么:
- 将 Selenium 启动和停止方法放置在 setup 和tearDown 类中,以防止 Selenium 使用每个新定义的验证/断言方法重新启动浏览器
- 通过inspect.stack()添加了包括测试用例名称的错误描述


import inspect, unittest, xmlrunner
from selenium import selenium

class TESTVerifications(unittest.TestCase):
@classmethod
def setUpClass(self):
    self.selenium = selenium("localhost", 4444, "*iexplore", "https://workflowy.com/")
    self.selenium.start() 
    self.selenium.set_timeout("60000")
    print("setUpClass")      
    self.selenium.window_maximize()
    self.selenium.open("/")


def setUp(self):
    self.verificationErrors = []

def test_verification1_error(self):
    try: self.assertEqual("This application is designed", "This application is designedZZZZ",(inspect.stack()[0][3]) +" text missing 'This application is designed'")
    except AssertionError, e: self.verificationErrors.append(str(e))
def test_verification2_error_two_times(self): 
    sel = self.selenium
    ##No such element exception
    try: self.assertEqual("First failure", "First failureZZZZ",(inspect.stack()[0][3]) +" First failure'")
    except AssertionError, e: self.verificationErrors.append(str(e))
    try: self.assertEqual("Second Failure", "Second FailureZZZZ",(inspect.stack()[0][3]) +" Second failure'")
    except AssertionError, e: self.verificationErrors.append(str(e))

def tearDown(self):
    #self.selenium.stop()
    self.assertEqual([], self.verificationErrors,"Results: " + str(self.verificationErrors))
@classmethod    
def tearDownClass(self):

    self.selenium.stop()
    print("tearDownClass")

if __name__ == "__main__":
#    unittest.main()
unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))

I've finally figured out how to make the verifications and assertions get reported in a useful format for my needs. The problem is that the default structure of the tests when simply exporting a Selenium IDE recorded script into a Python RC file lacks a lot of detail I needed.

What I changed:
- Placed the Selenium start and stop methods in the Setup and tearDown classes which prevented Selenium from restarting the browser with each newly defined verification/assertion method
- Added error descriptions that include the testcase name via inspect.stack()


import inspect, unittest, xmlrunner
from selenium import selenium

class TESTVerifications(unittest.TestCase):
@classmethod
def setUpClass(self):
    self.selenium = selenium("localhost", 4444, "*iexplore", "https://workflowy.com/")
    self.selenium.start() 
    self.selenium.set_timeout("60000")
    print("setUpClass")      
    self.selenium.window_maximize()
    self.selenium.open("/")


def setUp(self):
    self.verificationErrors = []

def test_verification1_error(self):
    try: self.assertEqual("This application is designed", "This application is designedZZZZ",(inspect.stack()[0][3]) +" text missing 'This application is designed'")
    except AssertionError, e: self.verificationErrors.append(str(e))
def test_verification2_error_two_times(self): 
    sel = self.selenium
    ##No such element exception
    try: self.assertEqual("First failure", "First failureZZZZ",(inspect.stack()[0][3]) +" First failure'")
    except AssertionError, e: self.verificationErrors.append(str(e))
    try: self.assertEqual("Second Failure", "Second FailureZZZZ",(inspect.stack()[0][3]) +" Second failure'")
    except AssertionError, e: self.verificationErrors.append(str(e))

def tearDown(self):
    #self.selenium.stop()
    self.assertEqual([], self.verificationErrors,"Results: " + str(self.verificationErrors))
@classmethod    
def tearDownClass(self):

    self.selenium.stop()
    print("tearDownClass")

if __name__ == "__main__":
#    unittest.main()
unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文