2 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
4 # This file is part of the LibreOffice project.
6 # This Source Code Form is subject to the terms of the Mozilla Public
7 # License, v. 2.0. If a copy of the MPL was not distributed with this
8 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
17 This function parses a line from log file
18 and returns the parsed values as a python dictionary
24 start_index_of_parameters
= line
.find("{")
25 end_index_of_parameters
= line
.find("}") + 1
26 parameters
= line
[start_index_of_parameters
:end_index_of_parameters
]
28 dict["parameters"] = parameters
29 line
= line
[:start_index_of_parameters
-1]
30 word_list
= line
.split()
31 dict["keyword"] = word_list
[0]
33 for index
in range(1,len(word_list
)):
34 key
, val
= word_list
[index
].split(":",1)
40 This function parses the command-line arguments
41 to get the input and output file details
43 parser
= argparse
.ArgumentParser(description
= "Generate a UI test file from log")
44 parser
.add_argument("input_address", type = str, help = "The log file address")
45 parser
.add_argument("output_address", type = str, help = "The test file address")
46 parser
.add_argument("-d", "--document", metavar
= "", help = "Address of the document to be opened")
47 args
= parser
.parse_args()
50 def get_log_file(input_address
):
52 with
open(input_address
) as f
:
53 content
= f
.readlines()
54 except IOError as err
:
55 print("IO error: {0}".format(err
))
56 print("Use " + os
.path
.basename(sys
.argv
[0]) + " -h to get usage instructions")
59 content
= [x
.strip() for x
in content
if not x
.startswith("Action on element")]
62 def initiate_test_generation(address
):
65 except IOError as err
:
66 print("IO error: {0}".format(err
))
67 print("Use " + os
.path
.basename(sys
.argv
[0]) + " -h to get usage instructions")
70 "# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-\n\n" + \
71 "from uitest.framework import UITestCase\n" + \
72 "from libreoffice.uno.propertyvalue import mkPropertyValues\n" + \
73 "import importlib\n\n" + \
74 "class TestClass(UITestCase):\n" + \
75 " def test_function(self):\n"
79 def get_coupling_type(line1
, line2
):
81 This function checks if two consecutive lines of log file
82 refer to the same event
84 action_dict1
= parse_line(line1
)
85 action_dict2
= parse_line(line2
)
87 if action_dict1
["keyword"] == "CommandSent" and \
88 action_dict2
["keyword"] == "ModalDialogExecuted":
89 return "COMMAND_MODAL_COUPLE"
91 elif action_dict1
["keyword"] == "CommandSent" and \
92 action_dict2
["keyword"] == "ModelessDialogConstructed":
93 return "COMMAND_MODELESS_COUPLE"
95 elif action_dict1
["keyword"] == "ButtonUIObject" and \
96 action_dict2
["keyword"] == "DialogClosed":
97 return "BUTTON_DIALOGCLOSE_COUPLE"
99 elif "parameters" in action_dict1
and \
100 "KEYCODE" in action_dict1
["parameters"] and \
101 action_dict2
["keyword"] == "CommandSent":
102 return "REDUNDANT_COUPLE"
104 return "NOT_A_COUPLE"
106 def check_app_starting_action(action_dict
):
107 app_starter_button_ids
= \
108 set(["draw_all", "impress_all", "calc_all" , "writer_all", "database_all", "math_all"])
110 if action_dict
["keyword"] == "ButtonUIObject" and action_dict
["Action"] == "CLICK" and \
111 action_dict
["Id"] in app_starter_button_ids
:
115 def get_test_line_from_one_log_line(log_line
):
116 action_dict
= parse_line(log_line
)
118 if action_dict
["keyword"].endswith("UIObject"):
119 parent
= action_dict
["Parent"]
120 if (check_app_starting_action(action_dict
)):
122 "MainDoc = self.ui_test.create_doc_in_start_center(\"" + \
123 action_dict
["Id"][:-4] +"\")\n MainWindow = " + \
124 "self.xUITest.getTopFocusWindow()\n"
128 parent
= "MainWindow"
130 action_dict
["Id"] + " = " + parent
+ ".getChild(\"" + \
131 action_dict
["Id"] + "\")\n " + \
132 action_dict
["Id"] + ".executeAction(\"" + \
133 action_dict
["Action"] + "\""
134 if "parameters" in action_dict
:
135 test_line
+= ", mkPropertyValues(" + \
136 action_dict
["parameters"] + "))\n"
138 test_line
+= ",tuple())\n"
140 elif action_dict
["keyword"] == "CommandSent":
141 if "parameters" not in action_dict
:
142 test_line
+= "self.xUITest.executeCommand(\"" + \
143 action_dict
["Name"] + "\")\n"
146 test_line
+= "self.xUITest.executeCommandWithParameters(\"" + \
147 action_dict
["Name"] + "\", mkPropertyValues(" + action_dict
["parameters"] + \
150 elif action_dict
["keyword"] == "ModalDialogExecuted" or \
151 action_dict
["keyword"] == "ModelessDialogConstructed":
152 test_line
+= action_dict
["Id"] + " = " + "self.xUITest.getTopFocusWindow()\n"
157 def get_test_line_from_two_log_lines(log_line1
,log_line2
):
158 coupling_type
= get_coupling_type(log_line1
, log_line2
)
159 action_dict1
= parse_line(log_line1
)
160 action_dict2
= parse_line(log_line2
)
162 if coupling_type
== "COMMAND_MODAL_COUPLE":
164 "self.ui_test.execute_dialog_through_command(\"" + \
165 action_dict1
["Name"] + "\")\n " + \
166 action_dict2
["Id"] + " = self.xUITest.getTopFocusWindow()\n"
167 elif coupling_type
== "COMMAND_MODELESS_COUPLE":
169 "self.ui_test.execute_modeless_dialog_through_command(\"" + \
170 action_dict1
["Name"] + "\")\n " + \
171 action_dict2
["Id"] + " = self.xUITest.getTopFocusWindow()\n"
172 elif coupling_type
== "BUTTON_DIALOGCLOSE_COUPLE":
174 action_dict1
["Id"] + " = " + action_dict1
["Parent"] + ".getChild(\"" + \
175 action_dict1
["Id"] + "\")\n self.ui_test.close_dialog_through_button(" + \
176 action_dict1
["Id"] + ")\n"
181 log_lines
= get_log_file(args
.input_address
)
182 output_stream
= initiate_test_generation(args
.output_address
)
183 if args
.document
is not None:
184 output_line
= " pathmodule = importlib.import_module(\"uitest.path\")\n" + \
185 " doc_path = pathmodule.get_srcdir_url() + \"" + args
.document
+ "\"\n" + \
186 " MainDoc = self.ui_test.load_file(doc_path)\n" + \
187 " MainWindow = self.xUITest.getTopFocusWindow()\n"
188 output_stream
.write(output_line
)
190 while line_number
< len(log_lines
):
191 if line_number
== len(log_lines
)-1 or \
192 get_coupling_type(log_lines
[line_number
],log_lines
[line_number
+ 1]) == "NOT_A_COUPLE":
193 test_line
= get_test_line_from_one_log_line(log_lines
[line_number
])
194 output_stream
.write(test_line
)
196 elif get_coupling_type(log_lines
[line_number
],log_lines
[line_number
+ 1]) == "REDUNDANT_COUPLE":
199 test_line
= get_test_line_from_two_log_lines(log_lines
[line_number
],log_lines
[line_number
+ 1])
200 output_stream
.write(test_line
)
202 output_stream
.write(" self.ui_test.close_doc()")
203 output_stream
.write("\n\n# vim: set shiftwidth=4 softtabstop=4 expandtab:")
204 output_stream
.close()
206 if __name__
== '__main__':
209 # vim: set shiftwidth=4 softtabstop=4 expandtab: