我是硒自动化的新手.我创建了一个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>'
Run Code Online (Sandbox Code Playgroud)
没有Python webdriver这样的东西.Webdriver是用于驱动网页的组件.它已经集成到Selenium 2.本地它在Java中工作,但是有许多语言可用的绑定,包括Python.
这是一个带注释的例子,来自webdriver文档的一些修改.要创建unittest,请创建一个继承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()
Run Code Online (Sandbox Code Playgroud)
关于webdriver的一个好处是你可以改变驱动程序行
driver = webdriver.Chrome()
driver = webdriver.Firefox()
driver = webdriver.Ie()
Run Code Online (Sandbox Code Playgroud)
取决于您需要测试的浏览器.除了ChromeDriver,FirefoxDriver或InternetExplorerDriver之外,还有HtmlUnitDriver,它是最轻量级的,可以运行无头(但可能运行一些javascript与浏览器不同),RemoteWebDriver允许在远程机器上并行运行测试,以及许多其他(iPhone,Android, Safari,Opera).
运行它可以像运行任何python脚本一样完成.要么只是:
python <script_name.py>
Run Code Online (Sandbox Code Playgroud)
或者!#/usr/bin/python如上所述在第一行包括解释器名称.最后两行
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
当这个文件直接运行时,让脚本运行测试./selenium_test.py.也可以从多个文件中自动收集测试用例并将它们一起运行(参见unittest文档).在某些模块或某些单独测试中运行测试的另一种方法是
python -m unittest selenium_test.GoogleTest
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3317 次 |
| 最近记录: |