View Javadoc
1   /*
2    * Copyright 2022 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.tutorial5;
18  
19  import static org.hamcrest.MatcherAssert.*;
20  import static org.hamcrest.Matchers.*;
21  
22  import java.net.*;
23  
24  import org.openqa.selenium.*;
25  import org.openqa.selenium.remote.*;
26  import org.testng.annotations.*;
27  
28  import edu.umd.cs.findbugs.annotations.*;
29  import io.appium.java_client.windows.*;
30  import lombok.*;
31  
32  public class TestingWindowsApplication {
33  
34      private WindowsDriver driver;
35  
36      @Test
37      public void shouldOpenCalculator() {
38          assertThat(driver.getTitle(), equalTo("Calculator"));
39      }
40  
41      @Test
42      public void shouldCalculate() {
43          driver.findElement(By.id("CalculatorResults"))
44              .sendKeys("8+7=");
45          assertThat(driver.findElement(By.id("CalculatorResults"))
46              .getText(),
47              stringContainsInOrder("Display is", "15"));
48      }
49  
50      @SuppressFBWarnings(
51          value = "UPM_UNCALLED_PRIVATE_METHOD",
52          justification = "called by testng framework")
53      @AfterClass(alwaysRun = true) // important, otherwise we may leak resources
54      private void afterClassCloseWebDriver() {
55          driver.quit();
56      }
57  
58      @SuppressFBWarnings(
59          value = "UPM_UNCALLED_PRIVATE_METHOD",
60          justification = "called by testng framework")
61      @BeforeClass
62      @SneakyThrows
63      private void beforeClassOpenWebDriver() {
64          // NOTE: ensure you have Appium-compatible Windows Application Driver
65          // from https://github.com/Microsoft/WinAppDriver/releases
66          val capabilities = new DesiredCapabilities();
67          capabilities.setCapability("app",
68              "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");
69          driver = new WindowsDriver(new URL("http://127.0.0.1:4723"),
70              capabilities);
71  
72          // NOTE: should uncomment in order to deal with latencies
73          // webDriver.manage().timeouts().implicitlyWait(10, SECONDS);
74      }
75  }