Implementing the other steps

Let's implement the preceding scenario. The first step to launch a quicker app is already automated, so let's figure out the dependency for the following step:

  And I choose "Bangalore" as my city
  1. Tap on SKIP.
  2. Tap on Select City.
  3. Enter Bangalore in the search for your city textbox.
  4. Select the appearing value from the dropdown.

So, when you use the Inspector, you will notice that all the preceding elements have an ID, which can be easily used.

Here's the implementation for the same. We can create these methods in the HomePageSteps class file. To ensure that the click action has the element visible, we have used Thread.sleep(). We will refactor the same in a later chapter to use WebDriver wait:

@And("^I choose "([^"]*)" as my city$")
public void iChooseAsMyCity(String city) throws Throwable {
appiumDriver.findElement(By.id("skip")).click();

Thread.sleep(2000);
appiumDriver.findElement(By.id("citySpinner")).click();

Thread.sleep(2000);
appiumDriver.findElement(By.id("search_ET")).click();
appiumDriver.findElement(By.id("search_ET")).sendKeys(city);

Thread.sleep(2000);
appiumDriver.findElement(By.id("city_name")).click();
}

Similarly, we can implement the other steps:

And I search for "Honda City" under Used Cars
Then I should see the first car search result with "Honda"

If you get the Upgrade Available popup, you can use the Appium Inspector window, as shown, and construct the xpath for the same:

appiumDriver.findElement(By.xpath("//android.widget.Button[@text='Later']"));

To implement the next step, which is And I search for "Honda City" under Used Cars, we need to perform the following steps:

  1. Tap on the Cars category.

Here's the code to do the same; the locator used is xpath:

    appiumDriver.findElement(By.xpath("//android.widget.
TextView[@text='Cars']")).click();
  1. Tap on the Find a Car textbox.

This is the code to do the same; the locator used is id:

    appiumDriver.findElement(By.id("cnb_hp_choose_et")).click();
  1. Type in Honda City in the textbox.

The following is the code to do the same; the locator used is id:

    appiumDriver.findElement(By.id("cnb_search_text_et"))
.sendKeys("Honda City");
  1. Select the matching item from the results.

The following is the code to do the same; the locator used is id. To find the result, we need to click on one result from the list of results. So, we will be using the findElements() API, which will return a list of results and here we will query for the result we want:

appiumDriver.findElements(By.id("text1"));

To filter the result we want, we will iterate through the list and check for the element that contains the text Honda City. Here's the code for the same:

List<WebElement> results = appiumDriver.findElements(By.id("text1"));
for (WebElement result : results) {
if (result.getText().contains(carName)){
result.click();
break;
}
}
  1. Tap on Find Used Cars.

Here's the code to do the same; the locator used is id:

   appiumDriver.findElement(By.id("cnb_search_button")).click();

So, the complete method will look like this:

@And("^I search for "([^"]*)" under Used Cars$")
public void iSearchForUnderUsedCars(String carName) throws Throwable {
appiumDriver.findElement(By.xpath("//android.widget.TextView[@text='Cars']")).click();
appiumDriver.findElement(By.id("cnb_hp_choose_et")).click();
appiumDriver.findElement(By.id("cnb_search_text_et")).sendKeys(carName);

List<WebElement> results = appiumDriver.findElements(By.id("text1"));
for (WebElement result : results) {
if (result.getText().contains(carName)) {
result.click();
break;
}
}
appiumDriver.findElement(By.id("cnb_search_button")).click();
}

The last step to be automated is as follows:

  Then I should see the first car search result with "Honda"

This is the code to do the same, and the locator used is id:

  appiumDriver.findElements(By.id("cars_ad_list_title_tv"));

In the next code, we are checking that the header of each result item contains Honda as we have searched for Honda City car:

@Then("^I should see the first car search result with "([^"]*)"$")
public void iShouldSeeTheFirstCarSearchResultWith(String arg0) throws Throwable {
List<WebElement> elements = appiumDriver.findElements(By.id("cars_ad_list_title_tv"));
Assert.assertTrue("Verified first result contains Honda",elements.get(0).getText().startsWith(arg0));
}

So, this completes the implementation of the new scenario. You would have noticed that, to use Appium inspector, we need to start the session from scratch. There are times when we perform certain transactions and steps to arrive on a screen and then see the locators. There is a direct way to check for locators without having to use Appium.

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

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