In our old selenium/C# test suite we are setting cookies thus, and this works very well:
private static void AddCookies()
{
var cDom = TestUrl.Replace("https://", "")
.Replace("/", "");
Cookie p1 = new(
name: "hideWelcomeMessage",
value: "true",
domain: $"{cDom}",
path: "/home",
expiry: DateTime.Today.AddDays(1),
secure: true,
isHttpOnly: false,
sameSite: "Lax"
);
Driver.Manage().Cookies.AddCookie(p1);
Cookie p2 = new(
name: "hideWelcomeWindow",
value: "true",
domain: $"{cDom}",
path: "/home",
expiry: DateTime.Today.AddDays(1),
secure: true,
isHttpOnly: false,
sameSite: "Lax"
);
Driver.Manage().Cookies.AddCookie(p2);
}
But I am now having problems translating this int out new Playwright/C# suite This is what I have so far and while there are errors in is also having no affect.
var cDom = TestUrl.Replace("https://", "")
.Replace("/", "")
.Replace("www", "");
Cookie p1 = new()
{
Name = "hideWelcomeMessage",
Value = "true",
Domain = $"{cDom}",
Path = "/home",
Expires = 86400,
Secure = true,
HttpOnly = false,
SameSite = SameSiteAttribute.Lax
};
Cookie p2 = new()
{
Name = "hideWelcomeWindow",
Value = "true",
Domain = $"{cDom}",
Path = "/home",
Expires = 86400,
Secure = true,
HttpOnly = false,
SameSite = SameSiteAttribute.Lax
};
await Context.AddCookiesAsync([p1, p2]);
Page = await Context.NewPageAsync();
This does not work, has anyone got any idea where I am going wrong please.
In our old selenium/C# test suite we are setting cookies thus, and this works very well:
private static void AddCookies()
{
var cDom = TestUrl.Replace("https://", "")
.Replace("/", "");
Cookie p1 = new(
name: "hideWelcomeMessage",
value: "true",
domain: $"{cDom}",
path: "/home",
expiry: DateTime.Today.AddDays(1),
secure: true,
isHttpOnly: false,
sameSite: "Lax"
);
Driver.Manage().Cookies.AddCookie(p1);
Cookie p2 = new(
name: "hideWelcomeWindow",
value: "true",
domain: $"{cDom}",
path: "/home",
expiry: DateTime.Today.AddDays(1),
secure: true,
isHttpOnly: false,
sameSite: "Lax"
);
Driver.Manage().Cookies.AddCookie(p2);
}
But I am now having problems translating this int out new Playwright/C# suite This is what I have so far and while there are errors in is also having no affect.
var cDom = TestUrl.Replace("https://", "")
.Replace("/", "")
.Replace("www", "");
Cookie p1 = new()
{
Name = "hideWelcomeMessage",
Value = "true",
Domain = $"{cDom}",
Path = "/home",
Expires = 86400,
Secure = true,
HttpOnly = false,
SameSite = SameSiteAttribute.Lax
};
Cookie p2 = new()
{
Name = "hideWelcomeWindow",
Value = "true",
Domain = $"{cDom}",
Path = "/home",
Expires = 86400,
Secure = true,
HttpOnly = false,
SameSite = SameSiteAttribute.Lax
};
await Context.AddCookiesAsync([p1, p2]);
Page = await Context.NewPageAsync();
This does not work, has anyone got any idea where I am going wrong please.
Share Improve this question edited Mar 24 at 8:16 Basheer Jarrah 5883 silver badges16 bronze badges asked Mar 21 at 15:05 KevKev 3786 silver badges27 bronze badges1 Answer
Reset to default 1I can explain to you in typescript and you can try this out in C# or your preferred language.
// Set a cookie
const cookies = [
{
name: 'example-cookie',
value: 'example-value',
domain: 'example',
path: '/',
httpOnly: true,
secure: true,
sameSite: 'Strict'
},
{
//add more cookies if you want
}
];
await context.addCookies(cookies);
// Verify the cookie is set
const storedCookies = await context.cookies();
console.log(storedCookies);
await browser.close();
Ref: https://playwright.dev/docs/api/class-browsercontext#browser-context-add-cookies
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744344480a4569598.html
评论列表(0条)