microsoft / playwright-java

Java version of the Playwright testing and automation library
https://playwright.dev/java/
Apache License 2.0
1.13k stars 205 forks source link

[Bug]: "APIRequest: Failure to Set SameSite Attribute for Cookies, Mismatch between SameSiteAttribute Enumeration Value and Required Value" #1607

Closed Dream-wu closed 3 months ago

Dream-wu commented 3 months ago

Version

1.41.0

Steps to reproduce

for example:

@BeforeClass
    public void beforeClass() throws Exception{
        playwright = Playwright.create();
        Account account = accountMapper.selectByState(1);
        if (account != null) {
            Cookie uid= new Cookie("uid", AutoCheckApi.uid);
        uid.setDomain(".xx.com").setPath("/").setExpires(0).setHttpOnly(true).setSecure(true).setSameSite(SameSiteAttribute.LAX);
            _cookies = Arrays.asList(uid);
            Gson gson = new Gson();
            JSONObject obj = new JSONObject();
            obj.put("cookies", _cookies);
            jsonObj = gson.toJson(obj);
            System.out.println(jsonObj);
        }
    }

    @Test
    public void testPostApi() {
        System.out.println("json:" + jsonObj);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        request = playwright.request().newContext(new APIRequest.NewContextOptions()
                .setBaseURL("xxxx")
                .setExtraHTTPHeaders(headers)
                .setStorageState(jsonObj));    // 这里设置cookie

        Map<String, String> data = new HashMap<>();
        data.put("pageNum", "1");
        data.put("type", "1");
        APIResponse postAPIResponse = request.post("xxxx/list",
                RequestOptions.create().setData(data));
        System.out.println(postAPIResponse.text());
        Assert.assertTrue(postAPIResponse.ok()); 
    }

Expected behavior

I am attempting to use the setSameSite method to set the SameSite attribute for cookies, but I am experiencing issues where the value is not being set as expected.

Actual behavior

com.microsoft.playwright.PlaywrightException: Error {
  message='storageState.cookies[0].sameSite: expected one of (Strict|Lax|None)
  name='Error
  stack='Error: storageState.cookies[0].sameSite: expected one of (Strict|Lax|None)
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:121:33
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:107:21
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:136:12
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:97:34
    at Array.map (<anonymous>)
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:97:16
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:90:12
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:107:21
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:90:12
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:107:21
}

    at com.microsoft.playwright.impl.WaitableResult.get(WaitableResult.java:54)
    at com.microsoft.playwright.impl.ChannelOwner.runUntil(ChannelOwner.java:120)com.microsoft.playwright.PlaywrightException: Error {
  message='storageState.cookies[0].sameSite: expected one of (Strict|Lax|None)
  name='Error
  stack='Error: storageState.cookies[0].sameSite: expected one of (Strict|Lax|None)
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:121:33
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:107:21
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:136:12
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:97:34
    at Array.map (<anonymous>)
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:97:16
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:90:12
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:107:21
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:90:12
    at C:\Users\daojia\AppData\Local\Temp\playwright-java-4265017127747444911\package\lib\protocol\validatorPrimitives.js:107:21
}

    at com.microsoft.playwright.impl.WaitableResult.get(WaitableResult.java:54)
    at com.microsoft.playwright.impl.ChannelOwner.runUntil(ChannelOwner.java:120)

Additional context

The two values do not match image image

Environment

yury-s commented 3 months ago

I don't see any calls to addCookies. Are you trying to set storage state to manually created json? Note that the value you set is expected to have been obtained via BrowserContext.storageState() rather than manually crafted. Can you share complete self contained project that demonstrates the problem?

yury-s commented 3 months ago

Yeah, the problem is that you try to manually create the storage state and has incompatible value for the same site attribute (LAX vs Lax). Even though you may get some luck with that, the format is considered an implementation detail and you should create it via BrowserContext.storageState(). You can create a context, add a bunch of cookies and then export them to a string using that method instead of manually calling Gson. That way you'll get storage state in correct format.

Dream-wu commented 3 months ago

Yeah, the problem is that you try to manually create the storage state and has incompatible value for the same site attribute (LAX vs Lax). Even though you may get some luck with that, the format is considered an implementation detail and you should create it via BrowserContext.storageState(). You can create a context, add a bunch of cookies and then export them to a string using that method instead of manually calling Gson. That way you'll get storage state in correct format.

now, I am using new APIRequest.NewContextOptions() not BrowserContext

Dream-wu commented 3 months ago

Yeah, the problem is that you try to manually create the storage state and has incompatible value for the same site attribute (LAX vs Lax). Even though you may get some luck with that, the format is considered an implementation detail and you should create it via BrowserContext.storageState(). You can create a context, add a bunch of cookies and then export them to a string using that method instead of manually calling Gson. That way you'll get storage state in correct format.

now, I am using new APIRequest.NewContextOptions() not BrowserContext