diff --git a/automation-tests/run.py b/automation-tests/run.py index 7cb1d55a64dd6cf77f1b74c4ecd666282e34f191..72affbffc425dc2dd24b46569aa332170877c858 100755 --- a/automation-tests/run.py +++ b/automation-tests/run.py @@ -25,7 +25,7 @@ def which(program): def main(): - # virtualenv location differs on windows + # get path to python: virtualenv location differs on windows # TODO platform detection is brittle. is there a better way? if platform.system() == 'Windows': env_path = 'bid_selenium\Scripts\\' @@ -44,14 +44,26 @@ def main(): default="dev", help='run tests against an ephemeral' + ' instance. Specify your instance\'s hostname ("foo"),' + ' not the full domain name ("foo.123done.org")') - # TODO add other options + parser.add_option('--everywhere', '-e', dest='run_everywhere', action='store_true', + help='like --all, but run all tests on all supported' + + ' browsers using sauce labs credentials either' + + ' specified in sauce.yaml or in environment' + + ' variables PERSONA_SAUCE_USER, PERSONA_SAUCE_PASSWORD,' + + ' and PERSONA_SAUCE_APIKEY.') options, arguments = parser.parse_args() - # 1. check that python is the right version TODO: would 2.6 actually work? + # you can't specify both --all and --everywhere + if options.run_everywhere and options.run_all: + sys.stderr.write("either use --all or --everywhere, not both") + exit(1) + + # 1. check that python is the right version + # TODO: would 2.6 actually work? if sys.version_info < (2,7,0): sys.stderr.write('python 2.7 or later is required to run the tests\n') exit(1) - # 2. TODO check that virtualenv and pip exist. if not, bail. + + # 2. check that virtualenv and pip exist. if not, bail. if not which('pip'): sys.stderr.write('pip must be installed; do "easy_install pip", ' + ' then try again\n') @@ -60,6 +72,7 @@ def main(): sys.stderr.write('virtualenv must be installed; do "pip install ' + 'virtualenv", then try again\n') exit(1) + # 3. create the virtualenv if they asked you to install it or it's missing if options.install or not os.path.exists(env_py): subprocess.call('virtualenv bid_selenium', shell=True) @@ -67,31 +80,75 @@ def main(): subprocess.call(env_path + 'pip install -Ur requirements.txt', shell=True) - # 4 1/2. check the ephemeral instance to hit. + # 4. check the ephemeral instance to hit. host = options.target_hostname - # 5. run the tests - # TODO parse arguments to know which tests to run - # TODO right now we only run one 123done test in the default case - if options.run_all: - subprocess.call(env_py + ' -m py.test --destructive ' + - '--credentials=credentials.yaml ' + - '--baseurl=http://' + host + '.123done.org ' + - '--driver=firefox -q browserid', shell=True); - subprocess.call(env_py + ' -m py.test --destructive ' + - '--credentials=credentials.yaml ' + - '--baseurl=http://' + host + '.myfavoritebeer.org ' + - '--driver=firefox -q myfavoritebeer', shell=True); - tests_123 = '123done' + # 5. check for/create sauce.yaml, if necessary + if options.run_everywhere: + # if sauce.yaml does not exist, + if not os.path.isfile('sauce.yaml'): + # look for environmental variables PERSONA_SAUCE_* + try: + username = os.environ['PERSONA_SAUCE_USER'] + password = os.environ['PERSONA_SAUCE_PASSWORD'] + api_key = os.environ['PERSONA_SAUCE_APIKEY'] + # if they are missing, bail + except KeyError: + sys.stderr.write('Sauce labs credentials are needed to run' + + ' tests everywhere. Add credentials to sauce.yaml or, if' + + ' you have access to persona dev secrets, check that' + + ' the PERSONA_SAUCE_USER, PERSONA_SAUCE_PASSWORD, and' + + ' PERSONA_SAUCE_APIKEY environmental variables are set.\n') + exit(1) + # if they are present, write them out to sauce.yaml + try: + saucefile = open('sauce.yaml', 'w') + saucefile.write('username: ' + username + '\n') + saucefile.write('password: ' + password + '\n') + saucefile.write('api-key: ' + api_key + '\n') + saucefile.close() + # if you can't open the file for editing, bail + except IOError: + sys.stderr.write('Unable to open sauce.yaml to write out' + + ' credentials. Either create sauce.yaml manually, or' + + ' ensure the test process has permission to create the file.\n') + exit(1) + + # 6. run the tests + + # TODO move the run_everywhere list into a config file? + if options.run_everywhere: + browsers = ['--platform=LINUX --browsername=firefox --browserver=13 ', + '--platform=LINUX --browsername=opera --browserver=12 ', + '--platform=MAC --browsername=firefox --browserver=14 ', + '--platform=VISTA --browsername=chrome ', + '--platform=VISTA --browsername=firefox --browserver=13 ', + '--platform=VISTA --browsername="internet explorer" --browserver=9 ', + '--platform=XP --browsername="internet explorer" --browserver=8 '] + sauce = '--saucelabs=sauce.yaml ' else: - tests_123 = '123done/tests/test_new_user.py' - # the 123done tests always run - subprocess.call(env_py + ' -m py.test --destructive ' + - '--credentials=credentials.yaml ' + - '--baseurl=http://' + host + '.123done.org ' + - '--driver=firefox -q ' + tests_123, shell=True); - - # 6. TODO deactivate/destroy virtualenv?? maybe '--cleanup' argument? + browsers = ['--driver=firefox '] + sauce = '' + + for browser in browsers: + if options.run_everywhere or options.run_all: + subprocess.call(env_py + ' -m py.test --destructive ' + + '--credentials=credentials.yaml ' + sauce + browser + + ' --baseurl=http://' + host + '.123done.org -q browserid', shell=True) + subprocess.call(env_py + ' -m py.test --destructive ' + + '--credentials=credentials.yaml ' + sauce + browser + + ' --baseurl=http://' + host + '.123done.org -q 123done', shell=True) + subprocess.call(env_py + ' -m py.test --destructive ' + + '--credentials=credentials.yaml ' + sauce + browser + + ' --baseurl=http://' + host + '.myfavoritebeer.org -q myfavoritebeer', shell=True) + # only run one test in the default case + else: + subprocess.call(env_py + ' -m py.test --destructive ' + + '--credentials=credentials.yaml ' + sauce + browser + + ' --baseurl=http://' + host + '.123done.org ' + + '-q 123done/tests/test_new_user.py', shell=True) + + # 7. TODO deactivate/destroy virtualenv?? maybe '--cleanup' argument? if __name__ == '__main__':