c# - How do I set cookies in Playwright test - Stack Overflow

In our old seleniumC# test suite we are setting cookies thus, and this works very well:private static

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

I 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

相关推荐

  • c# - How do I set cookies in Playwright test - Stack Overflow

    In our old seleniumC# test suite we are setting cookies thus, and this works very well:private static

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信