Skip to content

Instantly share code, notes, and snippets.

@thalespupo
Last active May 9, 2019 18:36
Show Gist options
  • Save thalespupo/74e44ef2d7b66bd70990aa41c7817a77 to your computer and use it in GitHub Desktop.
Save thalespupo/74e44ef2d7b66bd70990aa41c7817a77 to your computer and use it in GitHub Desktop.
AAA + TestingRobot + Kotlin DSL
package com.tapura.podmorecasts
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.action.ViewActions.pressImeActionButton
import android.support.test.espresso.matcher.ViewMatchers.withId
class ActRobot {
fun clickOkInSearchBar() {
onView(withId(android.support.design.R.id.search_src_text)).perform().perform(pressImeActionButton())
}
fun wait01(i: Long) {
Thread.sleep(i)
}
}
inline fun action(block: ActRobot.() -> Unit)= ActRobot().apply(block)
package com.tapura.podmorecasts
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class AndroidKotlinTest {
@get:Rule
var rule = ActivityTestRule(SplashActivity::class.java)
@Test
@Throws(InterruptedException::class)
fun whenSearching_shouldShowResultsCorrectlyTestRobot() {
arrange {
clickInSearchBar()
typeInSearchBar("Nerdcast")
}
action {
clickOkInSearchBar()
wait01(3000L)
}
assert {
assertListGreaterThan(0)
}
}
@Test
@Throws(InterruptedException::class)
fun whenSearching_shouldShowResultsCorrectlyTestRobotJake() {
mainScreen {
clickInSearchBox()
typeInSearchBox("Nerdcast")
clickOkInSearchBox()
wait(3000)
}
SearchableListScreenRobot()
.assertListGreaterThan(0)
}
}
inline fun mainScreen(block: MainScreenRobot.() -> Unit) = MainScreenRobot().apply(block)
package com.tapura.podmorecasts
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.action.ViewActions.click
import android.support.test.espresso.action.ViewActions.typeText
import android.support.test.espresso.matcher.ViewMatchers.withId
class ArrangeRobot {
fun clickInSearchBar() {
onView(withId(R.id.action_search)).perform(click())
}
fun typeInSearchBar(text: String) {
onView(withId(android.support.design.R.id.search_src_text)).perform(typeText(text))
}
}
inline fun arrange(block: ArrangeRobot.() -> Unit) = ArrangeRobot().apply(block)
package com.tapura.podmorecasts
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.matcher.ViewMatchers.withId
import org.hamcrest.Matchers.greaterThan
class AssertRobot {
fun assertListGreaterThan(i: Int) {
onView(withId(R.id.recycler_view_podcasts_discover_list)).check(RecyclerViewItemCountAssertion(greaterThan<Int>(0)))
}
}
inline fun assert(block: AssertRobot.() -> Unit)= AssertRobot().apply(block)
package com.tapura.podmorecasts;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.pressImeActionButton;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertEquals;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Rule
public ActivityTestRule<SplashActivity> rule = new ActivityTestRule<>(SplashActivity.class);
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.tapura.podmorecasts", appContext.getPackageName());
}
@Test
public void whenSearching_shouldShowResultsCorrectly() throws InterruptedException {
// Arrange
onView(withId(R.id.action_search)).perform(click());
onView(withId(android.support.design.R.id.search_src_text)).perform(typeText("Nerdcast"));
//Action
onView(withId(android.support.design.R.id.search_src_text)).perform().perform(pressImeActionButton());
Thread.sleep(3000);
//Assert
onView(withId(R.id.recycler_view_podcasts_discover_list)).check(new RecyclerViewItemCountAssertion(greaterThan(0)));
}
@Test
public void whenSearching_shouldShowResultsCorrectlyTestRobot() throws InterruptedException {
new MainScreenRobot()
.clickInSearchBox()
.typeInSearchBox("Nerdcast")
.clickOkInSearchBox()
.wait(3000);
new SearchableListScrfeenRobot()
.assertListGreaterThan(0);
}
}
package com.tapura.podmorecasts;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.pressImeActionButton;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
public class MainScreenRobot {
public MainScreenRobot clickInSearchBox() {
onView(withId(R.id.action_search)).perform(click());
return this;
}
public MainScreenRobot typeInSearchBox(String text) {
onView(withId(android.support.design.R.id.search_src_text)).perform(typeText(text));
return this;
}
public MainScreenRobot clickOkInSearchBox() {
onView(withId(android.support.design.R.id.search_src_text)).perform().perform(pressImeActionButton());
return this;
}
public MainScreenRobot wait(int i) {
try {
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
return this;
}
}
package com.tapura.podmorecasts;
import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.ViewAssertion;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import org.hamcrest.Matcher;
import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
import static org.hamcrest.Matchers.is;
public class RecyclerViewItemCountAssertion implements ViewAssertion {
private final Matcher<Integer> matcher;
public RecyclerViewItemCountAssertion(int expectedCount) {
this.matcher = is(expectedCount);
}
public RecyclerViewItemCountAssertion(Matcher<Integer> matcher) {
this.matcher = matcher;
}
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
if (noViewFoundException != null) {
throw noViewFoundException;
}
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.Adapter adapter = recyclerView.getAdapter();
assertThat(adapter.getItemCount(), matcher);
}
}
package com.tapura.podmorecasts;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.Matchers.greaterThan;
public class SearchableListScreenRobot {
public SearchableListScreenRobot assertListGreaterThan(int i) {
onView(withId(R.id.recycler_view_podcasts_discover_list)).check(new RecyclerViewItemCountAssertion(greaterThan(i)));
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment