Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Test] Added UI test for the new stats traffic tab. #20422

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.wordpress.android.e2e

import androidx.test.espresso.Espresso
import androidx.test.espresso.matcher.ViewMatchers
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.After
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Test
import org.wordpress.android.BuildConfig
import org.wordpress.android.R
import org.wordpress.android.e2e.pages.MySitesPage
import org.wordpress.android.support.BaseTest
import org.wordpress.android.support.ComposeEspressoLink
import org.wordpress.android.support.WPSupportUtils
import org.wordpress.android.util.StatsKeyValueData
import org.wordpress.android.util.StatsMocksReader
import org.wordpress.android.util.StatsVisitsData

@HiltAndroidTest
class StatsGranularTabsTest : BaseTest() {
@Before
fun setUp() {
assumeTrue(BuildConfig.IS_JETPACK_APP)
ComposeEspressoLink().unregister()
logoutIfNecessary()
wpLogin()
}

@After
fun tearDown() {
// "tabLayout" is a Tab switcher for stats.
// We need to leave stats at the end of test.
if (WPSupportUtils.isElementDisplayed(Espresso.onView(ViewMatchers.withId(R.id.tabLayout)))) {
Espresso.pressBack()
}
}

@Test
fun e2eAllDayStatsLoad() {
val todayVisits = StatsVisitsData("97", "28", "14", "11")
val postsList: List<StatsKeyValueData> = StatsMocksReader().readDayTopPostsToList()
val referrersList: List<StatsKeyValueData> = StatsMocksReader().readDayTopReferrersToList()
val clicksList: List<StatsKeyValueData> = StatsMocksReader().readDayClicksToList()
val authorsList: List<StatsKeyValueData> = StatsMocksReader().readDayAuthorsToList()
val countriesList: List<StatsKeyValueData> = StatsMocksReader().readDayCountriesToList()
val videosList: List<StatsKeyValueData> = StatsMocksReader().readDayVideoPlaysToList()
val downloadsList: List<StatsKeyValueData> = StatsMocksReader().readDayFileDownloadsToList()
MySitesPage()
.go()
.goToStats()
.openDayStats()
.assertVisits(todayVisits)
.scrollToPosts().assertPosts(postsList)
.scrollToReferrers().assertReferrers(referrersList)
.scrollToClicks().assertClicks(clicksList)
.scrollToAuthors().assertAuthors(authorsList)
.scrollToCountries().assertCountries(countriesList)
.scrollToVideos().assertVideos(videosList)
.scrollToFileDownloads().assertDownloads(downloadsList)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.test.espresso.Espresso
import androidx.test.espresso.matcher.ViewMatchers
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.After
import org.junit.Assume.assumeTrue
import org.junit.Assume
import org.junit.Before
import org.junit.Test
import org.wordpress.android.BuildConfig
Expand All @@ -13,15 +13,14 @@ import org.wordpress.android.e2e.pages.MySitesPage
import org.wordpress.android.support.BaseTest
import org.wordpress.android.support.ComposeEspressoLink
import org.wordpress.android.support.WPSupportUtils
import org.wordpress.android.util.StatsKeyValueData
import org.wordpress.android.util.StatsMocksReader
import org.wordpress.android.util.StatsVisitsData
import org.wordpress.android.wiremock.WireMockStub
import org.wordpress.android.wiremock.WireMockUrlPath

@HiltAndroidTest
class StatsTests : BaseTest() {
class StatsTests : BaseTest(listOf(WireMockStub(urlPath = WireMockUrlPath.FEATURE_RESPONSE, fileName = "new-stats-feature-response.json"))) {
@Before
fun setUp() {
assumeTrue(BuildConfig.IS_JETPACK_APP)
Assume.assumeTrue(BuildConfig.IS_JETPACK_APP)
ComposeEspressoLink().unregister()
logoutIfNecessary()
wpLogin()
Expand All @@ -35,28 +34,12 @@ class StatsTests : BaseTest() {
Espresso.pressBack()
}
}

@Test
fun e2eAllDayStatsLoad() {
val todayVisits = StatsVisitsData("97", "28", "14", "11")
val postsList: List<StatsKeyValueData> = StatsMocksReader().readDayTopPostsToList()
val referrersList: List<StatsKeyValueData> = StatsMocksReader().readDayTopReferrersToList()
val clicksList: List<StatsKeyValueData> = StatsMocksReader().readDayClicksToList()
val authorsList: List<StatsKeyValueData> = StatsMocksReader().readDayAuthorsToList()
val countriesList: List<StatsKeyValueData> = StatsMocksReader().readDayCountriesToList()
val videosList: List<StatsKeyValueData> = StatsMocksReader().readDayVideoPlaysToList()
val downloadsList: List<StatsKeyValueData> = StatsMocksReader().readDayFileDownloadsToList()
MySitesPage()
.go()
.goToStats()
.openDayStats()
.assertVisits(todayVisits)
.scrollToPosts().assertPosts(postsList)
.scrollToReferrers().assertReferrers(referrersList)
.scrollToClicks().assertClicks(clicksList)
.scrollToAuthors().assertAuthors(authorsList)
.scrollToCountries().assertCountries(countriesList)
.scrollToVideos().assertVideos(videosList)
.scrollToFileDownloads().assertDownloads(downloadsList)
.hasNewStatTabs()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ import org.wordpress.android.util.StatsKeyValueData
import org.wordpress.android.util.StatsVisitsData

class StatsPage {
/**
* Matcher to check that the right tabs exist.
*/
fun hasNewStatTabs(): StatsPage {
Espresso.onView(
Matchers.allOf(
ViewMatchers.isDescendantOfA(ViewMatchers.withId(R.id.tabLayout)),
ViewMatchers.withText("Traffic")
)
).check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
Espresso.onView(
Matchers.allOf(
ViewMatchers.isDescendantOfA(ViewMatchers.withId(R.id.tabLayout)),
ViewMatchers.withText("Insights")
)
).check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
return this
}
fun openDayStats(): StatsPage {
val daysStatsTab = Espresso.onView(
Matchers.allOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.wordpress.android.support;

import android.app.Instrumentation;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.compose.ui.test.junit4.ComposeTestRule;
import androidx.test.espresso.accessibility.AccessibilityChecks;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
Expand All @@ -10,6 +12,7 @@
import com.fasterxml.jackson.databind.util.ISO8601Utils;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.DateOffset;
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.HandlebarsHelper;
Expand All @@ -28,14 +31,17 @@
import org.wordpress.android.e2e.flows.LoginFlow;
import org.wordpress.android.e2e.pages.MePage;
import org.wordpress.android.e2e.pages.MySitesPage;
import org.wordpress.android.editor.Utils;
import org.wordpress.android.mocks.AndroidNotifier;
import org.wordpress.android.mocks.AssetFileSource;
import org.wordpress.android.ui.WPLaunchActivity;
import org.wordpress.android.wiremock.WireMockStub;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

Expand All @@ -52,6 +58,7 @@
import dagger.hilt.android.testing.HiltAndroidRule;

public class BaseTest {
static final String TAG = BaseTest.class.getSimpleName();
public static final int WIREMOCK_PORT = 8080;

@Rule(order = 0)
Expand All @@ -70,18 +77,45 @@ public class BaseTest {
@Rule(order = 4)
public WireMockRule wireMockRule;

{
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
public BaseTest() {
this(null);
}

/**
* Constructor
*
* @param wireMockStubs the wiremock stubs to use for this specific test.
*/
public BaseTest(@Nullable final List<WireMockStub> wireMockStubs) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also send a list. Nice!

Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
wireMockRule = new WireMockRule(
options().port(WIREMOCK_PORT)
.fileSource(new AssetFileSource(instrumentation.getContext().getAssets()))
.fileSource(
new AssetFileSource(instrumentation.getContext().getAssets())
)

.extensions(new ResponseTemplateTransformer(true, new HashMap<String, Helper>() {
{
put("fnow", new UnlocalizedDateHelper());
}
}))
.notifier(new AndroidNotifier()));
if (wireMockStubs != null && !wireMockStubs.isEmpty()) {
for (WireMockStub wireMockStub : wireMockStubs) {
try {
final String result = Utils.getStringFromInputStream(
instrumentation.getContext().getClassLoader().getResourceAsStream(
wireMockStub.getFileName()
)
);
// This is where we can stub out
wireMockRule.stubFor(WireMock.get(WireMock.urlPathMatching(wireMockStub.getUrlPath().getPath()))
.willReturn(WireMock.aResponse().withBody(result)));
} catch (final Exception exception) {
Log.e(TAG, "Problem stubbing endpoint", exception);
}
}
}
}

@Before
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.wordpress.android.wiremock

/**
* Represents a WireMock#stubFor call on a WireMockServer.
* @property apiPathUrl The API path we are matching against.
* @property fileName The filename used to return as the body stored in the androidTest resources sub directory.
*/
data class WireMockStub(
val urlPath: WireMockUrlPath,
val fileName: String,
)

/**
* Enum used to represent supported URLs we can stub.
*/
enum class WireMockUrlPath(val path: String) {
FEATURE_RESPONSE("/wpcom/v2/mobile/feature-flags/")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stats_traffic_tab": true
}
Loading