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 9050e6f
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 4 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 @@ -56,7 +56,13 @@ public BrowserContextProvider(BrowserType browserType, LaunchOptions launchOptio
public BrowserContext get()
{
return testContext.get(BROWSER_CONTEXT_KEY, () -> {
BrowserContext browserContext = getPlaywrightContext().browser.newContext();
PlaywrightContext context = getPlaywrightContext();
Browser browser = getPlaywrightContext().browser;
if (!browser.isConnected())
{
browser = launchBrowserWithCurrentOptions(context.playwright);
}
BrowserContext browserContext = browser.newContext();
networkContext.listenNetwork(browserContext);
if (browserContextConfiguration.isTracingEnabled())
{
Expand All @@ -72,7 +78,7 @@ public PlaywrightContext getPlaywrightContext()
{
return testContext.get(PLAYWRIGHT_CONTEXT_KEY, () -> {
Playwright playwright = Playwright.create();
Browser browser = browserType.launchBrowser(playwright, launchOptions);
Browser browser = launchBrowserWithCurrentOptions(playwright);
return new PlaywrightContext(playwright, browser);
});
}
Expand Down Expand Up @@ -106,6 +112,17 @@ public void closePlaywrightContext()
});
}

public void closeBrowserInstance()
{
Optional.ofNullable(testContext.get(PLAYWRIGHT_CONTEXT_KEY, PlaywrightContext.class))
.ifPresent(playwrightContext -> playwrightContext.browser.close());
}

private Browser launchBrowserWithCurrentOptions(Playwright playwright)
{
return browserType.launchBrowser(playwright, launchOptions);
}

private record PlaywrightContext(Playwright playwright, Browser browser)
{
}
Expand Down
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.closeBrowserInstance();
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 @@ -22,10 +22,12 @@
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down Expand Up @@ -90,6 +92,7 @@ void shouldCreateBrowserContextAtFirstRetrievalOnly()
Playwright playwright = mock();
playwrightStaticMock.when(Playwright::create).thenReturn(playwright);
Browser browser = mock();
when(browser.isConnected()).thenReturn(true);
when(browserType.launchBrowser(playwright, launchOptions)).thenReturn(browser);
BrowserContext browserContext = mock();
when(browser.newContext()).thenReturn(browserContext);
Expand Down Expand Up @@ -122,6 +125,23 @@ void shouldCreateBrowserContextAtFirstRetrievalOnly()
}
}

@Test
void shouldReCreateClosedBrowser()
{
Browser browserToClose = mock();
Browser newBrowserInstance = mock();
when(browserType.launchBrowser(any(), any())).thenReturn(browserToClose).thenReturn(newBrowserInstance);
when(browserToClose.isConnected()).thenReturn(false);
when(newBrowserInstance.newContext()).thenReturn(mock(BrowserContext.class));

browserContextProvider.get();
browserContextProvider.closeBrowserInstance();
browserContextProvider.get();

verify(browserToClose).close();
verify(browserType, times(2)).launchBrowser(any(), any());
}

static Stream<Arguments> tracingConfigurations()
{
return Stream.of(
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).closeBrowserInstance();
verify(uiContext).removePlaywrightContext();
}
}

0 comments on commit 9050e6f

Please sign in to comment.