-
Notifications
You must be signed in to change notification settings - Fork 30
/
assertj-swing-troubleshooting-lookups.html
181 lines (151 loc) · 9 KB
/
assertj-swing-troubleshooting-lookups.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
<!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">Troubleshooting component lookups</h1>
<p>AssertJ Swing throws a <code>ComponentLookupException</code> when a component lookup fails. Failures
are due to one of the following reasons</p>
<ul>
<li>a component that matches the specified search criteria could not be found</li>
<li>there is more than one component that matches the specified search criteria</li>
</ul>
<h2>No matching component could be found</h2>
<p>When a matching component could not be found, regardless of the lookup type, the thrown
<code>ComponentLookupException</code> includes a formatted component hierarchy that can help us determine why
the component lookup failed.</p>
<p>For our example, we are going to use a frame that contains a <code>JButton</code> with name
<code>click</code>. The following code listing shows a mistake we made when looking up that button in our
test. We accidentally used the name <code>ok</code> instead of <code>click</code>:</p>
<pre><code class="language-java">FrameFixture frame = new FrameFixture(new TestFrame());
frame.show();
frame.button("ok").click();</code></pre>
<p>When we run our test, it will obviously fail, because the frame does not have any button with name
<code>ok</code>. To help us diagnose the problem, the thrown <code>ComponentLookupException</code> displays
a nicely formatted component hierarchy:</p>
<pre><code> org.assertj.swing.exception.ComponentLookupException: Unable to find component using matcher org.assertj.swing.core.NameMatcher[name='ok', requireShowing=false].
Component hierarchy:
org.assertj.swing.test.TestFrame[name='testFrame', title='Test', enabled=true, showing=true]
javax.swing.JRootPane[]
javax.swing.JPanel[name='null.glassPane']
javax.swing.JLayeredPane[]
javax.swing.JPanel[name='null.contentPane']
javax.swing.JButton[name='click', text='Click Me', enabled=true]
at org.assertj.swing.core.BasicComponentFinder.componentNotFound(BasicComponentFinder.java:81)</code></pre>
<p>The displayed component hierarchy, unlike the default <code>toString()</code> method in Swing components,
does not contain any information related to how the components look (e.g. component sizes, colors,
layouts, etc.). That information is pretty much useless, since functional testing focuses on the behavior
of the GUI. Instead, AssertJ Swing uses custom formatters to show properties that can help us solve
problems in our functional tests. For more details about custom component formatters (including how to
override the provided ones with your own), please visit the <a
href="assertj-swing-advanced.html#formatters">Component Formatters</a> section.</p>
<h2>More than one matching component could be found</h2>
<p>There are cases that more than one GUI component matches the search criteria used in a lookup. For our
example, we are going to use a frame that contains two instances of <code>JButton</code> with names
<code>first</code> and <code>second</code>. In the following code listing we are looking up a
<code>JButton</code> by type:</p>
<pre><code class="language-java">FrameFixture frame = new FrameFixture(new TestFrame());
frame.show();
frame.button().click();</code></pre>
<p>This test will fail, because the frame has two components of type <code>JButton</code>. To help us diagnose
the problem, the thrown <code>ComponentLookupException</code> displays all the matching components found:</p>
<pre><code> org.assertj.swing.exception.ComponentLookupException: Found more than one component using matcher org.assertj.swing.core.TypeMatcher[type=javax.swing.JButton, requireShowing=false].
Found:
javax.swing.JButton[name='first', text='First Button', enabled=true]
javax.swing.JButton[name='second', text='Second Button', enabled=true]
at org.assertj.swing.core.BasicComponentFinder.multipleComponentsFound(BasicComponentFinder.java:102)
</code></pre>
<p>The thrown <code>ComponentLookupException</code> uses component formatters to display information that can
help us solve problems in our functional tests. Just like when finding no matching component.</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>