Demo entry 6663079

DHC

   

Submitted by anonymous on Nov 29, 2017 at 04:41
Language: Java. Code size: 3.9 kB.

package com.dhc.vfmautotest;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;
import android.util.Log;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

/**
 * Created by Administrator on 11/28/2017.
 */
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class LoginTest {
    private UiDevice mDevice;
    private static final String packageName = "com.tkeasia.intranet.apit.vfm.android";
    private static final int LAUNCH_TIMEOUT = 5000;

    @Before
    public void startMainActivityFromHomeScreen() {
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        mDevice.pressHome();
        assertThat(packageName, notNullValue());
        mDevice.wait(Until.hasObject(By.pkg(packageName).depth(0)), LAUNCH_TIMEOUT);
        Context context = InstrumentationRegistry.getContext();
        final Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage(packageName);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
        context.startActivity(intent);

        // Wait for the app to appear
        mDevice.wait(Until.hasObject(By.pkg(packageName).depth(0)), LAUNCH_TIMEOUT);
    }

    @Test
    public void loginTest() throws UiObjectNotFoundException {
        UiObject mainText = null;
        try {
            mainText = mDevice.findObject(new UiSelector().resourceId("com.tkeasia.intranet" +
                                                                              ".apit.vfm" +
                                                                              ".android:welcome_page/lyt_service"));
            if (mainText != null && mainText.getText() != null) {
                mDevice.pressBack();
                mDevice.findObject(new UiSelector().resourceId("android:id/button3")).click();
            }
        } catch (Exception e) {
            Log.d("TAGE", "PASS");
        }


        UiObject loginBtn = mDevice.findObject(new UiSelector().resourceId("com.tkeasia.intranet" +
                                                                                   ".apit.vfm" +
                                                                                   ".android:login/login_button"));
        UiObject edtUserName = mDevice.findObject(new UiSelector().resourceId("com.tkeasia.intranet.apit.vfm.android:login/username"));
        UiObject edtPwd = mDevice.findObject(new UiSelector().resourceId("com.tkeasia.intranet" +
                                                                                 ".apit.vfm" +
                                                                                 ".android:login/password"));

        edtUserName.setText("80000100");
        edtPwd.setText("abcd@1234#");
        loginBtn.click();
        mDevice.waitForWindowUpdate(packageName, 10 * 60 * 1000);
        mainText = mDevice.findObject(new UiSelector().resourceId("com.tkeasia.intranet" +
                                                                          ".apit.vfm" +
                                                                          ".android:welcome_page/lyt_service"));
        assertThat(mainText.toString(), notNullValue());
    }

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).