aboutsummaryrefslogtreecommitdiffstats
path: root/profiles/robot/src/main/resources/script/run-robot-testcase.py
blob: 9fe0f607009849d5be2ce1691fdd103dbf17d078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
# Copyright 2020 Simran Singhal.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import argparse
import uuid
from robot import run
from argparse import RawTextHelpFormatter

def execute_robot_run(request_id, test_suite, testcase, variables_file_path):
  output_dir = os.getenv('OPEN_CLI_HOME') + '/data/executions/' + request_id + '/logs'

  with open('run.txt', 'w') as output:
    if(variables_file_path):
      run(test_suite, report=None, log=None, test=testcase, variablefile=variables_file_path, stdout=output, stderr=output, outputdir=output_dir)
    else:
      run(test_suite, report=None, log=None, test=testcase, stdout=output, stderr=output, outputdir=output_dir)

  if os.path.exists('run.txt'):
    with open('run.txt', 'r') as log:
        print(log.read())
    os.remove('run.txt')

def main():
  text = 'This command helps to run a robot testcase\n' \
         'These python modules are need to be installed for running the tests\n' \
         'robotframework==3.1\n' \
         'RESTinstance==1.0.0rc4\n' \
         'robotframework-dependencylibrary==1.0.0.post1\n' \
         'robotframework-jsonlibrary==0.3\n' \
         'robotframework-jsonschemalibrary==1.0\n' \
         'robotframework-mockserver==0.0.4\n'

  parser = argparse.ArgumentParser(description=text, formatter_class=RawTextHelpFormatter)
  parser.add_argument('--request-id', action='store', dest='request_id',
                      help='Request Id to track the progress of running this script',
                      default=os.environ.get('OPEN_CLI_REQUEST_ID'))
  parser.add_argument('--test-suite', action='store', dest='test_suite',
                        help='Location to test suite file', required=True)
  parser.add_argument('--testcase', action='store', dest='testcase',
                        help='Name of the testcase', required=True, nargs='+')
  parser.add_argument('--variables-file-path', action='store', dest='variables_file_path',
                        help='Location to variable file', nargs='?', const='')

  args = parser.parse_args()

  request_id = args.request_id
  test_suite = args.test_suite
  testcase = ' '.join(args.testcase)
  variables_file_path = ''

  if not request_id:
    request_id = str(uuid.uuid4())

  if args.variables_file_path:
    variables_file_path = args.variables_file_path

    if os.path.exists(variables_file_path):

      if not os.path.isfile(variables_file_path):
        raise Exception('Given variable file path is not a file\n')

    else:
      raise Exception('Given variable file path does not exist\n')

  if not os.path.exists(test_suite):
    raise Exception('Given api-tests folder location does not exist\n')

  execute_robot_run(request_id,test_suite, testcase, variables_file_path)

if __name__ == '__main__':
  main()