1919
2020const assert = require ( 'node:assert' )
2121require ( '../../lib/test/fileserver' )
22- const { suite } = require ( '../../lib/test' )
22+ const { suite, ignore } = require ( '../../lib/test' )
2323const { Browser } = require ( 'selenium-webdriver' )
2424const Storage = require ( 'selenium-webdriver/bidi/storage' )
2525const fileserver = require ( '../../lib/test/fileserver' )
2626const { CookieFilter } = require ( 'selenium-webdriver/bidi/cookieFilter' )
2727const { BytesValue, SameSite } = require ( 'selenium-webdriver/bidi/networkTypes' )
28- const { BrowsingContextPartitionDescriptor } = require ( 'selenium-webdriver/bidi/partitionDescriptor' )
28+ const { BrowsingContextPartitionDescriptor, StorageKeyPartitionDescriptor } = require ( 'selenium-webdriver/bidi/partitionDescriptor' )
29+ const BrowserBiDi = require ( 'selenium-webdriver/bidi/browser' )
30+ const BrowsingContext = require ( 'selenium-webdriver/bidi/browsingContext' )
31+ const { CreateContextParameters } = require ( 'selenium-webdriver/bidi/createContextParameters' )
2932const { PartialCookie } = require ( 'selenium-webdriver/bidi/partialCookie' )
3033
3134suite (
5962 assert . strictEqual ( result . cookies [ 0 ] . value . value , cookie . value )
6063 } )
6164
62- xit ( 'can get cookie in default user context' , async function ( ) {
65+ ignore ( env . browsers ( Browser . CHROME , Browser . EDGE ) ) . it ( 'can get cookie in default user context' , async function ( ) {
6366 const windowHandle = await driver . getWindowHandle ( )
6467 const cookie = createCookieSpec ( )
6568
@@ -93,6 +96,69 @@ suite(
9396 assert . strictEqual ( partitionKey . userContext , 'default' )
9497 } )
9598
99+ it ( 'can get cookie in a user context' , async function ( ) {
100+ const browser = await BrowserBiDi ( driver )
101+ const userContext = await browser . createUserContext ( )
102+ const windowHandle = await driver . getWindowHandle ( )
103+
104+ const cookie = {
105+ name : getRandomString ( ) ,
106+ value : "set"
107+ }
108+
109+ const partitionDescriptor = new StorageKeyPartitionDescriptor ( ) . userContext ( userContext )
110+
111+ const storage = await Storage ( driver )
112+
113+ const cookieDomain = fileserver . getHostName ( )
114+ const partialCookie = new PartialCookie (
115+ cookie . name ,
116+ new BytesValue ( BytesValue . Type . STRING , cookie . value ) ,
117+ cookieDomain
118+ )
119+
120+ await storage . setCookie ( partialCookie , partitionDescriptor )
121+
122+ const cookieFilter = new CookieFilter ( )
123+ . name ( cookie . name )
124+ . value ( new BytesValue ( BytesValue . Type . STRING , "set" ) )
125+
126+ // Create a new browsing context with the user context
127+ const createParams = new CreateContextParameters ( )
128+ . userContext ( userContext )
129+
130+ const browsingContext = await BrowsingContext ( driver , {
131+ type : 'tab' ,
132+ createParameters : createParams
133+ } )
134+
135+ await driver . switchTo ( ) . window ( browsingContext . id )
136+
137+ const result = await storage . getCookies ( cookieFilter , partitionDescriptor )
138+
139+ assert . strictEqual ( result . cookies . length > 0 , true , 'Expected at least one cookie' )
140+ assert . strictEqual ( result . cookies [ 0 ] . value . value , cookie . value )
141+
142+ // Check if cookies were found
143+ assert . strictEqual ( result . cookies . length > 0 , true , 'Expected at least one cookie' )
144+ assert . strictEqual ( result . cookies [ 0 ] . value . value , cookie . value )
145+
146+ // Check partitionKey if it exists
147+ if ( result . partitionKey ) {
148+ const partitionKey = result . partitionKey
149+ assert . notEqual ( partitionKey . userContext , null , 'Expected userContext to be non-null' )
150+ assert . strictEqual ( partitionKey . userContext , userContext , 'Expected userContext to match' )
151+ }
152+
153+ await driver . switchTo ( ) . window ( windowHandle )
154+
155+ const browsingContextPartitionDescriptor = new BrowsingContextPartitionDescriptor ( windowHandle )
156+
157+ const resultAfterSwitchingBack = await storage . getCookies ( cookieFilter , browsingContextPartitionDescriptor )
158+
159+ assert . strictEqual ( resultAfterSwitchingBack . cookies . length , 0 )
160+ } )
161+
96162 it ( 'can add cookie' , async function ( ) {
97163 const cookie = createCookieSpec ( )
98164
@@ -166,7 +232,7 @@ suite(
166232 assert . strictEqual ( result . cookies [ 1 ] . value . value , cookie2 . value )
167233 } )
168234
169- xit ( 'can delete all cookies' , async function ( ) {
235+ it ( 'can delete all cookies' , async function ( ) {
170236 const cookie1 = createCookieSpec ( )
171237 const cookie2 = createCookieSpec ( )
172238
@@ -182,7 +248,7 @@ suite(
182248 assert . strictEqual ( result . cookies . length , 0 )
183249 } )
184250
185- xit ( 'can delete cookie by name' , async function ( ) {
251+ it ( 'can delete cookie by name' , async function ( ) {
186252 const cookie1 = createCookieSpec ( )
187253 const cookie2 = createCookieSpec ( )
188254
0 commit comments