-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[plugin-web-app-playwright] Add cookie steps (#5127)
- Loading branch information
1 parent
ffcb3bb
commit 7f5bd5a
Showing
19 changed files
with
866 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
vividus-extension-web-app/src/main/java/org/vividus/ui/web/action/InetAddressUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
vividus-extension-web-app/src/test/java/org/vividus/ui/web/action/InetAddressUtilsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...laywright/src/main/java/org/vividus/ui/web/playwright/action/PlaywrightCookieManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.