-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathassertj-swing-basics.html
266 lines (219 loc) · 12.6 KB
/
assertj-swing-basics.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
<!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">Basics of AssertJ Swing</h1>
<p>This section introduces the basics of AssertJ Swing to you. The basics are:</p>
<ul>
<li><a href="#base-test-case">Base test class</a></li>
<li><a href="#configuration">Configuration</a></li>
<li><a href="#verifications">Verifications with AssertJ Swing</a></li>
<li><a href="#verifying-colors-fonts">Special case: font and color verifications</a></li>
</ul>
<h3 class="page-header"><span id="base-test-case"></span>Base test class</h3>
<p>AssertJ Swing provides base test case classes that take care of most of the plumbing involved when writing a
GUI test. This feature is available for TestNG via <code>AssertJSwingTestngTestCase</code> and for JUnit via
<code>AssertJSwingJUnitTestCase</code>.</p>
<p>In general, the provided test classes do the following</p>
<ol>
<li>Install <code>FailOnThreadViolationRepaintManager</code> to check that all access to Swing components
is performed in the EDT</li>
<li>Create a new <code>Robot</code>, using a new component hierarchy</li>
<li>Clean up resources (e.g. release the semaphore that ensures sequential test execution)</li>
</ol>
<div class="panel panel-danger">
<div class="panel-heading">Do not create a new Robot!</div>
<div class="panel-body">When using a base test case, do not create a new <code>Robot</code>. The base test
case creates one for you! If there is more than one <code>Robot</code> in your test, <strong>only the
first one will have access to the screen</strong>, while the rest will block till they get the
<em>screen lock</em>. A <code>Robot</code> can be created manually or indirectly using the constructors
<code>FrameFixture(Frame)</code> or <code>DialogFixture(Dialog)</code>. Please use the overloaded
versions that take a <code>Robot</code> as parameter, passing the already created <code>Robot</code>
(<code>robot()</code>).
</div>
</div>
<p>To compare both scenarios (with and without using the base test cases), here is the example code from
the <a href="assertj-swing-getting-started.html">getting started</a> site. Without base test case:</p>
<pre class="pre-scrollable"><code class="language-java">public class SimpleCopyApplicationTest {
private FrameFixture window;
@BeforeClass
public static void setUpOnce() {
FailOnThreadViolationRepaintManager.install();
}
@Before
public void setUp() {
SimpleCopyApplication frame = GuiActionRunner.execute(() -> new SimpleCopyApplication());
window = new FrameFixture(frame);
window.show(); // shows the frame to test
}
@Test
public void shouldCopyTextInLabelWhenClickingButton() {
window.textBox("textToCopy").enterText("Some random text");
window.button("copyButton").click();
window.label("copiedText").requireText("Some random text");
}
@After
public void tearDown() {
window.cleanUp();
}
}</code></pre>
<p>And the following is with using the base test case:</p>
<pre class="pre-scrollable"><code class="language-java">
public class SimpleCopyApplication_UseBaseTest extends AssertJSwingJUnitTestCase {
private FrameFixture window;
@Override
protected void onSetUp() {
SimpleCopyApplication frame = GuiActionRunner.execute(() -> new SimpleCopyApplication());
// IMPORTANT: note the call to 'robot()'
// we must use the Robot from AssertJSwingJUnitTestCase
window = new FrameFixture(robot(), frame);
window.show(); // shows the frame to test
}
@Test
public void shouldCopyTextInLabelWhenClickingButton() {
window.textBox("textToCopy").enterText("Some random text");
window.button("copyButton").click();
window.label("copiedText").requireText("Some random text");
}
}</code></pre>
<h3 class="page-header"><span id="configuration"></span>Configuration</h3>
<p>AssertJ Swing provides the class <code>org.assertj.swing.core.Settings</code> which holds the configurable
properties. There are properties that can be modified at runtime. These options include scope of component look-ups, delays
and timeouts for different actions, and many more.</p>
<p>For example to set the delay between generated events to 50 ms, you would write</p>
<pre><code class="language-java">Robot robot = BasicRobot.robotWithNewAwtHierarchy();
robot.settings().delayBetweenEvents(50);</code></pre>
<p>Each property has a default value which is provided by AssertJ Swing and can be found in the JavaDoc
but these defaults can be changed. You can add a file called <code>assertj-swing.properties</code> to
the classpath. This file may contain key value pairs that the defaults provided byoverride AssertJ Swing.</p>
<p>If this is not enough one may also override specific properties via Java System Properties.</p>
<p>For example the default delay between generated events might be overridden with a <code>assertj-swing.properties</code>
file containing</p>
<pre><code>org.assertj.swing.delay.between_events = 75</code></pre>
<p>This might be overridden with the following additional parameter at startup</p>
<pre><code>-Dorg.assertj.swing.delay.between_events=80</code></pre>
<p>There are also properties that cannot be modified at runtime. These options can only be modified via a
properties file. Or via a system property.</p>
<p>For more information about the available configuration settings (including defaults and valid values),
see the
<a href="swing/api/org/assertj/swing/core/Settings.html" target="_blank">Settings Javadoc</a>.</p>
<h3 class="page-header"><span id="verifications"></span>Verifications with AssertJ Swing</h3>
<p>AssertJ Swing allows you to check the state of the components via the component fixtures. Each fixture
has different verification methods.</p>
<p>For example the <code>JLabelFixture</code> allows you to verify the state of a <code>JLabel</code> and
therefore provides the following methods</p>
<pre><code class="language-java">label.requireDisabled();
label.requireEnabled();
label.requireFocused();
label.requireText("expected");
label.requireToolTip("expected tooltip");
label.requireNotVisible();
label.requireVisible();</code></pre>
<p>As you can see, the verification methods usually start with <code>require</code>. Therefore your IDE
should support you finding all possible verification methods.</p>
<h3 class="page-header"><span id="verifying-colors-fonts"></span>Font and color verifications</h3>
<p>AssertJ Swing supports <em>just functional</em> GUI testing and therefore testing the <em>behavior</em>
of a system. We don't provide a pixel verification tool.</p>
<p>Regardless whether wise men think font and color verification is part of <em>functional</em> GUI
testing we allow you to do it! Whereas in general colors and fonts might not be part of the
functionality, we can think of cases where the color and font are part of the functionality.</p>
<p>All component fixtures allow you to verify their color and font via a <code>ColorFixture</code> or a
<code>FontFixture</code>. These verifications are accessed via the <code>foreground()</code>,
<code>background()</code> and <code>font()</code> methods of a component fixture. Let's assume we want
to check that a label shows black font on a blue background being bold:</p>
<pre><code class="language-java">label.background().requireEqualTo("0000FF");
label.foreground().requireEqualTo(Color.BLACK);
label.font().requireBold();</code></pre>
</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>