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?
CodePudding user response:
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()));
}
);
CodePudding user response:
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