View Javadoc
1   /*
2    * Copyright 2023 Adrian Herscu
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    *     http://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  
17  package dev.aherscu.qa.testing.example.scenarios.tutorial4;
18  
19  import static io.appium.java_client.remote.MobilePlatform.*;
20  import static io.appium.java_client.remote.options.SupportsAppOption.*;
21  import static io.appium.java_client.remote.options.SupportsAutoWebViewOption.*;
22  import static io.appium.java_client.remote.options.SupportsDeviceNameOption.*;
23  import static org.hamcrest.MatcherAssert.*;
24  import static org.hamcrest.Matchers.*;
25  import static org.openqa.selenium.remote.CapabilityType.*;
26  
27  import java.io.*;
28  import java.net.*;
29  
30  import org.openqa.selenium.*;
31  import org.openqa.selenium.remote.*;
32  import org.testng.annotations.*;
33  
34  import edu.umd.cs.findbugs.annotations.*;
35  import io.appium.java_client.android.*;
36  import lombok.*;
37  
38  /**
39   * TODO should run against one of the Android applications, e.g. Calculator
40   */
41  public class TestingAndroidApplication {
42      private WebDriver webDriver;
43  
44      @SneakyThrows
45      static AndroidDriver localApp() {
46          return new AndroidDriver(
47              new URL("http://127.0.0.1:4723/wd/hub"),
48              new DesiredCapabilities() {
49                  {
50                      setCapability(PLATFORM_NAME, ANDROID);
51                      setCapability(DEVICE_NAME_OPTION, "DONTCARE"); // local
52                      setCapability(AUTO_WEB_VIEW_OPTION, true);
53                      setCapability(APP_OPTION,
54                          new File(System.getProperty("user.dir"), "app.apk")
55                              .toString());
56                  }
57              });
58      }
59  
60      @Test
61      public void shouldRequireEmail() {
62          // ThreadUtils.sleep(4000); -- otherwise will test before stabilization
63          assertThat(webDriver
64              .findElements(By.xpath("//*[text()='Please enter valid email']")),
65              not(empty()));
66      }
67  
68      @SuppressFBWarnings(
69          value = "UPM_UNCALLED_PRIVATE_METHOD",
70          justification = "called by testng framework")
71      @AfterClass(alwaysRun = true) // important, otherwise we may leak resources
72      private void afterClassCloseWebDriver() {
73          webDriver.quit();
74      }
75  
76      @SuppressFBWarnings(
77          value = "UPM_UNCALLED_PRIVATE_METHOD",
78          justification = "called by testng framework")
79      @BeforeClass
80      private void beforeClassOpenWebDriver() {
81          // NOTE: ensure you have local Appium server started and Android
82          // emulator running and available via adb devices
83          // -- see README.md for details
84          webDriver = localApp();
85      }
86  }