|
| 1 | +<h2><img src="https://seleniumbase.io/img/logo6.png" title="SeleniumBase" width="32" /> 🐝 Behave test runner for SeleniumBase 🐝</h2> |
| 2 | + |
| 3 | +🐝 (Utilizes the [Behave BDD Python library](https://github.com/behave/behave). For more info, see the [Behave tutorial](https://behave.readthedocs.io/en/stable/tutorial.html) and read about [Behave's Gherkin model](https://behave.readthedocs.io/en/stable/gherkin.html).) |
| 4 | + |
| 5 | +🐝 Behave examples with SeleniumBase: [SeleniumBase/examples/behave_bdd](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/behave_bdd) |
| 6 | + |
| 7 | +```bash |
| 8 | +> cd examples/behave_bdd/ |
| 9 | +> behave features/realworld.feature -T -D dashboard -k |
| 10 | + |
| 11 | +Dashboard: /Users/michael/github/SeleniumBase/examples/behave_bdd/dashboard.html |
| 12 | +******************************************************************************** |
| 13 | +Feature: SeleniumBase scenarios for the RealWorld App # features/realworld.feature:1 |
| 14 | + |
| 15 | + Scenario: Verify RealWorld App (log in / sign out) # features/realworld.feature:3 |
| 16 | + Given Open "seleniumbase.io/realworld/login" # ../../sbase/steps.py:4 |
| 17 | + And Clear Session Storage # ../../sbase/steps.py:374 |
| 18 | + When Type "demo_user" into "#username" # ../../sbase/steps.py:22 |
| 19 | + And Type "secret_pass" into "#password" # ../../sbase/steps.py:22 |
| 20 | + And Do MFA "GAXG2MTEOR3DMMDG" into "#totpcode" # ../../sbase/steps.py:179 |
| 21 | + Then Assert exact text "Welcome!" in "h1" # ../../sbase/steps.py:75 |
| 22 | + And Highlight "img#image1" # ../../sbase/steps.py:82 |
| 23 | + And Click 'a:contains("This Page")' # ../../sbase/steps.py:13 |
| 24 | + And Save screenshot to logs # ../../sbase/steps.py:107 |
| 25 | + When Click link "Sign out" # ../../sbase/steps.py:91 |
| 26 | + Then Assert element 'a:contains("Sign in")' # ../../sbase/steps.py:52 |
| 27 | + And Assert text "You have been signed out!" # ../../sbase/steps.py:68 |
| 28 | + ✅ Scenario Passed! |
| 29 | + |
| 30 | +- Dashboard: /Users/michael/github/SeleniumBase/examples/behave_bdd/dashboard.html |
| 31 | +================================================================================== |
| 32 | +1 feature passed, 0 failed, 0 skipped |
| 33 | +1 scenario passed, 0 failed, 0 skipped |
| 34 | +12 steps passed, 0 failed, 0 skipped, 0 undefined |
| 35 | +Took 0m4.682s |
| 36 | +``` |
| 37 | + |
| 38 | +🐝 Another example, which uses higher-level Behave steps to simplify the ``.feature`` file: |
| 39 | + |
| 40 | +```bash |
| 41 | +> cd examples/behave_bdd/ |
| 42 | +> behave features/calculator.feature:61 -T -D dashboard -k |
| 43 | + |
| 44 | +Dashboard: /Users/michael/github/SeleniumBase/examples/behave_bdd/dashboard.html |
| 45 | +******************************************************************************** |
| 46 | +Feature: SeleniumBase scenarios for the Calculator App # features/calculator.feature:1 |
| 47 | + |
| 48 | + Background: # features/calculator.feature:3 |
| 49 | + |
| 50 | + Scenario: 7.0 × (3 + 3) = 42 # features/calculator.feature:49 |
| 51 | + Given Open the Calculator App # features/steps/calculator.py:4 |
| 52 | + When Press C # features/steps/calculator.py:9 |
| 53 | + And Press 7 # features/steps/calculator.py:79 |
| 54 | + And Press . # features/steps/calculator.py:104 |
| 55 | + And Press 0 # features/steps/calculator.py:94 |
| 56 | + And Press × # features/steps/calculator.py:29 |
| 57 | + And Press ( # features/steps/calculator.py:14 |
| 58 | + And Press 3 # features/steps/calculator.py:59 |
| 59 | + And Press + # features/steps/calculator.py:39 |
| 60 | + And Press 3 # features/steps/calculator.py:59 |
| 61 | + And Press ) # features/steps/calculator.py:19 |
| 62 | + Then Verify output is "7.0×(3+3)" # features/steps/calculator.py:135 |
| 63 | + When Press = # features/steps/calculator.py:44 |
| 64 | + Then Verify output is "42" # features/steps/calculator.py:135 |
| 65 | + ✅ Scenario Passed! |
| 66 | + |
| 67 | +- Dashboard: /Users/michael/github/SeleniumBase/examples/behave_bdd/dashboard.html |
| 68 | +================================================================================== |
| 69 | +1 feature passed, 0 failed, 0 skipped |
| 70 | +1 scenario passed, 0 failed, 8 skipped |
| 71 | +14 steps passed, 0 failed, 60 skipped, 0 undefined |
| 72 | +Took 0m1.672s |
| 73 | +``` |
| 74 | + |
| 75 | +🔵🐝⚪ With the Dashboard enabled, you'll get one of these: |
| 76 | + |
| 77 | +<img src="https://seleniumbase.io/cdn/img/sb_behave_dashboard.png" title="SeleniumBase" width="600"> |
| 78 | + |
| 79 | +### 🐝 Understanding Behave files: |
| 80 | + |
| 81 | +🐝 The ``*.feature`` files can use any step seen from: |
| 82 | + |
| 83 | +```bash |
| 84 | +behave --steps-catalog |
| 85 | +``` |
| 86 | + |
| 87 | +🐝 SeleniumBase includes several pre-made Behave steps, which you can use by creating a Python file with the following line in your ``features/steps/`` directory: |
| 88 | + |
| 89 | +```python |
| 90 | +from seleniumbase.behave import steps # noqa |
| 91 | +``` |
| 92 | + |
| 93 | +🐝 Inside your ``features/environment.py`` file, you should have the following: |
| 94 | + |
| 95 | +```python |
| 96 | +from seleniumbase import BaseCase |
| 97 | +from seleniumbase.behave import behave_sb |
| 98 | +behave_sb.set_base_class(BaseCase) # Accepts a BaseCase subclass |
| 99 | +from seleniumbase.behave.behave_sb import before_all # noqa |
| 100 | +from seleniumbase.behave.behave_sb import before_feature # noqa |
| 101 | +from seleniumbase.behave.behave_sb import before_scenario # noqa |
| 102 | +from seleniumbase.behave.behave_sb import before_step # noqa |
| 103 | +from seleniumbase.behave.behave_sb import after_step # noqa |
| 104 | +from seleniumbase.behave.behave_sb import after_scenario # noqa |
| 105 | +from seleniumbase.behave.behave_sb import after_feature # noqa |
| 106 | +from seleniumbase.behave.behave_sb import after_all # noqa |
| 107 | +``` |
| 108 | + |
| 109 | +🐝 If you've already created a subclass of ``BaseCase`` with custom methods, you can swap ``BaseCase`` in with your own subclass, which will allow you to easily use your own custom methods in your Behave step definitions. |
| 110 | + |
| 111 | +🐝 Here's an example Python file in the ``features/steps/`` folder: |
| 112 | + |
| 113 | +```python |
| 114 | +from behave import step |
| 115 | + |
| 116 | + |
| 117 | +@step("Open the Swag Labs Login Page") |
| 118 | +def go_to_swag_labs(context): |
| 119 | + sb = context.sb |
| 120 | + sb.open("https://www.saucedemo.com") |
| 121 | + sb.clear_local_storage() |
| 122 | + |
| 123 | + |
| 124 | +@step("Login to Swag Labs with {user}") |
| 125 | +def login_to_swag_labs(context, user): |
| 126 | + sb = context.sb |
| 127 | + sb.type("#user-name", user) |
| 128 | + sb.type("#password", "secret_sauce\n") |
| 129 | + |
| 130 | + |
| 131 | +@step("Verify that the current user is logged in") |
| 132 | +def verify_logged_in(context): |
| 133 | + sb = context.sb |
| 134 | + sb.assert_element("#header_container") |
| 135 | + sb.assert_element("#react-burger-menu-btn") |
| 136 | + sb.assert_element("#shopping_cart_container") |
| 137 | + |
| 138 | + |
| 139 | +@step('Add "{item}" to cart') |
| 140 | +def add_item_to_cart(context, item): |
| 141 | + sb = context.sb |
| 142 | + sb.click('div.inventory_item:contains("%s") button[name*="add"]' % item) |
| 143 | +``` |
| 144 | + |
| 145 | +🐝 A ``*.feature`` file could look like this: |
| 146 | + |
| 147 | +```bash |
| 148 | +Feature: SeleniumBase scenarios for the Swag Labs App |
| 149 | + |
| 150 | + Background: |
| 151 | + Given Open the Swag Labs Login Page |
| 152 | + |
| 153 | + Scenario: User can order a backpack from the store |
| 154 | + When Login to Swag Labs with standard_user |
| 155 | + Then Verify that the current user is logged in |
| 156 | + And Save price of "Backpack" to <item_price> |
| 157 | + When Add "Backpack" to Cart |
| 158 | + Then Verify shopping cart badge shows 1 item(s) |
| 159 | + When Click on shopping cart icon |
| 160 | + And Click Checkout |
| 161 | + And Enter checkout info: First, Last, 12345 |
| 162 | + And Click Continue |
| 163 | + Then Verify 1 "Backpack"(s) in cart |
| 164 | + And Verify cost of "Backpack" is <item_price> |
| 165 | + And Verify item total is $29.99 |
| 166 | + And Verify tax amount is $2.40 |
| 167 | + And Verify total cost is $32.39 |
| 168 | + When Click Finish |
| 169 | + Then Verify order complete |
| 170 | + When Logout from Swag Labs |
| 171 | + Then Verify on Login page |
| 172 | +``` |
| 173 | + |
| 174 | +🐝 Here's another example of a ``*.feature`` file: |
| 175 | + |
| 176 | +```bash |
| 177 | +Feature: SeleniumBase scenarios for the RealWorld App |
| 178 | + |
| 179 | + Scenario: Verify RealWorld App (log in / sign out) |
| 180 | + Given Open "seleniumbase.io/realworld/login" |
| 181 | + And Clear Session Storage |
| 182 | + When Type "demo_user" into "#username" |
| 183 | + And Type "secret_pass" into "#password" |
| 184 | + And Do MFA "GAXG2MTEOR3DMMDG" into "#totpcode" |
| 185 | + Then Assert text "Welcome!" in "h1" |
| 186 | + And Highlight element "img#image1" |
| 187 | + And Click 'a:contains("This Page")' |
| 188 | + And Save screenshot to logs |
| 189 | + When Click link "Sign out" |
| 190 | + Then Assert element 'a:contains("Sign in")' |
| 191 | + And Assert text "You have been signed out!" |
| 192 | +``` |
| 193 | + |
| 194 | +🐝 If there's a test failure, that's easy to spot: |
| 195 | + |
| 196 | +```bash |
| 197 | +Feature: SeleniumBase scenarios for the Fail Page # features/fail_page.feature:1 |
| 198 | + |
| 199 | + Scenario: Fail test on purpose to see what happens # features/fail_page.feature:3 |
| 200 | + When Open the Fail Page # features/steps/fail_page.py:4 |
| 201 | + Then Fail test on purpose # features/steps/fail_page.py:9 |
| 202 | + Assertion Failed: This test fails on purpose! |
| 203 | + Captured stdout: |
| 204 | + >>> STEP FAILED: (#2) Fail test on purpose |
| 205 | + Class / Feature: SeleniumBase scenarios for the Fail Page |
| 206 | + Test / Scenario: Fail test on purpose to see what happens |
| 207 | + |
| 208 | + ❌ Scenario Failed! |
| 209 | +``` |
| 210 | +
|
| 211 | +🐝🎖️ For convenience, the [SeleniumBase Behave GUI](https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/behave_gui.md) lets you run ``behave`` scripts from a Desktop app. |
| 212 | +
|
| 213 | +🐝🎖️ To launch it, call ``sbase behave-gui`` or ``sbase gui-behave``: |
| 214 | +
|
| 215 | +```bash |
| 216 | +sbase behave-gui |
| 217 | +* Starting the SeleniumBase Behave Commander GUI App... |
| 218 | +``` |
| 219 | +
|
| 220 | +<img src="https://seleniumbase.io/cdn/img/sbase_behave_gui_wide_5.png" title="SeleniumBase" width="600"> |
| 221 | +
|
| 222 | +🐝🎖️ You can customize the tests that show up there: |
| 223 | +
|
| 224 | +```bash |
| 225 | +sbase behave-gui # all tests |
| 226 | +sbase behave-gui -i=calculator # tests with "calculator" in the name |
| 227 | +sbase behave-gui features/ # tests located in the "features/" folder |
| 228 | +sbase behave-gui features/calculator.feature # tests in that feature |
| 229 | +``` |
| 230 | +
|
| 231 | +-------- |
| 232 | +
|
| 233 | +<div>To learn more about SeleniumBase, check out the Docs Site:</div> |
| 234 | +<a href="https://seleniumbase.io"> |
| 235 | +<img src="https://img.shields.io/badge/docs-%20%20SeleniumBase.io-11BBDD.svg" alt="SeleniumBase.io Docs" /></a> |
| 236 | +
|
| 237 | +<div>All the code is on GitHub:</div> |
| 238 | +<a href="https://github.com/seleniumbase/SeleniumBase"> |
| 239 | +<img src="https://img.shields.io/badge/✅%20💛%20View%20Code-on%20GitHub%20🌎%20🚀-02A79E.svg" alt="SeleniumBase on GitHub" /></a> |
0 commit comments