cochrane343.journal.test.JournalStepDefinitions.java Source code

Java tutorial

Introduction

Here is the source code for cochrane343.journal.test.JournalStepDefinitions.java

Source

/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2014 by cochrane343
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package cochrane343.journal.test;

import io.appium.java_client.AppiumDriver;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

/**
* @author cochrane343
* @since 0.10.2
*/
public class JournalStepDefinitions {
    /**
     * Timeout in seconds for waiting for elements to become present
     */
    private static final int WAIT_FOR_ELEMENT_TIMEOUT = 60;

    private AppiumDriver driver;

    @Given("^I have launched the app$")
    public void i_have_launched_the_app() throws MalformedURLException {
        final File apkFile = new File("../journal/bin/journal.apk");
        final URL serverURL = new URL("http://localhost:4723/wd/hub");

        final DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "4.4.2");
        capabilities.setCapability("deviceName", "Android Emulator");
        capabilities.setCapability("app", apkFile.getAbsolutePath());
        capabilities.setCapability("appPackage", "cochrane343.journal");
        capabilities.setCapability("appActivity", ".MainActivity");

        driver = new AppiumDriver(serverURL, capabilities);
    }

    @When("^I tap the add expense action bar button$")
    public void i_tap_the_add_expense_action_bar_button() throws Throwable {
        tap("Add Expense");
    }

    @Then("^the new expense dialog is opened$")
    public void the_new_expense_dialog_is_opened() throws Throwable {
        waitFor("New Expense");
    }

    /* - - - - - Helpers - - -  - - */

    private WebElement waitFor(final String text) {
        final String xPath = String.format("//*[@text = '%s' or @content-desc='%s']", text, text);
        final By by = By.xpath(xPath);

        final ExpectedCondition<WebElement> presenceCondition = ExpectedConditions.presenceOfElementLocated(by);
        final WebDriverWait driverWait = new WebDriverWait(driver, WAIT_FOR_ELEMENT_TIMEOUT);

        driverWait.until(presenceCondition);

        return driver.findElement(by);
    }

    private void tap(final String text) {
        waitFor(text).click();
    }
}