繁体   English   中英

Selenium的Python Webdriver

[英]Python webdriver from Selenium

我是硒自动化领域的新手。 我创建了一个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>'

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

这是来自webdriver文档的注释示例,做了一些修改。 为了创建单元测试,请创建一个继承unittest模块提供的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, Safari,Opera)。

运行它可以像运行任何python脚本一样完成。 只需使用:

python <script_name.py>

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

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

在直接运行此文件(如./selenium_test.py时,使脚本运行测试。 还可以从多个文件中自动收集测试用例,然后一起运行它们(请参阅unittest文档)。 在某个模块或某个单独的测试中运行测试的另一种方式是

python -m unittest selenium_test.GoogleTest

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM