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] Add step to set cookies without applying changes #5143

Merged
merged 1 commit into from
Jun 21, 2024
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
42 changes: 40 additions & 2 deletions docs/modules/plugins/pages/plugin-web-app.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,44 @@ When I set all cookies for current domain:
|cookieAgreed |2 |/ |
----

==== Set cookies without applying changes

Adds the cookies provided in the input xref:ROOT:glossary.adoc#_examplestable[ExamplesTable], but does not apply the
changes in cookies. The current page must be refreshed or the navigation must be performed to apply the cookie changes.
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 without applying changes:$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 without applying changes:
|cookieName |cookieValue |path |
|cookieAgreed |2 |/ |
When I refresh page
----

==== Get cookie value

Finds the cookie by the name and saves its value to a variable.
Expand Down Expand Up @@ -849,7 +887,7 @@ When I remove cookie with name `$cookieName` from current domain
When I remove cookie with name `JSESSIONID` from current domain
----

==== Remove cookie without apply
==== Remove cookie without applying changes

Removes the certain cookie from the current domain, but does not apply the
changes in cookies. The current page must be refreshed or the navigation must
Expand Down Expand Up @@ -880,7 +918,7 @@ Removes all cookies from the current domain. The actions performed by the step:
When I remove all cookies from current domain
----

==== Remove all cookies without apply
==== Remove all cookies without applying changes

Removes all cookies from the current domain, but does not apply the changes in
cookies. The current page must be refreshed or the navigation must be performed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 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 Down Expand Up @@ -182,6 +182,32 @@ public void thenCookieWithNameIsNotSet(String cookieName)
*/
@When("I set all cookies for current domain:$parameters")
public void setAllCookies(ExamplesTable parameters)
{
setAllCookiesWithoutApply(parameters);
navigateActions.refresh();
}

/**
* Adds the cookies provided in the input ExamplesTable, but does not apply the changes in cookies. The current
* page must be refreshed or the navigation must be performed to apply the cookie changes. It's allowed to add
* the cookies for the current domain only: make sure the web browser is opened at the expected domain.
* <p>The cookie parameters to be defined in the ExamplesTable</p>
* <ul>
* <li><b>cookieName</b> - the name of the cookie to set</li>
* <li><b>cookieValue</b> - the value of the cookie to set</li>
* <li><b>path</b> - the path of the cookie to set</li>
* </ul>
* <p>Usage example:</p>
* <code>
* <br>When I set all cookies for current domain:
* <br>|cookieName |cookieValue |path |
* <br>|cookieAgreed |2 |/ |
* </code>
*
* @param parameters The parameters of the cookies to set as ExamplesTable
*/
@When("I set all cookies for current domain without applying changes:$parameters")
public void setAllCookiesWithoutApply(ExamplesTable parameters)
{
String currentUrl = webDriverProvider.get().getCurrentUrl();
Validate.isTrue(null != currentUrl,
Expand All @@ -190,7 +216,6 @@ public void setAllCookies(ExamplesTable parameters)
{
cookieManager.addCookie(row.get("cookieName"), row.get("cookieValue"), row.get("path"), currentUrl);
});
navigateActions.refresh();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 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 Down Expand Up @@ -29,6 +29,7 @@

import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

import org.jbehave.core.model.ExamplesTable;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -136,15 +137,31 @@ void testThenCookieWithNameIsNotSet()
}

@Test
void testSetAllCookies()
void shouldSetAllCookies()
{
String testUrl = "https://www.vividus.org";
testSetAllCookies(cookieSteps::setAllCookies);
verify(navigateActions).refresh();
}

@Test
void shoutSetAllCookiesWithoutApplyingChanges()
{
testSetAllCookies(cookieSteps::setAllCookiesWithoutApply);
verifyNoInteractions(navigateActions);
}

private void testSetAllCookies(Consumer<ExamplesTable> test)
{
var testUrl = "https://www.vividus.org";
mockGetCurrentPageUrl(testUrl);
String tableAsString = "|cookieName|cookieValue|path|\n|hcpsid|1|/|\n|hcpsid|1|/|";
ExamplesTable table = new ExamplesTable(tableAsString);
cookieSteps.setAllCookies(table);
var tableAsString = """
|cookieName|cookieValue|path|
|hcpsid |1 |/ |
|hcpsid |1 |/ |
""";
var table = new ExamplesTable(tableAsString);
test.accept(table);
verify(cookieManager, times(2)).addCookie("hcpsid", "1", "/", testUrl);
verify(navigateActions).refresh();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ Meta:

Scenario: Validate steps for cookie management in web application
Given I am on the main application page
When I set all cookies for current domain:

When I set all cookies for current domain without applying changes:
|cookieName |cookieValue |path|
|cookieName1|cookieValue1|/ |
|cookieName2|cookieValue2|/ |
|cookieName3|cookieValue3|/ |
Then cookie with name `cookieName1` is set
When I remove cookie with name `cookieName1` from current domain
Then cookie with name `cookieName1` is not set

When I set all cookies for current domain:
|cookieName |cookieValue |path|
|cookieName2|cookieValue2|/ |
|cookieName3|cookieValue3|/ |
When I remove cookie with name `cookieName2` from current domain without applying changes
Then cookie with name `cookieName2` is not set
When I remove all cookies from current domain
Expand Down
Loading