简体   繁体   中英

Selenium webDriver - wait for cookies and local db cookies in java

I use selenium to get cookies and local db cookies in java.

if I use following code to wait the side will be ready I don't get all the cookies:

webDriverWait.until(webDriver -> "complete".equals((JavascriptExecutor) webDriver.executeScript("return document.readyState")))

I saw that if I wait enough of time eventually I will get all the cookies (regular cookies and userData cookies that saved in local db).

Is there a way to use webDriverWait to wait until all the cookies will be load?

You never know if your client loaded all the possible cookies. Because if you have asynchronous UI each new request might add new cookie or modify existing one.

So the better, more reliable and "proper" approach would be to first of all know which cookies exactly you expect to have. Say you keep the names in Set<String> cookieNames .

Then your wait would look like:

webDriverWait.until(
        webDriver -> {
            Set<Cookie> cookies = webDriver.manage().getCookies();
            return cookies.size() == cookieNames.size() &&
                    cookies.stream().allMatch(cookie -> cookieNames.contains(cookie.getName()));
        }
);

or if your frontend is using Angular, you can wait until this script is true (meaning the site is fully loaded) and then can continue with your script for fetching cookies:

return window.getAllAngularTestabilities().findIndex(x => !x.isStable()) === -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM