Skip to content

Instantly share code, notes, and snippets.

@swanhtet1992
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swanhtet1992/1937518f9005ca8afae6 to your computer and use it in GitHub Desktop.
Save swanhtet1992/1937518f9005ca8afae6 to your computer and use it in GitHub Desktop.
Simple UI test class to demonstrate screenshot automation
public class SimpleUiTest extends InstrumentationTestCase {
private UiDevice mDevice;
private String PACKAGE_NAME = "co.example.uitest";
public void setUp() {
// Initialize UiDevice instance
mDevice = UiDevice.getInstance(getInstrumentation());
// Let's start from Home
mDevice.pressHome();
}
private void openApp() {
// Launch the app
Context context = getInstrumentation().getContext();
Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // Clear out any previous instances
context.startActivity(intent);
}
public void testFeedList() throws Exception {
openApp();
waitABit();
takeScreenshot("category_list");
}
public void testButtonClick() throws Exception {
openApp();
UiScrollable recyclerView = getRecyclerView();
UiObject object = recyclerView.getChild(getObject("tv_more"));
object.click();
waitABit();
takeScreenshot("product_list");
}
private void waitABit() {
SystemClock.sleep(10000);
}
private void takeScreenshot(String name) {
File dir =
new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"app_screenshots");
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.d("Screenshot Test", "Oops! Failed create directory");
}
}
File file = new File(dir.getPath() + File.separator + name + ".jpg");
mDevice.takeScreenshot(file);
}
private UiScrollable getRecyclerView() {
return new UiScrollable(new UiSelector().resourceId(PACKAGE_NAME + ":id/recycler_root"));
}
private UiSelector getObject(String name) {
return new UiSelector().resourceId(PACKAGE_NAME + ":id/" + name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment