Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.wayd.prince;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.os.SystemClock;
- import android.support.test.InstrumentationRegistry;
- import android.support.test.espresso.Espresso;
- import android.support.test.espresso.NoMatchingViewException;
- import android.support.test.espresso.PerformException;
- import android.support.test.espresso.web.webdriver.Locator;
- import android.support.test.runner.AndroidJUnit4;
- import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
- import android.support.test.runner.lifecycle.Stage;
- import android.test.ActivityInstrumentationTestCase2;
- import android.test.suitebuilder.annotation.LargeTest;
- import android.util.Log;
- import android.view.View;
- import android.widget.ImageButton;
- import com.squareup.okhttp.OkHttpClient;
- import com.squareup.spoon.Spoon;
- import org.junit.Before;
- import org.junit.FixMethodOrder;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.junit.runners.MethodSorters;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import me.wayd.prince.screens.auth.AuthActivity;
- import static android.support.test.espresso.Espresso.onView;
- import static android.support.test.espresso.action.ViewActions.clearText;
- import static android.support.test.espresso.action.ViewActions.click;
- import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
- import static android.support.test.espresso.action.ViewActions.scrollTo;
- import static android.support.test.espresso.action.ViewActions.typeTextIntoFocusedView;
- import static android.support.test.espresso.assertion.ViewAssertions.matches;
- import static android.support.test.espresso.matcher.ViewMatchers.withId;
- import static android.support.test.espresso.matcher.ViewMatchers.withParent;
- import static android.support.test.espresso.matcher.ViewMatchers.withText;
- import static android.support.test.espresso.web.sugar.Web.onWebView;
- import static android.support.test.espresso.web.webdriver.DriverAtoms.findElement;
- import static android.support.test.espresso.web.webdriver.DriverAtoms.webClick;
- import static android.support.test.espresso.web.webdriver.DriverAtoms.webKeys;
- import static android.support.test.espresso.action.ViewActions.typeText;
- import static me.wayd.prince.R.string.btn_make_wish;
- import static org.hamcrest.Matchers.instanceOf;
- import static org.hamcrest.Matchers.allOf;
- import static org.hamcrest.Matchers.is;
- @RunWith(AndroidJUnit4.class)
- @LargeTest
- @FixMethodOrder(MethodSorters.NAME_ASCENDING)
- public class UITests extends ActivityInstrumentationTestCase2 {
- public UITests() {
- super(AuthActivity.class);
- }
- @Before
- public void setUp() throws Exception {
- super.setUp();
- injectInstrumentation(InstrumentationRegistry.getInstrumentation());
- getActivity();
- //PrinceApplication app = (PrinceApplication)getActivity().getApplication()
- // Espresso.registerIdlingResources(new OkHttpIdlingResource(PrinceApplication.getOkHttpClient()));
- }
- private void emailLogin(String email, String password, Boolean screenshot) {
- if (screenshot) takeScreenshot("login");
- onView(withId(R.id.btnEmailLogin)).perform(click());
- if (screenshot) takeScreenshot("email-login");
- onView(withId(R.id.etEmail)).perform(typeText(email));
- onView(withId(R.id.etPassword)).perform(typeText(password), closeSoftKeyboard());
- onView(withId(R.id.btnLogin)).perform(click());
- }
- private void login(String email, String password) {
- this.emailLogin(email, password, false);
- }
- private void logout() {
- boolean i = true;
- while (i) {
- try {
- onView(allOf(withParent(withId(R.id.toolbar)), is(instanceOf(ImageButton.class)))).perform(click());
- onView(withText(R.string.nav_exit)).perform(click());
- i = false;
- } catch (PerformException e) {
- onView(allOf(withParent(withId(R.id.toolbar)), is(instanceOf(ImageButton.class)))).perform(click());
- onView(withText(R.string.nav_exit)).perform(click());
- i = false;
- }
- }
- }
- private void VKLogin() {
- takeScreenshot("login");
- onView(withId(R.id.btnVkLogin)).perform(click());
- takeScreenshot("vk_login");
- try {
- onWebView().withElement(findElement(Locator.NAME, "email")).perform(webKeys(""));
- } catch (RuntimeException e) {
- onWebView().withElement(findElement(Locator.CLASS_NAME, "mh_btn_label")).perform(webClick());
- }
- onWebView().withElement(findElement(Locator.NAME, "email")).perform(webKeys("+79046442128"));
- onWebView().withElement(findElement(Locator.NAME, "pass")).perform(webKeys("qweasd123"));
- SystemClock.sleep(5000);
- onWebView().withElement(findElement(Locator.CLASS_NAME, "button")).perform(webClick());
- SystemClock.sleep(5000);
- onWebView().withElement(findElement(Locator.CLASS_NAME, "button")).perform(webClick());
- }
- @Test
- public void test01PostWish() {
- this.emailLogin("testfemale@gmail.com", "12345678", true);
- onView(allOf(withParent(withId(R.id.toolbar)), is(instanceOf(ImageButton.class)))).perform(click());
- takeScreenshot("female-navigation-drawer");
- onView(withText(R.string.nav_profile)).perform(click());
- try {
- Log.e("retrofit", "try find");
- onView(withId(R.id.btnChangeMake)).check(matches(withText(btn_make_wish)));
- } catch (AssertionError e) {
- Log.e("retrofit", "click cancel");
- onView(withId(R.id.btnChangeMake)).perform(click()); //remove wish
- } finally {
- Log.e("retrofit", "click perform");
- takeScreenshot("female-profile-no-wish");
- onView(withId(R.id.btnChangeMake)).perform(click());
- }
- takeScreenshot("female-new-wish-suggestion");
- Log.e("retrofit", "gonna push");
- onView(withId(R.id.action_refresh)).perform(click());
- onView(withId(R.id.action_refresh)).perform(click());
- onView(withId(R.id.etOfferWish)).perform(click());
- onView(withId(R.id.action_refresh)).perform(click());
- onView(withId(R.id.action_refresh)).perform(click());
- takeScreenshot("female-new-wish-suggestion-selected");
- onView(withId(R.id.etOfferWish)).perform(clearText());
- onView(withId(R.id.etOfferWish)).perform(typeTextIntoFocusedView("Female wish Two Lines Female wish Two Lines"));
- takeScreenshot("female-new-wish-filled");
- onView(withId(R.id.btnDone)).perform(click());
- takeScreenshot("female-profile-with-wish");
- }
- @Test
- public void test02HideWish() {
- this.login("testfemale@gmail.com", "12345678");
- try {
- onView(withId(R.id.btnLater)).perform(click());
- } catch (NoMatchingViewException e) {
- try {
- onView(withId(R.id.btnHide)).perform(click());
- } catch (NoMatchingViewException a) {
- takeScreenshot("No-wishes");
- onView(withId(R.id.btnWatchFromStart)).perform(click());
- }
- }
- }
- @Test
- public void test03LookAgain() {
- this.login("testfemale@gmail.com", "12345678");
- try {
- onView(withId(R.id.btnPerform)).check(matches(withText(R.string.btn_perform_female)));
- } catch (AssertionError e) {
- onView(withId(R.id.btnLater)).perform(click());
- }
- takeScreenshot("wishes-cards-from-start");
- boolean i = true;
- while (i) {
- try {
- onView(withId(R.id.btnHide)).perform(click());
- } catch (NoMatchingViewException e) {
- takeScreenshot("wishes_ended");
- onView(withId(R.id.btnWatchFromStart)).perform(click());
- i = false;
- }
- }
- takeScreenshot("wishes-cards-from-start");
- }
- @Test
- public void test04Teleport() {
- this.login("testmale@gmail.com", "12345678");
- takeScreenshot("wishes-cards");
- try {
- takeScreenshot("teleport");
- onView(withId(R.id.btnLater)).perform(click());
- onView(withId(R.id.btnLocation)).perform(click());
- onView(withText("My location")).perform(click());
- } catch (NoMatchingViewException e) {
- onView(withId(R.id.btnLocation)).perform(click());
- onView(withText("My location")).perform(click());
- }
- boolean i = true;
- while (i) {
- try {
- onView(withId(R.id.btnHide)).perform(click());
- } catch (NoMatchingViewException e) {
- takeScreenshot("wishes-ended");
- onView(withId(R.id.btnCheckOtherCity)).perform(click());
- takeScreenshot("teleport");
- onView(withText("Moscow")).perform(click());
- takeScreenshot("wishes-cards-moscow");
- while(i) {
- try {
- onView(withId(R.id.btnHide)).perform(click());
- } catch (NoMatchingViewException a) {
- takeScreenshot("wishes-ended-moscow");
- onView(withId(R.id.btnCheckOtherCity)).perform(click());
- takeScreenshot("teleport");
- onView(withText("My location")).perform(click());
- i = false;
- }
- }
- }
- }
- }
- @Test
- public void test05Request() {
- test01PostWish();
- SystemClock.sleep(5000);
- onView(allOf(withParent(withId(R.id.toolbar)), is(instanceOf(ImageButton.class)))).perform(click());
- onView(withText(R.string.nav_requests)).perform(click());
- onView(withId(R.id.btnChat)).perform(click());
- takeScreenshot("empty-offers");
- onView(withText(R.string.nav_chats)).perform(click());
- takeScreenshot("empty-chats");
- logout();
- this.login("testmale@gmail.com", "12345678");
- SystemClock.sleep(5000);
- boolean i = true;
- while (i) {
- try {
- onView(withId(R.id.tvWish)).check(matches(withText("Female wish Two Lines Female wish Two Lines")));
- onView(withId(R.id.btnPerform)).perform(click());
- i = false;
- } catch (AssertionError e) {
- onView(withId(R.id.btnHide)).perform(click());
- }
- }
- logout();
- this.login("testfemale@gmail.com", "12345678");
- onView(withId(R.id.btnChat)).perform(click());
- //onView(withId(R.id.tvName)).check(matches(withText("TestMale")));
- onView(withText("TestMale")).perform(click());
- onView(withText(R.string.btn_chat)).perform(click());
- }
- private Activity getCurrentActivity() throws Throwable {
- this.getInstrumentation().waitForIdleSync();
- final Activity[] activity = new Activity[1];
- this.runTestOnUiThread(new Runnable() {
- @Override
- public void run() {
- activity[0] = ActivityLifecycleMonitorRegistry.getInstance()
- .getActivitiesInStage(Stage.RESUMED).iterator().next();
- }
- });
- return activity[0];
- }
- public void takeScreenshot(String name) {
- try {
- Spoon.screenshot(this.getCurrentActivity(), name);
- } catch (Throwable e) {
- Log.e("UITest", "screenshot exception", e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement