1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.jpetstore;
17
18 import static com.codeborne.selenide.Browsers.CHROME;
19 import static com.codeborne.selenide.CollectionCondition.size;
20 import static com.codeborne.selenide.Condition.empty;
21 import static com.codeborne.selenide.Condition.text;
22 import static com.codeborne.selenide.Condition.value;
23 import static com.codeborne.selenide.Configuration.baseUrl;
24 import static com.codeborne.selenide.Configuration.browser;
25 import static com.codeborne.selenide.Configuration.headless;
26 import static com.codeborne.selenide.Configuration.timeout;
27 import static com.codeborne.selenide.Selenide.$;
28 import static com.codeborne.selenide.Selenide.$$;
29 import static com.codeborne.selenide.Selenide.open;
30 import static com.codeborne.selenide.Selenide.title;
31 import static org.assertj.core.api.Assertions.assertThat;
32
33 import com.codeborne.selenide.SelenideElement;
34 import com.codeborne.selenide.junit5.ScreenShooterExtension;
35
36 import java.util.concurrent.TimeUnit;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
39
40 import org.junit.jupiter.api.AfterEach;
41 import org.junit.jupiter.api.BeforeAll;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.openqa.selenium.By;
45
46
47
48
49
50
51 @ExtendWith(ScreenShooterExtension.class)
52 class ScreenTransitionIT {
53
54 @BeforeAll
55 static void setupSelenide() {
56 browser = CHROME;
57 headless = true;
58 timeout = TimeUnit.SECONDS.toMillis(10);
59 baseUrl = "http://localhost:8080/jpetstore";
60 }
61
62 @AfterEach
63 void logout() {
64 SelenideElement element = $(By.linkText("Sign Out"));
65 if (element.exists()) {
66 element.click();
67 }
68 }
69
70 @Test
71 void testOrder() {
72
73
74 open("/");
75 assertThat(title()).isEqualTo("JPetStore Demo");
76 $(By.cssSelector("#Content h2")).shouldBe(text("Welcome to JPetStore 6"));
77
78
79 $(By.linkText("Enter the Store")).click();
80 $(By.id("WelcomeContent")).shouldBe(empty);
81
82
83 $(By.linkText("Sign In")).click();
84 $(By.name("username")).setValue("j2ee");
85 $(By.name("password")).setValue("j2ee");
86 $(By.name("signon")).click();
87 $(By.id("WelcomeContent")).shouldBe(text("Welcome ABC!"));
88
89
90 $(By.name("keyword")).setValue("fish");
91 $(By.name("searchProducts")).click();
92 $$(By.cssSelector("#Catalog table tr")).shouldHave(size(4));
93
94
95 $(By.linkText("Fresh Water fish from China")).click();
96 $(By.cssSelector("#Catalog h2")).shouldBe(text("Goldfish"));
97
98
99 $(By.linkText("Add to Cart")).click();
100 $(By.cssSelector("#Catalog h2")).shouldBe(text("Shopping Cart"));
101
102
103 $(By.cssSelector("#QuickLinks a:nth-of-type(5)")).click();
104 $(By.linkText("AV-CB-01")).click();
105 $(By.linkText("EST-18")).click();
106 $(By.linkText("Add to Cart")).click();
107 $(By.cssSelector("#Cart tr:nth-of-type(4) td")).shouldBe(text("Sub Total: $199.00"));
108
109
110 $(By.name("EST-20")).setValue("10");
111 $(By.name("updateCartQuantities")).click();
112 $(By.cssSelector("#Catalog tr td:nth-of-type(7)")).shouldBe(text("$55.00"));
113 $(By.cssSelector("#Cart tr:nth-of-type(4) td")).shouldBe(text("Sub Total: $248.50"));
114
115
116 $(By.cssSelector("#Cart tr:nth-of-type(3) td:nth-of-type(8) a")).click();
117 $(By.cssSelector("#Cart tr:nth-of-type(3) td")).shouldBe(text("Sub Total: $55.00"));
118
119
120 $(By.linkText("Proceed to Checkout")).click();
121 assertThat(title()).isEqualTo("JPetStore Demo");
122
123
124 $(By.name("shippingAddressRequired")).click();
125 $(By.name("newOrder")).click();
126 $(By.cssSelector("#Catalog tr th")).shouldBe(text("Shipping Address"));
127 $(By.name("order.shipAddress2")).setValue("MS UCUP02-207");
128
129
130 $(By.name("newOrder")).click();
131 $(By.cssSelector("#Catalog")).shouldBe(text("Please confirm the information below and then press continue..."));
132
133
134 $(By.linkText("Confirm")).click();
135 $(By.cssSelector(".messages li")).shouldBe(text("Thank you, your order has been submitted."));
136 String orderId = extractOrderId($(By.cssSelector("#Catalog table tr")).text());
137
138
139 $(By.linkText("My Account")).click();
140 $(By.cssSelector("#Catalog h3")).shouldBe(text("User Information"));
141
142
143 $(By.linkText("My Orders")).click();
144 $(By.cssSelector("#Content h2")).shouldBe(text("My Orders"));
145
146
147 $(By.linkText(orderId)).click();
148 assertThat(extractOrderId($(By.cssSelector("#Catalog table tr")).text())).isEqualTo(orderId);
149
150
151 $(By.linkText("Sign Out")).click();
152 $(By.id("WelcomeContent")).shouldBe(empty);
153
154 }
155
156 @Test
157 void testUpdateProfile() {
158
159 open("/");
160 assertThat(title()).isEqualTo("JPetStore Demo");
161
162
163 $(By.linkText("Enter the Store")).click();
164 $(By.id("WelcomeContent")).shouldBe(empty);
165
166
167 $(By.linkText("Sign In")).click();
168 $(By.name("username")).setValue("j2ee");
169 $(By.name("password")).setValue("j2ee");
170 $(By.name("signon")).click();
171 $(By.id("WelcomeContent")).shouldBe(text("Welcome ABC!"));
172
173
174 $(By.linkText("My Account")).click();
175 $(By.cssSelector("#Catalog h3")).shouldBe(text("User Information"));
176 $$(By.cssSelector("#Catalog table td")).get(1).shouldBe(text("j2ee"));
177
178
179 $(By.name("account.phone")).setValue("555-555-5556");
180 $(By.name("editAccount")).click();
181 $(By.cssSelector("#Catalog h3")).shouldBe(text("User Information"));
182 $$(By.cssSelector("#Catalog table td")).get(1).shouldBe(text("j2ee"));
183 $(By.name("account.phone")).shouldBe(value("555-555-5556"));
184 }
185
186 @Test
187 void testRegistrationUser() {
188
189 open("/");
190 assertThat(title()).isEqualTo("JPetStore Demo");
191
192
193 $(By.linkText("Enter the Store")).click();
194 $(By.id("WelcomeContent")).shouldBe(empty);
195
196
197 $(By.linkText("Sign In")).click();
198 $(By.cssSelector("#Catalog p")).shouldBe(text("Please enter your username and password."));
199
200
201 $(By.linkText("Register Now!")).click();
202 $(By.cssSelector("#Catalog h3")).shouldBe(text("User Information"));
203
204
205 String userId = String.valueOf(System.currentTimeMillis());
206 $(By.name("username")).setValue(userId);
207 $(By.name("password")).setValue("password");
208 $(By.name("repeatedPassword")).setValue("password");
209 $(By.name("account.firstName")).setValue("Jon");
210 $(By.name("account.lastName")).setValue("MyBatis");
211 $(By.name("account.email")).setValue("jon.mybatis@test.com");
212 $(By.name("account.phone")).setValue("09012345678");
213 $(By.name("account.address1")).setValue("Address1");
214 $(By.name("account.address2")).setValue("Address2");
215 $(By.name("account.city")).setValue("Minato-Ku");
216 $(By.name("account.state")).setValue("Tokyo");
217 $(By.name("account.zip")).setValue("0001234");
218 $(By.name("account.country")).setValue("Japan");
219 $(By.name("account.languagePreference")).selectOption("japanese");
220 $(By.name("account.favouriteCategoryId")).selectOption("CATS");
221 $(By.name("account.listOption")).setSelected(true);
222 $(By.name("account.bannerOption")).setSelected(true);
223 $(By.name("newAccount")).click();
224 $(By.id("WelcomeContent")).shouldBe(empty);
225
226
227 $(By.linkText("Sign In")).click();
228 $(By.name("username")).setValue(userId);
229 $(By.name("password")).setValue("password");
230 $(By.name("signon")).click();
231 $(By.id("WelcomeContent")).shouldBe(text("Welcome Jon!"));
232
233 }
234
235 @Test
236 void testSelectItems() {
237
238 open("/");
239 assertThat(title()).isEqualTo("JPetStore Demo");
240
241
242 $(By.linkText("Enter the Store")).click();
243 $(By.id("WelcomeContent")).shouldBe(empty);
244
245
246 $(By.cssSelector("#SidebarContent a")).click();
247 $(By.cssSelector("#Catalog h2")).shouldBe(text("Fish"));
248
249
250 $(By.linkText("FI-SW-01")).click();
251 $(By.cssSelector("#Catalog h2")).shouldBe(text("Angelfish"));
252
253
254 $(By.linkText("EST-1")).click();
255 $$(By.cssSelector("#Catalog table tr td")).get(2).shouldBe(text("Large Angelfish"));
256
257
258 $(By.linkText("Return to FI-SW-01")).click();
259 $(By.cssSelector("#Catalog h2")).shouldBe(text("Angelfish"));
260
261
262 $(By.linkText("Return to FISH")).click();
263 $(By.cssSelector("#Catalog h2")).shouldBe(text("Fish"));
264
265
266 $(By.linkText("Return to Main Menu")).click();
267 $(By.id("WelcomeContent")).shouldBe(empty);
268
269 }
270
271 @Test
272 void testViewCart() {
273
274
275 open("/");
276 assertThat(title()).isEqualTo("JPetStore Demo");
277
278
279 $(By.linkText("Enter the Store")).click();
280 $(By.id("WelcomeContent")).shouldBe(empty);
281
282
283 $(By.name("img_cart")).click();
284 $(By.cssSelector("#Catalog h2")).shouldBe(text("Shopping Cart"));
285
286 }
287
288 @Test
289 void testViewHelp() {
290
291
292 open("/");
293 assertThat(title()).isEqualTo("JPetStore Demo");
294
295
296 $(By.linkText("Enter the Store")).click();
297 $(By.id("WelcomeContent")).shouldBe(empty);
298
299
300 $(By.linkText("?")).click();
301 $(By.cssSelector("#Content h1")).shouldBe(text("JPetStore Demo"));
302
303 }
304
305 @Test
306 void testSidebarContentOnTopPage() {
307
308 open("/");
309 assertThat(title()).isEqualTo("JPetStore Demo");
310
311
312 $(By.linkText("Enter the Store")).click();
313 $(By.id("WelcomeContent")).shouldBe(empty);
314
315
316 $(By.cssSelector("#SidebarContent a:nth-of-type(1)")).click();
317 $(By.cssSelector("#Catalog h2")).shouldBe(text("Fish"));
318 $(By.linkText("Return to Main Menu")).click();
319
320
321 $(By.cssSelector("#SidebarContent a:nth-of-type(2)")).click();
322 $(By.cssSelector("#Catalog h2")).shouldBe(text("Dogs"));
323 $(By.linkText("Return to Main Menu")).click();
324
325
326 $(By.cssSelector("#SidebarContent a:nth-of-type(3)")).click();
327 $(By.cssSelector("#Catalog h2")).shouldBe(text("Cats"));
328 $(By.linkText("Return to Main Menu")).click();
329
330
331 $(By.cssSelector("#SidebarContent a:nth-of-type(4)")).click();
332 $(By.cssSelector("#Catalog h2")).shouldBe(text("Reptiles"));
333 $(By.linkText("Return to Main Menu")).click();
334
335
336 $(By.cssSelector("#SidebarContent a:nth-of-type(5)")).click();
337 $(By.cssSelector("#Catalog h2")).shouldBe(text("Birds"));
338 $(By.linkText("Return to Main Menu")).click();
339 }
340
341 @Test
342 void testQuickLinks() {
343
344 open("/");
345 assertThat(title()).isEqualTo("JPetStore Demo");
346
347
348 $(By.linkText("Enter the Store")).click();
349 $(By.id("WelcomeContent")).shouldBe(empty);
350
351
352 $(By.cssSelector("#QuickLinks a:nth-of-type(1)")).click();
353 $(By.cssSelector("#Catalog h2")).shouldBe(text("Fish"));
354
355
356 $(By.cssSelector("#QuickLinks a:nth-of-type(2)")).click();
357 $(By.cssSelector("#Catalog h2")).shouldBe(text("Dogs"));
358
359
360 $(By.cssSelector("#QuickLinks a:nth-of-type(3)")).click();
361 $(By.cssSelector("#Catalog h2")).shouldBe(text("Reptiles"));
362
363
364 $(By.cssSelector("#QuickLinks a:nth-of-type(4)")).click();
365 $(By.cssSelector("#Catalog h2")).shouldBe(text("Cats"));
366
367
368 $(By.cssSelector("#QuickLinks a:nth-of-type(5)")).click();
369 $(By.cssSelector("#Catalog h2")).shouldBe(text("Birds"));
370 }
371
372 @Test
373 void testMainImageContentOnTopPage() {
374
375 open("/");
376 assertThat(title()).isEqualTo("JPetStore Demo");
377
378
379 $(By.linkText("Enter the Store")).click();
380 $(By.id("WelcomeContent")).shouldBe(empty);
381
382
383 $(By.cssSelector("#MainImageContent area:nth-of-type(1)")).click();
384 $(By.cssSelector("#Catalog h2")).shouldBe(text("Birds"));
385 $(By.linkText("Return to Main Menu")).click();
386
387
388 $(By.cssSelector("#MainImageContent area:nth-of-type(2)")).click();
389 $(By.cssSelector("#Catalog h2")).shouldBe(text("Fish"));
390 $(By.linkText("Return to Main Menu")).click();
391
392
393 $(By.cssSelector("#MainImageContent area:nth-of-type(3)")).click();
394 $(By.cssSelector("#Catalog h2")).shouldBe(text("Dogs"));
395 $(By.linkText("Return to Main Menu")).click();
396
397
398 $(By.cssSelector("#MainImageContent area:nth-of-type(4)")).click();
399 $(By.cssSelector("#Catalog h2")).shouldBe(text("Reptiles"));
400 $(By.linkText("Return to Main Menu")).click();
401
402
403 $(By.cssSelector("#MainImageContent area:nth-of-type(5)")).click();
404 $(By.cssSelector("#Catalog h2")).shouldBe(text("Cats"));
405 $(By.linkText("Return to Main Menu")).click();
406
407
408 $(By.cssSelector("#MainImageContent area:nth-of-type(6)")).click();
409 $(By.cssSelector("#Catalog h2")).shouldBe(text("Birds"));
410 $(By.linkText("Return to Main Menu")).click();
411 }
412
413 @Test
414 void testLogoContent() {
415
416 open("/");
417 assertThat(title()).isEqualTo("JPetStore Demo");
418
419
420 $(By.linkText("Enter the Store")).click();
421 $(By.id("WelcomeContent")).shouldBe(empty);
422
423
424 $(By.cssSelector("#MainImageContent area:nth-of-type(1)")).click();
425 $(By.cssSelector("#Catalog h2")).shouldBe(text("Birds"));
426
427
428 $(By.cssSelector("#LogoContent a")).click();
429
430
431 $(By.cssSelector("#MainImageContent area:nth-of-type(5)")).click();
432 $(By.cssSelector("#Catalog h2")).shouldBe(text("Cats"));
433 }
434
435 private static String extractOrderId(String target) {
436 Matcher matcher = Pattern.compile("Order #(\\d{4}) .*").matcher(target);
437 String orderId = "";
438 if (matcher.find()) {
439 orderId = matcher.group(1);
440 }
441 return orderId;
442 }
443
444 }