|
23 | 23 | * SOFTWARE.
|
24 | 24 | */
|
25 | 25 |
|
| 26 | +using System.Text.RegularExpressions; |
| 27 | + |
26 | 28 | namespace Microsoft.Playwright.Tests;
|
27 | 29 |
|
28 | 30 | public class BrowserContextClearCookiesTests : PageTestEx
|
@@ -82,4 +84,151 @@ await anotherContext.AddCookiesAsync(new[]
|
82 | 84 | Assert.IsEmpty(await Context.CookiesAsync());
|
83 | 85 | Assert.IsEmpty(await anotherContext.CookiesAsync());
|
84 | 86 | }
|
| 87 | + |
| 88 | + [PlaywrightTest("browsercontext-clearcookies.spec.ts", "should remove cookies by name")] |
| 89 | + public async Task ShouldRemoveCookiesByName() |
| 90 | + { |
| 91 | + await Context.AddCookiesAsync( |
| 92 | + [ |
| 93 | + new Cookie |
| 94 | + { |
| 95 | + Name = "cookie1", |
| 96 | + Value = "1", |
| 97 | + Domain = new Uri(Server.Prefix).Host, |
| 98 | + Path = "/" |
| 99 | + }, |
| 100 | + new Cookie |
| 101 | + { |
| 102 | + Name = "cookie2", |
| 103 | + Value = "2", |
| 104 | + Domain = new Uri(Server.Prefix).Host, |
| 105 | + Path = "/" |
| 106 | + } |
| 107 | + ]); |
| 108 | + await Page.GotoAsync(Server.Prefix); |
| 109 | + Assert.AreEqual("cookie1=1; cookie2=2", await Page.EvaluateAsync<string>("document.cookie")); |
| 110 | + await Context.ClearCookiesAsync(new() { Name = "cookie1" }); |
| 111 | + Assert.AreEqual("cookie2=2", await Page.EvaluateAsync<string>("document.cookie")); |
| 112 | + } |
| 113 | + |
| 114 | + [PlaywrightTest("browsercontext-clearcookies.spec.ts", "should remove cookies by name regex")] |
| 115 | + public async Task ShouldRemoveCookiesByNameRegex() |
| 116 | + { |
| 117 | + await Context.AddCookiesAsync( |
| 118 | + [ |
| 119 | + new Cookie |
| 120 | + { |
| 121 | + Name = "cookie1", |
| 122 | + Value = "1", |
| 123 | + Domain = new Uri(Server.Prefix).Host, |
| 124 | + Path = "/" |
| 125 | + }, |
| 126 | + new Cookie |
| 127 | + { |
| 128 | + Name = "cookie2", |
| 129 | + Value = "2", |
| 130 | + Domain = new Uri(Server.Prefix).Host, |
| 131 | + Path = "/" |
| 132 | + } |
| 133 | + ]); |
| 134 | + await Page.GotoAsync(Server.Prefix); |
| 135 | + Assert.AreEqual("cookie1=1; cookie2=2", await Page.EvaluateAsync<string>("document.cookie")); |
| 136 | + await Context.ClearCookiesAsync(new() { NameRegex = new Regex("coo.*1") }); |
| 137 | + Assert.AreEqual("cookie2=2", await Page.EvaluateAsync<string>("document.cookie")); |
| 138 | + } |
| 139 | + |
| 140 | + [PlaywrightTest("browsercontext-clearcookies.spec.ts", "should remove cookies by domain")] |
| 141 | + public async Task ShouldRemoveCookiesByDomain() |
| 142 | + { |
| 143 | + await Context.AddCookiesAsync( |
| 144 | + [ |
| 145 | + new Cookie |
| 146 | + { |
| 147 | + Name = "cookie1", |
| 148 | + Value = "1", |
| 149 | + Domain = new Uri(Server.Prefix).Host, |
| 150 | + Path = "/" |
| 151 | + }, |
| 152 | + new Cookie |
| 153 | + { |
| 154 | + Name = "cookie2", |
| 155 | + Value = "2", |
| 156 | + Domain = new Uri(Server.CrossProcessPrefix).Host, |
| 157 | + Path = "/" |
| 158 | + } |
| 159 | + ]); |
| 160 | + await Page.GotoAsync(Server.Prefix); |
| 161 | + Assert.AreEqual("cookie1=1", await Page.EvaluateAsync<string>("document.cookie")); |
| 162 | + await Page.GotoAsync(Server.CrossProcessPrefix); |
| 163 | + Assert.AreEqual("cookie2=2", await Page.EvaluateAsync<string>("document.cookie")); |
| 164 | + await Context.ClearCookiesAsync(new() { Domain = new Uri(Server.CrossProcessPrefix).Host }); |
| 165 | + Assert.IsEmpty(await Page.EvaluateAsync<string>("document.cookie")); |
| 166 | + await Page.GotoAsync(Server.Prefix); |
| 167 | + Assert.AreEqual("cookie1=1", await Page.EvaluateAsync<string>("document.cookie")); |
| 168 | + } |
| 169 | + |
| 170 | + [PlaywrightTest("browsercontext-clearcookies.spec.ts", "should remove cookies by path")] |
| 171 | + public async Task ShouldRemoveCookiesByPath() |
| 172 | + { |
| 173 | + await Context.AddCookiesAsync( |
| 174 | + [ |
| 175 | + new Cookie |
| 176 | + { |
| 177 | + Name = "cookie1", |
| 178 | + Value = "1", |
| 179 | + Domain = new Uri(Server.Prefix).Host, |
| 180 | + Path = "/api/v1" |
| 181 | + }, |
| 182 | + new Cookie |
| 183 | + { |
| 184 | + Name = "cookie2", |
| 185 | + Value = "2", |
| 186 | + Domain = new Uri(Server.Prefix).Host, |
| 187 | + Path = "/api/v2" |
| 188 | + }, |
| 189 | + new Cookie |
| 190 | + { |
| 191 | + Name = "cookie3", |
| 192 | + Value = "3", |
| 193 | + Domain = new Uri(Server.Prefix).Host, |
| 194 | + Path = "/" |
| 195 | + } |
| 196 | + ]); |
| 197 | + await Page.GotoAsync(Server.Prefix + "/api/v1"); |
| 198 | + Assert.AreEqual("cookie1=1; cookie3=3", await Page.EvaluateAsync<string>("document.cookie")); |
| 199 | + await Context.ClearCookiesAsync(new() { Path = "/api/v1" }); |
| 200 | + Assert.AreEqual("cookie3=3", await Page.EvaluateAsync<string>("document.cookie")); |
| 201 | + await Page.GotoAsync(Server.Prefix + "/api/v2"); |
| 202 | + Assert.AreEqual("cookie2=2; cookie3=3", await Page.EvaluateAsync<string>("document.cookie")); |
| 203 | + await Page.GotoAsync(Server.Prefix + "/"); |
| 204 | + Assert.AreEqual("cookie3=3", await Page.EvaluateAsync<string>("document.cookie")); |
| 205 | + } |
| 206 | + |
| 207 | + [PlaywrightTest("browsercontext-clearcookies.spec.ts", "should remove cookies by name and domain")] |
| 208 | + public async Task ShouldRemoveCookiesByNameAndDomain() |
| 209 | + { |
| 210 | + await Context.AddCookiesAsync( |
| 211 | + [ |
| 212 | + new Cookie |
| 213 | + { |
| 214 | + Name = "cookie1", |
| 215 | + Value = "1", |
| 216 | + Domain = new Uri(Server.Prefix).Host, |
| 217 | + Path = "/" |
| 218 | + }, |
| 219 | + new Cookie |
| 220 | + { |
| 221 | + Name = "cookie1", |
| 222 | + Value = "1", |
| 223 | + Domain = new Uri(Server.CrossProcessPrefix).Host, |
| 224 | + Path = "/" |
| 225 | + } |
| 226 | + ]); |
| 227 | + await Page.GotoAsync(Server.Prefix); |
| 228 | + Assert.AreEqual("cookie1=1", await Page.EvaluateAsync<string>("document.cookie")); |
| 229 | + await Context.ClearCookiesAsync(new() { Name = "cookie1", Domain = new Uri(Server.Prefix).Host }); |
| 230 | + Assert.IsEmpty(await Page.EvaluateAsync<string>("document.cookie")); |
| 231 | + await Page.GotoAsync(Server.CrossProcessPrefix); |
| 232 | + Assert.AreEqual("cookie1=1", await Page.EvaluateAsync<string>("document.cookie")); |
| 233 | + } |
85 | 234 | }
|
0 commit comments