簡體   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