使用Python和WebDriver斷言/ VerifyElementPresent?

[英]Assert/VerifyElementPresent with Python and WebDriver?


I may just be confused by the change from Selenium to WebDriver and their respective documentation. In a section about test design in the documentation there is talk of using Assert vs Verify such as AssertElementPresent. However in going through the WebDriver tutorial and beginning to setup tests this does not seem to be available from Python. Am I overlooking something in the documentation, is this not applicable to WebDriver, not applicable to using python, should I use capabilities of python and not assert/verify command from selenium, etc?

我可能會對從Selenium到WebDriver及其各自文檔的更改感到困惑。在文檔中有關測試設計的部分中,討論了如何使用Assert vs Verify,例如AssertElementPresent。但是,在瀏覽WebDriver教程並開始設置測試時,Python似乎無法提供。我是否忽略了文檔中的內容,這是不適用於WebDriver,不適用於使用python,我應該使用python的功能而不是來自selenium的assert / verify命令等?

5 个解决方案

#1


19  

webdriver is a library for driving browsers. what you want to use are the *find_element* methods to locate elements and then assert conditions against them.

webdriver是一個用於驅動瀏覽器的庫。你想要使用的是* find_element *方法來定位元素然后斷言它們的條件。

for example, this code does an assertion on content of an element:

例如,此代碼對元素的內容進行斷言:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://www.example.com')
element = browser.find_element_by_tag_name('h1')
assert element.text == 'Example Domains'
browser.quit()
  • note this example is pure python with a bare assert. It is better to use a test framework like python's unittest, which has more powerful assertions.
  • 注意這個例子是帶有裸斷言的純python。最好使用像python的unittest這樣的測試框架,它具有更強大的斷言。

#2


6  

In Selenium RC, verify/assert methods exist. In WebDriver, they don't. Also, its important to note what verify and assert does and their role in your tests. In Selenium RC, verify is used to perform a check in your test, whether it may be on text, elements, or what have you. Assert, on the other hand, will cause a test to fail and stop. The benefits and advantages are discussed in the link you referenced.

在Selenium RC中,存在驗證/斷言方法。在WebDriver中,他們沒有。此外,重要的是要注意驗證和斷言的作用以及它們在您的測試中的作用。在Selenium RC中,驗證用於執行測試,無論是文本,元素還是您的測試。另一方面,斷言會導致測試失敗並停止。您引用的鏈接中討論了優點和優點。

WebDriver doesn't have verify/assert methods per say. Assertions are performed in the test itself. If you take a look at Corey's answer, he performs an "assert" on an element's text. If the element's text is not 'Example Domains' an AssertionError will be raised, effectively failing your test. But what about a verify? Well as mentioned, WebDriver doesn't have one. But you could still perform something equivalent by doing a comparison.

WebDriver沒有每個說法的驗證/斷言方法。斷言在測試本身中執行。如果你看看Corey的答案,他會對元素的文本執行“斷言”。如果元素的文本不是“示例域”,則會引發AssertionError,從而導致測試失敗。但驗證怎么樣?如上所述,WebDriver沒有。但是你仍然可以通過比較來執行相同的操作。

if element.text != u'Example Domains':
    print "Verify Failed: element text is not %r" % element.text

So in this case, your test won't fail. But a verification will still take place and will print to stdout.

所以在這種情況下,您的測試不會失敗。但是仍然會進行驗證並打印到stdout。

So in the end, it's a matter of what you want to fail. It's more of a test design. Hope this helps.

所以最后,這是你想要失敗的問題。它更像是一種測試設計。希望這可以幫助。

#3


4  

You should use the following function to check that:

您應該使用以下函數來檢查:

def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException as e: return False
    return True

Which is default generated by Selenium IDE when exporting to Python code.

這是Selenium IDE在導出到Python代碼時默認生成的。

Then you can assert the element as below:

然后你可以斷言元素如下:

self.assertTrue(self.is_element_present(By.ID, "footer"))
self.assertTrue(self.is_element_present(By.CSS_SELECTOR, "header.global-header"))

Note that the following import is required to use By:

請注意,使用以下導入需要:

from selenium.webdriver.common.by import By

#4


0  

Another way is as follows:

另一種方式如下:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.testing.com')
element = driver.find_element_by_xpath('xpath of the element').text
assert element == 'Sample Text'
driver.quit()

#5


-1  

Verify the Assert

驗證斷言

driver= webdriver.Firefox()

String x =driver.findElement(By.xpath("//*[@id='userNavLabel']")).getText();

System.out.println(x);

//Assert.assertEquals(actual, expected)

Assert.assertEquals("Anandan Aranganath", x);

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/01/13/72f63810da374961bb1eba33596f0d6f.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com