11import { isUndefined } from '@guardian/libs' ;
2- import type { BrowserContext , Request } from '@playwright/test' ;
3- import { test } from '@playwright/test' ;
4- import { cmpAcceptAll } from '../lib/cmp' ;
2+ import type { BrowserContext , Page , Request } from '@playwright/test' ;
3+ import { expect , test } from '@playwright/test' ;
4+ import { allowRejectAll , cmpAcceptAll , cmpRejectAll } from '../lib/cmp' ;
55import { addCookie } from '../lib/cookies' ;
66import { loadPage } from '../lib/load-page' ;
77
@@ -12,6 +12,10 @@ const optOutOfArticleCountConsent = async (context: BrowserContext) => {
1212 } ) ;
1313} ;
1414
15+ const ARTICLE_PATH =
16+ '/Article/https://www.theguardian.com/politics/2019/nov/20/jeremy-corbyn-boris-johnson-tv-debate-watched-by-67-million-people' ;
17+ const RR_BANNER_URL = 'https://contributions.guardianapis.com/banner' ;
18+
1519const requestBodyHasProperties = (
1620 request : Request ,
1721 url : string | RegExp ,
@@ -25,21 +29,32 @@ const requestBodyHasProperties = (
2529 ) ;
2630} ;
2731
32+ const getBannerRequestField = (
33+ request : Request ,
34+ url : string | RegExp ,
35+ field : string ,
36+ ) : unknown => {
37+ if ( ! request . url ( ) . match ( url ) ) return undefined ;
38+ const postJSON = request . postDataJSON ( ) as {
39+ targeting ?: Record < string , unknown > ;
40+ } ;
41+ return postJSON . targeting ?. [ field ] ;
42+ } ;
43+
2844test . describe ( 'The banner' , function ( ) {
2945 test ( 'makes a request to the support-dotcom-components service' , async ( {
3046 page,
3147 context,
3248 } ) => {
3349 await optOutOfArticleCountConsent ( context ) ;
34- const rrBannerUrl = 'https://contributions.guardianapis.com/banner' ;
3550
3651 const rrBannerRequestPromise = page . waitForRequest ( ( request ) =>
37- requestBodyHasProperties ( request , rrBannerUrl , [ 'targeting' ] ) ,
52+ requestBodyHasProperties ( request , RR_BANNER_URL , [ 'targeting' ] ) ,
3853 ) ;
3954
4055 await loadPage ( {
4156 page,
42- path : `/Article/https://www.theguardian.com/politics/2019/nov/20/jeremy-corbyn-boris-johnson-tv-debate-watched-by-67-million-people` ,
57+ path : ARTICLE_PATH ,
4358 waitUntil : 'domcontentloaded' ,
4459 region : 'GB' ,
4560 preventSupportBanner : false ,
@@ -62,7 +77,7 @@ test.describe('Sign-in gate portal', function () {
6277
6378 await loadPage ( {
6479 page,
65- path : `/Article/https://www.theguardian.com/politics/2019/nov/20/jeremy-corbyn-boris-johnson-tv-debate-watched-by-67-million-people` ,
80+ path : ARTICLE_PATH ,
6681 waitUntil : 'domcontentloaded' ,
6782 region : 'GB' ,
6883 preventSupportBanner : false ,
@@ -82,3 +97,122 @@ test.describe('Sign-in gate portal', function () {
8297 await auxiaRequestPromise ;
8398 } ) ;
8499} ) ;
100+
101+ test . describe ( 'Banner browserId targeting' , function ( ) {
102+ const setAuxiaVariantCookie = async ( context : BrowserContext ) => {
103+ await addCookie ( context , {
104+ name : 'gu_client_ab_tests' ,
105+ value : 'growth-auxia-banner:variant' ,
106+ } ) ;
107+ } ;
108+ const setBwidCookie = async (
109+ context : BrowserContext ,
110+ value = 'test-browser-id' ,
111+ ) => {
112+ await addCookie ( context , {
113+ name : 'bwid' ,
114+ value,
115+ } ) ;
116+ } ;
117+
118+ const loadAndCaptureBannerRequest = async ( {
119+ page,
120+ context,
121+ acceptConsent,
122+ inAuxiaVariant,
123+ } : {
124+ page : Page ;
125+ context : BrowserContext ;
126+ acceptConsent : boolean ;
127+ inAuxiaVariant : boolean ;
128+ } ) => {
129+ if ( ! acceptConsent ) {
130+ await allowRejectAll ( context ) ;
131+ }
132+ await optOutOfArticleCountConsent ( context ) ;
133+ await setBwidCookie ( context ) ;
134+ if ( inAuxiaVariant ) {
135+ await setAuxiaVariantCookie ( context ) ;
136+ }
137+
138+ const bannerRequestPromise = page . waitForRequest (
139+ ( request ) =>
140+ request . url ( ) . includes ( RR_BANNER_URL ) &&
141+ request . method ( ) === 'POST' ,
142+ ) ;
143+
144+ await loadPage ( {
145+ page,
146+ path : ARTICLE_PATH ,
147+ waitUntil : 'domcontentloaded' ,
148+ region : 'GB' ,
149+ preventSupportBanner : false ,
150+ } ) ;
151+
152+ if ( acceptConsent ) {
153+ await cmpAcceptAll ( page ) ;
154+ } else {
155+ await cmpRejectAll ( page ) ;
156+ }
157+
158+ return bannerRequestPromise ;
159+ } ;
160+
161+ test ( 'sends browserId when user has consented and is in the auxia variant' , async ( {
162+ page,
163+ context,
164+ } ) => {
165+ const bannerRequest = await loadAndCaptureBannerRequest ( {
166+ page,
167+ context,
168+ acceptConsent : true ,
169+ inAuxiaVariant : true ,
170+ } ) ;
171+
172+ const browserId = getBannerRequestField (
173+ bannerRequest ,
174+ RR_BANNER_URL ,
175+ 'browserId' ,
176+ ) ;
177+ expect ( browserId ) . toBe ( 'test-browser-id' ) ;
178+ } ) ;
179+
180+ // Skip this test because it doesn't work in the github actions run. It does however work locally
181+ test . skip ( 'does not send browserId when user has not consented, even if in the auxia variant' , async ( {
182+ page,
183+ context,
184+ } ) => {
185+ const bannerRequest = await loadAndCaptureBannerRequest ( {
186+ page,
187+ context,
188+ acceptConsent : false ,
189+ inAuxiaVariant : true ,
190+ } ) ;
191+
192+ const browserId = getBannerRequestField (
193+ bannerRequest ,
194+ RR_BANNER_URL ,
195+ 'browserId' ,
196+ ) ;
197+ expect ( browserId ) . toBeUndefined ( ) ;
198+ } ) ;
199+
200+ test ( 'does not send browserId when user is not in the auxia variant, even if consented' , async ( {
201+ page,
202+ context,
203+ } ) => {
204+ const bannerRequest = await loadAndCaptureBannerRequest ( {
205+ page,
206+ context,
207+ acceptConsent : true ,
208+ inAuxiaVariant : false ,
209+ } ) ;
210+
211+ const browserId = getBannerRequestField (
212+ bannerRequest ,
213+ RR_BANNER_URL ,
214+ 'browserId' ,
215+ ) ;
216+ expect ( browserId ) . toBeUndefined ( ) ;
217+ } ) ;
218+ } ) ;
0 commit comments