com.nowsprinting.hellotesting.appiumtest.appium.page.DetailPage.java Source code

Java tutorial

Introduction

Here is the source code for com.nowsprinting.hellotesting.appiumtest.appium.page.DetailPage.java

Source

/*
 * Copyright 2015 TOYAMA Sumio
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nowsprinting.hellotesting.appiumtest.appium.page;

import static org.junit.Assert.*;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * Detail??(Appium)
 */
public class DetailPage {

    public static final String WIDGET_EDIT_TEXT = "android.widget.EditText";
    public static final String WIDGET_NUMBER_PICKER = "android.widget.NumberPicker";
    public static final String WIDGET_BUTTON = "android.widget.Button";
    private static final int AGE_MIN = 4;
    private static final int AGE_MAX = 120;
    protected AndroidDriver mDriver;
    protected WebDriverWait mWait;

    public DetailPage(AndroidDriver driver) {
        mDriver = driver;
        mWait = new WebDriverWait(mDriver, 60L);
    }

    /**
     * ???????
     * 
     * @return ?????true??????????false?
     */
    public boolean waitUntilLoad() {
        // ???????????
        try {
            mWait.until(ExpectedConditions.visibilityOfElementLocated(MobileBy.AccessibilityId("name textfield")));
        } catch (TimeoutException e) {
            return false;
        }
        return true;
    }

    /**
     * ??????
     * ?null?????????
     * 
     * @param name ?
     * @return ???
     */
    public DetailPage inputName(String name) {
        if (name != null) {
            WebElement nameTextField = mDriver.findElement(MobileBy.AccessibilityId("name textfield"));
            nameTextField.sendKeys(name);
        }
        return this;
    }

    /**
     * mail???
     * ?null?????????
     * 
     * @param mailAddress ?
     * @return ???
     */
    public DetailPage inputMailAddress(String mailAddress) {
        if (mailAddress != null) {
            WebElement mailTextField = mDriver.findElement(MobileBy.AccessibilityId("mail textfield"));
            mailTextField.sendKeys(mailAddress);

        }
        return this;
    }

    /**
     * ???
     * ?null?????????
     * 
     * @param age ?
     * @return ???
     */
    public DetailPage inputAge(int age) throws Exception {
        /*
         * ---------------------------
         * NumberPicker
         * Button (1??)
         * EditText (?: 4?)
         * Button (1??)
         * ---------------------------
         * ????????
         * ????EditText??????????????
         * ????????
         * https://github.com/appium/appium/issues/3662
         */
        if (age < AGE_MIN || age > AGE_MAX) {
            // ???????????
            return this;
        }

        WebElement numberPicker = mDriver.findElement(By.className(WIDGET_NUMBER_PICKER));
        int currentAge = getCurrentAge(numberPicker);
        int ageDiff = age - currentAge;
        if (ageDiff == 0) {
            // ??????????????
            return this;
        } else if (ageDiff > 0) {
            for (int i = 0; i < ageDiff; i++) {
                incrementCurrentAge(numberPicker);
            }
        } else if (ageDiff < 0) {
            for (int i = 0; i < Math.abs(ageDiff); i++) {
                decrementCurrentAge(numberPicker);
            }
        }
        return this;
    }

    /**
     * NumberPicker?EditText?????int?????
     */
    private int getCurrentAge(WebElement numberPicker) {
        WebElement ageTextField = numberPicker.findElement(By.className(WIDGET_EDIT_TEXT));
        // ??????EditText???????????
        return Integer.parseInt(ageTextField.getText(), 10);
    }

    /**
     * NumberPicker?1?????
     * ??????????????
     */
    private void incrementCurrentAge(WebElement numberPicker) throws Exception {
        int currentAge = getCurrentAge(numberPicker);
        if (currentAge == AGE_MAX) {
            return;
        }

        WebElement incButton;
        if (currentAge == AGE_MIN) {
            // ????????????????
            // ??????
            incButton = numberPicker.findElement(By.className(WIDGET_BUTTON));
        } else {
            // ????????????????
            // ??????2???(index?1)?????
            incButton = numberPicker.findElements(By.className(WIDGET_BUTTON)).get(1);
        }
        incButton.click();
        if (getCurrentAge(numberPicker) == currentAge + 1) {
            // click()??1?????return?
            return;
        }
        // click()???????tap()?Android 5.0??
        // ???????????
        TimeUnit.SECONDS.sleep(1);
        ((MobileElement) incButton).tap(1, 10);
        waitUntilAgeToBe(numberPicker, currentAge + 1);
    }

    /**
     * ????????????
     * 
     * @param numberPicker ??NumberPicker
     * @param expectedAge
     */
    private void waitUntilAgeToBe(WebElement numberPicker, int expectedAge) throws Exception {
        WebElement ageTextField = numberPicker.findElement(By.className(WIDGET_EDIT_TEXT));
        mWait.until(ExpectedConditions.textToBePresentInElement(ageTextField, String.valueOf(expectedAge)));
        TimeUnit.SECONDS.sleep(1);
    }

    /**
     * NumberPicker?1??????
     * ???????????????
     */
    private void decrementCurrentAge(WebElement numberPicker) throws Exception {
        int currentAge = getCurrentAge(numberPicker);
        if (currentAge == AGE_MIN) {
            return;
        }
        WebElement decButton;
        if (currentAge == AGE_MAX) {
            // ???????????????
            // ??????
            decButton = numberPicker.findElement(By.className(WIDGET_BUTTON));
        } else {
            // ????????????????
            // ??????1???(index?0)?????
            decButton = numberPicker.findElements(By.className(WIDGET_BUTTON)).get(0);
        }
        decButton.click();
        if (getCurrentAge(numberPicker) == currentAge - 1) {
            // click()??1?????return?
            return;
        }
        // click()???????tap()?Android 5.0??
        // ???????????
        TimeUnit.SECONDS.sleep(1);
        ((MobileElement) decButton).tap(1, 10);
        waitUntilAgeToBe(numberPicker, currentAge - 1);
    }

    /**
     * ???
     * ?null?????????
     * 
     * @param gender ????
     * @return ???
     */
    public DetailPage selectGender(Gender gender) {
        /*
         * ---------------------------
         * RadioGroup (contentDescription="gender segmentedcontrol")
         * RadioButton ("")
         * RadioButton ("")
         * ---------------------------
         * ???????????
         * contentDescrption?"gender segmentedcontrol"???????
         * ????????????
         */
        if (gender != null) {
            AndroidElement genderRadioGroup = (AndroidElement) mDriver
                    .findElement(MobileBy.AccessibilityId("gender segmentedcontrol"));
            // Appium??????text?API???????
            // ?????uiautomator???????????????
            //
            // ???findElement(By.name(...))??text???????
            // ????1.0?????????????????
            // https://github.com/appium/appium/blob/master/docs/en/advanced-concepts/migrating-to-1-0.md#new-locator-strategies
            String uiautomatorCriteria = String.format("new UiSelector().text(\"%s\")",
                    gender.getRadioButtonText());
            WebElement genderRadioButton = genderRadioGroup
                    .findElement(MobileBy.AndroidUIAutomator(uiautomatorCriteria));
            genderRadioButton.click();
        }
        return this;
    }

    /**
     * ???????
     * 
     * @return ?
     */
    public PreviewPage saveAndPreview() {
        mDriver.findElement(MobileBy.AccessibilityId("preview")).click();
        PreviewPage newPage = new PreviewPage(mDriver);
        assertTrue(newPage.waitUntilLoad());
        return newPage;
    }

}