View Javadoc
1   /*
2    *    Copyright 2010-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * Integration tests for screen transition.
48   *
49   * @author Kazuki Shimizu
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      // Open the home page
74      open("/");
75      assertThat(title()).isEqualTo("JPetStore Demo");
76      $(By.cssSelector("#Content h2")).shouldBe(text("Welcome to JPetStore 6"));
77  
78      // Move to the top page
79      $(By.linkText("Enter the Store")).click();
80      $(By.id("WelcomeContent")).shouldBe(empty);
81  
82      // Move to sign in page & sign
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      // Search items
90      $(By.name("keyword")).setValue("fish");
91      $(By.name("searchProducts")).click();
92      $$(By.cssSelector("#Catalog table tr")).shouldHave(size(4));
93  
94      // Select item
95      $(By.linkText("Fresh Water fish from China")).click();
96      $(By.cssSelector("#Catalog h2")).shouldBe(text("Goldfish"));
97  
98      // Add a item to the cart
99      $(By.linkText("Add to Cart")).click();
100     $(By.cssSelector("#Catalog h2")).shouldBe(text("Shopping Cart"));
101 
102     // Add a item to the cart
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     // Update quantity
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     // Remove item
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     // Checkout cart items
120     $(By.linkText("Proceed to Checkout")).click();
121     assertThat(title()).isEqualTo("JPetStore Demo");
122 
123     // Changing shipping address
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     // Confirm order information
130     $(By.name("newOrder")).click();
131     $(By.cssSelector("#Catalog")).shouldBe(text("Please confirm the information below and then press continue..."));
132 
133     // Submit order
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     // Show profile page
139     $(By.linkText("My Account")).click();
140     $(By.cssSelector("#Catalog h3")).shouldBe(text("User Information"));
141 
142     // Show orders
143     $(By.linkText("My Orders")).click();
144     $(By.cssSelector("#Content h2")).shouldBe(text("My Orders"));
145 
146     // Show order detail
147     $(By.linkText(orderId)).click();
148     assertThat(extractOrderId($(By.cssSelector("#Catalog table tr")).text())).isEqualTo(orderId);
149 
150     // Sign out
151     $(By.linkText("Sign Out")).click();
152     $(By.id("WelcomeContent")).shouldBe(empty);
153 
154   }
155 
156   @Test
157   void testUpdateProfile() {
158     // Open the home page
159     open("/");
160     assertThat(title()).isEqualTo("JPetStore Demo");
161 
162     // Move to the top page
163     $(By.linkText("Enter the Store")).click();
164     $(By.id("WelcomeContent")).shouldBe(empty);
165 
166     // Move to sign in page & sign
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     // Show profile page
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     // Edit account
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     // Open the home page
189     open("/");
190     assertThat(title()).isEqualTo("JPetStore Demo");
191 
192     // Move to the top page
193     $(By.linkText("Enter the Store")).click();
194     $(By.id("WelcomeContent")).shouldBe(empty);
195 
196     // Move to sign in page & sign
197     $(By.linkText("Sign In")).click();
198     $(By.cssSelector("#Catalog p")).shouldBe(text("Please enter your username and password."));
199 
200     // Move to use registration page
201     $(By.linkText("Register Now!")).click();
202     $(By.cssSelector("#Catalog h3")).shouldBe(text("User Information"));
203 
204     // Create a new user
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     // Move to sign in page & sign
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     // Open the home page
238     open("/");
239     assertThat(title()).isEqualTo("JPetStore Demo");
240 
241     // Move to the top page
242     $(By.linkText("Enter the Store")).click();
243     $(By.id("WelcomeContent")).shouldBe(empty);
244 
245     // Move to category
246     $(By.cssSelector("#SidebarContent a")).click();
247     $(By.cssSelector("#Catalog h2")).shouldBe(text("Fish"));
248 
249     // Move to items
250     $(By.linkText("FI-SW-01")).click();
251     $(By.cssSelector("#Catalog h2")).shouldBe(text("Angelfish"));
252 
253     // Move to item detail
254     $(By.linkText("EST-1")).click();
255     $$(By.cssSelector("#Catalog table tr td")).get(2).shouldBe(text("Large Angelfish"));
256 
257     // Back to items
258     $(By.linkText("Return to FI-SW-01")).click();
259     $(By.cssSelector("#Catalog h2")).shouldBe(text("Angelfish"));
260 
261     // Back to category
262     $(By.linkText("Return to FISH")).click();
263     $(By.cssSelector("#Catalog h2")).shouldBe(text("Fish"));
264 
265     // Back to the top page
266     $(By.linkText("Return to Main Menu")).click();
267     $(By.id("WelcomeContent")).shouldBe(empty);
268 
269   }
270 
271   @Test
272   void testViewCart() {
273 
274     // Open the home page
275     open("/");
276     assertThat(title()).isEqualTo("JPetStore Demo");
277 
278     // Move to the top page
279     $(By.linkText("Enter the Store")).click();
280     $(By.id("WelcomeContent")).shouldBe(empty);
281 
282     // Move to cart
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     // Open the home page
292     open("/");
293     assertThat(title()).isEqualTo("JPetStore Demo");
294 
295     // Move to the top page
296     $(By.linkText("Enter the Store")).click();
297     $(By.id("WelcomeContent")).shouldBe(empty);
298 
299     // Move to help
300     $(By.linkText("?")).click();
301     $(By.cssSelector("#Content h1")).shouldBe(text("JPetStore Demo"));
302 
303   }
304 
305   @Test
306   void testSidebarContentOnTopPage() {
307     // Open the home page
308     open("/");
309     assertThat(title()).isEqualTo("JPetStore Demo");
310 
311     // Move to the top page
312     $(By.linkText("Enter the Store")).click();
313     $(By.id("WelcomeContent")).shouldBe(empty);
314 
315     // Move to Fish category
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     // Move to Dogs category
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     // Move to Cats category
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     // Move to Reptiles category
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     // Move to Birds category
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     // Open the home page
344     open("/");
345     assertThat(title()).isEqualTo("JPetStore Demo");
346 
347     // Move to the top page
348     $(By.linkText("Enter the Store")).click();
349     $(By.id("WelcomeContent")).shouldBe(empty);
350 
351     // Move to Fish category
352     $(By.cssSelector("#QuickLinks a:nth-of-type(1)")).click();
353     $(By.cssSelector("#Catalog h2")).shouldBe(text("Fish"));
354 
355     // Move to Dogs category
356     $(By.cssSelector("#QuickLinks a:nth-of-type(2)")).click();
357     $(By.cssSelector("#Catalog h2")).shouldBe(text("Dogs"));
358 
359     // Move to Reptiles category
360     $(By.cssSelector("#QuickLinks a:nth-of-type(3)")).click();
361     $(By.cssSelector("#Catalog h2")).shouldBe(text("Reptiles"));
362 
363     // Move to Cats category
364     $(By.cssSelector("#QuickLinks a:nth-of-type(4)")).click();
365     $(By.cssSelector("#Catalog h2")).shouldBe(text("Cats"));
366 
367     // Move to Birds category
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     // Open the home page
375     open("/");
376     assertThat(title()).isEqualTo("JPetStore Demo");
377 
378     // Move to the top page
379     $(By.linkText("Enter the Store")).click();
380     $(By.id("WelcomeContent")).shouldBe(empty);
381 
382     // Move to Birds category
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     // Move to Fish category
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     // Move to Dogs category
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     // Move to Reptiles category
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     // Move to Cats category
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     // Move to Birds category
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     // Open the home page
416     open("/");
417     assertThat(title()).isEqualTo("JPetStore Demo");
418 
419     // Move to the top page
420     $(By.linkText("Enter the Store")).click();
421     $(By.id("WelcomeContent")).shouldBe(empty);
422 
423     // Move to Birds category
424     $(By.cssSelector("#MainImageContent area:nth-of-type(1)")).click();
425     $(By.cssSelector("#Catalog h2")).shouldBe(text("Birds"));
426 
427     // Move to top by clicking logo
428     $(By.cssSelector("#LogoContent a")).click();
429 
430     // Move to Cats category
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 }