Skip to content

Commit 0d61ce9

Browse files
authored
Merge pull request #1652 from seleniumbase/simplifying-and-upgrading
Simplifying, upgrading, and refactoring
2 parents 8930339 + 2f31b54 commit 0d61ce9

Some content is hidden

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

69 files changed

+551
-409
lines changed

README.md

Lines changed: 37 additions & 55 deletions
Large diffs are not rendered by default.

examples/ReadMe.md

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- SeleniumBase Docs -->
22

3-
## [<img src="https://seleniumbase.github.io/img/logo6.png" title="SeleniumBase" width="32">](https://github.com/seleniumbase/SeleniumBase/) Example Tests:
3+
## [<img src="https://seleniumbase.github.io/img/logo7.png" title="SeleniumBase" width="32">](https://github.com/seleniumbase/SeleniumBase/) Example Tests:
44

55
<p align="left"><a align="center" href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_demo_site.py"><img align="center" src="https://seleniumbase.github.io/cdn/img/sb_demo_page.png" alt="SeleniumBase Demo Page" width="420" /></a></p>
66

@@ -13,30 +13,30 @@
1313

1414
(NOTE: Some example tests fail on purpose to demonstrate [logging features](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/example_logs/ReadMe.md).)
1515

16-
<h3><img src="https://seleniumbase.github.io/img/logo6.png" title="SeleniumBase" width="32" /> Example tests with run commands to help you get started:</h3>
17-
18-
Run an [example test](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/my_first_test.py) in Chrome: (Default: ``--browser=chrome``)
16+
--------
1917

20-
```bash
21-
pytest my_first_test.py
22-
```
18+
<h3><img src="https://seleniumbase.github.io/img/logo7.png" title="SeleniumBase" width="32" /> Example tests with run commands to help you get started:</h3>
2319

24-
<img src="https://seleniumbase.github.io/cdn/gif/swag_labs_4.gif" title="Testing Swag Labs with SeleniumBase" /><br />
20+
--------
2521

26-
Run an [example test](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_demo_site.py) on the SeleniumBase Demo Site:
22+
Run an [example test](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_demo_site.py): (Default option: ``--browser=chrome``)
2723

2824
```bash
2925
pytest test_demo_site.py
3026
```
3127

3228
<img src="https://seleniumbase.github.io/cdn/gif/demo_page_4.gif" title="SeleniumBase Demo Page" /><br />
3329

30+
--------
31+
3432
Run an example test in Firefox:
3533

3634
```bash
3735
pytest my_first_test.py --browser=firefox
3836
```
3937

38+
--------
39+
4040
Run an example test in Demo Mode: (highlight assertions)
4141

4242
```bash
@@ -45,6 +45,18 @@ pytest test_swag_labs.py --demo
4545

4646
<img src="https://seleniumbase.github.io/cdn/gif/swag_demo_3.gif" /><br />
4747

48+
--------
49+
50+
Run [test_coffee_cart.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_coffee_cart.py) to test the [Coffee Cart](https://seleniumbase.io/coffee/) app:
51+
52+
```bash
53+
pytest test_coffee_cart.py --demo
54+
```
55+
56+
<img src="https://seleniumbase.github.io/cdn/gif/coffee_cart.gif" title="SeleniumBase Coffee App Example" alt="SeleniumBase Example" title="SeleniumBase Coffee App Example" />
57+
58+
--------
59+
4860
Run a [Wordle-solver example](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/wordle_test.py):
4961

5062
```bash
@@ -53,12 +65,16 @@ pytest wordle_test.py
5365

5466
<img src="https://seleniumbase.github.io/cdn/gif/wordle.gif" title="Solving Wordle with SeleniumBase" /><br />
5567

68+
--------
69+
5670
Run an example test in Headless Mode: (invisible browser)
5771

5872
```bash
5973
pytest my_first_test.py --headless
6074
```
6175

76+
--------
77+
6278
Run an [example test](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_swag_labs.py) using Chrome's mobile device emulator: (default settings)
6379

6480
```bash
@@ -67,6 +83,8 @@ pytest test_swag_labs.py --mobile
6783

6884
<img src="https://seleniumbase.github.io/cdn/gif/swag_mobile_2.gif" title="SeleniumBase Mobile Mode" /><br />
6985

86+
--------
87+
7088
Run another [example test](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_xkcd.py) in Demo Mode: (highlight assertions)
7189

7290
```bash
@@ -75,66 +93,88 @@ pytest test_xkcd.py --demo
7593

7694
<img src="https://seleniumbase.github.io/cdn/gif/xkcd_vid.gif" title="SeleniumBase Demo Mode" /><br />
7795

96+
--------
97+
7898
Run a [test suite](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_suite.py) with verbose output: (see more details)
7999

80100
```bash
81101
pytest test_suite.py -v
82102
```
83103

104+
--------
105+
84106
Run a test suite multi-threaded using [n] threads:
85107

86108
```bash
87109
pytest test_suite.py -n=4
88110
```
89111

112+
--------
113+
90114
Run a [parameterized test](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/parameterized_test.py): (Generates multiple tests from one)
91115

92116
```bash
93117
pytest parameterized_test.py -v
94118
```
95119

120+
--------
121+
96122
Run a test suite and generate a SeleniumBase Dashboard:
97123

98124
```bash
99125
pytest test_suite.py --dashboard
100126
```
101127

128+
--------
129+
102130
Run a test suite and generate a pytest report:
103131

104132
```bash
105133
pytest test_suite.py --html=report.html
106134
```
107135

136+
--------
137+
108138
Run a [failing test](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_fail.py): (See the ``latest_logs/`` folder for logs and screenshots)
109139

110140
```bash
111141
pytest test_fail.py
112142
```
113143

144+
--------
145+
114146
Run a failing test with Debug-mode enabled: (``pdb`` activates on failures)
115147

116148
```bash
117149
pytest test_fail.py --pdb -s
118150
```
119151

152+
--------
153+
120154
Run a test suite that demonstrates the use of pytest markers:
121155

122156
```bash
123157
pytest -m marker_test_suite -v
124158
```
125159

160+
--------
161+
126162
Run a test suite that reuses the browser session between tests:
127163

128164
```bash
129165
pytest test_suite.py --reuse-session
130166
```
131167

168+
--------
169+
132170
Run an [example test](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/rate_limiting_test.py) demonstrating the ``rate_limited`` Python decorator:
133171

134172
```bash
135173
pytest rate_limiting_test.py
136174
```
137175

176+
--------
177+
138178
Run an [example test](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/upload_file_test.py) that demonstrates how to upload a file to a website:
139179

140180
```bash
@@ -161,12 +201,16 @@ Run an example test with nosetests:
161201
nosetests my_first_test.py
162202
```
163203

204+
--------
205+
164206
Run an example test suite and generate a nosetest report:
165207

166208
```bash
167209
nosetests test_suite.py --report --show-report
168210
```
169211

212+
--------
213+
170214
Run an example test using a nosetest configuration file:
171215

172216
```bash
@@ -181,7 +225,7 @@ For more advanced run commands, such as using a proxy server, see [../help_docs/
181225

182226
If you just need to perform some quick website verification on various devices, you can use the <a href="https://seleniumbase.io/devices/">SeleniumBase Device Farm</a>. Just plug in a website URL, and it will display how the website looks on four different devices:
183227

184-
<a href="https://seleniumbase.io/devices/"><img src="https://seleniumbase.github.io/cdn/img/github_demo2.png" width="540" title="SeleniumBase Mobile Mode" /></a><br />
228+
<a href="https://seleniumbase.io/devices/?url=github.com"><img src="https://seleniumbase.github.io/cdn/img/github_demo2.png" width="540" title="SeleniumBase Mobile Mode" /></a><br />
185229

186230
--------
187231

@@ -193,6 +237,18 @@ python gui_test_runner.py
193237

194238
<img src="https://seleniumbase.github.io/cdn/img/gui_test_runner.png" title="GUI Test Runner" width="320" />
195239

240+
(The newer [SeleniumBase Commander](https://seleniumbase.io/help_docs/commander/) improves on that.)
241+
242+
--------
243+
244+
🎖️ To launch the SeleniumBase Commander GUI for pytest:
245+
246+
```bash
247+
sbase gui
248+
```
249+
250+
<img src="https://seleniumbase.github.io/cdn/img/sbase_commander_wide.png" title="SeleniumBase Commander" width="600">
251+
196252
--------
197253

198254
<img src="https://seleniumbase.github.io/cdn/img/super_logo_sb.png" title="SeleniumBase" width="320" />

examples/basic_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ def test_basics(self):
1919
self.assert_element("div#login_button_container")
2020

2121

22-
if __name__ == "__main__": # Use "python" to call "pytest"
22+
if __name__ == "__main__":
2323
from pytest import main
24-
main([__file__])
24+
main([__file__, "-s"])

examples/boilerplates/base_test_case.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
Define your customized library methods in a master class like this.
44
Then have all your test classes inherit it.
55
BaseTestCase will inherit SeleniumBase methods from BaseCase.
6-
With Python 3, simplify "super(...)" to super().setUp() and super().tearDown()
76
"""
87

98
from seleniumbase import BaseCase
109

1110

1211
class BaseTestCase(BaseCase):
1312
def setUp(self):
14-
super(BaseTestCase, self).setUp()
13+
super().setUp()
1514
# <<< Run custom setUp() code for tests AFTER the super().setUp() >>>
1615

1716
def tearDown(self):
@@ -24,7 +23,7 @@ def tearDown(self):
2423
pass
2524
# (Wrap unreliable tearDown() code in a try/except block.)
2625
# <<< Run custom tearDown() code BEFORE the super().tearDown() >>>
27-
super(BaseTestCase, self).tearDown()
26+
super().tearDown()
2827

2928
def login(self):
3029
# <<< Placeholder. Add your code here. >>>

examples/coffee_cart_tests.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""Use SeleniumBase to test a Coffee Cart App."""
2+
from seleniumbase import BaseCase
3+
BaseCase.main(__name__, __file__)
4+
5+
6+
class CoffeeCartTests(BaseCase):
7+
def test_buy_one_cappuccino(self):
8+
self.open("https://seleniumbase.io/coffee/")
9+
self.click('div[data-test="Cappuccino"]')
10+
self.assert_exact_text("cart (1)", 'a[aria-label="Cart page"]')
11+
self.click('a[aria-label="Cart page"]')
12+
self.assert_exact_text("Total: $19.00", 'button[data-test="checkout"]')
13+
self.click('button[data-test="checkout"]')
14+
self.type("input#name", "Selenium Coffee")
15+
self.type("input#email", "[email protected]")
16+
self.click("button#submit-payment")
17+
self.assert_text("Thanks for your purchase.", "div#app div.success")
18+
self.assert_exact_text("cart (0)", 'a[aria-label="Cart page"]')
19+
self.assert_exact_text("Total: $0.00", 'button[data-test="checkout"]')
20+
21+
def test_coffee_promo_with_preview(self):
22+
self.open("https://seleniumbase.io/coffee/")
23+
self.click('div[data-test="Espresso"]')
24+
self.click('div[data-test="Americano"]')
25+
self.click('div[data-test="Cafe_Latte"]')
26+
self.assert_exact_text("cart (3)", 'a[aria-label="Cart page"]')
27+
self.assert_text("Get an extra cup of Mocha for $4.", "div.promo")
28+
self.click("div.promo button.yes")
29+
self.assert_exact_text("cart (4)", 'a[aria-label="Cart page"]')
30+
self.hover('button[data-test="checkout"]')
31+
self.assert_text("(Discounted) Mocha", "ul.cart-preview")
32+
self.assert_exact_text("Total: $37.00", 'button[data-test="checkout"]')
33+
self.click('button[data-test="checkout"]')
34+
self.type("input#name", "Selenium Coffee")
35+
self.type("input#email", "[email protected]")
36+
self.click("button#submit-payment")
37+
self.assert_text("Thanks for your purchase.", "div#app div.success")
38+
39+
def test_context_click_add_coffee(self):
40+
self.open("https://seleniumbase.io/coffee/")
41+
self.context_click('div[data-test="Espresso_Macchiato"]')
42+
self.click('form button:contains("Yes")')
43+
self.assert_exact_text("cart (1)", 'a[aria-label="Cart page"]')
44+
self.click('a[aria-label="Cart page"]')
45+
self.assert_exact_text("Total: $12.00", 'button[data-test="checkout"]')
46+
self.click('button[data-test="checkout"]')
47+
self.type("input#name", "Selenium Coffee")
48+
self.type("input#email", "[email protected]")
49+
self.click("button#submit-payment")
50+
self.assert_text("Thanks for your purchase.", "div#app div.success")
51+
52+
def test_remove_added_coffee(self):
53+
self.open("https://seleniumbase.io/coffee/")
54+
self.assert_exact_text("cart (0)", 'a[aria-label="Cart page"]')
55+
self.assert_exact_text("Total: $0.00", "button.pay")
56+
self.click_visible_elements('div[class="cup-body"]', limit=6)
57+
self.assert_exact_text("cart (6)", 'a[aria-label="Cart page"]')
58+
self.assert_exact_text("Total: $74.00", 'button[data-test="checkout"]')
59+
self.click('a[aria-label="Cart page"]')
60+
self.click_visible_elements("button.delete")
61+
self.assert_text("No coffee, go add some.", "div#app")
62+
self.click('a[aria-label="Menu page"]')
63+
self.assert_exact_text("cart (0)", 'a[aria-label="Cart page"]')
64+
self.assert_exact_text("Total: $0.00", 'button[data-test="checkout"]')

examples/migration/protractor/mat_paginator_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
from seleniumbase import BaseCase
32

43

examples/my_first_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ def test_swag_labs(self):
2828
self.assert_element("div#login_button_container")
2929

3030

31-
if __name__ == "__main__": # Use "python" to call "pytest"
31+
if __name__ == "__main__": # If "python", run pytest
3232
from pytest import main
33-
main([__file__])
33+
from sys import argv
34+
main([*argv, "-s"]) # Run pytest using same args
3435

3536

3637
#######################################################################

examples/raw_call.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
import pytest
55
import subprocess
66

7-
pytest.main(["test_mfa_login.py", "--chrome", "-v"])
7+
pytest.main(["test_coffee_cart.py", "--chrome", "-v"])
88
subprocess.call(["pytest", "test_mfa_login.py", "--chrome", "-v"])

examples/raw_file_call.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""Call a file with "python" instead of using "pytest" directly.
22
To run, use: "python raw_file_call.py".
3-
Works by using pytest.main([__file__])."""
3+
On newer version of SeleniumBase, use:
4+
BaseCase.main(__name__, __file__)"""
45
from seleniumbase import BaseCase
56

6-
if __name__ == "__main__": # Use "python" to call "pytest"
7+
if __name__ == "__main__": # If "python", run pytest
78
from pytest import main
8-
main([__file__])
9+
main([__file__, "-s"])
910

1011

1112
class TinyMceTest(BaseCase):
@@ -19,5 +20,5 @@ def test_tinymce(self):
1920
with self.frame_switch("iframe"):
2021
self.add_text("#tinymce", "SeleniumBase!")
2122
self.highlight("#tinymce")
22-
self.post_message("SeleniumBase is fast!")
23-
self.post_message("And SeleniumBase is fun!")
23+
self.post_message("SeleniumBase is fast!", duration=1.5)
24+
self.post_message("And SeleniumBase is fun!", duration=1.5)

examples/raw_main_call.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Call a file with "python" instead of using "pytest" directly.
2+
To run, type: "python raw_main_call.py"."""
3+
from seleniumbase import BaseCase
4+
BaseCase.main(__name__, __file__)
5+
6+
7+
class TinyMceTest(BaseCase):
8+
def test_tinymce(self):
9+
self.open("https://seleniumbase.io/tinymce/")
10+
self.wait_for_element("div.mce-container-body")
11+
self.click('span:contains("File")')
12+
self.click('span:contains("New document")')
13+
self.click('span:contains("Paragraph")')
14+
self.click('span:contains("Heading 1")')
15+
with self.frame_switch("iframe"):
16+
self.add_text("#tinymce", "SeleniumBase!")
17+
self.highlight("#tinymce")
18+
self.post_message("SeleniumBase is fast!", duration=1.5)
19+
self.post_message("And SeleniumBase is fun!", duration=1.5)

0 commit comments

Comments
 (0)