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

Conversation

yauhenhapan
Copy link
Contributor

  • implement code logic for Playwright's cookie
  • write corresponding tests
  • write documentation

@yauhenhapan yauhenhapan requested a review from a team as a code owner June 14, 2024 08:16
@yauhenhapan yauhenhapan force-pushed the feature/implement_cookie_steps branch 3 times, most recently from 8847e38 to 54d6034 Compare June 14, 2024 12:25
@yauhenhapan yauhenhapan force-pushed the feature/implement_cookie_steps branch 3 times, most recently from 938a635 to 1bb6f04 Compare June 21, 2024 13:08
@valfirst valfirst force-pushed the feature/implement_cookie_steps branch from 1bb6f04 to 6de531b Compare June 21, 2024 13:52
import org.slf4j.LoggerFactory;
import org.vividus.ui.web.playwright.BrowserContextProvider;

public class CookieHelper
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class CookieHelper
public class CookieManager

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to implement ICookieManager ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • the name was changed to CookieManager
  • implemented ICookieManager

==== 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it true for Playwright?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checked that Playwright stores all together, so I removed the sentence It's allowed to add the cookies for the current domain only:...

.setPath(path)
.setDomain(domain);
LOGGER.debug("Adding cookie: {}", cookie);
addCookies(Collections.singletonList(cookie));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
addCookies(Collections.singletonList(cookie));
addCookies(List.of(cookie));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored, thanks

String host = URI.create(url).getHost();
String domain = host.contains(DOMAIN_PARTS_SEPARATOR) && !InetAddresses.isInetAddress(host)
? DOMAIN_PARTS_SEPARATOR + getTopDomain(host)
: host;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please avoid copy-pasting the logic, it's rather recommended to use common logic to common place and re-use in both plugins

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this common logic in DomainManager class in vividus-extension-web-app package


public Cookie getCookie(String name)
{
for (Cookie cookie : getBrowserContext().cookies())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Java Streams API

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored, thanks

private static final String COOKIE_VALUE = "cookieValue";
private static final String DOMAIN = "https://www.domain.com";

@InjectMocks private CookieHelper cookieHelper;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should go after all fields of type @Mock

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks


@InjectMocks private CookieHelper cookieHelper;
@Mock private BrowserContextProvider browserContextProvider;
@Captor private ArgumentCaptor<BrowserContext.ClearCookiesOptions> clearCookiesOptionsCaptor;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's used once and thus it can be inlined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored, thanks

@InjectMocks private CookieHelper cookieHelper;
@Mock private BrowserContextProvider browserContextProvider;
@Captor private ArgumentCaptor<BrowserContext.ClearCookiesOptions> clearCookiesOptionsCaptor;
@Captor private ArgumentCaptor<List<Cookie>> cookieCaptor;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's used once and thus it can be inlined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored, thanks

@Mock private CookieHelper cookieHelper;
@Mock private VariableContext variableContext;
@Mock private ISoftAssert softAssert;
@InjectMocks private CookieSteps cookieSteps;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should go after all fields of type @Mock

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not needed, I believe it is equal to the same story used for Selenium-based steps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i removed this file since we had discussed it's unnecessary right now

@yauhenhapan yauhenhapan force-pushed the feature/implement_cookie_steps branch 4 times, most recently from 2816c96 to 8426539 Compare June 26, 2024 07:32
Copy link

codecov bot commented Jun 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.43%. Comparing base (8dc4a57) to head (43fb77f).
Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5127      +/-   ##
============================================
- Coverage     97.43%   97.43%   -0.01%     
- Complexity     6747     6999     +252     
============================================
  Files           933      936       +3     
  Lines         19520    19580      +60     
  Branches       1298     1299       +1     
============================================
+ Hits          19020    19078      +58     
- Misses          391      392       +1     
- Partials        109      110       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

public final class DomainManager
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public final class DomainManager
public final class InetAddressUtils

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed, thanks

import org.junit.jupiter.params.provider.CsvSource;
import org.vividus.ui.web.action.DomainManager;

public class DomainManagerTests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class DomainManagerTests
class DomainManagerTests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed, thanks

"http://127.0.0.1:8080, 127.0.0.1",
"http://localhost:8080, localhost"
})
void shouldAddCookie(String urlAsString, String domain)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update method name to reflect the actual test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, thanks

* limitations under the License.
*/

package org.vividus.ui.web.playwright.action;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to vividus-extension-web-app and reuse in both vividus-plugin-web-app and vividus-plugin-web-app-plyawright

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

@Override
public String getValue(Cookie cookie)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this method

import org.vividus.ui.web.playwright.BrowserContextProvider;

@ExtendWith(MockitoExtension.class)
public class CookieManagerTests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class CookieManagerTests
class CookieManagerTests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed, thanks

import org.vividus.variable.VariableScope;

@ExtendWith(MockitoExtension.class)
public class CookieStepsTests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class CookieStepsTests
class CookieStepsTests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed, thanks

@yauhenhapan yauhenhapan force-pushed the feature/implement_cookie_steps branch from 8426539 to a6b66d6 Compare June 26, 2024 17:43

T getCookie(String cookieName);

S getCookies();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
S getCookies();
Collection<T> getCookies();

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, thanks


package org.vividus.ui.web.action;

public interface IBaseCookieManager<T, S>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public interface IBaseCookieManager<T, S>
public interface CookieManager<T, S>

(feel free to rename current classes to PlaywrightCookieManager and WebDriverCookieManager)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, thanks

Cookie getCookie(String cookieName);

Set<Cookie> getCookies();

CookieStore getCookiesAsHttpCookieStore();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's implement this method for playwright cookie manager as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@yauhenhapan yauhenhapan force-pushed the feature/implement_cookie_steps branch 2 times, most recently from 1ba9122 to 8a760f5 Compare June 27, 2024 15:09

public interface ICookieManager
public interface ICookieManager<T>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public interface ICookieManager<T>
public interface CookieManager<T>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed, thanks

@yauhenhapan yauhenhapan force-pushed the feature/implement_cookie_steps branch from 8a760f5 to 43fb77f Compare June 28, 2024 07:58
@valfirst valfirst merged commit 7f5bd5a into vividus-framework:master Jun 28, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants