How to check a cookie name is "not" present on page

Hi,

I would really appreciate some help on this one?

I have managed to get a cookie name from the browser, store it on a variable and then apply a verifymatch to a predefined string so then I can check that the cookies are there.

Example:
To the test cases I added these:
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import org.openqa.selenium.WebDriver as WebDriver

and wrote the following example where affinitty is a cookie name:

arraffinity = driver.manage().getCookieNamed(‘ARRAffinity’).getName()
WebUI.verifyMatch(‘ARRAffinity’, arraffinity, false)

This is working fine.

But now I would like to verify that certain cookies are “not” there. This is because of GDRP rules that certain cookies should not be there until we click on a accept button.

So for example, I would like to check that at this point there is “no” facebook cookie, cookie called “_fbp”

If I apply this get statement to above to a none exiting cookie
_fbp = driver.manage().getCookieNamed(’_fbp’).getName()

it will give an error message and fails. It will not save a en empty value to the variable:

_fbp = getCookieNamed("_fbp").getName() FAILED.
Reason:
java.lang.NullPointerException: Cannot invoke method getName() on null object

  • at Functional and Marketing cookies.run(Functional and Marketing cookies:32)*
  • at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)*
  • at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)*
  • at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)*
  • at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)*
  • at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)*
  • at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)*
  • at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:224)*
  • at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:106)*
  • at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:97)*
  • at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)*
  • at TempTestCase1590144699572.run(TempTestCase1590144699572.groovy:22)*

So I thought of getting maybe all list of cookies present, store them into a variable and then apply the verifytext not present

all_cookies = driver.manage().getCookies()
WebUI.verifyTextNotPresent(’_fbp’, all_cookies, FailureHandling.OPTIONAL)

But I get again this error message:

VerifyTextNotPresent("_fbp", all_cookies, OPTIONAL) FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.verifyTextNotPresent() is applicable for argument types: (java.lang.String, java.util.HashSet, com.kms.katalon.core.model.FailureHandling) values: [_fbp, [ASP.NET_SessionId=zmoawq20oeblv3rdy3pud4lo; path=/; domain=pre.yara.com, …], …]
Possible solutions: verifyTextNotPresent(java.lang.String, boolean, com.kms.katalon.core.model.FailureHandling), verifyTextNotPresent(java.lang.String, boolean), verifyTextPresent(java.lang.String, boolean, com.kms.katalon.core.model.FailureHandling), verifyAlertNotPresent(int, com.kms.katalon.core.model.FailureHandling), verifyTextPresent(java.lang.String, boolean)

  • at Functional and Marketing cookies.run(Functional and Marketing cookies:44)*
  • at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)*
  • at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)*
  • at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)*
  • at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)*
  • at*

Any ideas on how to check that the specifc cookie name is “NOT” in the cookies in the browser?

Thank you in advance!

Try something like…

try {
  _fbp = driver.manage().getCookieNamed(’_fbp’).getName()
} catch(Exception e) {
  // handle the null pointer exception
}
// rest of test case...

Hello Thomas, thank you so much for pointing me this direction. I am not a programmer so this helps a world.

I actually managed to write it like this so far:

try {
_fbp = driver.manage().getCookieNamed(’_fbp’).getName()
}
catch (Exception e) {
String _fbp = new String(‘Something went wrong’)
System.out.println(‘Something went wrong’)
}

WebUI.verifyMatch(‘Something went wrong’, _fbp, false)

I would actually like to store the string ‘Something went wrong’ in _fbp variable, so that then I can do a verify match. Since the idea is that it should be an error. I am expecting that the cookie should not be there.

With the previous statement is not being taken I get an error on the verify match part, I am possibly not writting it the right way. Could you please give me a hint on how to write it the right way?

Result:

e[39m8: trye[0;39m
2020-05-25 10:39:45.964 e[39mDEBUGe[0;39m e[36mt.Functional and Marketing cookies -e[0;39m e[39m1: _fbp = getCookieNamed("_fbp").getName()e[0;39m
2020-05-25 10:39:45.991 e[39mDEBUGe[0;39m e[36mt.Functional and Marketing cookies -e[0;39m e[39m9: catch (Exception e)e[0;39m
2020-05-25 10:39:45.993 e[39mDEBUGe[0;39m e[36mt.Functional and Marketing cookies -e[0;39m e[39m1: _fbp = new java.lang.String(Something went wrong)e[0;39m
2020-05-25 10:39:45.994 e[39mDEBUGe[0;39m e[36mt.Functional and Marketing cookies -e[0;39m e[39m2: out.println(“Something went wrong”)e[0;39m
Something went wrong
2020-05-25 10:39:46.012 e[39mDEBUGe[0;39m e[36mt.Functional and Marketing cookies -e[0;39m e[39m11: verifyMatch(“Something went wrong”, _fbp, false)e[0;39m
2020-05-25 10:39:46.029 e[1;31mERRORe[0;39m e[36mc.k.katalon.core.main.TestCaseExecutor -e[0;39m e[31m
:x: verifyMatch(“Something went wrong”, _fbp, false) FAILED.e[0;39m

e[31mReason:e[0;39m
e[31mgroovy.lang.MissingPropertyException: No such property: _fbp for class: Script1590139992204e[0;39m
e[31m at Functional and Marketing cookies.run(Functional and Marketing cookies:41)e[0;39m
e[31m at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)e[0;39m
e[31m at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)e[0;39m
e[31m at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:328)e[0;39m
e[31m at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:319)e[0;39m
e[31m at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:298)e[0;39m
e[31m at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:290)

It will be highly appreciated with some help!
Alexander