-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathassertj-swing-input.html
312 lines (262 loc) · 17.5 KB
/
assertj-swing-input.html
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="AssertJ site">
<meta name="author" content="Joel Costigliola">
<title>AssertJ / Fluent assertions for java</title>
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Inconsolata|Source+Code+Pro|Open+Sans|Ubuntu|Varela+Round|Karla">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet">
<script src="highlight/highlight.pack.js"></script>
<link rel="stylesheet" href="highlight/styles/railscasts.css">
<script>hljs.initHighlightingOnLoad();</script>
<link href="css/assertj.min.css" rel="stylesheet">
<link rel="shortcut icon" href="favicon.png" />
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- You'll want to use a responsive image option so this logo looks good on devices - I recommend using something like retina.js (do a quick Google search for it and you'll find it) -->
<a class="navbar-brand" href="index.html">AssertJ</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="assertj-core-quick-start.html">Quick start</a></li>
<li><a href="assertj-news.html">News</a></li>
<li><a href="assertj-core.html">Core</a></li>
<li><a href="assertj-assertions-generator.html">Assertions generator</a></li>
<li><a href="assertj-guava.html">Guava</a></li>
<li><a href="assertj-joda-time.html">Joda-Time</a></li>
<li><a href="assertj-db.html">DB</a></li>
<li><a href="assertj-neo4j.html">Neo4j</a></li>
<li><a href="assertj-swing.html">Swing</a></li>
<li><a href="assertj-help.html">Help</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="row" >
<div class="col-md-2 assertj-sidebar-menu">
<div class="bs-sidebar hidden-print affix-top" role="complementary">
<ul class="bs-sidenav nav ">
<li class="sidenav-header">About</li>
<li><a href="assertj-swing.html">Overview</a></li>
<li><a href="assertj-swing-quick-start.html">Quick start</a></li>
<li><a href="assertj-swing-news.html">News & releases</a></li>
<li><a href="swing/api/index.html">Javadoc</a></li>
<li><a href="assertj-swing.html#code">Code & issues <i class="fa fa-github"></i></a></li>
<li><a href="assertj-swing.html#contributing">Contributing</a></li>
<li class="sidenav-header">Working with it</li>
<li><a href="assertj-swing-getting-started.html">Getting started</a></li>
<li><a href="assertj-swing-basics.html">Basics</a></li>
<li><a href="assertj-swing-edt.html">EDT</a></li>
<li><a href="assertj-swing-lookup.html">Component lookup</a></li>
<li><a href="assertj-swing-launch.html">Launching</a></li>
<li><a href="assertj-swing-input.html">Input simulation</a></li>
<li><a href="assertj-swing-running.html">Running tests</a></li>
<li><a href="assertj-swing-troubleshooting.html">Troubleshooting</a></li>
<li><a href="assertj-swing-advanced.html">Advanced features</a></li>
<li class="sidenav-header">Migrating</li>
<li><a href="assertj-swing-migrating.html">From Fest</a></li>
</ul>
</div>
</div>
<div class="col-lg-10 col-md-10 col-sm-10 text-left" >
<h1 class="page-header">Input Simulation</h1>
<p>AssertJ Swing uses an <a href="http://java.sun.com/javase/6/docs/api/java/awt/Robot.html"
target="_blank">AWT Robot</a> to generate user input. The AWT Robot is
capable of generating native events, providing the most accurate simulation of an actual user. The
<code>Robot</code> can move the mouse pointer and press keys just like a user would.</p>
<h2>Simulating keyboard input</h2>
<p>All AssertJ Swing fixtures can simulate user input using a keyboard. This simulation is not limited
to send events to <code>KeyListeners</code>. AssertJ Swing provides true keyboard input, at the
operating system level.</p>
<h3>Pressing and releasing one or more keys</h3>
<p>The method <code>pressAndReleaseKeys(int...)</code> can simulate a user pressing and releasing one or more
keys. If more than one key code is passed as parameter, this method will simulate pressing a key, releasing
it and then will process the next key code. Key codes are integers specified in
<code>java.awt.event.KeyEvent</code>.</p>
<p>The following example shows how to simulate a user pressing the keys <code>A</code>, <code>S</code>,
<code>D</code>, <code>F</code> while having focus on a text field with name <code>username</code> which is
similar to typing <code>asdf</code> on that text field.</p>
<pre><code class="language-java">// import static java.awt.event.KeyEvent.*;
dialog.textBox("username").pressAndReleaseKeys(VK_A, VK_S, VK_D, VK_F);</code></pre>
<p>Simulating a user pressing the key <kbd>F1</kbd> while having focus on a button with name
<code>configure</code> would be</p>
<pre><code class="language-java">// import static java.awt.event.KeyEvent.*;
dialog.button("configure").pressAndReleaseKeys(VK_F1);</code></pre>
<h3>Pressing a key, doing something else, and releasing the key</h3>
<p>The methods <code>pressKey(int)</code> and <code>releaseKey(int)</code> can simulate a user pressing and
releasing a key. Although it may sound similar to what the method <code>pressAndReleaseKeys(int...)</code>
does, it is a bit different. The <code>pressAndReleaseKeys(int...)</code> simulates a user pressing a key
and immediately releasing the same key. On the other hand, <code>pressKey(int)</code> and
<code>releaseKey(int)</code> are two separate actions. They can be helpful when you want to simulate a case
of <em>press a key, do something else, and then release the key</em>.</p>
<p>An example will illustrate this and give a better idea what these methods are good for. In the following
code we're simulating clicking the left mouse button while having the <kbd>Ctrl</kbd> key pressed:</p>
<pre><code class="language-java">// import static java.awt.event.KeyEvent.*;
dialog.list("employees").pressKey(VK_CONTROL)
.click()
.releaseKey(VK_CONTROL);</code></pre>
<p>You can even simplify this by using <code>pressKeyWhileRunning(int, Runnable)</code> which takes a
<code>Runnable</code> to run between pressing and releasing keys:</p>
<pre><code class="language-java">// import static java.awt.event.KeyEvent.*;
dialog.list("employees").pressKeyWhileRunning(VK_CONTROL, dialog.list("employees")::click);</code></pre>
<h3>Typing text</h3>
<p>Often we just want to enter some text into a text field. Using <code>pressAndReleaseKeys(int...)</code>
is unhandy for this purpose. The text component fixtures have a method <code>enterText(String)</code> to
simplify entering text. The example from above, typing <code>asdf</code> into a text field then looks
like</p>
<pre><code class="language-java">dialog.textBox("username").enterText("asdf");</code></pre>
<h2>Simulating mouse clicks</h2>
<p>All AssertJ Swing fixtures can simulate user input using a mouse. This simulation is not limited to
send events to implementations of <code>MouseListener</code>. AssertJ Swing provides true mouse
input, at the operating system level, too. For example, simulating a user moving the mouse will result in
the mouse pointer moving to a specified location.</p>
<h3>Simple click</h3>
<p>The method <code>click()</code> can simulate a user clicking a component once, using the left mouse
button. Let's see how to simulate a user clicking the button with name <code>connect</code>:</p>
<pre><code class="language-java">frame.button("connect").click();</code></pre>
<p>The following are alternative (and more verbose) ways to click a component once, using the left mouse
button:</p>
<pre><code class="language-java">// import static org.assertj.swing.core.MouseButton.LEFT_BUTTON;
frame.button("connect").click(LEFT_BUTTON);
// import static org.assertj.swing.core.MouseClickInfo.leftButton;
frame.button("connect").click(leftButton().times(1));</code></pre>
<h3>Double click and right click</h3>
<p>In addition to <code>click()</code> there are the methods <code>doubleClick()</code> (for
double-clicking a component, using the left mouse button) and <code>rightClick()</code> (for clicking a
component once, using the right mouse button).</p>
<h3>Click using specific button, many times</h3>
<p>Since there aren't only left and right mouse buttons and you may want to click more than twice,
AssertJ Swing has the method <code>click(MouseClickInfo)</code> to allow such a functionality.
With <code>MouseClickInfo</code> you can specify the mouse button and the amount of clicks.</p>
<p>How does that look like? We already used this method above. But let's see how you would make
AssertJ Swing clicking three times the radio button with name <code>monthly</code> using the left
mouse button:</p>
<pre><code class="language-java">// import static org.fest.swing.core.MouseClickInfo.leftButton;
frame.radioButton("monthly").click(leftButton().times(3));</code></pre>
<h2>Simulating Drag 'n Drop</h2>
<p>AssertJ Swing supports simulation of drag 'n drop via the class
<a href="swing/api/org/assertj/swing/core/ComponentDragAndDrop.html" target="_blank">ComponentDragAndDrop</a>.
This class supports drag 'n drop operations on screen coordinates and swing components. In addition, the
following fixtures provide component-specific implementations of drag 'n drop, to simulate a user dragging
'n dropping on <code>JList</code>, <code>JTable</code> and <code>JTree</code>, respectively:</p>
<ol>
<li><code>JListFixture</code></li>
<li><code>JTableFixture</code></li>
<li><code>JTreeFixture</code></li>
</ol>
<div class="panel panel-info">
<div class="panel-heading">Note</div>
<div class="panel-body">
AssertJ Swing's drag 'n drop will effectively simulate a user pressing the left mouse button,
moving the mouse to a particular location and releasing the mouse. However, it is up to you to
implement drag 'n drop in your Swing components. To learn more about drag 'n drop, please visit
<a href="http://java.sun.com/docs/books/tutorial/uiswing/dnd/intro.html" target="_blank">Sun's Swing
Tutorial</a>.
</div>
</div>
<h3>Examples</h3>
<h4>JList</h4>
<p>The following example shows dragging the element <em>Swing</em> from the list <code>source</code> and
dropping it in the list <code>destination</code> on the element <em>AWT</em>. Which might insert
<em>Swing</em> before or after <em>AWT</em>, depending on the implementation of the
<code>TransferHandler</code> used.</p>
<pre><code class="language-java">dialog.list("source").drag("Swing");
dialog.list("destination").drop("AWT");</code></pre>
<h4>JTable</h4>
<p>The following example shows dragging the content of the cell at row 3, column 0 from the table
<code>source</code> and dropping it on the table <code>destination</code> at row 1, column 0.</p>
<pre><code class="language-java">// import static org.assertj.swing.fixture.TableCell.TableCellBuilder.row;
dialog.table("source").drag(row(3).column(0));
dialog.table("destination").drop(row(1).column(0));</code></pre>
<h4>JTree</h4>
<p>The following example shows dragging the node <em>branch1.1</em> (in the path <em>root/branch1</em>) from
the tree <code>source</code> and dropping it in the tree <code>destination</code> on the node <em>root</em>.
</p>
<pre><code class="language-java">dialog.tree("source").drag(path("root", "branch1", "branch1.1"));
dialog.tree("destination").drop(path("root"));</code></pre>
<h2>Simulating editing table cells</h2>
<p>AssertJ Swing provides support for editing <code>JTable</code> cells as if a user was doing it. Out
of the box <code>JCheckBox</code>, <code>JComboBox</code> and <code>JTextField</code> are supported as
custom cell editors:</p>
<img src="images/swing-input-table.png" alt="Screenshot of the sample table to test" />
<p>There are many ways to simulate a user editing a <code>JTable</code> cell:</p>
<h3><code>JTableFixture</code></h3>
<p>A <code>JTableFixture</code> provides the method <code>enterValue(TableCell, String)</code> which simulates
a user entering the given value in the given cell, regardless of the underlying cell editor. Even though
the value to enter is always a String, <code>JTableFixture</code> can figure out how to use the underlying
cell editor.</p>
<p>The following code illustrates how to select the value <em>Pool</em> from the <code>JComboBox</code> in
row 0, column 2 (see picture above).</p>
<pre><code class="language-java">JTableFixture table = dialog.table("data");
// import static org.assertj.swing.data.TableCell.row;
table.enterValue(row(0).column(2), "Pool");</code></pre>
<p>Here we are selecting the <code>JCheckBox</code> in row 1, column 4:</p>
<pre><code class="language-java">JTableFixture table = dialog.table("data");
// import static org.assertj.swing.data.TableCell.row;
table.enterValue(row(1).column(4), "true");</code></pre>
<h3><code>JTableCellFixture</code></h3>
<p>A <code>JTableCellFixture</code> provides the method <code>enterValue(String)</code>, which can also
simulate a user editing a table cell, just like <code>JTableFixture</code>. The following code illustrates
how to select the value <em>Pool</em> from the <code>JComboBox</code> in row 0, column 2:</p>
<pre><code class="language-java">JTableFixture table = dialog.table("data");
// import static org.assertj.swing.data.TableCell.row;
JTableCellFixture cell = table.cell(row(0).column(2));
cell.enterValue("Pool");</code></pre>
<p>Here we are selecting the <code>JCheckBox</code> in row 1, column 4:</p>
<pre><code class="language-java">JTableFixture table = dialog.table("data");
// import static org.assertj.swing.data.TableCell.row;
JTableCellFixture cell = table.cell(row(1).column(4));
cell.enterValue("true");</code></pre>
<h3>Having more control of the cell editor</h3>
<p>To have more control of the underlying cell editor, it is possible to wrap it with an implementation of
<code>ComponentFixture</code>. The <code>JTableCellFixture</code> provides the method <code>editor()</code>,
which returns the <code>Component</code> used as editor of a cell. <code>JTableCellFixture</code> also
provides the methods <code>startEditing</code>, <code>stopEditing</code> and <code>cancelEditing</code>.
The following example illustrates how to use them:</p>
<pre><code class="language-java">// import static org.assertj.swing.data.TableCell.row;
TableCellFixture cell = table.cell(row(6).column(8));
Component editor = cell.editor();
// assume editor is a JTextField
JTextComponentFixture textBoxEditor = new JTextComponentFixture(robot, (JTextField)editor);
cell.startEditing();
textBoxEditor.enterText("Hello");
cell.stopEditing();</code></pre>
<h3>Custom cell editors</h3>
<p>AssertJ Swing also supports custom cell editors. For more information, please read
<a href="assertj-swing-advanced.html#custom-editors">Custom Cell Editors</a>.</p>
</div>
</div>
</div>
<br>
<!--
<div class="container">
<footer>
<div class="row">
<div class="col-lg-12">
<p>AssertJ - Licensed under the Apache License, Version 2.0.</p>
</div>
</div>
</footer>
</div>
-->
<script src="js/jquery-1.10.2.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/modern-business.js"></script>
<script src="js/assertj.js"></script>
</body>
</html>