Add seleniumbase console scripts interface

This commit is contained in:
Michael Mintz 2018-07-25 02:15:12 -04:00
parent c62419920e
commit 6bae5d9b8b
5 changed files with 461 additions and 3 deletions

54
console_scripts/ReadMe.md Executable file
View File

@ -0,0 +1,54 @@
## Console Scripts
### mkdir
* Usage:
``seleniumbase mkdir [DIRECTORY_NAME]``
* Output:
Creates a new folder for running SeleniumBase scripts.
The new folder contains default config files,
sample tests for helping new users get started, and
Python boilerplates for setting up customized
test frameworks.
### convert
* Usage:
``seleniumbase convert [MY_TEST.py]``
* Output:
Converts a Selenium IDE exported WebDriver unittest
file into a SeleniumBase file. Adds _SB to the new
file name while keeping the original file intact.
Works with Katalon Recorder scripts.
See: http://www.katalon.com/automation-recorder
### grid-hub
* Usage:
``seleniumbase grid-hub {start|stop|restart}``
* Options:
``-v``, ``--verbose`` (Increases verbosity of logging output.)
* Output:
Controls the Selenium Grid Hub server, which allows
for running tests on multiple machines in parallel
to speed up test runs and reduce the total time
of test suite execution.
You can start, restart, or stop the Grid Hub server.
### grid-node
* Usage:
``seleniumbase grid-node {start|stop|restart} [OPTIONS]``
* Options:
``--hub=HUB_IP`` (The Grid Hub IP Address to connect to.) (Default: ``127.0.0.1``)
``-v``, ``--verbose`` (Increases verbosity of logging output.)
* Output:
Controls the Selenium Grid node, which serves as a
worker machine for your Selenium Grid Hub server.
You can start, restart, or stop the Grid node.

0
console_scripts/__init__.py Executable file
View File

157
console_scripts/run.py Normal file
View File

@ -0,0 +1,157 @@
"""
SeleniumBase console scripts runner
Usage:
seleniumbase [COMMAND] [PARAMETERS]
Examples:
seleniumbase mkdir [DIRECTORY_NAME]
seleniumbase convert [PYTHON_WEBDRIVER_UNITTEST_FILE].py
seleniumbase grid-hub start
seleniumbase grid-node start --hub=127.0.0.1
"""
import sys
from console_scripts import sb_mkdir
from integrations.selenium_grid import grid_hub
from integrations.selenium_grid import grid_node
from integrations.selenium_ide import convert_ide
def show_usage():
show_basic_usage()
print('Type "seleniumbase help" for more details.\n')
def show_basic_usage():
print("")
print(">>>>>>>>>>>>")
print("")
print('Usage: "seleniumbase [command] [parameters]"')
print("")
print("Commands:")
print("")
print(" mkdir [DIRECTORY]")
print(" convert [FILENAME]")
print(" grid-hub {start|stop|restart} [OPTIONS]")
print(" grid-node {start|stop|restart} --hub=[HUB_IP] [OPTIONS]")
print("")
def show_mkdir_usage():
print(" ** mkdir **")
print("")
print(" Usage:")
print(" seleniumbase mkdir [DIRECTORY_NAME]")
print(" Output:")
print(" Creates a new folder for running SeleniumBase scripts.")
print(" The new folder contains default config files,")
print(" sample tests for helping new users get started, and")
print(" Python boilerplates for setting up customized")
print(" test frameworks.")
print("")
def show_convert_usage():
print(" ** convert **")
print("")
print(" Usage:")
print(" seleniumbase convert [MY_TEST.py]")
print(" Output:")
print(" Converts a Selenium IDE exported WebDriver unittest")
print(" file into a SeleniumBase file. Adds _SB to the new")
print(" file name while keeping the original file intact.")
print(" Works with Katalon Recorder scripts.")
print(" See: http://www.katalon.com/automation-recorder")
print("")
def show_grid_hub_usage():
print(" ** grid-hub **")
print("")
print(" Usage:")
print(" seleniumbase grid-hub {start|stop|restart}")
print(" Options:")
print(" -v, --verbose (Increase verbosity of logging output.)")
print(" (Default: Quiet logging / not verbose.)")
print(" Output:")
print(" Controls the Selenium Grid Hub Server, which allows")
print(" for running tests on multiple machines in parallel")
print(" to speed up test runs and reduce the total time")
print(" of test suite execution.")
print(" You can start, restart, or stop the Grid Hub server.")
print("")
def show_grid_node_usage():
print(" ** grid-node **")
print("")
print(" Usage:")
print(" seleniumbase grid-node {start|stop|restart} [OPTIONS]")
print(" Options:")
print(" --hub=HUB_IP (The Grid Hub IP Address to connect to.)")
print(" (Default: 127.0.0.1 if not set)")
print(" -v, --verbose (Increase verbosity of logging output.)")
print(" (Default: Quiet logging / not verbose.)")
print(" Output:")
print(" Controls the Selenium Grid node, which serves as a")
print(" worker machine for your Selenium Grid Hub server.")
print(" You can start, restart, or stop the Grid node.")
print("")
def show_detailed_help():
show_basic_usage()
print("More Info:")
print("")
show_mkdir_usage()
show_convert_usage()
show_grid_hub_usage()
show_grid_node_usage()
def main():
num_args = len(sys.argv)
if num_args == 1:
show_usage()
return
elif num_args == 2:
command = sys.argv[1]
command_args = []
elif num_args > 2:
command = sys.argv[1]
command_args = sys.argv[2:]
print command
if command == "convert":
if len(command_args) == 1:
convert_ide.main()
else:
show_basic_usage()
show_convert_usage()
elif command == "mkdir":
if len(command_args) == 1:
sb_mkdir.main()
else:
show_basic_usage()
show_mkdir_usage()
elif command == "grid-hub":
if len(command_args) >= 1:
grid_hub.main()
else:
show_basic_usage()
show_grid_hub_usage()
elif command == "grid-node":
if len(command_args) >= 1:
grid_node.main()
else:
show_basic_usage()
show_grid_node_usage()
elif command == "help" or command == "--help":
show_detailed_help()
else:
show_usage()
if __name__ == "__main__":
main()

239
console_scripts/sb_mkdir.py Executable file
View File

@ -0,0 +1,239 @@
"""
Creates a new folder for running SeleniumBase scripts.
Usage:
seleniumbase mkdir [DIRECTORY_NAME]
Output:
A new folder for running SeleniumBase scripts.
Contains default config files, boilerplates, and sample tests.
"""
import codecs
import os
import sys
def main():
expected_arg = ("[DIRECTORY_NAME]")
num_args = len(sys.argv)
if sys.argv[0].split('/')[-1] == "seleniumbase" or (
sys.argv[0].split('\\')[-1] == "seleniumbase"):
if num_args < 3 or num_args > 3:
raise Exception('\n* INVALID RUN COMMAND! * Usage:\n'
'"seleniumbase convert %s"\n' % expected_arg)
else:
raise Exception('\n* INVALID RUN COMMAND! * Usage:\n'
'"seleniumbase convert %s"\n' % expected_arg)
dir_name = sys.argv[num_args-1]
if len(str(dir_name)) < 2:
raise Exception('Directory name length must be at least 2 '
'characters long!')
if os.path.exists(os.getcwd() + '/' + dir_name):
raise Exception('Directory "%s" already exists '
'in the current path!\n' % dir_name)
else:
os.mkdir(dir_name)
data = []
data.append("[pytest]")
data.append("addopts = --capture=no --ignore conftest.py")
file_path = "%s/%s" % (dir_name, "pytest.ini")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
data = []
data.append("[nosetests]")
data.append("nocapture=1")
data.append("logging-level=INFO")
data.append("")
data.append("[bdist_wheel]")
data.append("universal=1")
file_path = "%s/%s" % (dir_name, "config.cfg")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
data = []
data.append("from seleniumbase import BaseCase")
data.append("")
data.append("")
data.append("class MyTestClass(BaseCase):")
data.append("")
data.append(" def test_basic(self):")
data.append(" self.open('http://xkcd.com/353/')")
data.append(" self.assert_element('img[alt=\"Python\"]')")
data.append(" self.click('a[rel=\"license\"]')")
data.append(" self.assert_text('free to copy', 'div center')")
data.append(" self.open(\"http://xkcd.com/1481/\")")
data.append(
" title = self.get_attribute(\"#comic img\", \"title\")")
data.append(
" self.assertTrue(\"86,400 seconds per day\" in title)")
data.append(" self.click('link=Blag')")
data.append(
" self.assert_text('The blag of the webcomic', 'h2')")
data.append(" self.update_text('input#s', 'Robots!\\n')")
data.append(" self.assert_text('Hooray robots!', '#content')")
data.append(" self.open('http://xkcd.com/1319/')")
data.append(" self.assert_text('Automation', 'div#ctitle')")
data.append("")
file_path = "%s/%s" % (dir_name, "my_first_test.py")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
dir_name_2 = dir_name + "/" + "boilerplates"
os.mkdir(dir_name_2)
data = []
data.append("")
file_path = "%s/%s" % (dir_name_2, "__init__.py")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
data = []
data.append("from seleniumbase import BaseCase")
data.append("")
data.append("")
data.append("class BaseTestCase(BaseCase):")
data.append("")
data.append(" def setUp(self):")
data.append(" super(BaseTestCase, self).setUp()")
data.append(" # Add custom setUp code AFTER the super() line")
data.append("")
data.append(" def tearDown(self):")
data.append(" # Add custom code BEFORE the super() line")
data.append(" super(BaseTestCase, self).tearDown()")
data.append("")
data.append(" def login(self):")
data.append(" # <<< Placeholder. Add your code here. >>>")
data.append(" pass")
data.append("")
data.append(" def example_method(self):")
data.append(" # <<< Placeholder. Add your code here. >>>")
data.append(" pass")
data.append("")
file_path = "%s/%s" % (dir_name_2, "base_test_case.py")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
data = []
data.append("class Page(object):")
data.append(" html = 'html'")
data.append("")
file_path = "%s/%s" % (dir_name_2, "page_objects.py")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
data = []
data.append("from .base_test_case import BaseTestCase")
data.append("from .page_objects import Page")
data.append("")
data.append("")
data.append("class MyTestClass(BaseTestCase):")
data.append("")
data.append(" def test_boilerplate(self):")
data.append(" self.login()")
data.append(" self.example_method()")
data.append(" self.assert_element(Page.html)")
data.append("")
file_path = "%s/%s" % (dir_name_2, "boilerplate_test.py")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
dir_name_3 = dir_name_2 + "/" + "samples"
os.mkdir(dir_name_3)
data = []
data.append("")
file_path = "%s/%s" % (dir_name_3, "__init__.py")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
data = []
data.append("from seleniumbase import BaseCase")
data.append("from .bing_objects import Page")
data.append("")
data.append("")
data.append("class BingTests(BaseCase):")
data.append("")
data.append(" def test_bing(self):")
data.append(" self.open('https://www.bing.com/')")
data.append(" self.update_text(Page.search_box, 'github')")
data.append(" self.assert_element('li[query=\"github\"]')")
data.append(" self.click(Page.search_button)")
data.append(
" self.assert_text('github.com', Page.search_results)")
data.append(" self.click_link_text('Images')")
data.append(" self.assert_element('img[alt*=\"github\"]')")
data.append("")
file_path = "%s/%s" % (dir_name_3, "bing_test.py")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
data = []
data.append("class Page(object):")
data.append(" search_box = 'input.b_searchbox'")
data.append(" search_button = 'input[name=\"go\"]'")
data.append(" search_results = '#b_results'")
data.append("")
file_path = "%s/%s" % (dir_name_3, "bing_objects.py")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
data = []
data.append("from seleniumbase import BaseCase")
data.append("from .google_objects import HomePage, ResultsPage")
data.append("")
data.append("")
data.append("class GoogleTests(BaseCase):")
data.append("")
data.append(" def test_google_dot_com(self):")
data.append(" self.open('https://www.google.com')")
data.append(" self.assert_element(HomePage.search_button)")
data.append(
" self.assert_element(HomePage.feeling_lucky_button)")
data.append(
" self.update_text(HomePage.search_box, 'github\\n')")
data.append(
" self.assert_text('github.com', "
"ResultsPage.search_results)")
data.append(" self.click_link_text('Images')")
data.append(
" self.assert_element("
"'img[alt=\"Image result for github\"]')")
data.append("")
file_path = "%s/%s" % (dir_name_3, "google_test.py")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
data = []
data.append("class HomePage(object):")
data.append(" search_box = 'input[title=\"Search\"]'")
data.append(" search_button = 'input[value=\"Google Search\"]'")
data.append(
" feeling_lucky_button = "
"'''input[value=\"I'm Feeling Lucky\"]'''")
data.append("")
data.append("")
data.append("class ResultsPage(object):")
data.append(" google_logo = 'img[alt=\"Google\"]'")
data.append(" search_results = 'div#center_col'")
data.append("")
file_path = "%s/%s" % (dir_name_3, "google_objects.py")
file = codecs.open(file_path, "w+", "utf-8")
file.writelines("\r\n".join(data))
file.close()
if __name__ == "__main__":
main()

View File

@ -36,13 +36,21 @@ setup(
'PyVirtualDisplay==0.2.1',
],
packages=['seleniumbase',
'seleniumbase.common',
'seleniumbase.config',
'seleniumbase.core',
'seleniumbase.plugins',
'seleniumbase.fixtures',
'seleniumbase.masterqa',
'seleniumbase.common',
'seleniumbase.config'],
'seleniumbase.plugins',
'console_scripts',
'integrations',
'integrations.selenium_grid',
'integrations.selenium_ide',
],
entry_points={
'console_scripts': [
'seleniumbase = console_scripts.run:main',
],
'nose.plugins': [
'base_plugin = seleniumbase.plugins.base_plugin:Base',
'selenium = seleniumbase.plugins.selenium_plugin:SeleniumBrowser',