Fastlane Screengrab - 不要在屏幕截图文件名中包含时间戳

Fastlane Screengrab - don't include timestamp in screenshot file names

背景

我正在使用 Fastlane's screengrab 自动截取我的 Android 应用程序的屏幕截图。但是,Screengrab.screenshot() 的默认行为会在文件名中添加时间戳。这是不可取的,甚至是出乎意料的。

This issue on Github 建议扩展 ScreenshotCallback 以从文件名中删除时间戳。我发现默认行为是在 FileWritingScreenshotCallback 中实现的,所以决定扩展它并覆盖 getScreenshotFile():

class ScreenshotCallback extends FileWritingScreenshotCallback {
    public ScreenshotCallback(Context appContext) {
        super(appContext);
    }

    @Override
    protected File getScreenshotFile(File screenshotDirectory, String screenshotName) {
        return new File(screenshotDirectory, screenshotName);
    }
}

然后我的 JUnit 测试截图如下所示:

代码

main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
>

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    />

    <!-- Other stuff -->
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
}

Screenshots.java

public class Screenshots {
    @Rule
    public ActivityScenarioRule<LiteActivity> activityRule = new ActivityScenarioRule<>(
        MainActivity.class
    );

    private ScreenshotCallback screenshotCallback;
    private UiAutomatorScreenshotStrategy screenshotStrategy;

    @Before
    public void setUp() throws UiObjectNotFoundException {
        Instrumentation inst = InstrumentationRegistry.getInstrumentation();
        Context context = inst.getTargetContext();
        screenshotCallback = new ScreenshotCallback(context);
        screenshotStrategy = new UiAutomatorScreenshotStrategy();

        ActivityScenario<LiteActivity> scenario = activityRule.getScenario();
        Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy());
    }

    @Test
    public void takeScreenshot() {
        onView(withId(R.id.toolbar)).check(matches(isDisplayed()));
        Screengrab.screenshot("MainScreen", screenshotStrategy, screenshotCallback);
    }
}

当我 运行 使用 bundle exec fastlane screengrab 时,我得到以下输出:

[22:31:52]: $ adb.exe -s emulator-5554 pull /sdcard/com.example/screengrab/en-US/images/screenshots C:/Users/user-name/AppData/Local/Temp/d20210808-12584-jlogjl
ls: /sdcard/Android/data/com.example/files/screengrab/en-US/images/screenshots: No such file or directory
[22:31:53]: Exit status: 1
ls: /data/user/0/com.example/files/com.example/screengrab/en-US/images/screenshots: No such file or directory
[22:31:53]: Exit status: 1
ls: /data/data/com.example/files/com.example/screengrab/en-US/images/screenshots: No such file or directory
[22:31:53]: Exit status: 1
ls: /data/data/com.example/app_screengrab/en-US/images/screenshots: No such file or directory
[22:31:53]: Exit status: 1
ls: /data/data/com.example/screengrab/en-US/images/screenshots: No such file or directory
[22:31:53]: Exit status: 1
[22:31:53]: Screenshots copied to fastlane/metadata/android/en-US/images/phoneScreenshots
[22:31:53]: Make sure you've used Screengrab.screenshot() in your tests and that your expected tests are being run.

为什么我的 none 屏幕截图在设备上?我在 logcat 中没有看到任何明显的错误。如果我使用 Screengrab.screenshot("MainScreen"); 而不是 Screengrab.screenshot("MainScreen", screenshotStrategy, screenshotCallback);,屏幕截图文件就可以很好地复制到我的开发文件系统中。我可以做些什么来进一步调试它?

支持截屏

您必须找到带有配置和设置字段的 Screengrabfile

use_timestamp_suffix: false

Documentation

Merged pull request with this changes