Advertisement
romkazem

Untitled

Feb 29th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.75 KB | None | 0 0
  1. package me.wayd.prince;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.Bitmap;
  5. import android.os.SystemClock;
  6. import android.support.test.InstrumentationRegistry;
  7. import android.support.test.espresso.Espresso;
  8. import android.support.test.espresso.NoMatchingViewException;
  9. import android.support.test.espresso.PerformException;
  10. import android.support.test.espresso.web.webdriver.Locator;
  11. import android.support.test.runner.AndroidJUnit4;
  12. import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
  13. import android.support.test.runner.lifecycle.Stage;
  14. import android.test.ActivityInstrumentationTestCase2;
  15. import android.test.suitebuilder.annotation.LargeTest;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.widget.ImageButton;
  19.  
  20. import com.squareup.okhttp.OkHttpClient;
  21. import com.squareup.spoon.Spoon;
  22.  
  23. import org.junit.Before;
  24. import org.junit.FixMethodOrder;
  25. import org.junit.Test;
  26. import org.junit.runner.RunWith;
  27. import org.junit.runners.MethodSorters;
  28.  
  29. import java.io.File;
  30. import java.io.FileNotFoundException;
  31. import java.io.FileOutputStream;
  32. import java.io.IOException;
  33. import java.io.OutputStream;
  34.  
  35. import me.wayd.prince.screens.auth.AuthActivity;
  36.  
  37. import static android.support.test.espresso.Espresso.onView;
  38. import static android.support.test.espresso.action.ViewActions.clearText;
  39. import static android.support.test.espresso.action.ViewActions.click;
  40. import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
  41. import static android.support.test.espresso.action.ViewActions.scrollTo;
  42. import static android.support.test.espresso.action.ViewActions.typeTextIntoFocusedView;
  43. import static android.support.test.espresso.assertion.ViewAssertions.matches;
  44. import static android.support.test.espresso.matcher.ViewMatchers.withId;
  45. import static android.support.test.espresso.matcher.ViewMatchers.withParent;
  46. import static android.support.test.espresso.matcher.ViewMatchers.withText;
  47. import static android.support.test.espresso.web.sugar.Web.onWebView;
  48. import static android.support.test.espresso.web.webdriver.DriverAtoms.findElement;
  49. import static android.support.test.espresso.web.webdriver.DriverAtoms.webClick;
  50. import static android.support.test.espresso.web.webdriver.DriverAtoms.webKeys;
  51. import static android.support.test.espresso.action.ViewActions.typeText;
  52. import static me.wayd.prince.R.string.btn_make_wish;
  53. import static org.hamcrest.Matchers.instanceOf;
  54. import static org.hamcrest.Matchers.allOf;
  55. import static org.hamcrest.Matchers.is;
  56.  
  57. @RunWith(AndroidJUnit4.class)
  58. @LargeTest
  59. @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  60. public class UITests extends ActivityInstrumentationTestCase2 {
  61.  
  62. public UITests() {
  63. super(AuthActivity.class);
  64. }
  65.  
  66. @Before
  67. public void setUp() throws Exception {
  68. super.setUp();
  69. injectInstrumentation(InstrumentationRegistry.getInstrumentation());
  70. getActivity();
  71.  
  72. //PrinceApplication app = (PrinceApplication)getActivity().getApplication()
  73. // Espresso.registerIdlingResources(new OkHttpIdlingResource(PrinceApplication.getOkHttpClient()));
  74. }
  75.  
  76. private void emailLogin(String email, String password, Boolean screenshot) {
  77. if (screenshot) takeScreenshot("login");
  78. onView(withId(R.id.btnEmailLogin)).perform(click());
  79.  
  80. if (screenshot) takeScreenshot("email-login");
  81. onView(withId(R.id.etEmail)).perform(typeText(email));
  82. onView(withId(R.id.etPassword)).perform(typeText(password), closeSoftKeyboard());
  83. onView(withId(R.id.btnLogin)).perform(click());
  84. }
  85.  
  86. private void login(String email, String password) {
  87. this.emailLogin(email, password, false);
  88. }
  89.  
  90. private void logout() {
  91. boolean i = true;
  92. while (i) {
  93. try {
  94. onView(allOf(withParent(withId(R.id.toolbar)), is(instanceOf(ImageButton.class)))).perform(click());
  95. onView(withText(R.string.nav_exit)).perform(click());
  96. i = false;
  97. } catch (PerformException e) {
  98. onView(allOf(withParent(withId(R.id.toolbar)), is(instanceOf(ImageButton.class)))).perform(click());
  99. onView(withText(R.string.nav_exit)).perform(click());
  100. i = false;
  101. }
  102. }
  103.  
  104. }
  105.  
  106. private void VKLogin() {
  107. takeScreenshot("login");
  108. onView(withId(R.id.btnVkLogin)).perform(click());
  109. takeScreenshot("vk_login");
  110. try {
  111. onWebView().withElement(findElement(Locator.NAME, "email")).perform(webKeys(""));
  112. } catch (RuntimeException e) {
  113. onWebView().withElement(findElement(Locator.CLASS_NAME, "mh_btn_label")).perform(webClick());
  114. }
  115.  
  116. onWebView().withElement(findElement(Locator.NAME, "email")).perform(webKeys("+79046442128"));
  117. onWebView().withElement(findElement(Locator.NAME, "pass")).perform(webKeys("qweasd123"));
  118. SystemClock.sleep(5000);
  119. onWebView().withElement(findElement(Locator.CLASS_NAME, "button")).perform(webClick());
  120. SystemClock.sleep(5000);
  121. onWebView().withElement(findElement(Locator.CLASS_NAME, "button")).perform(webClick());
  122. }
  123.  
  124. @Test
  125. public void test01PostWish() {
  126. this.emailLogin("testfemale@gmail.com", "12345678", true);
  127. onView(allOf(withParent(withId(R.id.toolbar)), is(instanceOf(ImageButton.class)))).perform(click());
  128. takeScreenshot("female-navigation-drawer");
  129. onView(withText(R.string.nav_profile)).perform(click());
  130.  
  131. try {
  132. Log.e("retrofit", "try find");
  133. onView(withId(R.id.btnChangeMake)).check(matches(withText(btn_make_wish)));
  134. } catch (AssertionError e) {
  135. Log.e("retrofit", "click cancel");
  136. onView(withId(R.id.btnChangeMake)).perform(click()); //remove wish
  137. } finally {
  138. Log.e("retrofit", "click perform");
  139. takeScreenshot("female-profile-no-wish");
  140. onView(withId(R.id.btnChangeMake)).perform(click());
  141. }
  142. takeScreenshot("female-new-wish-suggestion");
  143. Log.e("retrofit", "gonna push");
  144. onView(withId(R.id.action_refresh)).perform(click());
  145. onView(withId(R.id.action_refresh)).perform(click());
  146.  
  147. onView(withId(R.id.etOfferWish)).perform(click());
  148. onView(withId(R.id.action_refresh)).perform(click());
  149. onView(withId(R.id.action_refresh)).perform(click());
  150. takeScreenshot("female-new-wish-suggestion-selected");
  151.  
  152. onView(withId(R.id.etOfferWish)).perform(clearText());
  153. onView(withId(R.id.etOfferWish)).perform(typeTextIntoFocusedView("Female wish Two Lines Female wish Two Lines"));
  154. takeScreenshot("female-new-wish-filled");
  155. onView(withId(R.id.btnDone)).perform(click());
  156. takeScreenshot("female-profile-with-wish");
  157. }
  158.  
  159. @Test
  160. public void test02HideWish() {
  161. this.login("testfemale@gmail.com", "12345678");
  162.  
  163. try {
  164. onView(withId(R.id.btnLater)).perform(click());
  165. } catch (NoMatchingViewException e) {
  166. try {
  167. onView(withId(R.id.btnHide)).perform(click());
  168. } catch (NoMatchingViewException a) {
  169. takeScreenshot("No-wishes");
  170. onView(withId(R.id.btnWatchFromStart)).perform(click());
  171. }
  172. }
  173. }
  174.  
  175. @Test
  176. public void test03LookAgain() {
  177. this.login("testfemale@gmail.com", "12345678");
  178.  
  179. try {
  180. onView(withId(R.id.btnPerform)).check(matches(withText(R.string.btn_perform_female)));
  181. } catch (AssertionError e) {
  182. onView(withId(R.id.btnLater)).perform(click());
  183. }
  184.  
  185. takeScreenshot("wishes-cards-from-start");
  186. boolean i = true;
  187. while (i) {
  188. try {
  189. onView(withId(R.id.btnHide)).perform(click());
  190. } catch (NoMatchingViewException e) {
  191. takeScreenshot("wishes_ended");
  192. onView(withId(R.id.btnWatchFromStart)).perform(click());
  193. i = false;
  194. }
  195. }
  196. takeScreenshot("wishes-cards-from-start");
  197. }
  198.  
  199. @Test
  200. public void test04Teleport() {
  201. this.login("testmale@gmail.com", "12345678");
  202.  
  203. takeScreenshot("wishes-cards");
  204.  
  205. try {
  206. takeScreenshot("teleport");
  207. onView(withId(R.id.btnLater)).perform(click());
  208. onView(withId(R.id.btnLocation)).perform(click());
  209. onView(withText("My location")).perform(click());
  210. } catch (NoMatchingViewException e) {
  211. onView(withId(R.id.btnLocation)).perform(click());
  212. onView(withText("My location")).perform(click());
  213. }
  214.  
  215. boolean i = true;
  216. while (i) {
  217. try {
  218. onView(withId(R.id.btnHide)).perform(click());
  219. } catch (NoMatchingViewException e) {
  220. takeScreenshot("wishes-ended");
  221. onView(withId(R.id.btnCheckOtherCity)).perform(click());
  222. takeScreenshot("teleport");
  223. onView(withText("Moscow")).perform(click());
  224. takeScreenshot("wishes-cards-moscow");
  225. while(i) {
  226. try {
  227. onView(withId(R.id.btnHide)).perform(click());
  228. } catch (NoMatchingViewException a) {
  229. takeScreenshot("wishes-ended-moscow");
  230. onView(withId(R.id.btnCheckOtherCity)).perform(click());
  231. takeScreenshot("teleport");
  232. onView(withText("My location")).perform(click());
  233. i = false;
  234. }
  235. }
  236. }
  237. }
  238. }
  239.  
  240. @Test
  241. public void test05Request() {
  242. test01PostWish();
  243. SystemClock.sleep(5000);
  244. onView(allOf(withParent(withId(R.id.toolbar)), is(instanceOf(ImageButton.class)))).perform(click());
  245. onView(withText(R.string.nav_requests)).perform(click());
  246. onView(withId(R.id.btnChat)).perform(click());
  247. takeScreenshot("empty-offers");
  248. onView(withText(R.string.nav_chats)).perform(click());
  249. takeScreenshot("empty-chats");
  250.  
  251.  
  252.  
  253.  
  254. logout();
  255. this.login("testmale@gmail.com", "12345678");
  256. SystemClock.sleep(5000);
  257.  
  258. boolean i = true;
  259. while (i) {
  260. try {
  261. onView(withId(R.id.tvWish)).check(matches(withText("Female wish Two Lines Female wish Two Lines")));
  262. onView(withId(R.id.btnPerform)).perform(click());
  263. i = false;
  264. } catch (AssertionError e) {
  265. onView(withId(R.id.btnHide)).perform(click());
  266. }
  267. }
  268.  
  269. logout();
  270.  
  271. this.login("testfemale@gmail.com", "12345678");
  272. onView(withId(R.id.btnChat)).perform(click());
  273. //onView(withId(R.id.tvName)).check(matches(withText("TestMale")));
  274. onView(withText("TestMale")).perform(click());
  275. onView(withText(R.string.btn_chat)).perform(click());
  276.  
  277.  
  278.  
  279.  
  280.  
  281. }
  282.  
  283. private Activity getCurrentActivity() throws Throwable {
  284. this.getInstrumentation().waitForIdleSync();
  285. final Activity[] activity = new Activity[1];
  286. this.runTestOnUiThread(new Runnable() {
  287. @Override
  288. public void run() {
  289. activity[0] = ActivityLifecycleMonitorRegistry.getInstance()
  290. .getActivitiesInStage(Stage.RESUMED).iterator().next();
  291. }
  292. });
  293. return activity[0];
  294. }
  295.  
  296. public void takeScreenshot(String name) {
  297. try {
  298. Spoon.screenshot(this.getCurrentActivity(), name);
  299. } catch (Throwable e) {
  300. Log.e("UITest", "screenshot exception", e);
  301. }
  302. }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement