Skip to content

Commit

Permalink
[plugin-web-app-playwright] Add steps for text manipulations and vali…
Browse files Browse the repository at this point in the history
…dations (#4678)
  • Loading branch information
valfirst authored Dec 19, 2023
1 parent 46a2243 commit 443d293
Show file tree
Hide file tree
Showing 23 changed files with 612 additions and 226 deletions.
1 change: 1 addition & 0 deletions docs/modules/plugins/pages/plugin-mobile-app.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ When I download file `/sdcard/file.json` from device and save its content to sce

include::partial$ui-context-management-steps.adoc[]

include::plugins:partial$ui-selenium-text-content-steps.adoc[]

=== Navigate back

Expand Down
23 changes: 23 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,3 +106,26 @@ When I clear field located by `$locator`
----
When I clear field located by `id(email)`
----

include::plugins:partial$ui-text-content-steps.adoc[]

==== Save the text of the context

Saves the text of the context into a variable.

[source,gherkin]
----
When I save text of context to $scopes variable `$variableName`
----

* `$scopes` - The comma-separated set of the xref:commons:variables.adoc#_scopes[variables scopes].
* `$variableName` - The name of the variable to save the text content.

.Save the text of the context
[source,gherkin]
----
When I change context to element located by `id(username)`
When I save text of context element to scneario variable `username`
----

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

The context can be set by the <<_change_context,corresponding steps>>. If no context is set, the text will be searched across the whole page.


==== Validate the text exists

Validates the text is present in the current context. The expected text is *case-sensitive*.

[source,gherkin]
----
Then text `$text` exists
----
* `$text` - The expected text to be found in the context text.

.Check the text 'Contract Us' is present on the page
[source,gherkin]
----
Given I am on page with URL `https://docs.vividus.dev/`
Then text `Contact Us` exists
----


==== Validate the text does not exists

Validates the text is not present in the current context.

[source,gherkin]
----
Then text `$text` does not exist
----
* `$text` - The text that should not be present in the context.

.Check the text 'Deprecated' is not present in the element
[source,gherkin]
----
When I change context to element located by `id(code)`
Then text `Deprecated` does not exist
----


==== Validate the text matches regular expression

Validates the text from current context matches the specified regular expression.

[source,gherkin]
----
Then text matches `$regex`
----
* `$regex` - The https://www.regular-expressions.info[regular expression] used to validate the context text.

.Check the text with pattern 'User ".*" successfully logged in' is present in the current context
[source,gherkin]
----
When I change context to element located by `id(message)`
Then text matches `User ".*" successfully logged in`
----
43 changes: 0 additions & 43 deletions docs/modules/plugins/partials/generic-ui-steps.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -197,49 +197,6 @@ Then number of $state elements found by `$locator` is $comparisonRule `$quantity
Then number of VISIBLE elements found by `tagName(img)` is = `1`
----

=== Save text of an element

Saves text of an element into a variable.

[source,gherkin]
----
When I save text of element located by `$locator` to $scopes variable `$variableName`
----

* `$locator` - <<_locator>>.
* `$scopes` - xref:commons:variables.adoc#_scopes[The comma-separated set of the variables scopes].
* `$variableName` - The name of the variable to save the text.

.Save the text of H1 element
[source,gherkin]
----
When I save text of element located by `tagName(h1)` to scenario variable `headingText`
----


=== Save the text of the context

Saves the text of the context element into a variable.

[WARNING]
Step will throw an error if the context element is not set.

[source,gherkin]
----
When I save text of context element to $scopes variable `$variableName`
----

* `$scopes` - xref:commons:variables.adoc#_scopes[The comma-separated set of the variables scopes].
* `$variableName` - The name of the variable to save the text.

.Save the text of the context element
[source,gherkin]
----
When I change context to element located by `id(username)`
When I save text of context element to SCENARIO variable `username`
----


=== Saves the attribute value of the context

Saves the attribute value of the context element into a variable.
Expand Down
67 changes: 2 additions & 65 deletions docs/modules/plugins/partials/plugin-web-app-steps.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -170,72 +170,9 @@ Then page is scrolled to element located by `$locator`
Then page is scrolled to element located by `xpath(//a[text()="Contact"])`
----

=== Text validation steps
include::plugins:partial$ui-selenium-text-content-steps.adoc[]
include::plugins:partial$common-web-app-text-steps.adoc[]

:context-description: The context can be set by the <<_change_context,corresponding steps>>. If no context is set, the text will be searched on the whole page.

==== Validate the text exists

Validates that the text is presented in the current context. Expected text is *case sensitive*.

{context-description}

[source,gherkin]
----
Then text `$text` exists
----
* `$text` - Expected text.

.Check the text 'Contract Us' is presented on the page
[source,gherkin]
----
Given I am on page with URL `https://docs.vividus.dev/`
Then text `Contract Us` exists
----

==== Validate the text does not exists

Validates that the text is not presented in the current context.

{context-description}

[source,gherkin]
----
Then text `$text` does not exist
----
* `$text` - Text that should not exist.

.Check the text 'Deprecated' is not presented in the element
[source,gherkin]
----
When I change context to element located by `id(code)`
Then text `Deprecated` does not exist
----

==== Validate the text matches regex

Validates that the text from current context matches the specified regular expression.

{context-description}

[source,gherkin]
----
Then text matches `$regex`
----

_Deprecated syntax (will be removed in VIVIDUS 0.7.0)_:
[source,gherkin]
----
Then the text matches '$regex'
----
* `$regex` - The https://www.regular-expressions.info[regular expression] used to validate the text of the context.

.Check the text with pattern 'User ".*" successfully logged in' is presented in the current context
[source,gherkin]
----
When I change context to element located by `id(code)`
Then text matches `User ".*" successfully logged in`
----

=== Tab steps
==== Open a new tab
Expand Down
23 changes: 23 additions & 0 deletions docs/modules/plugins/partials/ui-selenium-text-content-steps.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
include::plugins:partial$ui-text-content-steps.adoc[]

==== Save the text of the context

Saves the text of the context element into a variable.

[NOTE]
An error is thrown if the context is not set to the element.

[source,gherkin]
----
When I save text of context element to $scopes variable `$variableName`
----

* `$scopes` - The comma-separated set of the xref:commons:variables.adoc#_scopes[variables scopes].
* `$variableName` - The name of the variable to save the text content.

.Save the text of the context element
[source,gherkin]
----
When I change context to element located by `id(username)`
When I save text of context element to scneario variable `username`
----
20 changes: 20 additions & 0 deletions docs/modules/plugins/partials/ui-text-content-steps.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== Text content manupulations

==== Save the text of an element

Finds the element by the given locator and saves its text into a variable.

[source,gherkin]
----
When I save text of element located by `$locator` to $scopes variable `$variableName`
----

* `$locator` - The <<_locator,locator>> used to find the element whose text content will be saved.
* `$scopes` - The comma-separated set of the xref:commons:variables.adoc#_scopes[variables scopes].
* `$variableName` - The name of the variable to save the text content.

.Save the text of the header element
[source,gherkin]
----
When I save text of element located by `id(header)` to scenario variable `heading-text`
----
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<property name="storyLoader" ref="storyLoader" />
<property name="storyReporterBuilder" ref="storyReporterBuilder" />
<property name="placeholderResolver" ref="placeholderResolver" />
<property name="compositePaths" value="${engine.composite-paths},steps/defaults/*.steps"/>
<property name="compositePaths" value="${internal.engine.composite-paths}"/>
<property name="aliasPaths" value="${engine.alias-paths}"/>
<property name="storyControls" ref="springStoryControls" />
<property name="parallelStoryExamplesEnabled" value="${bdd.configuration.parallel-story-examples-enabled}"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,18 @@ public GenericSetVariableSteps(ISoftAssert softAssert, IBaseValidations baseVali
}

/**
* Extracts the <b>text</b> of element found in the context and saves it to the <b>variable</b> with the specified
* <b>variableName</b>
* Actions performed at this step:
* <ul>
* <li>Saves the text of element into the <i>variable name</i>
* </ul>
* @param scopes The set (comma separated list of scopes e.g.: STORY, NEXT_BATCHES) of variable's scope<br>
* <i>Available scopes:</i>
* <ul>
* <li><b>STEP</b> - the variable will be available only within the step
* <li><b>SCENARIO</b> - the variable will be available only within the scenario
* <li><b>STORY</b> - the variable will be available within the whole story
* <li><b>NEXT_BATCHES</b> - the variable will be available starting from next batch
* </ul>
* @param variableName A name under which the value should be saved
* Saves the text of the context element into a variable.
*
* @param scopes The set (comma separated list of scopes e.g.: STORY, NEXT_BATCHES) of the variable scopes.
* <br>
* <i>Available scopes:</i>
* <ul>
* <li><b>STEP</b> - the variable will be available only within the step,
* <li><b>SCENARIO</b> - the variable will be available only within the scenario,
* <li><b>STORY</b> - the variable will be available within the whole story,
* <li><b>NEXT_BATCHES</b> - the variable will be available starting from next batch
* </ul>
* @param variableName The name of the variable to save the text content.
*/
@When("I save text of context element to $scopes variable `$variableName`")
public void saveContextElementTextToVariable(Set<VariableScope> scopes, String variableName)
Expand All @@ -79,18 +76,19 @@ public void saveContextElementTextToVariable(Set<VariableScope> scopes, String v
}

/**
* Saves text of an element into a variable.
* Finds the element by the given locator and saves its text into a variable.
*
* @param locator The locator to find an element
* @param scopes The set (comma separated list of scopes e.g.: STORY, NEXT_BATCHES) of variable's scope<br>
* <i>Available scopes:</i>
* <ul>
* <li><b>STEP</b> - the variable will be available only within the step,
* <li><b>SCENARIO</b> - the variable will be available only within the scenario,
* <li><b>STORY</b> - the variable will be available within the whole story,
* <li><b>NEXT_BATCHES</b> - the variable will be available starting from next batch
* </ul>
* @param variableName the variable name to store the text.
* @param locator The locator used to find the element whose text content will be saved.
* @param scopes The set (comma separated list of scopes e.g.: STORY, NEXT_BATCHES) of the variable scopes.
* <br>
* <i>Available scopes:</i>
* <ul>
* <li><b>STEP</b> - the variable will be available only within the step,
* <li><b>SCENARIO</b> - the variable will be available only within the scenario,
* <li><b>STORY</b> - the variable will be available within the whole story,
* <li><b>NEXT_BATCHES</b> - the variable will be available starting from next batch
* </ul>
* @param variableName The name of the variable to save the text content.
*/
@When("I save text of element located by `$locator` to $scopes variable `$variableName`")
public void saveTextOfElement(Locator locator, Set<VariableScope> scopes, String variableName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.vividus.ui.web.playwright;

import java.util.Optional;
import java.util.function.Function;

import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
Expand Down Expand Up @@ -55,11 +56,21 @@ public void reset()

public Locator locateElement(PlaywrightLocator playwrightLocator)
{
PlaywrightContext playwrightContext = getPlaywrightContext();
String locator = playwrightLocator.getLocator();
return getInCurrentContext(context -> context.locator(locator), page -> page.locator(locator));
}

public Locator getCurrentContexOrPageRoot()
{
return getInCurrentContext(context -> context, page -> page.locator("//html/body"));
}

private <R> R getInCurrentContext(Function<Locator, R> elementContextAction, Function<Page, R> pageContextAction)
{
PlaywrightContext playwrightContext = getPlaywrightContext();
return Optional.ofNullable(playwrightContext.context)
.map(context -> context.locator(locator))
.orElseGet(() -> playwrightContext.page.locator(locator));
.map(elementContextAction)
.orElseGet(() -> pageContextAction.apply(playwrightContext.page));
}

private PlaywrightContext getPlaywrightContext()
Expand Down
Loading

0 comments on commit 443d293

Please sign in to comment.