46

Ruby has this great abstraction layer on top of Selenium called Capybara, which you can use do functional/acceptance/integration testing. It also has another library called Cucumber which takes this a step further and lets you actually write tests in English.

Both libraries are built on top of Selenium, and can be used to test against any major browser, but because of their abstraction layers it's super easy to write tests using them (well, as easy as functional testing gets at least).

My question is: does Python have anything like that? I have found Pythonistas doing functional testing with various tools but ...

A) Splinter: doesn't use Selenium (and doesn't have an IE driver)

-EDIT- It appears Spliter now does use Selenium (see answers below).

B) Alfajor: hasn't been updated in over a year; looks dead

C) Selenium (raw): a lot of people seem to be using Selenium directly, but it seems like an abstraction layer could make it a lot easier to use

So, does anyone know of anything Capybara-like, or better yet Cucumber-like, for Python (it doesn't have to actually use Selenium, but it needs to support all major browsers)?

* EDIT *

For those that aren't familiar with Capybara, it basically just adds an API so that instead of the normal Selenium API you can do something like this:

When /I sign in/ do
  within("#session") do
    fill_in 'Login', :with => 'user@example.com'
    fill_in 'Password', :with => 'password'
  end
  click_link 'Sign in'
end

It's used by Cucumber, which let's you further abstract (almost to English):

Scenario Outline: Add two numbers
Given I have entered <input_1> into the calculator
And I have entered <input_2> into the calculator
When I press <button>
Then the result should be <output> on the screen

Examples:
| input_1 | input_2 | button | output |
| 20 | 30 | add | 50 |

I would LOVE a Python Cucumber equivalent, but even just a Capybara equivalent would be helpful.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
machineghost
  • 33,529
  • 30
  • 159
  • 234
  • 1
    While it's true that splinter doesn't support IE, it does use Selenium. Take a look at the how the web drivers are implemented https://github.com/cobrateam/splinter/tree/master/splinter/driver/webdriver – Lorin Hochstein Aug 31 '12 at 18:51
  • Well, it looks like Splinter has improved significantly since I last looked at it (instead of the professional-looking site it has now, I remember a white page with a rat sketch). That being said, IMHO if someone is evaluating a library, they shouldn't have to dig in to the driver code to learn about it; the library should say as much on the front page. While the new site is great, I still see nothing about Selenium on the front page. – machineghost Aug 31 '12 at 21:16
  • 2
    http://behave.readthedocs.org/en/latest/index.html – yurisich May 30 '13 at 13:26

7 Answers7

24

You can test Python code using Cucumber - see the Cucumber wiki on github for more information.

If you want a pure Python solution, check out Lettuce. I've never used it, but there is a fairly useful looking blog entry about it and splinter here.

D_Bye
  • 869
  • 6
  • 10
  • Lettuce looks awesome! I knew I could use Cucumber with any language, but doing so would have required A) programming my tests in Ruby ("elsif" ... who does that?) and B) getting all of my co-workers to learn Ruby. With Lettuce, it looks like I won't have to suffer either of those trade-offs, thanks a bunch. – machineghost Mar 01 '12 at 16:09
  • 2
    Follow-up comment: Now that I've actually used it, Lettuce doesn't just look awesome ... it is in fact awesome :-) Not only does it work great and do everything (that I cared about) that Cucumber did, but it also makes the Python Selenium driver work better! Thanks again for the suggestion. – machineghost Apr 06 '12 at 18:36
  • 1
    Out of interest, did you find any of these issues a problem with Lettuce? http://packages.python.org/behave/comparison.html#lettuce – Dave C Apr 08 '12 at 18:56
  • 1
    I didn't find any of those things to be issues for me (I don't even know what he means by "tags"). However, we're mainly using Lettuce for it's Selenium helper stuff these days; for totally unrelated reasons we abandoned the BDD-style text-based tests stuff. It worked great while we were using it though :-) – machineghost Aug 31 '12 at 21:11
  • Looks like cucumber is no longer being maintained – Bryce Guinta Oct 02 '18 at 22:41
21

A. Cucumber like: ( English like)

  • Lettuce (Approach Gherkin) or
  • Behave (Approach Gherkin) or
  • Robotframework (Approach keyword based) (Additional Info: RF is larger than English Like criteria. Its keyword-based and offers loads of helper method and inbuilt libraries. Great eco-sysstem for external libraries. Any python script can also be modified and used along with RF)
  • Freshen (Approach Gherkin) or
  • Pea (Approach Gherkin) or
  • RedwoodHQ (Approach keyword based) (RedwoodHQ has features larger than 'English-Like' criteria and encapsulates the following features: Keyword based, web-based test-framework, supports python as one of languages and much more. Additional info on RedwoodHQ: Theoretically its possible, all the existing robot-framework inbuilt libraries and all robot-framework external test-libraries or for that matter any python library, can be called or used from this web based test framework with little modification)

  • Gauge (Gherkin approach): Reference for python: (https://gauge-python.readthedocs.io/en/latest/index.html)


Underneath Cucumber, one could have Capybara like abstraction layer that hides/ groups many of selenium actions


B. Capybara like: ( Abstraction: hides / groups action)

As one e.g to click an element, its sufficient to provide command like click(locator) instead of working with raw selenium api, where one needs to find an element and then click. Many more such abstraction exist in the optional libraries below

  • Option-1 (see below)
  • Option-2 (see below)
  • Option-3 (see below)
  • Option-4 (see below)
  • Option-5 (see below)
  • Option-6 : Helium (from others Answer/comment)
  • Option-7 : (see below)
  • Option-8 : (see below)
  • Option-9: (see below)
  • Option-10: (see below)
  • Option-11: (see below)
  • Option-12: (see below)
  • Option-13: (see below)

My research: There exist almost half a dozen a. active, b. mature c.developed options.

python comes with variety of batteries included !!


Option-1: Selenium2Library

Github url: https://github.com/rtomac/robotframework-selenium2library

Developement: Active

Purpose: one of the many libraries of robotframework, can also be used as "standalone" library for your framework (Check e.g below for usage).

Thoughts:

  • It provides an abstraction over selenium
  • input of arguments to methods in this library are lot simpler. Abstraction provided by library, one e.g, hides many of the unnecessary details for finding elements. For more details one needs to understand the library
  • It is possible to use this library outside robot-framework context as such without any modification, though it could be using utilities of robot package. (Its your homework to do further experiments with this lib, on this note!)
  • Therefore, can be used as a standalone library for your framework.

Usage:

pip install robotframework-selenium2library

import in your ipython or idle console and start playing e.g:

>>from Selenium2Library import Selenium2Library
>>start_testing= Selenium2Library()
>>start_testing.create_webdriver("Firefox")
>>start_testing.go_to("http://www.google.com")
>>.
...so on

Option-2: Pageobjects

Github url: https://github.com/ncbi/robotframework-pageobjects

Development: InActive (no show stoppers with latest release)

Purpose: One of the libraries of robotframework. Provides a page object abstraction over Selenium2Library. Can be used as Standalone for your framework (Check e.g below for usage) or can be used along with robotframework.

Thoughts:

  • It provides a "pageobject abstraction" support on top of Selenium2Library
  • It is possible to use this library outside robot-framework context as such without any modification, though it could be using utilities of robot package. (Its your homework to do further experiments on this note!)
  • Therefore, can be used as a standalone library

Usage:

pip install robotframework-pageobjects

e.g: in ipython or idle do:

>>from robotpageobjects import Page
>>start_testing=Page()
>>start_testing.create_webdriver("Firefox")
>>start_testing.go_to("http://google.com")

Option-3: robotframework-pageobjectlibrary

Github Url: https://github.com/boakley/robotframework-pageobjectlibrary

Development: Active

Hopefully Author supports LTS (Long Term Support) : )) , Fingers crossed !!

Usage:

pip install robotframework-pageobjectlibrary

Thoughts:

  • It is NOT possible to use this library outside robot-framework context. A minor change to how the page context is handled would help this library to be used outside robot-framework context (Its your homework to find out how!)

Option-4: Splinter

Github url: https://github.com/cobrateam/splinter

Developement: Active

Usage: splinter.readthedocs.org/en/latest/index.html

pip install splinter

On ipython or idle do:

>>from splinter import Browser    
>>browser = Browser()
>>browser.visit('http://google.com')
>>browser.fill('q', 'splinter - python acceptance testing for web applications')
>>browser.find_by_name('btnG').click()

Option-5: SST library

Github url: https://github.com/Work4Labs/selenium-simple-test

Development: Feature complete / Active

Usage: testutils.org/sst/

pip install -U sst

on ipython or idle do:

>>> from sst.actions import *
>>> start()

    Starting Firefox
>>> go_to('http://google.com')
    Going to... http://google.com
    Waiting for get_element

Option-6: helium Not open source (Commercial)

Option-7: holmium.core

Github url: https://github.com/alisaifee/holmium.core

Option-8: wtframework

Github url: https://github.com/wiredrive/wtframework

Option-9: webium

Github url: https://github.com/wgnet/webium

Option-10: elementium

Github url: https://github.com/actmd/elementium

Option-11: saunter

Github url: https://github.com/Element-34/py.saunter

Usage: saunter

Option-12: webdriverplus

Github url: https://github.com/tomchristie/webdriverplus

Usage: webdriverplus

Comments: repository not maintained but decent reference

Option-12: Simple-Pageobject

Github url: https://github.com/rama-bornfree/simple-pageobject/tree/master/PageObjectLibrary

Comments: Simplest pageobject wrapper built around selenium2library. I am the owner of the repo


Test-setup:

"All" the Test libraries in Option-1-13; can be run using any of the following framework: Lettuce, Behave, Robotframework or for that matter, any unit test framework(e.g PyUnit, Nose)...so on .

Test Framework is in general used to manage test-cases e.g

  • English formats like gherkin, keyword, tabular so on ...
  • reporting a test-run
  • hooking to CI
  • set-up/tear-down of test-cases and test-suites
  • tagging of test-cases
  • other functionalities that one could think on about any test-framework

What matters is how comfortable one gets with libraries in above options.

Option-5: As far as SST is concerned, it has features of a framework itself, e.g it can generate report and do many more things.

So the definition of library and framework in the case of SST is blurred, depending on the extent of features one would like to use from that package


Some Math for Fun:

Total number of ways one could have a good, bad, and ugly Test-setup = (Test framework AND Test library + your custom code sandwiched b/w the framework and library ):

7 * 13 = 91 Ways

Choose the best combination (of Test Framework And Test library) that suits ones need !!

I would personally go for Robot-framework with Selenium2Library or Robot-framework with some pageobject library

ofcourse, I am leaned and positively biased in my post about robot-framework and Selenium2Library

Steve Harrison
  • 315
  • 3
  • 8
  • At first I downvoted this answer because none of the libraries listed seemed to be abstracting close to English. However I re-read my question and realized it wasn't as clear on that point as it could have been, and since all of these libraries do abstract things *to some degree* closer to English they are in fact "Capybara-like". I've edited my question to be clearer, and if you edit this answer I will be happy to change my vote (SO won't let me change it until then). – machineghost Feb 27 '15 at 20:47
  • sure, i will edit my answer, would appreciate if up voted as I had tried my best to be precise and helpful – Steve Harrison Feb 28 '15 at 08:10
  • libraries makes coding lesser, focussed, with various choices underneath by offering abstraction. I wouldnot agree to the statement where it seems to be understood that libraries abstract close to english – Steve Harrison Feb 28 '15 at 09:03
  • only the tool above the library layer, offers testcases close to english – Steve Harrison Feb 28 '15 at 09:04
12

While the OP was happy with finding a Python Cucumber equivalent, what led me here was the question title: a Python equivalent of Capybara. While Cucumber uses Capybara, Cucumber itself is a whole different "solution" that is only incidentally related to Capybara.

If you're looking for something Capybara-like without having to deal with Cucumber, check out splinter. I don't know what was true when the question was posted, but Splinter is now built on Selenium, and supports other engines as well (Webkit, PhantomJS, zope.browsertest, and others), and supports both visual and headless testing.

Evan O.
  • 53
  • 4
Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
  • 1
    I've edited both the title and content of my question to try and be clearer. My original goal (which the title didn't quite convey) was to find Cucumber-like libraries, but since I wasn't sure any existed I was also curious about Capybara-like libraries. So answers containing either Cucumber-like OR Capybara-like libraries are relevant; thanks. – machineghost Feb 27 '15 at 20:55
9

How about Robot Framework. It's pretty awesome. And with Selenium2Library it works really well with SE2. http://robotframework.org/

Aaron
  • 3,249
  • 4
  • 35
  • 51
9

There does now exist a port of Capybara itself to Python:

https://github.com/elliterate/capybara.py

You can find its documentation here:

https://elliterate.github.io/capybara.py/

Capybara helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and comes with Selenium support built in.

Brandon Rhodes
  • 83,755
  • 16
  • 106
  • 147
  • 3
    Brandon Rhodes gave a presentation about capybara being used for acceptance testing at Dropbox: [slides](http://rhodesmill.org/brandon/slides/2016-11-pyconca/), [video](https://www.youtube.com/watch?v=z1aUuqKg_gA). Apparently, it was some engineer at Dropbox who made the python port of capybara. – ccpizza Jun 25 '17 at 19:44
7

Have you checked freshen, or pea?

Pea does not use the syntax of cucumber, but the author says that is easier https://github.com/gfxmonk/pea

And Freshen is trying to clone Cucumber's syntax and functionalities

https://github.com/rlisagor/freshen

Tony S.
  • 79
  • 1
  • 2
3

The OP asked for Python implementations of Cucumber or Capybara but as Jim Stewart pointed out in his answer, Cucumber and Capybara are very different things. Since the title of the question is about Capybara, that's what I will answer.

I am one of the developers of a commercial Selenium wrapper called Helium. Like Capybara, it offers a very high-level API for web automation. For example, here is a script that updates your Facebook status:

from helium.api import *
start_chrome("facebook.com")
write(your_fb_email, into="Email or Phone")
write(your_fb_password, into="Password")
click("Log In")
write("Test", into="Update Status")
click("Post")

Calls to Helium can freely be mixed with calls to Selenium. Eg. we could extend the above script by:

# get_driver() returns the WebDriver created by start_chrome() above.
chrome = get_driver()
chrome.find_element_by_id('btnG').click()
Community
  • 1
  • 1
Michael Herrmann
  • 4,832
  • 3
  • 38
  • 53
  • They're very different, but also intrinsically linked (you can't have Cucumber without Capybara). I tried to make clear in my original answer that I was looking for something Cucumber-like, but since I wasn't sure that even existed I'd also like to know about Capybara-like libraries. I've since edited my answer to try and make this clearer. – machineghost Feb 27 '15 at 20:50