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

Java tutorial

Introduction

Here is the source code for com.nowsprinting.hellotesting.appiumtest.appium.page.PreviewPage.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 io.appium.java_client.android.AndroidDriver;

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

/**
 * Preview??(Appium)
 */
public class PreviewPage {

    protected AndroidDriver mDriver;
    protected WebDriverWait mWait;

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

    /**
     * ???????
     * 
     * @return ?????true??????????false?
     */
    public boolean waitUntilLoad() {
        try {
            // WebView?????
            mWait.until(ExpectedConditions.presenceOfElementLocated(By.className("android.webkit.WebView")));
        } catch (TimeoutException e) {
            return false;
        }
        return true;
    }

    /**
     * ????????????
     */
    public String getName() {
        ensureWebViewContext();
        return mDriver.findElement(By.name("name")).getText();
    }

    /**
     * ?????????
     */
    public String getMailAddress() {
        ensureWebViewContext();
        return mDriver.findElement(By.name("mail")).getText();
    }

    /**
     * ?????????
     */
    public String getGender() {
        ensureWebViewContext();
        return mDriver.findElement(By.name("gender")).getText();
    }

    /**
     * ?????????
     */
    public String getAge() {
        ensureWebViewContext();
        return mDriver.findElement(By.name("age")).getText();
    }

    /**
     * ?????????
     */
    public String getDivision() {
        ensureWebViewContext();
        return mDriver.findElement(By.name("division")).getText();
    }

    /**
     * ???WebView???????
     * 
     * @return ???WebView????true?????????false?
     */
    private boolean isInWebViewContext() {
        String currentContext = mDriver.getContext();
        return isWebViewContext(currentContext);
    }

    /**
     * ???WebView???????
     * ?"NATIVE_APP"?<b>???</b>?WebView????
     * 
     * @return WebView???true?????????false?
     */
    private boolean isWebViewContext(String context) {
        return !context.equals("NATIVE_APP");
    }

    /**
     * WebView??
     * ?WebView??????
     */
    private void ensureWebViewContext() {
        if (isInWebViewContext()) {
            // ???WebView??????
            return;
        }

        for (String context : mDriver.getContextHandles()) {
            if (isWebViewContext(context)) {
                mDriver.context(context);
                return;
            }
        }
        Assert.fail("WebView context doesn't exist");
    }
}