diff options
author | Saurav Chirania <saurav.chir@gmail.com> | 2018-06-19 19:56:43 +0530 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2018-06-24 12:57:23 +0200 |
commit | 04645273d7c9dd6927035b571464148a38d3db9f (patch) | |
tree | 638b1df0297a65fa90504fe19bcb7b22c9061bca /uitest | |
parent | b691e5824a6346d2fe7f702b5280b56532a2f89e (diff) |
uitest: interpreter for log files
this file interprets the following grammar:
<keyword> <object_description> [<parameters>]
where keyword describes about the UIObject the user interacts with
object_description tells the ID, parent, etc. of the UIObject
and parameters are the parameters of the action user performs
in a format directly readable by mkPropertyValues
keyword examples - ModalDialogExecuted, ButtonUIObject
parameter examples - {"TEXT":"A"}, {"POS": 1}, etc.
Line-by-line, this interpreter tries to convert given
log into python code. An example can be found at-
"https://pastebin.com/raw/KQi7Y6ie"
Change-Id: Iade4388b16094a94df1893f9925605bee51b164f
Reviewed-on: https://gerrit.libreoffice.org/56120
Reviewed-by: Markus Mohrhard <markus.mohrhard@googlemail.com>
Tested-by: Markus Mohrhard <markus.mohrhard@googlemail.com>
Diffstat (limited to 'uitest')
-rw-r--r-- | uitest/loginterpreter.py | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/uitest/loginterpreter.py b/uitest/loginterpreter.py new file mode 100644 index 000000000000..252fc4bd6c42 --- /dev/null +++ b/uitest/loginterpreter.py @@ -0,0 +1,156 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# + +import os +import sys + +def usage(): + message = "usage: {program} inputfile outputfile" + + print(message.format(program = os.path.basename(sys.argv[0]))) + +def parse_line(line): + """ + This function parses a line from log file + and returns the parsed values as a python dictionary + """ + if (line == "" or line.startswith("Action on element")): + return + dict = {} + if "{" in line: + start_index_of_parameters = line.find("{") + end_index_of_parameters = line.find("}") + 1 + parameters = line[start_index_of_parameters:end_index_of_parameters] + dict["parameters"] = parameters + line = line[:start_index_of_parameters-1] + word_list = line.split() + dict["keyword"] = word_list[0] + + for index in range(1,len(word_list)): + key, val = word_list[index].split(":",1) + dict[key] = val + return dict + +def parse_args(argv): + """ + This function parses the command-line arguments + to get the input and output file details + """ + if len(argv) != 3: + usage() + sys.exit(1) + else: + input_address = argv[1] + output_address = argv[2] + + return input_address, output_address + +def get_log_file(input_address): + try: + with open(input_address) as f: + content = f.readlines() + except IOError as err: + print("IO error: {0}".format(err)) + usage() + sys.exit(1) + + content = [x.strip() for x in content] + return content + +def initiate_test_generation(address): + try: + f = open(address,"w") + except IOError as err: + print("IO error: {0}".format(err)) + usage() + sys.exit(1) + initial_text = \ + "from uitest.framework import UITestCase\n" + \ + "import importlib\n\n" + \ + "class TestClass(UITestCase):\n" + \ + " def test_function(self):\n" + f.write(initial_text) + return f + +def get_coupling_type(line1, line2): + """ + This function checks if two consecutive lines of log file + refer to the same event + """ + action_dict1 = parse_line(line1) + action_dict2 = parse_line(line2) + + if action_dict1["keyword"] == "CommandSent" and \ + action_dict2["keyword"] == "ModalDialogExecuted": + return "COMMAND_MODAL_COUPLE" + + elif action_dict1["keyword"] == "CommandSent" and \ + action_dict2["keyword"] == "ModelessDialogExecuted": + return "COMMAND_MODELESS_COUPLE" + + return "NOT_A_COUPLE" + +def get_test_line_from_one_log_line(log_line): + action_dict = parse_line(log_line) + test_line = " " + if action_dict["keyword"].endswith("UIObject"): + parent = action_dict["Parent"] + if (parent != ""): + test_line += \ + action_dict["Id"] + " = " + parent + ".getChild(\"" + \ + action_dict["Id"] + "\")\n " + \ + action_dict["Id"] + ".executeAction(\"" + \ + action_dict["Action"] + "\"" + if "parameters" in action_dict: + test_line += ", mkPropertyValues(" + \ + action_dict["parameters"] + "))\n" + else: + test_line += ",tuple())\n" + return test_line + + return "" + +def get_test_line_from_two_log_lines(log_line1,log_line2): + coupling_type = get_coupling_type(log_line1, log_line2) + action_dict1 = parse_line(log_line1) + action_dict2 = parse_line(log_line2) + test_line = " " + if coupling_type == "COMMAND_MODAL_COUPLE": + test_line += \ + "self.ui_test.execute_dialog_through_command(\"" + \ + action_dict1["Name"] + "\")\n " + \ + action_dict2["Id"] + " = self.xUITest.getTopFocusWindow()\n" + elif coupling_type == "COMMAND_MODELESS_COUPLE": + test_line += \ + "self.ui_test.execute_modeless_dialog_through_command(\"" + \ + action_dict1["Name"] + "\")\n " + \ + action_dict2["Id"] + " = self.xUITest.getTopFocusWindow()\n" + return test_line + +def main(): + input_address, output_address = parse_args(sys.argv) + log_lines = get_log_file(input_address) + output_stream = initiate_test_generation(output_address) + line_number = 0 + while line_number < len(log_lines): + if line_number == len(log_lines)-1 or \ + get_coupling_type(log_lines[line_number],log_lines[line_number + 1]) == "NOT_A_COUPLE": + test_line = get_test_line_from_one_log_line(log_lines[line_number]) + output_stream.write(test_line) + line_number += 1 + else: + test_line = get_test_line_from_two_log_lines(log_lines[line_number],log_lines[line_number + 1]) + output_stream.write(test_line) + line_number += 2 + output_stream.close() + +if __name__ == '__main__': + main() + +# vim: set shiftwidth=4 softtabstop=4 expandtab:
\ No newline at end of file |