Writing our first Appium test

Until now, we have created a sample Java project and added the Appium dependency. Next, we need to add a feature file and implement the code behind. Let's start that:

  1. Under the Project folder, create the src/test/java/features directory structure.
  2. Right click on the features folder, select New > File, and enter name as Sample.feature.
  1. You will notice that the file is associated with a Cucumber feature icon if the plugin is installed correctly.
  2. We need to explore the Quikr mobile app; when you install it and play around the first scenario, you will notice the login scenario. Quikr gives you an option to log in using Google or Facebook.
  3. In the Sample.feature file, let's write a sample scenario, as shown, which is about logging in using Google.
  4. Detailed steps will be clicking on log in using Google, and then verifying that the account picker screen has a valid email ID:
 Feature: Hello World

Scenario: Registration Flow Validation via App
As a user I should be able to see my google account
when I try to register myself in Quikr

When I launch Quikr app
And I choose to log in using Google
Then I see account picker screen with my email address
"[email protected]"
  1. When the Cucumber steps are not implemented, it will highlight them in yellow. Right now, all the steps will be highlighted. The implementation of these steps will be java class, and they can be hosted under different packages.
  2. Right-click on the java folder, select New > Package, and enter name as steps.
  3. The next step is to implement the Cucumber steps; click on the first line in the Sample.feature file When I launch Quikr app and press Alt+Enter. Then, select the Create step definition option:
  1. It will present you with a popup to enter File name, File location, and File type. We need to enter the step's class name; select the shown values. Since the step belongs to Home Page (or we can even call it Landing Page), we create the HomePageSteps class:
  1. So, the idea is that the steps will belong to a page and each page will typically have its own step implementation class.
  2. Once you click on OK, it will create the given template in the HomePageSteps class file:
public class HomePageSteps {
@When("^I launch Quikr app$")
public void iLaunchQuikrApp() throws Throwable {
// Write code here that turns the phrase above into
concrete actions
throw new PendingException();
}
}

We have written our test steps; the next step is to implement the actual code behind which will launch the Appium server and then deploy the app on the emulator. So, let's start with launching the emulator first and then the Appium App (For Windows user, the screen below may be totally different and some options may not be present; you can choose the mandatory options and ignore others if it is not present):

  1. Download the Quikr app (version 9.16).
  2. Create a folder named app under the HelloAppium project and copy the downloaded apk under that folder. Ideally, this will host the app under test:
  1. Launch the Appium GUI app:
    • On Mac, navigate to Finder > Applications > Appium.
    • On Windows, navigate to Start Menu > Type Appium > Press Enter:
  1. Launch the Genymotion emulator (the one we created in Chapter 2, Setting Up the Machine) by selecting the virtual device and clicking on the Start icon, as highlighted; wait for it to get started:

  1. Once launched, we will see the Genymotion emulator, as shown:
  1. Switch to the Appium GUI app, click on the android icon, and enter the following details:
    • App Path: Browse to the .apk location under the app folder
    • Platform Name: Select Android
    • Automation Name: Select Appium
    • Platform Version: Select 5.1 Lollipop (API Level 22) from the dropdown, as the emulator created in Chapter 2, Setting Up the Machine or the device. Also, even though it's a dropdown, it allows you to edit the value and it behaves as a text input field rather than a dropdown. On Windows app , it's just a drop down so make sure you have a emulator version which is supported by the Appium windows app.
Android N (version 7.0) has support issues with Appium. Be cautious while you are trying your hands with version 7.0.
    • Device Name: Enter any string, such as Nexus6:
  1. Once the preceding settings are done, click on the General Settings icon----and choose the following settings:
    • Select Pre-Launch Application.
    • Select Strict Capabilities.
    • Select Override Existing Sessions.
    • Select Kill Processes Using Server Port Before Launch.
    • Select New Command Timeout and enter the value 7200. Refer to the following screenshot:
  1. Once done, close the popup by clicking on the General settings icon----again. Then, click on Launch.
  2. You will see a bunch of logs on the Appium console and will be able to find the given line:
[Appium] Appium REST http interface listener started on 0.0.0.0:4723
[HTTP] --> GET /wd/hub/status {}
[MJSONWP] Calling AppiumDriver.getStatus() with args: []
[MJSONWP] Responding to client with driver.getStatus() result: {"build":{"version":"1.5.3"...
[HTTP] <-- GET /wd/hub/status 200 17 ms - 83

So basically, it indicates that the Appium server has been started on the default server and port, and it has returned the status HTTP 200:

  1. Click on the Inspector icon----to launch the Appium Inspector popup.
  2. The preceding step will install the app on the emulator and launch the inspector window. It will also install the Appium Settings app and Unlock app on the emulator.
  3. If the emulator image in the right pane of the Inspector is not fully loaded, then click on the Refresh button to sync with the t on the emulator. Here's how the Inspector popup will look after clicking on the Refresh button:

If you notice the Appium console, you will find the following log when you click on Inspector:

[HTTP] --> POST /wd/hub/session {"desiredCapabilities":{"platformName":"Android","platformVersion":"5.1","newCommandTimeout":"7200","app":"/Users/nishant/Development/HelloAppium/app/quikr.apk","automationName":"Appium","deviceName":"Nexus6"}}

[MJSONWP] Calling AppiumDriver.createSession() with args:
[{"platformName":"Android",...
[Appium] Creating new AndroidDriver session
[Appium] Capabilities:
[Appium] platformName: 'Android'
[Appium] platformVersion: '5.1'
[Appium] newCommandTimeout: '7200'
[Appium] app:
'/Users/nishant/Development/HelloAppium/app/quikr.apk'
[Appium] automationName: 'Appium'
[Appium] deviceName: 'Nexus6'
[Appium] fullReset: true
[BaseDriver] Capability 'newCommandTimeout' changed from string
('7200') to integer (7200). This may cause unexpected behavior
[BaseDriver] Session created with session id: 6c61a910-5c7e-
44ff-9cb1-fb2413805cda
[debug] [AndroidDriver] Getting Java version

[AndroidDriver] Java version is: 1.8.0_73

[ADB] Checking whether adb is present

[ADB] Using adb from /usr/local/Cellar/android-
sdk/24.4.1_1/platform-tools/adb
[AndroidDriver] Retrieving device list
[debug] [ADB] Trying to find a connected android device
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[AndroidDriver] Looking for a device with Android 5.1
[debug] [ADB] Setting device id to 192.168.56.101:5555

[ADB] Getting device platform version

[debug] [ADB] Getting connected devices...

The last line of the preceding log shows that the Appium server has created a session with session ID 6c61a910-5c7e-44ff-9cb1-fb2413805cda. For all future communication with the device until the server is alive, this session ID will be the context. For example, when you click on the Refresh button on the inspector, the console will log the request like [HTTP] --> GET /wd/hub/session/6c61a910-5c7e-44ff-9cb1-fb2413805cda/source {}.

The session gets killed when we click on Stop on the Appium GUI. So every time we click on Start, Appium gives us a new session; this is because we selected the parameter Override ExistingSession. This makes sure that the previous Appium session is killed before creating a new one.

Now Appium has started the server and created a session based on the parameters we passed in both the Android Settings and General Settings, which are also known as Desired Capabilities. We will take a detailed look at Desired Capabilities in the next chapter.

  1. Coming back to automation of the steps, we need to implement whatever we have done until now as code.
  2. Click on the Record button on Appium Inspector; it will generate the boilerplate code, which will perform the function of setting device-related capabilities (such as Platform name and version) when the Appium server is already running:
Windows machine do not have the Record button. Windows users can skip to step 3 and copy the code mentioned below. The code generated is OS independent, it will work seamlessly for both Mac OSX and Windows user.
  1. Copy the preceding code (generated by appium) and paste the same in the iLaunchQuikrApp method of the HomePageSteps class. Delete the line throw new PendingException();.
  2. Resolve the dependencies by importing the necessary class or add the following code to the class file:
 import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
  1. Remove the wd.close line as this will kill the session. We need the session to perform other tests, and then kill the session once done.
  2. This is how the code snippet will look:
 @When("^I launch Quikr app$")
public void iLaunchQuikrApp() throws Throwable {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appium-version", "1.0");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "5.1");
capabilities.setCapability("deviceName", "Nexus6");
capabilities.setCapability("app",
"/Users/nishant/Development/HelloAppium/app/quikr.apk");
AppiumDriver wd = new AppiumDriver(new
URL("http://0.0.0.0:4723/wd/hub"), capabilities);
wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
  1. So, the boilerplate typically has all the settings we made under Android Settings.
  2. Close the Appium Inspector window and click on Stop on the Appium GUI app. This kills the current Appium session.

We will discuss some of the concepts, such as and implicit wait, in the upcoming chapters. To create an Appium session we need only 4 capabilities to be passed which is generated by the boiler plate code: platformName, platformVersion, deviceName and app.

Let's try to run the generated code to see whether it works seamlessly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.150.231