Last active
August 29, 2015 14:20
-
-
Save swanhtet1992/1937518f9005ca8afae6 to your computer and use it in GitHub Desktop.
Simple UI test class to demonstrate screenshot automation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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