0

我是硒自动化的新手。我创建了一个 Selenium 测试用例和测试套件。我将测试套件导出为 Python webdriver。

我应该如何执行这个 python 代码?我试过这个:

./pythonwebdriver <selenium test case.html>

我收到此错误:

Traceback (most recent call last):
File "./pythondriver.py", line 52, in <module>
unittest.main()
File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
self.parseArgs(argv)
File "/usr/lib/python2.7/unittest/main.py", line 149, in parseArgs
self.createTests()
File "/usr/lib/python2.7/unittest/main.py", line 158, in createTests
self.module)
File "/usr/lib/python2.7/unittest/loader.py", line 128, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute '<testcasename>'
4

2 回答 2

6

没有 Python webdriver 这样的东西。Webdriver是一个驱动网页的组件。它已被集成到 Selenium 2。它本身在 Java 中工作,但有许多语言可用的绑定,包括Python

这是 webdriver 文档中的一个带注释的示例,稍作修改。为了创建一个单元测试,创建一个继承单元测试模块提供的类 TestCase 的测试类。

#!/usr/bin/python

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import unittest

class GoogleTest(unittest.TestCase):
    def test_basic_search(self):
        # Create a new instance of the Firefox driver
        driver = webdriver.Firefox()
        driver.implicitly_wait(10)

        # go to the google home page
        driver.get("http://www.google.com")

        # find the element that's name attribute is q (the google search box)
        inputElement = driver.find_element_by_name("q")

        # type in the search
        inputElement.send_keys("Cheese!")

        # submit the form (although google automatically searches 
        # now without submitting)
        inputElement.submit()

        # the page is ajaxy so the title is originally this:
        original_title = driver.title

        try:
            # we have to wait for the page to refresh, the last thing 
            # that seems to be updated is the title
            WebDriverWait(driver, 10).until(lambda driver :  
                                     driver.title != original_title)
            self.assertIn("cheese!", driver.title.lower())

            # You should see "cheese! - Google Search"
            print driver.title
        finally:
            driver.quit()

if __name__ == '__main__':
    unittest.main()

关于 webdriver 的一个好处是您可以将驱动程序行更改为

driver = webdriver.Chrome()
driver = webdriver.Firefox()
driver = webdriver.Ie()

取决于您需要测试的浏览器。除了ChromeDriverFirefoxDriverInternetExplorerDriver之外,还有最轻量级的HtmlUnitDriver可以无头运行(但可能运行一些与浏览器不同的 javascript),RemoteWebDriver允许在远程机器上并行运行测试,以及许多其他(iPhone、Android、野生动物园,歌剧)。

运行它可以像运行任何 python 脚本一样完成。要么只是:

python <script_name.py>

或在第一行包含解释器名称,!#/usr/bin/python如上。最后两行

if __name__ == '__main__':
    unittest.main()

当这个文件像./selenium_test.py. 也可以从多个文件中自动收集测试用例并将它们一起运行(参见 unittest 文档)。在某些模块或某些单独测试中运行测试的另一种方法是

python -m unittest selenium_test.GoogleTest
于 2012-11-26T18:41:44.047 回答
1

您的脚本调用unittest.main()处理命令行参数:<selenium test case.html>. unittest.main()期望测试模块、测试类或测试方法的名称作为命令行参数,而不是<selenium test case.html>.

于 2012-11-27T07:19:03.497 回答