19
19
20
20
const assert = require ( 'node:assert' )
21
21
require ( '../../lib/test/fileserver' )
22
- const { suite } = require ( '../../lib/test' )
22
+ const { suite, ignore } = require ( '../../lib/test' )
23
23
const { Browser } = require ( 'selenium-webdriver' )
24
24
const Storage = require ( 'selenium-webdriver/bidi/storage' )
25
25
const fileserver = require ( '../../lib/test/fileserver' )
26
26
const { CookieFilter } = require ( 'selenium-webdriver/bidi/cookieFilter' )
27
27
const { 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' )
29
35
const { PartialCookie } = require ( 'selenium-webdriver/bidi/partialCookie' )
30
36
31
37
suite (
@@ -59,38 +65,101 @@ suite(
59
65
assert . strictEqual ( result . cookies [ 0 ] . value . value , cookie . value )
60
66
} )
61
67
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 ( )
65
73
66
- await driver . manage ( ) . addCookie ( cookie )
74
+ await driver . manage ( ) . addCookie ( cookie )
67
75
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 )
71
86
72
- await driver . switchTo ( ) . newWindow ( 'window' )
87
+ assert . strictEqual ( resultAfterSwitchingContext . cookies [ 0 ] . value . value , cookie . value )
73
88
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 )
75
116
76
117
const storage = await Storage ( driver )
77
- const resultAfterSwitchingContext = await storage . getCookies ( cookieFilter , descriptor )
78
118
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 )
80
127
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 )
82
139
83
- const descriptorAfterSwitchingBack = new BrowsingContextPartitionDescriptor ( await driver . getWindowHandle ( ) )
140
+ const result = await storage . getCookies ( cookieFilter , partitionDescriptor )
84
141
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 )
86
144
145
+ // Check if cookies were found
146
+ assert . strictEqual ( result . cookies . length > 0 , true , 'Expected at least one cookie' )
87
147
assert . strictEqual ( result . cookies [ 0 ] . value . value , cookie . value )
88
148
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 )
90
161
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 )
94
163
} )
95
164
96
165
it ( 'can add cookie' , async function ( ) {
@@ -166,7 +235,7 @@ suite(
166
235
assert . strictEqual ( result . cookies [ 1 ] . value . value , cookie2 . value )
167
236
} )
168
237
169
- xit ( 'can delete all cookies' , async function ( ) {
238
+ it ( 'can delete all cookies' , async function ( ) {
170
239
const cookie1 = createCookieSpec ( )
171
240
const cookie2 = createCookieSpec ( )
172
241
@@ -182,7 +251,7 @@ suite(
182
251
assert . strictEqual ( result . cookies . length , 0 )
183
252
} )
184
253
185
- xit ( 'can delete cookie by name' , async function ( ) {
254
+ it ( 'can delete cookie by name' , async function ( ) {
186
255
const cookie1 = createCookieSpec ( )
187
256
const cookie2 = createCookieSpec ( )
188
257
0 commit comments