Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[plugin-web-app-playwright] Implement Cookie Steps code logic for Playwright #5127

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -350,3 +350,5 @@ include::plugins:partial$ui-element-css-property-steps.adoc[]
include::plugins:partial$ui-elements-conditional-iteration-steps.adoc[]

include::plugins:partial$ui-execution-on-each-element-steps.adoc[]

include::plugins:partial$web-cookie-steps.adoc[]
129 changes: 129 additions & 0 deletions docs/modules/plugins/partials/web-cookie-steps.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
=== 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].

[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
----
2 changes: 2 additions & 0 deletions vividus-extension-web-app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
project.description = 'VIVIDUS extension for web application testing plugins'

dependencies {
implementation project(':vividus-http-client')
implementation project(':vividus-util')
implementation(group: 'com.google.guava', name: 'guava', version: '33.2.1-jre')

testImplementation platform(group: 'org.junit', name: 'junit-bom', version: '5.10.3')
testImplementation(group: 'org.junit.jupiter', name: 'junit-jupiter')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* 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.
Expand All @@ -16,22 +16,21 @@

package org.vividus.ui.web.action;

import java.util.Set;
import java.util.Collection;

import org.apache.hc.client5.http.cookie.CookieStore;
import org.openqa.selenium.Cookie;

public interface ICookieManager
public interface CookieManager<T>
{
void addCookie(String cookieName, String cookieValue, String path, String urlAsString);

void deleteAllCookies();

void deleteCookie(String cookieName);

Cookie getCookie(String cookieName);
T getCookie(String cookieName);

Set<Cookie> getCookies();
Collection<T> getCookies();

CookieStore getCookiesAsHttpCookieStore();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.action;

import java.net.URI;

import com.google.common.net.InetAddresses;
import com.google.common.net.InternetDomainName;

public final class InetAddressUtils
{
private static final String DOMAIN_PARTS_SEPARATOR = ".";

private InetAddressUtils()
{
}

/**
* Returns the domain of the URL.
*
* @param urlAsString String representation of the URL.
* @return The domain of the URL.
*/
public static String getDomain(String urlAsString)
{
String host = URI.create(urlAsString).getHost();
return host.contains(DOMAIN_PARTS_SEPARATOR) && !InetAddresses.isInetAddress(host)
? DOMAIN_PARTS_SEPARATOR + getTopDomain(host)
: host;
}

private static String getTopDomain(String host)
{
return InternetDomainName.from(host).topPrivateDomain().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.action;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class InetAddressUtilsTests
{
@ParameterizedTest
@CsvSource({
"https://www.domain.com, .domain.com",
"http://127.0.0.1:8080, 127.0.0.1",
"http://localhost:8080, localhost"
})
void shouldGetDomain(String urlAsString, String domain)
{
assertEquals(domain, InetAddressUtils.getDomain(urlAsString));
}
}
1 change: 1 addition & 0 deletions vividus-plugin-web-app-playwright/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ configurations {

dependencies {
implementation project(':vividus-engine')
implementation project(':vividus-http-client')
implementation project(':vividus-reporter')
implementation project(':vividus-soft-assert')
implementation project(':vividus-test-context')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.action;

import java.time.Instant;
import java.util.List;
import java.util.Optional;

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

import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vividus.http.client.CookieStoreCollector;
import org.vividus.ui.web.action.CookieManager;
import org.vividus.ui.web.action.InetAddressUtils;
import org.vividus.ui.web.playwright.BrowserContextProvider;

public class PlaywrightCookieManager implements CookieManager<Cookie>
{
private static final Logger LOGGER = LoggerFactory.getLogger(PlaywrightCookieManager.class);

private final BrowserContextProvider browserContextProvider;

public PlaywrightCookieManager(BrowserContextProvider browserContextProvider)
{
this.browserContextProvider = browserContextProvider;
}

@Override
public void addCookie(String cookieName, String cookieValue, String path, String urlAsString)
{
String domain = InetAddressUtils.getDomain(urlAsString);
Cookie cookie = new Cookie(cookieName, cookieValue)
.setPath(path)
.setDomain(domain);
LOGGER.debug("Adding cookie: {}", cookie);
getBrowserContext().addCookies(List.of(cookie));
}

@Override
public void deleteAllCookies()
{
getBrowserContext().clearCookies();
}

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

@Override
public Cookie getCookie(String cookieName)
{
return getBrowserContext().cookies().stream()
.filter(cookie -> cookie.name.equals(cookieName))
.findFirst()
.orElse(null);
}

@Override
public List<Cookie> getCookies()
{
return getBrowserContext().cookies();
}

@Override
public CookieStore getCookiesAsHttpCookieStore()
{
return getCookies().stream().map(PlaywrightCookieManager::createHttpClientCookie).collect(
new CookieStoreCollector());
}

private static org.apache.hc.client5.http.cookie.Cookie createHttpClientCookie(
com.microsoft.playwright.options.Cookie cookie)
{
BasicClientCookie httpClientCookie = new BasicClientCookie(cookie.name, cookie.value);
httpClientCookie.setDomain(cookie.domain);
httpClientCookie.setPath(cookie.path);
Optional.ofNullable(cookie.expires).map(val -> Instant.ofEpochSecond(val.longValue()))
.ifPresent(httpClientCookie::setExpiryDate);
httpClientCookie.setSecure(cookie.secure);
httpClientCookie.setAttribute(org.apache.hc.client5.http.cookie.Cookie.DOMAIN_ATTR, cookie.domain);
httpClientCookie.setAttribute(org.apache.hc.client5.http.cookie.Cookie.PATH_ATTR, cookie.path);
return httpClientCookie;
}

private BrowserContext getBrowserContext()
{
return browserContextProvider.get();
}
}
Loading
Loading