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] Add tab steps #5177

Merged
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
37 changes: 37 additions & 0 deletions docs/modules/plugins/pages/plugin-web-app-playwright.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,43 @@ By. prefix is optional.
include::plugins:partial$common-web-app-steps.adoc[]
include::plugins:partial$ui-context-management-steps.adoc[]

=== Tab steps
==== Open a new tab

Opens a new browser tab and switches the focus for future commands to this tab.

[source,gherkin]
----
When I open new tab
----

.Open page in a new tab
[source,gherkin]
----
When I open new tab
Given I am on page with URL `https://docs.vividus.dev`
----

==== Close current tab

Closes the current tab and switches to the last browser tab.

[source,gherkin]
----
When I close current tab
----

NOTE: This step can only be applied to a session with multiple tabs open.

.Open URL in new tab, close it and switch to the previous page
[source,gherkin]
----
Given I am on page with URL `https://example.com`
When I open new tab
Given I am on page with URL `https://example.com/contact-us`
When I close current tab
----

=== Mouse Actions
==== Click on the element

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.vividus.ui.web.playwright.steps;

import java.net.URI;
import java.util.List;
import java.util.Optional;

import com.microsoft.playwright.Page;
Expand Down Expand Up @@ -65,7 +66,45 @@ public void openMainApplicationPage()
@Given("I am on page with URL `$pageUrl`")
public void openPage(String pageUrl)
{
Optional.ofNullable(uiContext.getCurrentPage()).orElseGet(this::openNewTab).navigate(pageUrl);
Optional.ofNullable(uiContext.getCurrentPage()).orElseGet(this::openNewPage).navigate(pageUrl);
}

/**
* Opens a new browser tab and switches the focus for future commands to this tab.
*/
@When("I open new tab")
public void openNewTab()
{
openNewPage();
}

/**
* Closes <b>current tab</b> and switches to the tab from which redirection to current tab was performed
* Actions performed at this step:
* <ul>
* <li>Receives all opened browser tabs
* <li>Closes current tab
* <li>Switches back to the last browser tab
* </ul>
*/
@When("I close current tab")
public void closeCurrentTab()
{
Page currentPage = uiContext.getCurrentPage();
List<Page> pages = browserContextProvider.get().pages();
for (int i = pages.size() - 1; i >= 0; i--)
{
Page page = pages.get(i);
if (!page.equals(currentPage))
{
currentPage.close();
uiContext.reset();
uiContext.setCurrentPage(page);
break;
}
}
softAssert.recordAssertion(!browserContextProvider.get().pages().contains(currentPage),
"Current tab has been closed");
}

/**
Expand Down Expand Up @@ -144,7 +183,7 @@ public void assertPageTitle(StringComparisonRule comparisonRule, String text)
softAssert.assertThat("Page title", uiContext.getCurrentPage().title(), comparisonRule.createMatcher(text));
}

private Page openNewTab()
private Page openNewPage()
{
uiContext.reset();
Page page = browserContextProvider.get().newPage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import java.net.URI;
import java.util.List;
import java.util.function.Consumer;

import com.microsoft.playwright.BrowserContext;
Expand All @@ -48,6 +51,7 @@
class PageStepsTests
{
private static final String PAGE_URL = "https://docs.vividus.dev";
private static final String TAB_CLOSED = "Current tab has been closed";

@Mock private BrowserContextProvider browserContextProvider;
@Mock private UiContext uiContext;
Expand Down Expand Up @@ -90,7 +94,55 @@ void shouldOpenPageInNewTab()
when(browserContext.newPage()).thenReturn(page);

pageSteps.openPage(PAGE_URL);
verifyInteractionsForOpenPageInNewTap(page);
verifyInteractionsForNewPage(page, 1);
}

@Test
void shouldOpenNewTab()
{
BrowserContext browserContext = mock();
when(browserContextProvider.get()).thenReturn(browserContext);
Page newTab = mock();
when(browserContext.newPage()).thenReturn(newTab);

pageSteps.openNewTab();
verifyInteractionsForNewPage(newTab, 0);
}

@Test
void shouldCloseCurrentTab()
{
Page currentTab = mock();
when(uiContext.getCurrentPage()).thenReturn(currentTab);

Page page1 = mock();
Page page2 = mock();
List<Page> allTabs = List.of(page1, page2, currentTab);

BrowserContext browserContext = mock();
when(browserContextProvider.get()).thenReturn(browserContext);
when(browserContext.pages()).thenReturn(allTabs).thenReturn(allTabs.subList(0, allTabs.size() - 2));

pageSteps.closeCurrentTab();
verify(currentTab).close();
verify(uiContext).reset();
verify(uiContext).setCurrentPage(page2);
verify(softAssert).recordAssertion(true, TAB_CLOSED);
}

@Test
void shouldNotCloseCurrentTabIfOnlyOneTabPresent()
{
Page currentTab = mock();
when(uiContext.getCurrentPage()).thenReturn(currentTab);

BrowserContext browserContext = mock();
when(browserContextProvider.get()).thenReturn(browserContext);
when(browserContext.pages()).thenReturn(List.of(currentTab));

pageSteps.closeCurrentTab();
verifyNoInteractions(currentTab);
verify(softAssert).recordAssertion(false, TAB_CLOSED);
}

@Test
Expand Down Expand Up @@ -144,14 +196,14 @@ void shouldAssertPageTitle()
}

@SuppressWarnings("unchecked")
private void verifyInteractionsForOpenPageInNewTap(Page page)
private void verifyInteractionsForNewPage(Page page, int navigateTimes)
{
ArgumentCaptor<Consumer<Frame>> frameConsumerCaptor = ArgumentCaptor.forClass(Consumer.class);
var ordered = inOrder(uiContext, page);
ordered.verify(uiContext).reset();
ordered.verify(uiContext).setCurrentPage(page);
ordered.verify(page).onFrameNavigated(frameConsumerCaptor.capture());
ordered.verify(page).navigate(PAGE_URL);
ordered.verify(page, times(navigateTimes)).navigate(PAGE_URL);
ordered.verifyNoMoreInteractions();
Consumer<Frame> frameConsumer = frameConsumerCaptor.getValue();
Frame frame = mock();
Expand Down
Loading