Skip to content

Commit

Permalink
Implement Cookie Steps code logic for Playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
Yauheni Hapanovich authored and Yauheni Hapanovich committed Jun 14, 2024
1 parent a42ec4c commit 2c298be
Show file tree
Hide file tree
Showing 7 changed files with 546 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/modules/plugins/pages/plugin-web-app-playwright.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,5 @@ Given I am on page with URL `https://vividus-test-site-a92k.onrender.com/`
When I execute javascript `JSON.stringify(window.performance.timing)` and save result to scenario variable `timings`
Then number of JSON elements from `${timings}` by JSON path `$.connectStart` is = 1
----

include::plugins:partial$web-cookie-steps.adoc[]
131 changes: 131 additions & 0 deletions docs/modules/plugins/partials/web-cookie-steps.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
=== Cookie Steps

==== Validate cookie presence

Validates whether the certain cookie is set.

[source,gherkin]
----
Then cookie with name `$cookieName` is set
----
* $cookieName - The name of the cookie to check presence.

.Check the session cookie is present
[source,gherkin]
----
Then cookie with name `JSESSIONID` is set
----

==== Validate cookie absence

Validates whether the certain cookie is not set.

[source,gherkin]
----
Then cookie with name `$cookieName` is not set
----
* $cookieName - The name of the cookie to check absence.

.Check the session cookie is not present
[source,gherkin]
----
Then cookie with name `JSESSIONID` is not set
----

==== Set cookies

Adds the cookies provided in the input xref:ROOT:glossary.adoc#_examplestable[ExamplesTable]. It's allowed to add the
cookies for the current domain only: make sure the web browser is opened at the
expected domain.

[source,gherkin]
----
When I set all cookies for current domain:$parameters
----
* `$parameters` - The parameters of the cookies to set as xref:ROOT:glossary.adoc#_examplestable[ExamplesTable]:
+
[cols="1,2", options="header"]
|===

|Column Name
|Description

|`cookieName`
|the name of the cookie to set

|`cookieValue`
|the value of the cookie to set

|`path`
|the path of the cookie to set

|===

.Set the cookie for the current domain
[source,gherkin]
----
When I set all cookies for current domain:
|cookieName |cookieValue |path |
|cookieAgreed |2 |/ |
----

==== Get cookie value

Finds the cookie by the name and saves its value to a variable.

[source,gherkin]
----
When I save value of cookie with name `$cookieName` to $scopes variable `$variableName`
----
* `$cookieName` - The name of the cookie to save the value.
* `$scopes` - xref:commons:variables.adoc#_scopes[The comma-separated set of the variables scopes].
* `$variableName` - The variable name to save the cookie value.

.Get the value of the session cookie
[source,gherkin]
----
When I save value of cookie with name `JSESSIONID` to scenario variable `session-id`
----

==== Get cookie

Finds the cookie by the name and saves all its parameters as JSON to a variable.

[source,gherkin]
----
When I save cookie with name `$cookieName` as JSON to $scopes variable `$variableName`
----
* `$cookieName` - The name of the cookie to save.
* `$scopes` - xref:commons:variables.adoc#_scopes[The comma-separated set of the variables scopes].
* `$variableName` - The variable name to save the cookie.

.Get the session cookie
[source,gherkin]
----
When I save cookie with name `JSESSIONID` as JSON to scenario variable `session-id`
----

==== Remove cookie

Removes the certain cookie from the current domain.

[source,gherkin]
----
When I remove cookie with name `$cookieName` from current domain
----
* `$cookieName` - The name of the cookie to remove.

.Remove the session cookie
[source,gherkin]
----
When I remove cookie with name `JSESSIONID` from current domain
----

==== Remove all cookies

Removes all cookies from the current domain.

[source,gherkin]
----
When I remove all cookies from current domain
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.ui.web.playwright.helper;

import java.util.Collections;
import java.util.List;

import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.options.Cookie;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vividus.ui.web.playwright.BrowserContextProvider;


public class CookieHelper
{
private static final Logger LOGGER = LoggerFactory.getLogger(CookieHelper.class);

private final BrowserContext browserContext;

public CookieHelper(BrowserContextProvider browserContextProvider)
{
this.browserContext = browserContextProvider.get();
}

public void addCookie(String cookieName, String cookieValue, String path, String url)
{
Cookie cookie = new Cookie(cookieName, cookieValue)
.setPath(path)
.setDomain(url);
LOGGER.debug("Adding cookie: {}", cookie);
addCookies(Collections.singletonList(cookie));
}

public void addCookies(List<Cookie> cookies)
{
browserContext.addCookies(cookies);
}

public void deleteAllCookies()
{
browserContext.clearCookies();
}

public void deleteCookie(String cookieName)
{
browserContext.clearCookies(new BrowserContext.ClearCookiesOptions().setName(cookieName));
}

public Cookie getCookie(String name)
{
for (Cookie cookie : getCookies())
{
if (cookie.name.equals(name))
{
return cookie;
}
}

return null;
}

public String getValue(Cookie cookie)
{
return cookie.value;
}

private List<Cookie> getCookies()
{
return browserContext.cookies();
}
}
Loading

0 comments on commit 2c298be

Please sign in to comment.