|
| 1 | +#!/usr/bin/env python |
| 2 | +import os |
| 3 | +try: |
| 4 | + from shlex import quote |
| 5 | +except ImportError: |
| 6 | + from pipes import quote |
| 7 | +try: |
| 8 | + from subprocess import run |
| 9 | +except ImportError: # py2 |
| 10 | + from subprocess import call as run |
| 11 | + |
| 12 | + |
| 13 | +def prettify_path(path): |
| 14 | + return path.split('/')[-1].replace('.json', '') |
| 15 | + |
| 16 | + |
| 17 | +if __name__ == "__main__": |
| 18 | + environment = os.getenv('ENVIRONMENT') |
| 19 | + markers = quote(os.getenv('MARKERS')) |
| 20 | + keywords = quote(os.getenv('KEYWORDS')) |
| 21 | + os_version = os.getenv('OS') |
| 22 | + browser = os.getenv('BROWSER') |
| 23 | + parallel_sessions = os.getenv('PARALLEL_SESSIONS') |
| 24 | + build_id = quote('results/{0}.xml'.format(os.getenv('BUILD_ID'))) |
| 25 | + grid_url = os.getenv('SELENIUM_GRID_URL', '') |
| 26 | + fallback_grid_url = 'http://{0}:{1}@hub.browserstack.com:80/wd/hub'.format( |
| 27 | + '{{cookiecutter.browserstack_username}}', |
| 28 | + '{{cookiecutter.browserstack_access_key}}', |
| 29 | + ) |
| 30 | + selenium_grid_url = grid_url and quote(grid_url) or fallback_grid_url |
| 31 | + |
| 32 | + os_file = 'capabilities/{0}'.format(os_version) |
| 33 | + browser_file = 'capabilities/{0}'.format(browser) |
| 34 | + resolution_file = 'capabilities/{0}'.format( |
| 35 | + os.getenv('RESOLUTION')) |
| 36 | + credentials_file = 'credentials/credentials-{0}.yml'.format(environment) |
| 37 | + |
| 38 | + assert os.path.isfile(os_file) |
| 39 | + assert os.path.isfile(browser_file) |
| 40 | + assert os.path.isfile(resolution_file) |
| 41 | + assert os.path.isfile(credentials_file) |
| 42 | + |
| 43 | + pytest_cmd = [ |
| 44 | + 'tox', |
| 45 | + '-epy36', |
| 46 | + '--', |
| 47 | + '-vvv', |
| 48 | + '--variables=capabilities/project.json', |
| 49 | + '--variables={0}'.format(os_file), |
| 50 | + '--variables={0}'.format(browser_file), |
| 51 | + '--variables={0}'.format(resolution_file), |
| 52 | + '--splinter-webdriver=remote', |
| 53 | + '--splinter-remote-url={0}'.format( |
| 54 | + selenium_grid_url), |
| 55 | + '--variables={0}'.format(credentials_file), |
| 56 | + '--junitxml={0}'.format(build_id), |
| 57 | + ] |
| 58 | + |
| 59 | + if markers: |
| 60 | + pytest_cmd.append('-m={0}'.format(markers)) |
| 61 | + |
| 62 | + if keywords: |
| 63 | + pytest_cmd.append('-k={0}'.format(keywords)) |
| 64 | + |
| 65 | + if os.getenv('DEBUG') == 'true': |
| 66 | + pytest_cmd.append('--variables=capabilities/debug.json') |
| 67 | + |
| 68 | + if os.getenv('TESTRAIL_ENABLE') == 'true': |
| 69 | + if 'pcmw' in environment: |
| 70 | + env = 'pcmw' |
| 71 | + else: |
| 72 | + env = 'sust' |
| 73 | + |
| 74 | + testrail_file = 'conf/testrail/testrail-{0}.cfg'.format(env) |
| 75 | + assert os.path.isfile(testrail_file) |
| 76 | + |
| 77 | + tr_name = quote('[{0}][{1}][{2}][{3}][{4}]'.format( |
| 78 | + prettify_path(environment), prettify_path(os_version), |
| 79 | + prettify_path(browser), markers, keywords)) |
| 80 | + |
| 81 | + pytest_cmd.extend([ |
| 82 | + '--testrail={0}'.format(testrail_file), |
| 83 | + '--tr_name={0}"'.format(tr_name), |
| 84 | + ]) |
| 85 | + |
| 86 | + if os.getenv('BLOCK_FIRST_FAILURE') == 'true': |
| 87 | + pytest_cmd.append('-x') |
| 88 | + |
| 89 | + if parallel_sessions: |
| 90 | + assert isinstance(parallel_sessions, int) |
| 91 | + pytest_cmd.append('-n {0}'.format(parallel_sessions)) |
| 92 | + |
| 93 | + print(str(pytest_cmd)) |
| 94 | + run(pytest_cmd) |
0 commit comments