Implementing the remaining steps

Come back to the Sample.feature file and create step definitions for the other two steps in the sample class file, HomePageSteps.

Step 2 implementation:

  1. In the And I choose to log in using Google step, we are supposed to click on the Google icon once the app launches.
  2. The sequence of steps is:
    1. Find the locator for the Google icon.
    2. Click on the icon.
  1. To find the locator, we need the Appium GUI app (Since the android settings are correct and unaltered, we can start the session again by clicking on Launch).
  2. Once the server has started, click on the Appium inspector icon and wait for the app to launch on emulator.
  3. Click on the Refresh button in the Appium Inspector popup.
  4. Once done, click on the Google icon in the Appium Inspector right panel; it will show you the layout details and button attributes in the Details panel. The id for the Google button is highlighted in red with row name resource-id in the Details section. The first part there--com.quikr--is the package name; the value of id is sign_in_button. The inspector also gives you the xpath, which is the last item under the Details section. There are different ways to find an element: id, className, cssSelector, linkText, partialLinkText, name, tagName, xpath. We can choose one of these based on what is available. We will see locators in detail in Chapter 5, Understanding Appium Inspector to Find r:
  1. For now, we can use the id field. Once we get the value of id, we tell the r to find an element by the property name id and value sign_in_button; then, perform a click operation on it.
  2. Navigate to the iChooseToLogInUsingGoogle method under the HomePageSteps class and paste the following code. Add an import statement import org.openqa.selenium.By for:
 appiumDriver.findElement(By.id("sign_in_button")).click();

Step 3 implementation:

  1. In the Then I see account picker screen with my email address "[email protected]" step, we are required to get the value of the email displayed on the Google account picker popup and match it with the expected value passed.
  2. The sequence of steps is:
    1. Find the locator of the email ID field
    2. Get the value of that field
    3. Perform string comparison and pass or fail accordingly.
  3. If you have stopped the Appium session, launch the Appium GUI app and wait for the app to be launched. Click on the Google icon and wait for the account picker to show up.

 

  1. Once done, click on the Inspector icon on Appium GUI; here's how the Appium inspector screen will be:
  1. Let's quickly verify if using the ID value will give us that element. Click on Locator in the inspector screen. Select the strategy as id, enter value as account_name, and click on Search. It throws up an error, as illustrated:
  1. An important thing to notice in resource-id is that the package name has been changed to com.google.android.gms. Let's use the complete value of ID in this case--com.google.android.gms:id/account_name--and click on Search.
  2. It works this time, showing one element found.
  3. So, we are good to implement the iSeeAccountPickerScreenWithMyEmailAddress method, as follows. Rename the parameter from arg0 to expected and copy and paste the following implementation. Since clicking on the Google button and showing up the popup will take time, we added a Thread.sleep() before the assert statement:
@Then("^I see account picker screen with my email address "([^"]*)"$")
public void iSeeAccountPickerScreenWithMyEmailAddress(String expected) throws Throwable {
Thread.sleep(5000);
Assert.assertEquals("Email Id matches", expected, appiumDriver.findElement(By.id("com.google.android.gms:id/account_name")).getText());
}
  1. In the preceding method, we are using JUnit assertion--Assert.assertEquals--for string comparison. The advantage of this method is that it will show both the expected and the actual string in case of failure:
 org.junit.ComparisonFailure: Email Id matches expected:
<vagrantlab@gmail[1].com> but was:<vagrantlab@gmail[].com>
at org.junit.Assert.assertEquals(Assert.java:115)

The complete HomePageStep class will look like this:

package steps;

import cucumber.api.java.en.And;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import io.appium.java_client.AppiumDriver;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;
import java.util.concurrent.TimeUnit;

public class HomePageSteps {

private AppiumDriver appiumDriver;

@When("^I launch Quikr app$")
public void iLaunchQuikrApp() throws Throwable {

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "5.0");
capabilities.setCapability("deviceName", "Nexus");
capabilities.setCapability("noReset", false);
capabilities.setCapability("fullReset", true);
capabilities.setCapability("app",
"/Users/nishant/Development/HelloAppium/app/quikr.apk");
System.out.println(capabilities.toString());
appiumDriver = new AppiumDriver(new
URL("http://0.0.0.0:4723/wd/hub"),
capabilities);
appiumDriver.manage().timeouts().implicitlyWait(60,
TimeUnit.SECONDS);
}

@And("^I choose to log in using Google$")
public void iChooseToLogInUsingGoogle() throws Throwable {
appiumDriver.findElement(By.id("sign_in_button")).click();
}

@Then("^I see account picker screen with my email address "([^"]*)"$")
public void iSeeAccountPickerScreenWithMyEmailAddress(String expected) throws Throwable {
Thread.sleep(5000);
Assert.assertEquals("Email Id matches", expected, appiumDriver.findElement(By.id("com.google.android.gms:id/account_name")).getText());
}

}

Let's now move to the execution of our preceding code. Once we are done with the step implementation, stop the Appium GUI session.

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

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