Skip to content

Commit

Permalink
[plugin-web-app-playwright] Add steps to save attribute value of elem…
Browse files Browse the repository at this point in the history
…ent (#5103)
  • Loading branch information
avinBar committed Jun 5, 2024
1 parent 95e9426 commit effc24d
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 4 deletions.
40 changes: 40 additions & 0 deletions docs/modules/plugins/pages/plugin-web-app-playwright.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,46 @@ When I change context to element located by `id(username)`
When I save text of context element to scneario variable `username`
----

=== Saves the attribute value of the context

Saves the attribute value of the context element into a variable.

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

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

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

=== Save the attribute value of the element

Saves the attribute value of the element located by locator into a variable.

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

* `$attributeName` - The name of an element attribute.
* `$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 attribute value.

Save the attribute value of the element
[source,gherkin]
----
When I save `innerText` attribute value of element located by `id(username)` to SCENARIO variable `username`
----

include::plugins:partial$ui-elements-quantity-steps.adoc[]

include::plugins:partial$ui-size-and-coordinates-steps.adoc[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.vividus.ui.web.playwright.steps;

import java.util.Map;
import java.util.Optional;
import java.util.Set;

import com.microsoft.playwright.Locator;
import com.microsoft.playwright.options.BoundingBox;

import org.jbehave.core.annotations.Then;
Expand Down Expand Up @@ -164,4 +166,65 @@ public void saveNumberOfElementsToVariable(PlaywrightLocator locator, Set<Variab
int elementCount = uiContext.locateElement(locator).count();
variableContext.putVariable(scopes, variableName, elementCount);
}

/**
* Extracts the value of <b>attribute</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 value of attribute with name <i>attributeName</i> to the <i>variableName</i>
* </ul>
* @param attributeName The name of the attribute (for ex. 'name', 'id')
* @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 name under which the attribute value should be saved
*/
@When("I save `$attributeName` attribute value of context element to $scopes variable `$variableName`")
public void saveContextElementAttributeValueToVariable(String attributeName, Set<VariableScope> scopes,
String variableName)
{
saveAttributeValueOfElement(uiContext.getCurrentContexOrPageRoot(), attributeName, scopes, variableName);
}

/**
* Gets the value of <b>attribute</b> from element located by <b>locator</b> and saves it to the <b>variable</b>
* with the specified <b>variableName</b>
* Actions performed at this step:
* <ul>
* <li>Saves the value of attribute with name <i>attributeName</i> to the <i>variableName</i>
* </ul>
* @param attributeName The name of the attribute (for ex. 'name', 'id')
* @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 name under which the attribute value should be saved
*/
@When("I save `$attributeName` attribute value of element located by `$locator` to $scopes variable "
+ "`$variableName`")
public void saveAttributeValueOfElement(String attributeName, PlaywrightLocator locator, Set<VariableScope> scopes,
String variableName)
{
saveAttributeValueOfElement(uiContext.locateElement(locator), attributeName, scopes, variableName);
}

private void saveAttributeValueOfElement(Locator element, String attributeName, Set<VariableScope> scopes,
String variableName)
{
Optional.ofNullable(element.getAttribute(attributeName))
.ifPresentOrElse(value -> variableContext.putVariable(scopes, variableName, value),
() -> softAssert.recordFailedAssertion(
String.format("The '%s' attribute does not exist", attributeName)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import java.util.Map;
Expand Down Expand Up @@ -46,6 +47,9 @@ class ElementStepsTests
private static final String XPATH = "xpath";
private static final String VARIABLE_NAME = "variableName";
private static final String LOCATOR_VALUE = "div";
private static final String ATTRIBUTE_NAME = "attributeName";
private static final String ATTRIBUTE_VALUE = "attributeValue";
private static final Set<VariableScope> VARIABLE_SCOPE = Set.of(VariableScope.STORY);

@Mock private UiContext uiContext;
@Mock private ISoftAssert softAssert;
Expand Down Expand Up @@ -99,9 +103,9 @@ void shouldSaveElementCoordinatesAndSize()
box.width = 4;
when(locator.boundingBox()).thenReturn(box);

steps.saveElementCoordinatesAndSize(playwrightLocator, Set.of(VariableScope.STORY), VARIABLE_NAME);
steps.saveElementCoordinatesAndSize(playwrightLocator, VARIABLE_SCOPE, VARIABLE_NAME);

verify(variableContext).putVariable(Set.of(VariableScope.STORY), VARIABLE_NAME, Map.of(
verify(variableContext).putVariable(VARIABLE_SCOPE, VARIABLE_NAME, Map.of(
"x", box.x,
"y", box.y,
"height", box.height,
Expand All @@ -117,7 +121,57 @@ void shouldSaveNumberOfElementsToVariable()
Locator locator = mock();
when(uiContext.locateElement(playwrightLocator)).thenReturn(locator);
when(locator.count()).thenReturn(elementCount);
steps.saveNumberOfElementsToVariable(playwrightLocator, Set.of(VariableScope.STORY), VARIABLE_NAME);
verify(variableContext).putVariable(Set.of(VariableScope.STORY), VARIABLE_NAME, elementCount);
steps.saveNumberOfElementsToVariable(playwrightLocator, VARIABLE_SCOPE, VARIABLE_NAME);
verify(variableContext).putVariable(VARIABLE_SCOPE, VARIABLE_NAME, elementCount);
}

@Test
void shouldSaveContextElementAttributeValueToVariable()
{
Locator locator = mock();
when(uiContext.getCurrentContexOrPageRoot()).thenReturn(locator);
when(locator.getAttribute(ATTRIBUTE_NAME)).thenReturn(ATTRIBUTE_VALUE);
steps.saveContextElementAttributeValueToVariable(ATTRIBUTE_NAME, VARIABLE_SCOPE, VARIABLE_NAME);
verify(variableContext).putVariable(VARIABLE_SCOPE, VARIABLE_NAME, ATTRIBUTE_VALUE);
verifyNoInteractions(softAssert);
}

@Test
void shouldSaveAttributeValueOfElement()
{
var playwrightLocator = new PlaywrightLocator(XPATH, LOCATOR_VALUE);
Locator locator = mock();
when(uiContext.locateElement(playwrightLocator)).thenReturn(locator);
when(locator.getAttribute(ATTRIBUTE_NAME)).thenReturn(ATTRIBUTE_VALUE);
steps.saveAttributeValueOfElement(ATTRIBUTE_NAME, playwrightLocator, VARIABLE_SCOPE, VARIABLE_NAME);
verify(variableContext).putVariable(VARIABLE_SCOPE, VARIABLE_NAME, ATTRIBUTE_VALUE);
verifyNoInteractions(softAssert);
}

@Test
void shouldNotSaveAttributeValueOfContextIfAttributeIsNotPresent()
{
Locator locator = mock();
when(uiContext.getCurrentContexOrPageRoot()).thenReturn(locator);
when(locator.getAttribute(ATTRIBUTE_NAME)).thenReturn(null);
steps.saveContextElementAttributeValueToVariable(ATTRIBUTE_NAME, VARIABLE_SCOPE, VARIABLE_NAME);
verifyIfAttributeIsNotPresent();
}

@Test
void shouldNotSaveAttributeValueOfElementIfAttributeIsNotPresent()
{
var playwrightLocator = new PlaywrightLocator(XPATH, LOCATOR_VALUE);
Locator locator = mock();
when(uiContext.locateElement(playwrightLocator)).thenReturn(locator);
when(locator.getAttribute(ATTRIBUTE_NAME)).thenReturn(null);
steps.saveAttributeValueOfElement(ATTRIBUTE_NAME, playwrightLocator, VARIABLE_SCOPE, VARIABLE_NAME);
verifyIfAttributeIsNotPresent();
}

private void verifyIfAttributeIsNotPresent()
{
verify(softAssert).recordFailedAssertion(String.format("The '%s' attribute does not exist", ATTRIBUTE_NAME));
verifyNoInteractions(variableContext);
}
}

0 comments on commit effc24d

Please sign in to comment.