From 19b9206a90591c5785e427e46a07f3130c2308f5 Mon Sep 17 00:00:00 2001 From: Edwin Takahashi Date: Fri, 8 Sep 2023 23:36:27 +0900 Subject: [PATCH] packages/calypso-e2e/src/rest-api-client.ts - implement new method to see if there are posts on the site. test/e2e/specs/tools/advertising__promote.ts - stop creating new post unnecessarily --- packages/calypso-e2e/src/rest-api-client.ts | 28 +++++++++++++++++++ .../src/types/rest-api-client.types.ts | 2 ++ test/e2e/specs/tools/advertising__promote.ts | 26 ++++++++++++----- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/packages/calypso-e2e/src/rest-api-client.ts b/packages/calypso-e2e/src/rest-api-client.ts index d74bca72e06c8..996a12a6ad5d0 100644 --- a/packages/calypso-e2e/src/rest-api-client.ts +++ b/packages/calypso-e2e/src/rest-api-client.ts @@ -42,6 +42,7 @@ import type { JetpackSearchResponse, JetpackSearchParams, Subscriber, + SitePostState, } from './types'; import type { BodyInit, HeadersInit, RequestInit } from 'node-fetch'; @@ -635,6 +636,33 @@ export class RestAPIClient { /* Posts */ + /** + * Given a siteID, checks whether any posts exists of a given state. + * + * @param {number} siteID Site ID. + * @param param1 Keyed object parameter. + * @param {SitePostState} param1.state State of the published post. + */ + async siteHasPost( + siteID: number, + { state = 'publish' }: { state: SitePostState } + ): Promise< boolean > { + const params: RequestParams = { + method: 'get', + headers: { + Authorization: await this.getAuthorizationHeader( 'bearer' ), + 'Content-Type': this.getContentTypeHeader( 'json' ), + }, + }; + + const response = await this.sendRequest( + this.getRequestURL( '1.1', `/sites/${ siteID }/post-counts/post` ), + params + ); + + return response.counts.all[ state ] !== 0 ? true : false; + } + /** * Creates a post on the site. * diff --git a/packages/calypso-e2e/src/types/rest-api-client.types.ts b/packages/calypso-e2e/src/types/rest-api-client.types.ts index b84e3ced768c8..085e8cae0f85b 100644 --- a/packages/calypso-e2e/src/types/rest-api-client.types.ts +++ b/packages/calypso-e2e/src/types/rest-api-client.types.ts @@ -1,5 +1,7 @@ /* Parameter Interfaces */ +export type SitePostState = 'publish' | 'draft' | 'private'; + export interface AccountDetails { userID: number; username: string; diff --git a/test/e2e/specs/tools/advertising__promote.ts b/test/e2e/specs/tools/advertising__promote.ts index 5e569bc857421..d075f378aea8c 100644 --- a/test/e2e/specs/tools/advertising__promote.ts +++ b/test/e2e/specs/tools/advertising__promote.ts @@ -50,16 +50,23 @@ skipDescribeIf( envVariables.ATOMIC_VARIATION === 'private' )( const accountName = getTestAccountByFeature( envToFeatureKey( envVariables ) ); testAccount = new TestAccount( accountName ); - // Createa a new test post before starting the test, to ensure at least one - // available post. restAPIClient = new RestAPIClient( testAccount.credentials ); - newPostDetails = await restAPIClient.createPost( + + // Createa a new test post before starting the test if site has no published post. + const hasPosts = await restAPIClient.siteHasPost( testAccount.credentials.testSites?.primary.id as number, - { - title: pageTitle, - } + { state: 'publish' } ); + if ( ! hasPosts ) { + newPostDetails = await restAPIClient.createPost( + testAccount.credentials.testSites?.primary.id as number, + { + title: pageTitle, + } + ); + } + await testAccount.authenticate( page ); advertisingPage = new AdvertisingPage( page ); @@ -75,11 +82,12 @@ skipDescribeIf( envVariables.ATOMIC_VARIATION === 'private' )( } ); it( 'Click on Promote for the first post', async function () { - await advertisingPage.clickButtonByNameOnRow( 'Promote', { postTitle: pageTitle } ); + await advertisingPage.clickButtonByNameOnRow( 'Promote', { row: 1 } ); } ); it( 'Land in Blaze campaign landing page', async function () { await page.waitForURL( /advertising/ ); + blazeCampaignPage = new BlazeCampaignPage( page ); } ); @@ -102,6 +110,10 @@ skipDescribeIf( envVariables.ATOMIC_VARIATION === 'private' )( } ); afterAll( async function () { + if ( ! newPostDetails ) { + return; + } + await restAPIClient.deletePost( testAccount.credentials.testSites?.primary.id as number, newPostDetails.ID