Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert/VerifyElementPresent with Python and WebDriver?

Tags:

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?

like image 349
Dan Roberts Avatar asked Jan 13 '12 13:01

Dan Roberts


People also ask

Can you use Python with Selenium WebDriver?

Selenium is an open source automation testing tool that supports a number of scripting languages like Python, C#, Java, Perl, Ruby, JavaScript, etc.

Can we do assertion using Selenium WebDriver?

Assert in Selenium WebDriver is used for verifying or validating the scenario under test. Based on the result of the Assert, the outcome (i.e. pass/fail) of the test can be decided. A test scenario is considered as passed if the 'achieved test result' matches with the 'expected test result'.


2 Answers

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.

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.
like image 134
Corey Goldberg Avatar answered Sep 28 '22 10:09

Corey Goldberg


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.

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.

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.

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

like image 20
kenneth koontz Avatar answered Sep 28 '22 11:09

kenneth koontz