-
-
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.
Implement Cookie Steps code logic for Playwright
- Loading branch information
Yauheni Hapanovich
authored and
Yauheni Hapanovich
committed
Jun 26, 2024
1 parent
0ef42fe
commit a6b66d6
Showing
13 changed files
with
808 additions
and
29 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
30 changes: 30 additions & 0 deletions
30
vividus-extension-web-app/src/main/java/org/vividus/ui/web/action/IBaseCookieManager.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,30 @@ | ||
/* | ||
* 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; | ||
|
||
public interface IBaseCookieManager<T, S> | ||
{ | ||
void addCookie(String cookieName, String cookieValue, String path, String urlAsString); | ||
|
||
void deleteAllCookies(); | ||
|
||
void deleteCookie(String cookieName); | ||
|
||
T getCookie(String cookieName); | ||
|
||
S getCookies(); | ||
} |
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)); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/action/CookieManager.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,83 @@ | ||
/* | ||
* 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.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.action.IBaseCookieManager; | ||
import org.vividus.ui.web.action.InetAddressUtils; | ||
import org.vividus.ui.web.playwright.BrowserContextProvider; | ||
|
||
public class CookieManager implements IBaseCookieManager<Cookie, List<Cookie>> | ||
{ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(CookieManager.class); | ||
|
||
private final BrowserContextProvider browserContextProvider; | ||
|
||
public CookieManager(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(); | ||
} | ||
|
||
private BrowserContext getBrowserContext() | ||
{ | ||
return browserContextProvider.get(); | ||
} | ||
} |
Oops, something went wrong.