From 33a2805ca1534159eba4abe8260940f2ba2e2f61 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Mon, 25 Jul 2022 13:56:44 +0300 Subject: [PATCH] Refactor swipeToAvoidGrayOverlay to ignore failures and become reusable --- .../ui/screenshots/JPScreenshotTest.java | 16 ++---------- .../ui/screenshots/WPScreenshotTest.java | 20 +++------------ .../wordpress/android/util/UiTestingUtils.kt | 25 +++++++++++++++++++ 3 files changed, 30 insertions(+), 31 deletions(-) create mode 100644 WordPress/src/androidTest/java/org/wordpress/android/util/UiTestingUtils.kt diff --git a/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/JPScreenshotTest.java b/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/JPScreenshotTest.java index e00d5f420a02..0aa2e39fcbea 100644 --- a/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/JPScreenshotTest.java +++ b/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/JPScreenshotTest.java @@ -19,6 +19,7 @@ import org.wordpress.android.support.BaseTest; import org.wordpress.android.support.DemoModeEnabler; import org.wordpress.android.ui.WPLaunchActivity; +import org.wordpress.android.util.UiTestingUtils; import org.wordpress.android.util.image.ImageType; import java.util.Locale; @@ -46,8 +47,6 @@ import static org.wordpress.android.support.WPSupportUtils.isTextDisplayed; import static org.wordpress.android.support.WPSupportUtils.pressBackUntilElementIsDisplayed; import static org.wordpress.android.support.WPSupportUtils.setNightMode; -import static org.wordpress.android.support.WPSupportUtils.swipeLeftOnViewPager; -import static org.wordpress.android.support.WPSupportUtils.swipeRightOnViewPager; import static org.wordpress.android.support.WPSupportUtils.waitForAtLeastOneElementWithIdToBeDisplayed; import static org.wordpress.android.support.WPSupportUtils.waitForElementToBeDisplayed; import static org.wordpress.android.support.WPSupportUtils.waitForElementToBeDisplayedWithoutFailure; @@ -221,7 +220,7 @@ private void generateStats() { clickOn(R.id.nav_sites); (new MySitesPage()).goToStats().dismissUpdateAlertDialogFragmentIfDisplayed(); - swipeToAvoidGrayOverlay(R.id.statsPager); + UiTestingUtils.swipeToAvoidGrayOverlayIgnoringFailures(R.id.statsPager); if (isElementDisplayed(R.id.button_negative)) { clickOn(R.id.button_negative); @@ -428,17 +427,6 @@ private void screenshotPostWithName(String name, pressBackUntilElementIsDisplayed(R.id.tabLayout); } - // In some cases there's a gray overlay on view pager screens when taking screenshots - // this function swipes left and then right as a workaround to clear it - // resourceID should be the ID of the viewPager - private void swipeToAvoidGrayOverlay(int resourceID) { - // Workaround to avoid gray overlay - swipeLeftOnViewPager(resourceID); - idleFor(1000); - swipeRightOnViewPager(resourceID); - idleFor(1000); - } - private void setNightModeAndWait(boolean isNightMode) { setNightMode(isNightMode); idleFor(5000); diff --git a/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPScreenshotTest.java b/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPScreenshotTest.java index a8a53c414888..f4f9be3a6405 100644 --- a/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPScreenshotTest.java +++ b/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPScreenshotTest.java @@ -18,6 +18,7 @@ import org.wordpress.android.support.DemoModeEnabler; import org.wordpress.android.ui.WPLaunchActivity; import org.wordpress.android.ui.posts.EditPostActivity; +import org.wordpress.android.util.UiTestingUtils; import org.wordpress.android.util.image.ImageType; import static org.wordpress.android.support.WPSupportUtils.clickOn; @@ -29,8 +30,6 @@ import static org.wordpress.android.support.WPSupportUtils.pressBackUntilElementIsDisplayed; import static org.wordpress.android.support.WPSupportUtils.setNightMode; import static org.wordpress.android.support.WPSupportUtils.swipeDownOnView; -import static org.wordpress.android.support.WPSupportUtils.swipeLeftOnViewPager; -import static org.wordpress.android.support.WPSupportUtils.swipeRightOnViewPager; import static org.wordpress.android.support.WPSupportUtils.swipeUpOnView; import static org.wordpress.android.support.WPSupportUtils.waitForAtLeastOneElementWithIdToBeDisplayed; import static org.wordpress.android.support.WPSupportUtils.waitForElementToBeDisplayed; @@ -152,8 +151,7 @@ private void navigateDiscover() { // Workaround to avoid gray overlay try { - swipeToAvoidGrayOverlay(R.id.view_pager); - + UiTestingUtils.swipeToAvoidGrayOverlayIgnoringFailures(R.id.view_pager); if (isTabletScreen()) { swipeDownOnView(R.id.view_pager, (float) 0.5); idleFor(1000); @@ -178,7 +176,7 @@ private void navigateStats() { clickOn(R.id.nav_sites); (new MySitesPage()).goToStats(); - swipeToAvoidGrayOverlay(R.id.statsPager); + UiTestingUtils.swipeToAvoidGrayOverlayIgnoringFailures(R.id.statsPager); if (isElementDisplayed(R.id.button_negative)) { clickOn(R.id.button_negative); @@ -261,18 +259,6 @@ private boolean runningInTestLab() { return "true".equals(testLabSetting); } - - // In some cases there's a gray overlay on view pager screens when taking screenshots - // this function swipes left and then right as a workaround to clear it - // resourceID should be the ID of the viewPager - private void swipeToAvoidGrayOverlay(int resourceID) { - // Workaround to avoid gray overlay - swipeLeftOnViewPager(resourceID); - idleFor(1000); - swipeRightOnViewPager(resourceID); - idleFor(1000); - } - private boolean editPostActivityIsNoLongerLoadingImages() { EditPostActivity editPostActivity = (EditPostActivity) getCurrentActivity(); return editPostActivity.getAztecImageLoader().getNumberOfImagesBeingDownloaded() == 0; diff --git a/WordPress/src/androidTest/java/org/wordpress/android/util/UiTestingUtils.kt b/WordPress/src/androidTest/java/org/wordpress/android/util/UiTestingUtils.kt new file mode 100644 index 000000000000..5fa652b68658 --- /dev/null +++ b/WordPress/src/androidTest/java/org/wordpress/android/util/UiTestingUtils.kt @@ -0,0 +1,25 @@ +package org.wordpress.android.util + +import org.wordpress.android.support.WPSupportUtils + +object UiTestingUtils { + /** + * Workaround to avoid gray overlay. + * In some cases there's a gray overlay on view pager screens when taking screenshots. + * This function swipes left and then right as a workaround to clear it. + * @param resourceID: Int the ID of the viewPager + */ + @JvmStatic + fun swipeToAvoidGrayOverlayIgnoringFailures(resourceID: Int) { + try { + WPSupportUtils.swipeLeftOnViewPager(resourceID) + WPSupportUtils.idleFor(1000) + WPSupportUtils.swipeRightOnViewPager(resourceID) + WPSupportUtils.idleFor(1000) + } catch (e: Throwable) { + // Fail softly + @Suppress("PrintStackTrace") + e.printStackTrace() + } + } +}