Skip to content

Commit

Permalink
[plugin-web-app-playwright] Add step to close browser
Browse files Browse the repository at this point in the history
  • Loading branch information
web-flow committed Jul 25, 2024
1 parent 90e727f commit 16e3b63
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 2 deletions.
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 @@ -635,3 +635,5 @@ When I execute sequence of actions:
|KEY_UP |c, ControlOrMeta |
----
====

include::partial$common-web-app-browser-steps.adoc[]
20 changes: 20 additions & 0 deletions docs/modules/plugins/partials/common-web-app-browser-steps.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== Close browser

Closes the browser.

[source,gherkin]
----
When I close browser
----

.Browser restart
[source,gherkin]
----
Given I am on page with URL `https://vividus-test-site-a92k.onrender.com`
Then number of elements found by `xpath(//img)` is = `1`
When I execute javascript `document.querySelector('[name="vividus-logo"]').remove()`
Then number of elements found by `xpath(//img)` is = `0`
When I close browser
Given I am on page with URL `https://vividus-test-site-a92k.onrender.com`
Then number of elements found by `xpath(//img)` is = `1`
----
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

public class UiContext
{
private static final Class<PlaywrightContext> PLAYWRIGHT_CONTEXT_KEY = PlaywrightContext.class;

private final TestContext testContext;

public UiContext(TestContext testContext)
Expand Down Expand Up @@ -102,6 +104,11 @@ public Locator getCurrentContexOrPageRoot()
return getInCurrentContext(context -> context, page -> page.locator("//html/body"), FrameLocator::owner);
}

public void removePlaywrightContext()
{
testContext.remove(PLAYWRIGHT_CONTEXT_KEY);
}

private <R> R getInCurrentContext(Function<Locator, R> elementContextAction, Function<Page, R> pageContextAction,
Function<FrameLocator, R> frameContextAction)
{
Expand All @@ -115,7 +122,7 @@ private <R> R getInCurrentContext(Function<Locator, R> elementContextAction, Fun

private PlaywrightContext getPlaywrightContext()
{
return testContext.get(PlaywrightContext.class, PlaywrightContext::new);
return testContext.get(PLAYWRIGHT_CONTEXT_KEY, PlaywrightContext::new);
}

private static final class PlaywrightContext
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.steps;

import org.jbehave.core.annotations.When;
import org.vividus.ui.web.playwright.BrowserContextProvider;
import org.vividus.ui.web.playwright.UiContext;

public class BrowserSteps
{
private final BrowserContextProvider browserContextProvider;
private final UiContext uiContext;

public BrowserSteps(BrowserContextProvider browserContextProvider, UiContext uiContext)
{
this.browserContextProvider = browserContextProvider;
this.uiContext = uiContext;
}

/**
* Closes the browser
*/
@When("I close browser")
public void closeBrowser()
{
browserContextProvider.closeBrowserContext();
browserContextProvider.closePlaywrightContext();
uiContext.removePlaywrightContext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<bean id="scrollSteps" class="org.vividus.ui.web.playwright.steps.ScrollSteps" />
<bean id="networkValidationSteps" class="org.vividus.ui.web.playwright.steps.NetworkValidationSteps" />
<bean id="actionsSequenceSteps" class="org.vividus.ui.web.playwright.steps.ActionsSequenceSteps" />
<bean id="browserSteps" class="org.vividus.ui.web.playwright.steps.BrowserSteps" />

<util:list id="stepBeanNames-WebPlaywrightUi" value-type="java.lang.String">
<idref bean="cookieSteps" />
Expand All @@ -94,6 +95,7 @@
<idref bean="scrollSteps" />
<idref bean="networkValidationSteps" />
<idref bean="actionsSequenceSteps" />
<idref bean="browserSteps" />
</util:list>

<util:list id="stepBeanNames-Playwright-Hooks">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
class UiContextTests
{
private static final PlaywrightLocator PLAYWRIGHT_LOCATOR = new PlaywrightLocator("xpath", "//div");
private final UiContext uiContext = new UiContext(new SimpleTestContext());
private final SimpleTestContext testContext = new SimpleTestContext();
private final UiContext uiContext = new UiContext(testContext);

static
{
Expand Down Expand Up @@ -200,6 +201,15 @@ void shouldNotThrowExceptionWhenResetFrameWithNoFrames()
assertNull(uiContext.getCurrentFrame());
}

@Test
void shouldRemovePlaywrightContext()
{
uiContext.reset();
assertEquals(1, testContext.size());
uiContext.removePlaywrightContext();
assertEquals(0, testContext.size());
}

private FrameLocator setupFrameMock(boolean visibility)
{
FrameLocator frame = mock();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.steps;

import static org.mockito.Mockito.verify;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.vividus.ui.web.playwright.BrowserContextProvider;
import org.vividus.ui.web.playwright.UiContext;

@ExtendWith(MockitoExtension.class)
class BrowserStepsTests
{
@Mock private BrowserContextProvider browserContextProvider;
@Mock private UiContext uiContext;

@Test
void shouldCloseBrowser()
{
BrowserSteps steps = new BrowserSteps(browserContextProvider, uiContext);
steps.closeBrowser();
verify(browserContextProvider).closeBrowserContext();
verify(browserContextProvider).closePlaywrightContext();
verify(uiContext).removePlaywrightContext();
}
}

0 comments on commit 16e3b63

Please sign in to comment.