Skip to content

Commit 88fa040

Browse files
authored
Merge pull request #1313 from seleniumbase/seleniumbase-3
SeleniumBase 3
2 parents 30eafb0 + a38f010 commit 88fa040

File tree

91 files changed

+4860
-447
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+4860
-447
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ geckodriver.log
9191
pytestdebug.log
9292

9393
# Reports
94+
reports/*.xml
9495
latest_report
9596
report_archives
9697
archived_reports

README.md

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<meta property="og:site_name" content="SeleniumBase">
22
<meta property="og:title" content="SeleniumBase: Python Web Automation and E2E Testing" />
33
<meta property="og:description" content="Fast, easy, and reliable Web/UI testing with Python." />
4-
<meta property="og:keywords" content="Python, pytest, selenium, webdriver, testing, automation, seleniumbase, framework, RPA, dashboard, recorder, reports">
4+
<meta property="og:keywords" content="Python, pytest, selenium, webdriver, testing, automation, seleniumbase, framework, RPA, behave, nosetests, dashboard, recorder, reports, gui">
55
<meta property="og:image" content="https://seleniumbase.io/cdn/img/mac_sb_logo_5.png" />
66
<link rel="icon" href="https://seleniumbase.io/img/green_logo.png" />
77

8-
<h3 align="center"><a href="https://github.com/seleniumbase/SeleniumBase/"><img src="https://seleniumbase.io/cdn/img/mac_sb_logo_8.png" alt="SeleniumBase" title="SeleniumBase" width="310" /></a></h3>
8+
<h3 align="center"><a href="https://github.com/seleniumbase/SeleniumBase/"><img src="https://seleniumbase.io/cdn/img/mac_sb_logo_9.png" alt="SeleniumBase" title="SeleniumBase" width="340" /></a></h3>
99
<!-- View on GitHub -->
10-
<h4 align="center">Better Web Testing with <a href="https://www.selenium.dev/documentation/" target="_blank">Selenium</a> and <a href="https://docs.pytest.org/en/stable/" target="_blank">pytest</a>.</h4>
10+
11+
<div align="center">A <b>Python</b> ecosystem for <a href="https://www.selenium.dev/documentation/" target="_blank">Selenium</a>. Supports <a href="https://docs.pytest.org/en/stable/" target="_blank">pytest</a> and <a href="https://behave.readthedocs.io/en/stable/index.html" target="_blank">behave-BDD</a>.</div>
12+
1113
<p align="center">
1214
<a href="https://github.com/seleniumbase/SeleniumBase/releases">
1315
<img src="https://img.shields.io/github/v/release/seleniumbase/SeleniumBase.svg?color=2277EE" alt="Latest Release on GitHub" /></a> <a href="https://pypi.python.org/pypi/seleniumbase" target="_blank">
@@ -37,15 +39,15 @@
3739
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/integrations/github/workflows/ReadMe.md">🤖 CI</a> |
3840
<a href="https://github.com/seleniumbase/SeleniumBase/tree/master/examples/boilerplates">♻️ Templates</a> |
3941
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/translations.md">🌏 Translator</a> |
40-
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/presenter/ReadMe.md">🎞️ Presenter</a> |
42+
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/presenter/ReadMe.md">🎞️ Slides</a> |
4143
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/mobile_testing.md">📱 Mobile</a> |
4244
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/utilities/selenium_grid/ReadMe.md">🌐 Grid</a> |
43-
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/dialog_boxes/ReadMe.md">🛂 Dialog</a>
45+
<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/dialog_boxes/ReadMe.md">🛂 DialogBox</a>
4446
</p>
4547

4648
--------
4749

48-
<p align="left">↘️ An example test using the <code>BaseCase</code> class:</p>
50+
<p align="left">↘️ Example test with the <b>BaseCase</b> class. Runs with <code>pytest</code> or <code>nosetests</code>. (<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/ReadMe.md">Learn more</a>)</p>
4951

5052
```python
5153
from seleniumbase import BaseCase
@@ -55,28 +57,44 @@ class TestMFALogin(BaseCase):
5557
self.open("https://seleniumbase.io/realworld/login")
5658
self.type("#username", "demo_user")
5759
self.type("#password", "secret_pass")
58-
self.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG")
60+
self.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG") # 6-digit
5961
self.assert_element("img#image1")
6062
self.assert_text("Welcome!", "h1")
6163
self.click('a:contains("This Page")')
6264
self.save_screenshot_to_logs()
6365
```
6466

65-
<p align="left">↘️ An example test using the <code>sb</code> pytest fixture:</p>
67+
<p align="left">↘️ Example test with the <b>sb</b> pytest fixture. Runs with <code>pytest</code>.</p>
6668

6769
```python
6870
def test_mfa_login(sb):
6971
sb.open("https://seleniumbase.io/realworld/login")
7072
sb.type("#username", "demo_user")
7173
sb.type("#password", "secret_pass")
72-
sb.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG")
74+
sb.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG") # 6-digit
7375
sb.assert_element("img#image1")
7476
sb.assert_text("Welcome!", "h1")
7577
sb.click('a:contains("This Page")')
7678
sb.save_screenshot_to_logs()
7779
```
7880

79-
<p align="left">↘️ An example of running a test: <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_demo_site.py">test_demo_site.py</a></p>
81+
<p align="left">↘️ Example test with <b>behave-BDD</b> <a href="https://behave.readthedocs.io/en/stable/gherkin.html">Gherkin</a> structure. Runs with <code>behave</code>. (<a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/behave_bdd/ReadMe.md">Learn more</a>)</p>
82+
83+
```bash
84+
Feature: SeleniumBase scenarios for the RealWorld App
85+
86+
Scenario: Verify RealWorld App (log in / sign out)
87+
Given Open "seleniumbase.io/realworld/login"
88+
When Type "demo_user" into "#username"
89+
And Type "secret_pass" into "#password"
90+
And Do MFA "GAXG2MTEOR3DMMDG" into "#totpcode"
91+
Then Assert exact text "Welcome!" in "h1"
92+
And Highlight "img#image1"
93+
And Click 'a:contains("This Page")'
94+
And Save screenshot to logs
95+
```
96+
97+
<p align="left">✅ An example of running a test: <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_demo_site.py">test_demo_site.py</a></p>
8098

8199
```bash
82100
cd examples/
@@ -145,14 +163,17 @@ pip install -U seleniumbase
145163
* OR: "sbase [COMMAND] [PARAMETERS]"
146164
147165
COMMANDS:
148-
install [DRIVER] [OPTIONS]
166+
get / install [DRIVER] [OPTIONS]
149167
methods (List common Python methods)
150168
options (List common pytest options)
151-
commander / gui [OPTIONAL PATH or TEST FILE]
169+
behave-options (List common Behave options)
170+
gui / commander [OPTIONAL PATH or TEST FILE]
171+
behave-gui (SBase Commander for Behave)
152172
mkdir [DIRECTORY] [OPTIONS]
153173
mkfile [FILE.py] [OPTIONS]
154174
mkrec / codegen [FILE.py] [OPTIONS]
155-
recorder (Open Recorder Desktop App)
175+
recorder (Open Recorder Desktop App.)
176+
record (If args: mkrec. Else: App.)
156177
mkpres [FILE.py] [LANG]
157178
mkchart [FILE.py] [LANG]
158179
print [FILE] [OPTIONS]
@@ -167,7 +188,7 @@ COMMANDS:
167188
download server (Get Selenium Grid JAR file)
168189
grid-hub [start|stop] [OPTIONS]
169190
grid-node [start|stop] --hub=[HOST/IP]
170-
* (EXAMPLE: "sbase install chromedriver latest") *
191+
* (EXAMPLE: "sbase get chromedriver latest") *
171192
172193
Type "sbase help [COMMAND]" for specific command info.
173194
For info on all commands, type: "seleniumbase --help".
@@ -226,7 +247,7 @@ class MyTestClass(BaseCase):
226247
227248
* By default, **[CSS Selectors](https://www.w3schools.com/cssref/css_selectors.asp)** are used for finding page elements.
228249
* If you're new to CSS Selectors, games like [CSS Diner](http://flukeout.github.io/) can help you learn.
229-
* Here are some common ``SeleniumBase`` methods that you might find in tests:
250+
* Here are some common SeleniumBase methods that you might find in tests:
230251
231252
```python
232253
self.open(url) # Navigate the browser window to the URL.
@@ -375,6 +396,7 @@ The code above will leave your browser window open in case there's a failure. (i
375396
--var1=STRING # (Extra test data. Access with "self.var1" in tests.)
376397
--var2=STRING # (Extra test data. Access with "self.var2" in tests.)
377398
--var3=STRING # (Extra test data. Access with "self.var3" in tests.)
399+
--variables=DICT # (Extra test data. Access with "self.variables".)
378400
--user-data-dir=DIR # (Set the Chrome user data directory to use.)
379401
--protocol=PROTOCOL # (The Selenium Grid protocol: http|https.)
380402
--server=SERVER # (The Selenium Grid server/IP used for tests.)

examples/ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
* <b>SeleniumBase</b> tests are run with <b>pytest</b>.
66
* Chrome is the default browser if not specified.
7-
* Tests are structured using [17 unique syntax formats](https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md).
7+
* Tests are structured using [20 unique syntax formats](https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md).
88
* Logs from test failures are saved to ``./latest_logs/``.
99
* Tests can be run with [multiple command-line options](https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/customizing_test_runs.md).
1010
* Example tests are found in: **[SeleniumBase/examples/](https://github.com/seleniumbase/SeleniumBase/tree/master/examples)**.

examples/behave_bdd/ReadMe.md

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
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>

examples/behave_bdd/behave.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[behave]
2+
show_skipped=false
3+
show_timings=false

examples/behave_bdd/features/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[behave]
2+
show_skipped=false
3+
show_timings=false

0 commit comments

Comments
 (0)