Created
February 11, 2021 00:24
-
-
Save FanchenBao/eb9486654c2a92a0d19262dc2c38e491 to your computer and use it in GitHub Desktop.
Source code 01 used in medium article "Make Fastlane Screengrab Work with React Native"
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
<!--For fastlane screengrab use--> | |
package com.your.app; | |
import androidx.test.rule.ActivityTestRule; | |
import org.junit.AfterClass; | |
import org.junit.BeforeClass; | |
import org.junit.ClassRule; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.JUnit4; | |
import tools.fastlane.screengrab.Screengrab; | |
import tools.fastlane.screengrab.UiAutomatorScreenshotStrategy; | |
import tools.fastlane.screengrab.cleanstatusbar.BluetoothState; | |
import tools.fastlane.screengrab.cleanstatusbar.CleanStatusBar; | |
import tools.fastlane.screengrab.cleanstatusbar.MobileDataType; | |
import tools.fastlane.screengrab.locale.LocaleTestRule; | |
import androidx.test.espresso.NoMatchingViewException; | |
import static androidx.test.espresso.Espresso.onView; | |
import static androidx.test.espresso.action.ViewActions.click; | |
import static androidx.test.espresso.assertion.ViewAssertions.matches; | |
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription; | |
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; | |
import static androidx.test.espresso.matcher.ViewMatchers.withId; | |
import static androidx.test.espresso.matcher.ViewMatchers.withText; | |
import static org.hamcrest.core.AllOf.allOf; | |
@RunWith(JUnit4.class) | |
public class ScreenshotTest { | |
@ClassRule | |
public static final LocaleTestRule localeTestRule = new LocaleTestRule(); | |
@Rule | |
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class); | |
@BeforeClass | |
public static void beforeAll() { | |
Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy()); | |
CleanStatusBar.enableWithDefaults(); | |
} | |
@AfterClass | |
public static void afterAll() { | |
CleanStatusBar.disable(); | |
} | |
/* | |
Custom wait function. In order to make sure each button press yields a | |
desirable screen, we use the wait function to delay further actions until | |
the current one has achieved its purpose. | |
`duration` indicates the amount of milli-seconds to wait. The value of | |
`duration` is acquired by emperical trial-and-error. | |
*/ | |
public void wait(int duration) { | |
try { | |
Thread.sleep(duration); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
/* | |
Custom function to press a button. | |
The function contains two logic. First, it checks whether the button, | |
identified by a string `id`, exists. If the button does not exist, | |
which is most likely due to the slow speed of rendering on an old machine, | |
we will wait for one second and try again. Since we are sure that the | |
button exists, this retry will continue indefinitely until the target | |
button is available. | |
The second logic is to repeat the number of times the button is pressed by | |
the param `repeatTimes`. Since some button press involves API query, and | |
one call might not be successful, we incorporate `repeatTimes` to indicate | |
how many times a button should be pressed to ensure that at least one of | |
those presses yields successful API response. The value of `repeatTImes` is | |
acquired via emperical trial-and-error. | |
*/ | |
public void buttonPress(String id, int repeatTimes) { | |
while (true) { | |
try { | |
onView(allOf(withContentDescription(id))).perform(click()); | |
if (repeatTimes > 1) { | |
wait(4000); | |
repeatTimes -= 1; | |
continue; | |
} | |
break; | |
} catch (NoMatchingViewException e) { | |
e.printStackTrace(); | |
wait(1000); | |
continue; | |
} | |
} | |
} | |
@Test | |
public void testTakeScreenshot() { | |
Screengrab.screenshot("01Before_API_Call"); | |
buttonPress("some_api_query_button", 3); | |
wait(3000); | |
Screengrab.screenshot("02After_API_Call"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment