Skip to content

Commit bc822c9

Browse files
authored
Merge pull request #174 from seleniumbase/cmd-interface
Add seleniumbase command-line interface for console scripts.
2 parents d761ee5 + 0c0f248 commit bc822c9

Some content is hidden

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

47 files changed

+954
-196
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ nosetests.xml
3737

3838
# Other
3939
selenium-server-standalone.jar
40+
verbose_hub_server.dat
41+
verbose_node_server.dat
42+
ip_of_grid_hub.dat
4043
geckodriver.log
4144
downloaded_files
4245
archived_files
@@ -51,4 +54,5 @@ html_report.html
5154
report.html
5255
assets
5356
temp
57+
temp*
5458
node_modules

MANIFEST.in

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
include README.md
2+
include pytest.ini
3+
include setup.cfg
4+
include .travis.yml
5+
include .gitignore
6+
include requirements.txt
7+
include seleniumbase/core/create_db_tables.sql
8+
include integrations/selenium_grid/grid-hub
9+
include integrations/selenium_grid/grid-node
10+
include integrations/selenium_grid/font_color
11+
include integrations/selenium_grid/start-grid-hub.bat
12+
include integrations/selenium_grid/register-grid-node.bat
13+
include integrations/selenium_grid/start-grid-hub.sh
14+
include integrations/selenium_grid/register-grid-node.sh

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ SeleniumBase automatically handles common WebDriver actions such as spinning up
2121

2222
(<i>By default, [CSS Selectors](https://www.w3schools.com/cssref/css_selectors.asp) are used for finding page elements.</i>)
2323

24-
**Run tests with Pytest or Nose in any browser:**
24+
**Run tests with Pytest or Nose in any browser:**<br />
25+
(<i>Using **Pytest** is strongly recommended</i>)
2526

2627
```bash
2728
pytest my_first_test.py --browser=chrome
@@ -137,7 +138,7 @@ class MyTestClass(BaseCase):
137138

138139
**Here's how to run the example script on various web browsers:**
139140

140-
(NOTE: You can interchange **nosetests** with **pytest** at anytime.)
141+
(NOTE: You can interchange **pytest** with **nosetests** at anytime.)
141142

142143
```bash
143144
cd examples/
@@ -158,7 +159,7 @@ pytest my_first_test.py --browser=chrome --demo_mode
158159
You can override the default wait time by either updating [settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/settings.py) or by using ``--demo_sleep={NUM}`` when using Demo Mode. (NOTE: If you use ``--demo_sleep={NUM}`` without using ``--demo_mode``, nothing will happen.)
159160

160161
```bash
161-
nosetests my_first_test.py --browser=chrome --demo_mode --demo_sleep=1.2
162+
pytest my_first_test.py --browser=chrome --demo_mode --demo_sleep=1.2
162163
```
163164

164165
You can also use the following in your scripts to slow down the tests:
@@ -168,14 +169,14 @@ import time; time.sleep(5) # sleep for 5 seconds (add this after the line you w
168169
import ipdb; ipdb.set_trace() # waits for your command. n = next line of current method, c = continue, s = step / next executed line (will jump)
169170
```
170171

171-
(NOTE: If you're using pytest instead of nosetests and you want to use ipdb in your script for debugging purposes, you'll either need to add ``--capture=no`` on the command line, or use ``import pytest; pytest.set_trace()`` instead of using ipdb. More info on that [here](http://stackoverflow.com/questions/2678792/can-i-debug-with-python-debugger-when-using-py-test-somehow).)
172+
(NOTE: If you're using pytest instead of nosetests and you want to use ipdb in your script for debugging purposes, you'll need to add ``--capture=no`` (or ``-s``) on the command line, or use ``import pytest; pytest.set_trace()`` instead of using ipdb. More info on that [here](http://stackoverflow.com/questions/2678792/can-i-debug-with-python-debugger-when-using-py-test-somehow).)
172173

173174
You may also want to have your test sleep in other situations where you need to have your test wait for something. If you know what you're waiting for, you should be specific by using a command that waits for something specific to happen.
174175

175176
If you need to debug things on the fly (in case of errors), use this:
176177

177178
```bash
178-
nosetests my_first_test.py --browser=chrome --pdb --pdb-failures -s
179+
pytest my_first_test.py --browser=chrome --pdb --pdb-failures -s
179180
```
180181

181182
The above code (with --pdb) will leave your browser window open in case there's a failure, which is possible if the web pages from the example change the data that's displayed on the page. (ipdb commands: 'c', 's', 'n' => continue, step, next). You may need the ``-s`` in order to see all console output.
@@ -207,7 +208,7 @@ For running tests outside of the SeleniumBase repo with **Nosetests**, you'll wa
207208

208209
If you want to pass additional data from the command line to your tests, you can use ``--data=STRING``. Now inside your tests, you can use ``self.data`` to access that.
209210

210-
To run Pytest multithreaded on multiple CPUs at the same time, add ``-n NUM`` on the command line, where NUM is the number of CPUs you want to use.
211+
To run Pytest multithreaded on multiple CPUs at the same time, add ``-n=NUM`` or ``-n NUM`` on the command line, where NUM is the number of CPUs you want to use.
211212

212213
<img src="https://cdn2.hubspot.net/hubfs/100006/images/logo_base_4b.png" title="SeleniumBase" height="120">
213214

@@ -282,7 +283,7 @@ pip install mysqlclient==1.3.12
282283

283284
Here's an example of running tests with additional features enabled:
284285
```bash
285-
nosetests [YOUR_TEST_FILE].py --browser=chrome --with-db_reporting --with-s3_logging -s
286+
pytest [YOUR_TEST_FILE].py --browser=chrome --with-db_reporting --with-s3_logging -s
286287
```
287288
(NOTE: If you haven't configured your MySQL or S3 connections in [settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/settings.py), don't use ``--with-db_reporting`` or ``--with-s3_logging``.)
288289

@@ -296,10 +297,11 @@ nosetests [YOUR_TEST_FILE].py --config=[MY_CONFIG_FILE].cfg
296297

297298
You can simplify that even more by using a setup.cfg file, such as the one provided for you in the examples folder. If you kick off a test run from within the folder that setup.cfg is location in, that file will automatically be used as your configuration, meaning that you wouldn't have to type out all the plugins that you want to use (or include a config file) everytime you run tests.
298299

299-
If you tell nosetests to run an entire file, it will run every method in that python file that starts with "test". You can be more specific on what to run by doing something like:
300+
If you tell pytest/nosetests to run an entire file, it will run every method in that python file that starts with "test". You can be more specific on what to run by doing something like the following: (<i>Note that the syntax is different for pytest vs nosetests.</i>)
300301

301302
```bash
302-
nosetests [YOUR_TEST_FILE].py:[SOME_CLASS_NAME].test_[SOME_TEST_NAME] --config=[MY_CONFIG_FILE].cfg
303+
pytest [YOUR_TEST_FILE].py::[SOME_CLASS_NAME]::test_[SOME_TEST_NAME]
304+
nosetests [YOUR_TEST_FILE].py:[SOME_CLASS_NAME].test_[SOME_TEST_NAME]
303305
```
304306

305307
Let's try an example of a test that fails:
@@ -317,7 +319,7 @@ class MyTestClass(BaseCase):
317319
You can run it from the ``examples`` folder like this:
318320

319321
```bash
320-
nosetests test_fail.py
322+
pytest test_fail.py
321323
```
322324

323325
You'll notice that a logs folder, "latest_logs", was created to hold information about the failing test, and screenshots. Take a look at what you get. Remember, this data can be saved in your MySQL DB and in S3 if you include the necessary plugins in your run command (and if you set up the neccessary connections properly). For future test runs, past test results will get stored in the archived_logs folder if you have ARCHIVE_EXISTING_LOGS set to True in [settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/settings.py).

console_scripts/ReadMe.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
## Console Scripts
2+
3+
### mkdir
4+
5+
* Usage:
6+
``seleniumbase mkdir [DIRECTORY_NAME]``
7+
8+
* Output:
9+
Creates a new folder for running SeleniumBase scripts.
10+
The new folder contains default config files,
11+
sample tests for helping new users get started, and
12+
Python boilerplates for setting up customized
13+
test frameworks.
14+
15+
### convert
16+
17+
* Usage:
18+
``seleniumbase convert [MY_TEST.py]``
19+
20+
* Output:
21+
Converts a Selenium IDE exported WebDriver unittest
22+
file into a SeleniumBase file. Adds _SB to the new
23+
file name while keeping the original file intact.
24+
Works with Katalon Recorder scripts.
25+
See: http://www.katalon.com/automation-recorder
26+
27+
### grid-hub
28+
29+
* Usage:
30+
``seleniumbase grid-hub {start|stop|restart}``
31+
32+
* Options:
33+
``-v``, ``--verbose`` (Increases verbosity of logging output.)
34+
35+
* Output:
36+
Controls the Selenium Grid Hub server, which allows
37+
for running tests on multiple machines in parallel
38+
to speed up test runs and reduce the total time
39+
of test suite execution.
40+
You can start, restart, or stop the Grid Hub server.
41+
42+
### grid-node
43+
44+
* Usage:
45+
``seleniumbase grid-node {start|stop|restart} [OPTIONS]``
46+
47+
* Options:
48+
``--hub=HUB_IP`` (The Grid Hub IP Address to connect to.) (Default: ``127.0.0.1``)
49+
``-v``, ``--verbose`` (Increases verbosity of logging output.)
50+
51+
* Output:
52+
Controls the Selenium Grid node, which serves as a
53+
worker machine for your Selenium Grid Hub server.
54+
You can start, restart, or stop the Grid node.

console_scripts/__init__.py

Whitespace-only changes.

console_scripts/run.py

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
"""
2+
SeleniumBase console scripts runner
3+
4+
Usage:
5+
seleniumbase [COMMAND] [PARAMETERS]
6+
7+
Examples:
8+
seleniumbase mkdir [DIRECTORY_NAME]
9+
seleniumbase convert [PYTHON_WEBDRIVER_UNITTEST_FILE].py
10+
seleniumbase grid-hub start
11+
seleniumbase grid-node start --hub=127.0.0.1
12+
"""
13+
14+
import sys
15+
from console_scripts import sb_mkdir
16+
from integrations.selenium_grid import grid_hub
17+
from integrations.selenium_grid import grid_node
18+
from integrations.selenium_ide import convert_ide
19+
20+
21+
def show_usage():
22+
show_basic_usage()
23+
print('Type "seleniumbase help" for more details.\n')
24+
25+
26+
def show_basic_usage():
27+
print("")
28+
print(">>>>>>>>>>>>")
29+
print("")
30+
print('Usage: "seleniumbase [command] [parameters]"')
31+
print("")
32+
print("Commands:")
33+
print("")
34+
print(" mkdir [DIRECTORY]")
35+
print(" convert [FILENAME]")
36+
print(" grid-hub {start|stop|restart} [OPTIONS]")
37+
print(" grid-node {start|stop|restart} --hub=[HUB_IP] [OPTIONS]")
38+
print("")
39+
40+
41+
def show_mkdir_usage():
42+
print(" ** mkdir **")
43+
print("")
44+
print(" Usage:")
45+
print(" seleniumbase mkdir [DIRECTORY_NAME]")
46+
print(" Output:")
47+
print(" Creates a new folder for running SeleniumBase scripts.")
48+
print(" The new folder contains default config files,")
49+
print(" sample tests for helping new users get started, and")
50+
print(" Python boilerplates for setting up customized")
51+
print(" test frameworks.")
52+
print("")
53+
54+
55+
def show_convert_usage():
56+
print(" ** convert **")
57+
print("")
58+
print(" Usage:")
59+
print(" seleniumbase convert [MY_TEST.py]")
60+
print(" Output:")
61+
print(" Converts a Selenium IDE exported WebDriver unittest")
62+
print(" file into a SeleniumBase file. Adds _SB to the new")
63+
print(" file name while keeping the original file intact.")
64+
print(" Works with Katalon Recorder scripts.")
65+
print(" See: http://www.katalon.com/automation-recorder")
66+
print("")
67+
68+
69+
def show_grid_hub_usage():
70+
print(" ** grid-hub **")
71+
print("")
72+
print(" Usage:")
73+
print(" seleniumbase grid-hub {start|stop|restart}")
74+
print(" Options:")
75+
print(" -v, --verbose (Increase verbosity of logging output.)")
76+
print(" (Default: Quiet logging / not verbose.)")
77+
print(" Output:")
78+
print(" Controls the Selenium Grid Hub Server, which allows")
79+
print(" for running tests on multiple machines in parallel")
80+
print(" to speed up test runs and reduce the total time")
81+
print(" of test suite execution.")
82+
print(" You can start, restart, or stop the Grid Hub server.")
83+
print("")
84+
85+
86+
def show_grid_node_usage():
87+
print(" ** grid-node **")
88+
print("")
89+
print(" Usage:")
90+
print(" seleniumbase grid-node {start|stop|restart} [OPTIONS]")
91+
print(" Options:")
92+
print(" --hub=HUB_IP (The Grid Hub IP Address to connect to.)")
93+
print(" (Default: 127.0.0.1 if not set)")
94+
print(" -v, --verbose (Increase verbosity of logging output.)")
95+
print(" (Default: Quiet logging / not verbose.)")
96+
print(" Output:")
97+
print(" Controls the Selenium Grid node, which serves as a")
98+
print(" worker machine for your Selenium Grid Hub server.")
99+
print(" You can start, restart, or stop the Grid node.")
100+
print("")
101+
102+
103+
def show_detailed_help():
104+
show_basic_usage()
105+
print("More Info:")
106+
print("")
107+
show_mkdir_usage()
108+
show_convert_usage()
109+
show_grid_hub_usage()
110+
show_grid_node_usage()
111+
112+
113+
def main():
114+
num_args = len(sys.argv)
115+
if num_args == 1:
116+
show_usage()
117+
return
118+
elif num_args == 2:
119+
command = sys.argv[1]
120+
command_args = []
121+
elif num_args > 2:
122+
command = sys.argv[1]
123+
command_args = sys.argv[2:]
124+
print command
125+
126+
if command == "convert":
127+
if len(command_args) == 1:
128+
convert_ide.main()
129+
else:
130+
show_basic_usage()
131+
show_convert_usage()
132+
elif command == "mkdir":
133+
if len(command_args) == 1:
134+
sb_mkdir.main()
135+
else:
136+
show_basic_usage()
137+
show_mkdir_usage()
138+
elif command == "grid-hub":
139+
if len(command_args) >= 1:
140+
grid_hub.main()
141+
else:
142+
show_basic_usage()
143+
show_grid_hub_usage()
144+
elif command == "grid-node":
145+
if len(command_args) >= 1:
146+
grid_node.main()
147+
else:
148+
show_basic_usage()
149+
show_grid_node_usage()
150+
elif command == "help" or command == "--help":
151+
show_detailed_help()
152+
else:
153+
show_usage()
154+
155+
156+
if __name__ == "__main__":
157+
main()

0 commit comments

Comments
 (0)