Delete cookies does not work

Hello, i started using the option delete cookies:

  • WebUI.deleteAllCookies()

The line above does not seem to work. I even tried it within a custom keyword, still not working.

Anyone please?
@kazurayam

We need more information to help you. I believe that “WebUI.deleteAllCookies()” works very well for others. How do you tell it does not work for you? What do you expect to see, what do you actually see? Any screenshot? Console message? How does your script look like?

Hello @kazurayam, good morning.

I am using a chrome profile to do my tests. In my script, at the end of my test, i called the WebUI.deleteAllCookies(). So, i’ll give you screenshot of my browser before and after executing the test.

Before:

Expected After:

Actual After:

So you see that the delete cookies is not working.

No, I do not see.

All I see is that in the screenshot named “Actual After” there is an image labeled “Application T…” which is missing in another screenshot named “Expected After”. I do not know how this web page is compiled by web application. I do not know the condition when the “Application T…” appears and when not. It is not obvious to me if any cookie causes this difference.

If you want to check how WebUI.deleteAllCookie() behaves, you should rather write a JavaScript code which retrieves cookies linked to that URL and ran it. How to? You can learn it at https://www.w3schools.com/js/js_cookies.asp

A sample Katalon Test Case script that manipulates cookies:

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebUI.openBrowser('')
WebUI.navigateToUrl('https://www.google.com/')
WebUI.waitForPageLoad(10)

String s = WebUI.executeJavaScript("return document.cookie", null)
println("1st cookie: " + s)

WebUI.executeJavaScript('document.cookie = "username=John Doe; expires=Thu, 18 Dec 2021 12:00:00 UTC";', null)
s = WebUI.executeJavaScript("return document.cookie", null)
println("2nd cookie: " + s)

WebUI.deleteAllCookies()

s = WebUI.executeJavaScript("return document.cookie", null)
println("3rd cookie: " + s)

WebUI.closeBrowser()

When I ran this, I saw in the console;:

2021-03-23 15:01:28.185 DEBUG testcase.TCf                             - 2: navigateToUrl("https://www.google.com/")
2021-03-23 15:01:31.404 DEBUG testcase.TCf                             - 3: waitForPageLoad(10)
2021-03-23 15:01:31.614 DEBUG testcase.TCf                             - 4: s = executeJavaScript("return document.cookie", null)
2021-03-23 15:01:31.710 DEBUG testcase.TCf                             - 5: println("1st cookie: " + s)
1st cookie: 1P_JAR=2021-03-23-06
2021-03-23 15:01:31.715 DEBUG testcase.TCf                             - 6: executeJavaScript("document.cookie = "username=John Doe; expires=Thu, 18 Dec 2021 12:00:00 UTC";", null)
2021-03-23 15:01:31.745 DEBUG testcase.TCf                             - 7: s = executeJavaScript("return document.cookie", null)
2021-03-23 15:01:31.785 DEBUG testcase.TCf                             - 8: println("2nd cookie: " + s)
2nd cookie: 1P_JAR=2021-03-23-06; username=John Doe
2021-03-23 15:01:31.787 DEBUG testcase.TCf                             - 9: deleteAllCookies()
2021-03-23 15:01:32.044 DEBUG testcase.TCf                             - 10: s = executeJavaScript("return document.cookie", null)
2021-03-23 15:01:32.081 DEBUG testcase.TCf                             - 11: println("3rd cookie: " + s)
3rd cookie: 
2021-03-23 15:01:32.082 DEBUG testcase.TCf                             - 12: closeBrowser()
2021-03-23 15:01:32.198 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/TCf

As you can see, WebUI.deleteAllCookies() successfully deleted the cookies stored in the browser on my machine for the URL.

@ziyaad.ellahee

Please prove in this way that WebUI.delteAllCookies() does not work for you.

Hello @kazurayam,
this is my custom keyword for deleting cookies:

package open_browser
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
class delete_all_cookies{
/**
* Delete all cookies after browser is opened
*/
@Keyword
static deleteCookies() {

	WebDriver webDriver = DriverFactory.getWebDriver()
	webDriver.get('chrome://settings/clearBrowserData')
	WebUI.delay(5)
	webDriver.findElement(By.cssSelector(".#clearBrowsingDataConfirm")).click()

}

}

I obtained the screen below, but it does not click on the button clear data.

CSS Selector ".clearBrowsingDataConfirm" and "#clearBrowsingDataConfirm" would be syntactically correct.

But ".#clearBrowsingDataConfirm" does not make sense as a CSS Selector.

In the initial post, you wrote:

Now you shared your code where I can not find WebUI.deleteAllCoocikes() at all. I am puzzled. I do not see what you asking to us.

Well, i will draft everything in an attachment and upload it.

Hello @kazurayam, please find attached document whereby i have listed the issue in details regarding the delete cookies. I hope to have a response soon.

I will describe my testcase with screenshot below.zip (989.0 KB)

I quickly went through your document. The document is not clear enough to me.

  1. In the doc you gave many page screenshots and descriptions. It is not clear if you operate those pages manually or your script(s) operate them. Especially, you wrote >“Open Chrome browser (a chrome profile which I named is as poc)” — do you do this manually? or by script?
  2. I assume you do “Open Chrome browser (a chrome profile which I named is as poc)” manually by hand. Then which Chrome Profile do you use for your test scripts?

I guess, your test case script opens browser using WebUI.openBrowser() keyword without paying any attention which Chrome Profile to use. I guess, your test scripts opens Chrome browser without specifyinng --use-data-dir option.

Then, the Chrome browser which was activated by your test case script will run with a Chrome Profile other than poc. The Chrome Profile poc contains valid cookies, and other Profiles can not touch the cookies of poc at all. Therefore your test case can NOT read/write/delete the cookies stored in the poc profile.

It seems you want to drive multiple webdriver-browser sessions. In the 1st webdirver-browser session, you create some cookies, and close the session. And in the 2nd webdriver-browser session, you want to modify/delete cookies created in the 1st session.

I do not think you can do it. I suppose Chrome browser does not allow you to do it. I have already expressed my opinion to your plan:

The screenshots shows the steps, both manually and automated it works, except that last part where it has to clear the browser which is not working.

@kazurayam trust you doing well. Want to share with you that i was successful in implementing my case scenario as described in the attached document i have uploaded. Indeed the delete cookies works as well. Thank you.

Hello @kazurayam

Thx ur text code.
I found that it doesn’t work on safari.

I am doing a web testing on IOS safari. It also happens.
Now i use Appium to go into settings to clear cookies.
Is there a better way to clear safari’s cookies.
Looking forward to your reply. :slight_smile:

2022-06-15 08:39:05.131 INFO  c.k.k.core.webui.driver.DriverFactory    - browser = Safari 15
2022-06-15 08:39:05.133 INFO  c.k.k.core.webui.driver.DriverFactory    - platform = Mac OS X
2022-06-15 08:39:05.133 INFO  c.k.k.core.webui.driver.DriverFactory    - seleniumVersion = 3.141.59
2022-06-15 08:39:05.141 INFO  c.k.k.core.webui.driver.DriverFactory    - proxyInformation = ProxyInformation { proxyOption=NO_PROXY, proxyServerType=HTTP, username=, password=********, proxyServerAddress=, proxyServerPort=0, executionList="", isApplyToDesiredCapabilities=true }
2022-06-15 08:39:05.157 DEBUG testcase.demo                            - 2: navigateToUrl("https://www.google.com/")
2022-06-15 08:39:07.196 DEBUG testcase.demo                            - 3: waitForPageLoad(10)
2022-06-15 08:39:08.981 DEBUG testcase.demo                            - 4: s = executeJavaScript("return document.cookie", null)
2022-06-15 08:39:09.045 DEBUG testcase.demo                            - 5: println("1st cookie: " + s)
1st cookie: 1P_JAR=2022-06-15-00; OGPC=19027681-1:; PREF=ID=fc733cc6307382a5:FF=0:TM=1655253545:LM=1655253546:V=1:S=9FYQn-ntLivrYe91
2022-06-15 08:39:09.054 DEBUG testcase.demo                            - 6: executeJavaScript("document.cookie = "username=John Doe; expires=Thu, 18 Dec 2021 12:00:00 UTC";", null)
2022-06-15 08:39:09.074 DEBUG testcase.demo                            - 7: s = executeJavaScript("return document.cookie", null)
2022-06-15 08:39:09.091 DEBUG testcase.demo                            - 8: println("2nd cookie: " + s)
2nd cookie: 1P_JAR=2022-06-15-00; OGPC=19027681-1:; PREF=ID=fc733cc6307382a5:FF=0:TM=1655253545:LM=1655253546:V=1:S=9FYQn-ntLivrYe91
2022-06-15 08:39:09.092 DEBUG testcase.demo                            - 9: deleteAllCookies()
2022-06-15 08:39:09.119 DEBUG testcase.demo                            - 10: s = executeJavaScript("return document.cookie", null)
2022-06-15 08:39:09.138 DEBUG testcase.demo                            - 11: println("3rd cookie: " + s)
3rd cookie: 1P_JAR=2022-06-15-00; OGPC=19027681-1:; PREF=ID=fc733cc6307382a5:FF=0:TM=1655253545:LM=1655253546:V=1:S=9FYQn-ntLivrYe91
2022-06-15 08:39:09.139 DEBUG testcase.demo                            - 12: closeBrowser()
2022-06-15 08:39:09.480 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/demo