Package: behave-webdriver
behave-webdriverbehave-webdriver is a step library intended to allow users to easily writeseleniumwebdriver tests with the
behave BDD testing framework.
Inspired by thewebdriverio/cucumber-boilerplateproject.For more details, see thebehave-webdriver documentationInstallationInstallation is easy via pip. The install will requirebehaveandselenium.:pip install behave-webdriverUsing webdriversSelenium requires that you provide executables for the webdriver you want to use. Further, unless you specify the path to
the binary explicitly, selenium expects that this executable is in PATH. See thesedriver installation notesfor more details.UsageBasic usage of this library with behave requires the following steps:write your feature fileimport the step implementationsset thebehave_driverattribute on the behavecontextin yourenvironment.pyfile.runbehaveWriting the feature file# my-minimal-project/features/myFeature.featureFeature:Sample Snippets test
As a developer
I should be able to use given text snippetsScenario:open URLGiventhe page url is not "http://webdriverjs.christian-bromann.com/"AndI open the url "http://webdriverjs.christian-bromann.com/"ThenI expect that the url is "http://webdriverjs.christian-bromann.com/"AndI expect that the url is not "http://google.com"Scenario:click on linkGiventhe title is not "two"AndI open the url "http://webdriverjs.christian-bromann.com/"WhenI click on the link "two"ThenI expect that the title is "two"Importing the step implementationsIn order for your feature file steps to match our step implementations, behave needs to find them in your project.
This is as simple as importing our step definitions into your own step implementation file.# features/steps/webdriver_example.pyfrombehave_webdriver.stepsimport*For more information aboutstep implementations, see the behave tutorial.Set behave_driver in the environmentOur step implementations specifically look at the behave context for abehave_driverattribute to use to run your tests.
In order for that to work, you’ll have to provide this attribute in yourenvironment.pyfile.# features/environment.pyimportbehave_webdriverdefbefore_all(context):context.behave_driver=behave_webdriver.Chrome()defafter_all(context):# cleanup after tests runcontext.behave_driver.quit()The webdriver classes provided by behave-webdriver inherit from selenium’s webdriver classes, so they will accept all
same positional and keyword arguments that selenium accepts.Some webdrivers, such as Chrome, provide special classmethods likeChrome.headlesswhich instantiatesChromewith
options to run headless. This is useful, for example in headless testing environments.defbefore_all(context):context.behave_driver=behave_webdriver.Chrome.headless()Using a fixtureNew in 0.1.1You may also find it convenient to use a fixture to setup your driver as well. For example, to use our fixture with Firefoxfrombehave_webdriver.fixturesimportfixture_browserdefbefore_all(context):use_fixture(fixture_browser,context,webdriver='Firefox')This will also ensure that the browser is torn down at the correspondingcleanup point.Run behaveThen run the tests, just like any other behave testbehaveYou should then see an output as follows:Feature: Sample Snippets test # features/myFeature.feature:2
  As a developer
  I should be able to use given text snippets
  Scenario: open URL                                                          # features/myFeature.feature:6
    Given the page url is not "http://webdriverjs.christian-bromann.com/"     # ../../behave_webdriver/steps/given.py:136 0.012s
    And I open the url "http://webdriverjs.christian-bromann.com/"            # ../../behave_webdriver/steps/given.py:10 1.414s
    Then I expect that the url is "http://webdriverjs.christian-bromann.com/" # ../../behave_webdriver/steps/then.py:102 0.007s
    And I expect that the url is not "http://google.com"                      # ../../behave_webdriver/steps/then.py:102 0.007s

  Scenario: click on link                                          # features/myFeature.feature:13
    Given the title is not "two"                                   # ../../behave_webdriver/steps/given.py:81 0.006s
    And I open the url "http://webdriverjs.christian-bromann.com/" # ../../behave_webdriver/steps/given.py:10 0.224s
    When I click on the link "two"                                 # ../../behave_webdriver/steps/when.py:21 0.622s
    Then I expect that the title is "two"                          # ../../behave_webdriver/steps/then.py:10 0.006s

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
8 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m2.298sAdvanced usage; extending behave-webdriverbehave-webdriver is designed withyouin-mind. You are free to extend the behavior of our webdriver classes to suit your
unique needs. You can subclass our webdriver classes, use a custom selenium webdriver, write your own mixin, or use
a mixin somebody else provides for selenium.Example: selenium-requestsselenium-requestsis a preexisting project that adds functionality of the popularrequestslibrary to selenium.
It is simple to useselenium-requestswith behave-webdriver.
The following, and other examples, are available in the repoexamplesdirectory and in the full documentation.# examples/selenium-requests/features/environment.pyfromseleniumimportwebdriver# or any custom webdriverfrombehave_webdriver.driverimportBehaveDriverMixinfromseleniumrequestsimportRequestMixin# or your own mixinclassBehaveRequestDriver(BehaveDriverMixin,RequestMixin,webdriver.Chrome):passdefbefore_all(context):context.behave_driver=BehaveRequestDriver()# examples/selenium-requests/features/steps/selenium_steps.pyfrombehaveimport*frombehave_webdriver.stepsimport*fromurllib.parseimporturljoin@given('I send a{method}request to the page "{page}"')defsend_request_page(context,method,page):url=urljoin(context.base_url,page)context.response=context.behave_driver.request(method,url)@then('I expect the response text contains "{text}"')defcheck_response_text_contains(context,text):asserttextincontext.response.text# examples/selenium-requests/features/selenium-requests.featureFeature:Using selenium-requests
  As a developer
  I should be able to extend behave-webdriver with selenium-requestsScenario:use selenium-requests with behave-webdriver# use a behave-webdriver stepGiventhe base url is "http://127.0.0.1:8000"# use your own steps using selenium-requests featuresGivenI send a GET request to the page "/"ThenI expect the response text contains "<h1>DEMO APP</h1>"Assuming you’re in the repository root (and have the demo app running) just run like any other project withbehaveResults ✨(behave-webdriver) $ behave examples/selenium-requests/features

DevTools listening on ws://127.0.0.1:12646/devtools/browser/1fe75b44-1c74-49fa-8e77-36c54d50cd24
Feature: Using selenium-requests # examples/selenium-requests/features/requests.feature:1
  As a developer
  I should be able to extend behave-webdriver with selenium-requests
  Scenario: use selenium-requests with behave-webdriver          # examples/selenium-requests/features/requests.feature:6
    Given the base url is "http://127.0.0.1:8000"                # behave_webdriver/steps/actions.py:162
    Given I send a GET request to the page "/"                   # examples/selenium-requests/features/steps/selenium_steps.py:11
    Then I expect the response text contains "<h1>DEMO APP</h1>" # examples/selenium-requests/features/steps/selenium_steps.py:17

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m1.385sGetting help ⛑If you have any unanswered questions or encounter any issues, please feel welcome to raise an issue. We recognize that
testers come in all different shapes, sizes, and backgrounds. We welcome any and all questions that may arise from using
this library.ContributingContributions are very much welcomed! If you have ideas or suggestions, please raise an issue or submit a PR.List of step definitions 📝We support all the steps supported by webdriverio/cucumber-boilerplate.
We also support some additional niceties and plan to add more step definitions.Given Steps 👷I open the site"([^"]*)?"I open the url"([^"]*)?"I have a screen that is([\d]+)by([\d]+)pixelsI have a screen that is([\d]+)pixels (broad|tall)I have closed all but the first (window|tab)I pause for(\d+)*msa (alertbox|confirmbox|prompt) is( not)* openedthe base url is"([^"]*)?"the checkbox"([^"]*)?"is( not)* checkedthe cookie"([^"]*)?"contains( not)* the value"([^"]*)?"the cookie"([^"]*)?"does( not)* existthe element"([^"]*)?"contains( not)* the same text as element"([^"]*)?"the element"([^"]*)?"is( not)*([\d]+)px(broad|tall)the element"([^"]*)?"is( not)* emptythe element"([^"]*)?"is( not)* enabledthe element"([^"]*)?"is( not)* positioned at([\d]+)pxon the (x|y) axisthe element"([^"]*)?"is( not)* selectedthe element"([^"]*)?"is( not)* visiblethe element"([^"]*)?"(not)* contains any textthe element"([^"]*)?"(not)* contains the text"([^"]*)?"the element"([^"]*)?"(not)* matches the text"([^"]*)?"the page url is( not)*"([^"]*)?"the title is( not)*"([^"]*)?"the( css)* attribute"([^"]*)?"from element"([^"]*)?"is( not)*"([^"]*)?"there is (an|no) element"([^"]*)?"on the pageWhen Steps ▶️I open the site"([^"]*)?"I open the url"([^"]*)?"I accept the (alertbox|confirmbox|prompt)I add "{value}" to the inputfield "{element}"I clear the inputfield "{element}"I click on the button "{element}"I click on the element "{element}"I click on the link "{link_text}"I close the last opened (tab|window)I delete the cookie "{cookie_key}"I dismiss the (alertbox|confirmbox|prompt)I doubleclick on the element "{element}"I drag element "{from_element}" to element "{to_element}"I enter"([^"]*)?"into the (alertbox|confirmbox|prompt)I focus the last opened (tab|window)I move to element "{element}" with an offset of{x_offset:d},{y_offset:d}I move to element "{element}"I pause for {milliseconds:d}msI press "{key}"I scroll to element "{element}"I select the option with the (text|value|name)"([^"]*)?"for element"([^"]*)?"I select the {nth} option for element "{element}"I set "{value}" to the inputfield "{element}"I set a cookie "{cookie_key}" with the content "{value}"I submit the form "{element}"Then Steps ✔️I expect the screen is([\d]+)by([\d]+)pixelsI expect a new (window|tab) has( not)* been openedI expect that a (alertbox|confirmbox|prompt) is( not)* openedI expect that a (alertbox|confirmbox|prompt)( not)* contains the text"([^"]*)?"I expect that checkbox"([^"]*)?"is( not)* checkedI expect that cookie"([^"]*)?"(not)* contains"([^"]*)?"I expect that cookie"([^"]*)?"(not)* existsI expect that element"([^"]*)?"(has|does not have) the class"([^"]*)?"I expect that element"([^"]*)?"becomes( not)* visibleI expect that element"([^"]*)?"does( not)* existI expect that element"([^"]*)?"is( not)*([\d]+)px(broad|tall)I expect that element"([^"]*)?"is( not)* emptyI expect that element"([^"]*)?"is( not)* enabledI expect that element"([^"]*)?"is( not)* focusedI expect that element"([^"]*)?"is( not)* positioned at([\d]+)pxon the (x|y) axisI expect that element"([^"]*)?"is( not)* selectedI expect that element"([^"]*)?"is( not)* visibleI expect that element"([^"]*)?"is( not)* within the viewportI expect that element"([^"]*)?"(not)* contains any textI expect that element"([^"]*)?"(not)* contains the same text as element"([^"]*)?"I expect that element"([^"]*)?"(not)* contains the text"([^"]*)?"I expect that element"([^"]*)?"(not)* matches the text"([^"]*)?"I expect that the path is( not)*"([^"]*)?"I expect that the title is( not)*"([^"]*)?"I expect that the url is( not)*"([^"]*)?"I expect that the( css)* attribute"([^"]*)?"from element"([^"]*)?"is( not)*"([^"]*)?"I expect the url"([^"]*)?"is opened in a new (tab|window)I expect the url to( not)* contain"([^"]*)?"I wait on element"([^"]*)?"(?:for(\d+)ms)*(?:to( not)* (be checked|be enabled|be selected|be visible|contain a text|contain avalue|exist))*Acknowledgements ❤️Special thanks to the authors and contributors of thewebdriverio/cucumber-boilerplateprojectSpecial thanks to the authors and contributors ofbehave