-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathassertj-swing-troubleshooting-screenshots.html
242 lines (201 loc) · 12.1 KB
/
assertj-swing-troubleshooting-screenshots.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
<!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">Screenshots of test failures</h1>
<p>On some occasions a functional GUI test will run perfectly from within the IDE but will break when executed
in a batch with other tests (such as when you are using Ant). This is because functional GUI tests are
vulnerable to certain environment-related events, and AssertJ Swing is no exception.</p>
<p>It occasionally may happen that anti-virus software runs a scheduled scan while a GUI is under test. If the
anti-virus software pops up a dialog in front of the GUI, the AssertJ Swing robot will not be able to
access the GUI and will time out eventually, making the test fail. In this case, the failure is not related
to a programming error; it is just a case of bad timing.</p>
<p>AssertJ Swing is capable of taking a screenshot of the desktop when a GUI test fails, regardless of
the test framework you use (TestNG or JUnit). You then can use this screenshot to analyse the cause of a
failed test and discover whether it is programmatic or environmental.</p>
<p>There are many ways to take a screenshot of the desktop when a GUI test fails. Regardless of how
AssertJ Swing takes a screenshot, it needs to know which tests should be considered <em>GUI
tests</em>. In order to do so, we only need to add the annotation <code>@GUITest</code>. This annotation
can be placed at class or method level, and it is inherited by subclasses of annotated classes.</p>
<p>Once our GUI tests have the <code>@GUITest</code> annotation, we can take a screenshot of the desktop when
a GUI test fails. See the following sections for the how-to.</p>
<h2>How to take screenshots manually</h2>
<p>Taking a screenshot of the desktop is quite simple. The class <code>ScreenshotTaker</code> provides two
methods:</p>
<ul>
<li><code>takeDesktopScreenshot()</code> takes a screenshot of the desktop and returns it as a
<code>BufferedImage</code>.</li>
<li><code>saveDesktopAsPng(String)</code> takes a screenshot of the desktop and saves it as a PNG image
using the file path passed as argument.</li>
</ul>
<h3>Example</h3>
<p>This example takes a screenshot of the desktop when a test fails and saves the image as
<code>myTest.png</code>:</p>
<pre class="pre-scrollable"><code class="language-java">// import static org.assertj.core.util.Files.currentFolder;
// import static java.io.File.separator
ScreenshotTaker screenshotTaker = new ScreenshotTaker();
String currentFolderPath = currentFolder().getCanonicalPath();
File imageFolder = new File(currentFolderPath + separator + "failed-tests");
String imageFolderPath = imageFolder.getCanonicalPath() + separator;
try {
// perform your test
} catch (Exception e) {
// test failed
screenshotTaker.saveDesktopAsPng(imageFolderPath + "myTest.png");
throw e;
}</code></pre>
<h2>How to take screenshots of test failures with JUnit</h2>
<p>AssertJ Swing can take a screenshot of the desktop when a JUnit GUI test fails, either when running
tests using Ant or inside an IDE (e.g. Eclipse). To take screenshots of failed GUI tests you have to
add the <code>@GUITest</code> annotation and make sure that you've included the
<code>assertj-swing-junit</code> artifact.</p>
<h3>Running with the JUnit Runner</h3>
<p>For running GUI tests in an IDE, AssertJ Swing provides a custom JUnit runner,
<code>GUITestRunner</code>, which takes screenshots of failed GUI tests. To use it, just annotate your test
class with <code>@RunWith(GUITestRunner.class)</code>. Screenshots of failed tests will be saved in the
directory <code>failed-gui-tests</code> (relative to the directory where tests are executed).</p>
<h3>Running with Ant</h3>
<p>In order to take screenshots of failed GUI tests with Ant please follow these steps</p>
<ul>
<li>Add a definition of the Ant task <code>assertjreport</code></li>
<li>Use the formatter <code>ScreenshotOnFailureResultFormatter</code> inside the junit Ant task</li>
<li>Use the Ant task <code>assertjreport</code> instead of <code>junitreport</code>, and specify in its
classpath where the <code>assertj-swing-junit</code> jar file is.</li>
</ul>
<h4>Example</h4>
<pre class="pre-scrollable"><code class="language-xml"><target name="test" depends="compile">
<taskdef resource="assertjjunittasks" classpathref="lib.classpath" />
<junit forkmode="perBatch" printsummary="yes" haltonfailure="no" haltonerror="no">
<classpath refid="lib.classpath" />
<classpath location="${target.test.classes.dir}" />
<classpath location="${target.classes.dir}" />
<formatter classname="org.fest.swing.junit.ant.ScreenshotOnFailureResultFormatter" extension=".xml" />
<batchtest fork="yes" todir="${target.junit.results.dir}">
<fileset dir="${target.test.classes.dir}" includes="**/*Test*.class" />
</batchtest>
</junit>
<festreport todir="${target.junit.report.dir}">
<classpath refid="lib.classpath" />
<fileset dir="${target.junit.results.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${target.junit.report.dir}/html" />
</festreport>
</target></code></pre>
<p><code>assertjreport</code> works exactly as <code>junitreport</code> but additionally produces a link as seen
in this screenshot:</p>
<img src="images/swing-screenshot-junit-ant.jpg" alt="Screenshot of the test report" />
<h2>How to take screenshots of test failures with TestNG</h2>
<p>AssertJ Swing can take a screenshot of the desktop when a TestNG GUI test fails. Because of TestNG's
excellent support for extensibility, it is quite easy to setup AssertJ Swing with TestNG. Just make
sure that you've included the <code>assertj-swing-testng</code> artifact.</p>
<p>The provided listener, <code>ScreenshotOnFailureListener</code>, is capable of taking a screenshot of the
desktop when a test fails and saves it in TestNG's default output directory. Due to TestNG's flexible
architecture, this listener can be called either from Ant, TestNG plug-in for Eclipse, command line, etc.</p>
<p>To take screenshots of failed GUI tests, regardless of how they are executed, just add the
<code>@GUITest</code> annotation.</p>
<h3>Running with Ant</h3>
<p>This example shows how to register the <code>ScreenshotOnFailureListener</code> with TestNG's ant task.</p>
<pre><code class="language-xml"><testng listeners="org.assertj.swing.testng.ScreenshotOnFailureListener"
outputDir="${target.test.results.dir}" haltOnFailure="true"
verbose="2">
<classfileset dir="${target.test.classes.dir}" includes="**/*Test.class" />
<classpath location="${target.test.classes.dir}" />
<classpath location="${target.classes.dir}" />
<classpath refid="test.classpath" />
</testng></code></pre>
<p>A screenshot of a failed TestNG GUI test</p>
<img src="images/swing-screenshot-testng-ant.png" alt="Screenshot of a failed TestNG GUI test" />
</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>