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  { 
29+   BrowsingContextPartitionDescriptor, 
30+   StorageKeyPartitionDescriptor, 
31+ }  =  require ( 'selenium-webdriver/bidi/partitionDescriptor' ) 
32+ const  BrowserBiDi  =  require ( 'selenium-webdriver/bidi/browser' ) 
33+ const  BrowsingContext  =  require ( 'selenium-webdriver/bidi/browsingContext' ) 
34+ const  {  CreateContextParameters }  =  require ( 'selenium-webdriver/bidi/createContextParameters' ) 
2935const  {  PartialCookie }  =  require ( 'selenium-webdriver/bidi/partialCookie' ) 
3036
3137suite ( 
@@ -59,38 +65,101 @@ suite(
5965        assert . strictEqual ( result . cookies [ 0 ] . value . value ,  cookie . value ) 
6066      } ) 
6167
62-       xit ( 'can get cookie in default user context' ,  async  function  ( )  { 
63-         const  windowHandle  =  await  driver . getWindowHandle ( ) 
64-         const  cookie  =  createCookieSpec ( ) 
68+       ignore ( env . browsers ( Browser . CHROME ,  Browser . EDGE ) ) . it ( 
69+         'can get cookie in default user context' , 
70+         async  function  ( )  { 
71+           const  windowHandle  =  await  driver . getWindowHandle ( ) 
72+           const  cookie  =  createCookieSpec ( ) 
6573
66-         await  driver . manage ( ) . addCookie ( cookie ) 
74+            await  driver . manage ( ) . addCookie ( cookie ) 
6775
68-         const  cookieFilter  =  new  CookieFilter ( ) 
69-           . name ( cookie . name ) 
70-           . value ( new  BytesValue ( BytesValue . Type . STRING ,  cookie . value ) ) 
76+           const  cookieFilter  =  new  CookieFilter ( ) 
77+             . name ( cookie . name ) 
78+             . value ( new  BytesValue ( BytesValue . Type . STRING ,  cookie . value ) ) 
79+ 
80+           await  driver . switchTo ( ) . newWindow ( 'window' ) 
81+ 
82+           const  descriptor  =  new  BrowsingContextPartitionDescriptor ( await  driver . getWindowHandle ( ) ) 
83+ 
84+           const  storage  =  await  Storage ( driver ) 
85+           const  resultAfterSwitchingContext  =  await  storage . getCookies ( cookieFilter ,  descriptor ) 
7186
72-         await   driver . switchTo ( ) . newWindow ( 'window' ) 
87+            assert . strictEqual ( resultAfterSwitchingContext . cookies [ 0 ] . value . value ,   cookie . value ) 
7388
74-         const  descriptor  =  new  BrowsingContextPartitionDescriptor ( await  driver . getWindowHandle ( ) ) 
89+           await  driver . switchTo ( ) . window ( windowHandle ) 
90+ 
91+           const  descriptorAfterSwitchingBack  =  new  BrowsingContextPartitionDescriptor ( await  driver . getWindowHandle ( ) ) 
92+ 
93+           const  result  =  await  storage . getCookies ( cookieFilter ,  descriptorAfterSwitchingBack ) 
94+ 
95+           assert . strictEqual ( result . cookies [ 0 ] . value . value ,  cookie . value ) 
96+ 
97+           const  partitionKey  =  result . partitionKey 
98+ 
99+           assert . notEqual ( partitionKey . userContext ,  null ) 
100+           assert . notEqual ( partitionKey . sourceOrigin ,  null ) 
101+           assert . strictEqual ( partitionKey . userContext ,  'default' ) 
102+         } , 
103+       ) 
104+ 
105+       it ( 'can get cookie in a user context' ,  async  function  ( )  { 
106+         const  browser  =  await  BrowserBiDi ( driver ) 
107+         const  userContext  =  await  browser . createUserContext ( ) 
108+         const  windowHandle  =  await  driver . getWindowHandle ( ) 
109+ 
110+         const  cookie  =  { 
111+           name : getRandomString ( ) , 
112+           value : 'set' , 
113+         } 
114+ 
115+         const  partitionDescriptor  =  new  StorageKeyPartitionDescriptor ( ) . userContext ( userContext ) 
75116
76117        const  storage  =  await  Storage ( driver ) 
77-         const  resultAfterSwitchingContext  =  await  storage . getCookies ( cookieFilter ,  descriptor ) 
78118
79-         assert . strictEqual ( resultAfterSwitchingContext . cookies [ 0 ] . value . value ,  cookie . value ) 
119+         const  cookieDomain  =  fileserver . getHostName ( ) 
120+         const  partialCookie  =  new  PartialCookie ( 
121+           cookie . name , 
122+           new  BytesValue ( BytesValue . Type . STRING ,  cookie . value ) , 
123+           cookieDomain , 
124+         ) 
125+ 
126+         await  storage . setCookie ( partialCookie ,  partitionDescriptor ) 
80127
81-         await  driver . switchTo ( ) . window ( windowHandle ) 
128+         const  cookieFilter  =  new  CookieFilter ( ) . name ( cookie . name ) . value ( new  BytesValue ( BytesValue . Type . STRING ,  'set' ) ) 
129+ 
130+         // Create a new browsing context with the user context 
131+         const  createParams  =  new  CreateContextParameters ( ) . userContext ( userContext ) 
132+ 
133+         const  browsingContext  =  await  BrowsingContext ( driver ,  { 
134+           type : 'tab' , 
135+           createParameters : createParams , 
136+         } ) 
137+ 
138+         await  driver . switchTo ( ) . window ( browsingContext . id ) 
82139
83-         const  descriptorAfterSwitchingBack  =  new   BrowsingContextPartitionDescriptor ( await  driver . getWindowHandle ( ) ) 
140+         const  result  =  await  storage . getCookies ( cookieFilter ,   partitionDescriptor ) 
84141
85-         const  result  =  await  storage . getCookies ( cookieFilter ,  descriptorAfterSwitchingBack ) 
142+         assert . strictEqual ( result . cookies . length  >  0 ,  true ,  'Expected at least one cookie' ) 
143+         assert . strictEqual ( result . cookies [ 0 ] . value . value ,  cookie . value ) 
86144
145+         // Check if cookies were found 
146+         assert . strictEqual ( result . cookies . length  >  0 ,  true ,  'Expected at least one cookie' ) 
87147        assert . strictEqual ( result . cookies [ 0 ] . value . value ,  cookie . value ) 
88148
89-         const  partitionKey  =  result . partitionKey 
149+         // Check partitionKey if it exists 
150+         if  ( result . partitionKey )  { 
151+           const  partitionKey  =  result . partitionKey 
152+           assert . notEqual ( partitionKey . userContext ,  null ,  'Expected userContext to be non-null' ) 
153+           assert . strictEqual ( partitionKey . userContext ,  userContext ,  'Expected userContext to match' ) 
154+         } 
155+ 
156+         await  driver . switchTo ( ) . window ( windowHandle ) 
157+ 
158+         const  browsingContextPartitionDescriptor  =  new  BrowsingContextPartitionDescriptor ( windowHandle ) 
159+ 
160+         const  resultAfterSwitchingBack  =  await  storage . getCookies ( cookieFilter ,  browsingContextPartitionDescriptor ) 
90161
91-         assert . notEqual ( partitionKey . userContext ,  null ) 
92-         assert . notEqual ( partitionKey . sourceOrigin ,  null ) 
93-         assert . strictEqual ( partitionKey . userContext ,  'default' ) 
162+         assert . strictEqual ( resultAfterSwitchingBack . cookies . length ,  0 ) 
94163      } ) 
95164
96165      it ( 'can add cookie' ,  async  function  ( )  { 
@@ -166,7 +235,7 @@ suite(
166235        assert . strictEqual ( result . cookies [ 1 ] . value . value ,  cookie2 . value ) 
167236      } ) 
168237
169-       xit ( 'can delete all cookies' ,  async  function  ( )  { 
238+       it ( 'can delete all cookies' ,  async  function  ( )  { 
170239        const  cookie1  =  createCookieSpec ( ) 
171240        const  cookie2  =  createCookieSpec ( ) 
172241
@@ -182,7 +251,7 @@ suite(
182251        assert . strictEqual ( result . cookies . length ,  0 ) 
183252      } ) 
184253
185-       xit ( 'can delete cookie by name' ,  async  function  ( )  { 
254+       it ( 'can delete cookie by name' ,  async  function  ( )  { 
186255        const  cookie1  =  createCookieSpec ( ) 
187256        const  cookie2  =  createCookieSpec ( ) 
188257
0 commit comments