Robot Framework Integrated Development Environment (RIDE)
ui.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import sys
17 import wx
18 
19 from .process import Process
20 from ..widgets import Label, Font
21 
22 
23 class Runner(wx.EvtHandler):
24 
25  def __init__(self, config, notebook):
26  wx.EvtHandler.__init__(self)
27  self.Bind(wx.EVT_TIMER, self.OnTimerOnTimer)
28  self.namename = config.name
29  self._timer_timer = wx.Timer(self)
30  self._config_config = config
31  self._window_window = self._get_output_window_get_output_window(notebook)
32 
33  def _get_output_window(self, notebook):
34  return _OutputWindow(notebook, self)
35 
36  def run(self):
37  self._process_process = Process(self._config_config.command)
38  self._process_process.start()
39  self._timer_timer.Start(500)
40 
41  def OnTimer(self, event=None):
42  finished = self._process_process.is_finished()
43  self._window_window.update_output(self._process_process.get_output(), finished)
44  if finished:
45  self._timer_timer.Stop()
46 
47  def stop(self):
48  try:
49  self._process_process.stop()
50  except Exception as err:
51  wx.MessageBox(str(err), style=wx.ICON_ERROR)
52 
53 
54 class _OutputWindow(wx.ScrolledWindow):
55 
56  def __init__(self, notebook, runner):
57  wx.ScrolledWindow.__init__(self, notebook)
58  self._create_ui_create_ui()
59  self._add_to_notebook_add_to_notebook(notebook, runner.name)
60  self._runner_runner = runner
61 
62  def _create_ui(self):
63  sizer = wx.BoxSizer(wx.VERTICAL)
64  sizer.Add(self._create_state_button_create_state_button())
65  sizer.Add(self._create_output_create_output())
66  self.SetSizer(sizer)
67  self.SetScrollRate(20, 20)
68 
70  if sys.version_info[:2] >= (2,6):
71  self._state_button_state_button = _StopAndRunAgainButton(self)
72  else:
73  self._state_button_state_button = _RunAgainButton(self)
74  return self._state_button_state_button
75 
76  def _create_output(self):
77  self._output_output = _OutputDisplay(self)
78  return self._output_output
79 
80  def _add_to_notebook(self, notebook, name):
81  notebook.add_tab(self, '%s (running)' % name, allow_closing=False)
82  notebook.show_tab(self)
83 
84  def update_output(self, output, finished=False):
85  if output:
86  self._output_output.update(output)
87  self.SetVirtualSize(self._output_output.Size)
88  if finished:
89  self._rename_tab_rename_tab('%s (finished)' % self._runner_runner.name)
90  self.Parent.allow_closing(self)
91  self._state_button_state_button.enable_run_again()
92 
93  def OnStop(self):
94  self._runner_runner.stop()
95 
96  def OnRunAgain(self):
97  self._output_output.clear()
98  self._rename_tab_rename_tab('%s (running)' % self._runner_runner.name)
99  self.Parent.disallow_closing(self)
100  self._state_button_state_button.reset()
101  self._runner_runner.run()
102 
103  def _rename_tab(self, name):
104  self.Parent.rename_tab(self, name)
105 
106 
107 class _OutputDisplay(Label):
108 
109  def __init__(self, parent):
110  Label.__init__(self, parent)
111  self.SetFont(Font().fixed)
112 
113  def update(self, addition):
114  self.SetLabel(self.LabelText + addition.decode('UTF-8', 'ignore'))
115 
116  def clear(self):
117  self.SetLabel('')
118 
119 
120 class _StopAndRunAgainButton(wx.Button):
121 
122  def __init__(self, parent):
123  wx.Button.__init__(self, parent, label='Stop')
124  self.Bind(wx.EVT_BUTTON, self.OnClickOnClick, self)
125 
126  def OnClick(self, event):
127  self.Enable(False)
128  getattr(self.Parent, 'On' + self.LabelText.replace(' ', ''))()
129 
130  def enable_run_again(self):
131  self.Enable()
132  self.SetLabel('Run Again')
133 
134  def reset(self):
135  self.Enable()
136  self.SetLabel('Stop')
137 
138 
139 class _RunAgainButton(wx.Button):
140 
141  def __init__(self, parent):
142  wx.Button.__init__(self, parent, label='Run Again')
143  self.Bind(wx.EVT_BUTTON, self.OnClickOnClick, self)
144  self.Enable(False)
145 
146  def OnClick(self, event):
147  self.Parent.OnRunAgain()
148 
149  def enable_run_again(self):
150  self.Enable()
151 
152  def reset(self):
153  self.Enable(False)
154 
def OnTimer(self, event=None)
Definition: ui.py:41
def __init__(self, config, notebook)
Definition: ui.py:25
def stop(self)
Definition: ui.py:47
def run(self)
Definition: ui.py:36
def _get_output_window(self, notebook)
Definition: ui.py:33
def __init__(self, parent)
Definition: ui.py:109
def update(self, addition)
Definition: ui.py:113
def OnRunAgain(self)
Definition: ui.py:96
def update_output(self, output, finished=False)
Definition: ui.py:84
def _create_state_button(self)
Definition: ui.py:69
def _rename_tab(self, name)
Definition: ui.py:103
def _create_output(self)
Definition: ui.py:76
def __init__(self, notebook, runner)
Definition: ui.py:56
def _add_to_notebook(self, notebook, name)
Definition: ui.py:80
def _create_ui(self)
Definition: ui.py:62
def OnClick(self, event)
Definition: ui.py:146
def enable_run_again(self)
Definition: ui.py:149
def __init__(self, parent)
Definition: ui.py:141
def __init__(self, parent)
Definition: ui.py:122
def run(*tests, **options)
Programmatic entry point for running tests.
Definition: run.py:546