From effc24df33d541d2e69beea3dec082a3bab3f4c8 Mon Sep 17 00:00:00 2001 From: avinBar Date: Wed, 5 Jun 2024 12:17:22 +0300 Subject: [PATCH] [plugin-web-app-playwright] Add steps to save attribute value of element (#5103) --- .../pages/plugin-web-app-playwright.adoc | 40 ++++++++++++ .../ui/web/playwright/steps/ElementSteps.java | 63 +++++++++++++++++++ .../playwright/steps/ElementStepsTests.java | 62 ++++++++++++++++-- 3 files changed, 161 insertions(+), 4 deletions(-) diff --git a/docs/modules/plugins/pages/plugin-web-app-playwright.adoc b/docs/modules/plugins/pages/plugin-web-app-playwright.adoc index 2d3dc61efd..e7a8779815 100644 --- a/docs/modules/plugins/pages/plugin-web-app-playwright.adoc +++ b/docs/modules/plugins/pages/plugin-web-app-playwright.adoc @@ -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[] diff --git a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/steps/ElementSteps.java b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/steps/ElementSteps.java index a4fde3a6b8..7df256c196 100644 --- a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/steps/ElementSteps.java +++ b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/steps/ElementSteps.java @@ -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; @@ -164,4 +166,65 @@ public void saveNumberOfElementsToVariable(PlaywrightLocator locator, Setattribute of element found in the context and saves it to the variable with + * the specified variableName + * Actions performed at this step: + * + * @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
+ * Available scopes: + * + * @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 scopes, + String variableName) + { + saveAttributeValueOfElement(uiContext.getCurrentContexOrPageRoot(), attributeName, scopes, variableName); + } + + /** + * Gets the value of attribute from element located by locator and saves it to the variable + * with the specified variableName + * Actions performed at this step: + *
    + *
  • Saves the value of attribute with name attributeName to the variableName + *
+ * @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
+ * Available scopes: + *
    + *
  • STEP - the variable will be available only within the step, + *
  • SCENARIO - the variable will be available only within the scenario, + *
  • STORY - the variable will be available within the whole story, + *
  • NEXT_BATCHES - the variable will be available starting from next batch + *
+ * @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 scopes, + String variableName) + { + saveAttributeValueOfElement(uiContext.locateElement(locator), attributeName, scopes, variableName); + } + + private void saveAttributeValueOfElement(Locator element, String attributeName, Set 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))); + } } diff --git a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/ElementStepsTests.java b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/ElementStepsTests.java index d310fd0586..576c048f22 100644 --- a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/ElementStepsTests.java +++ b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/ElementStepsTests.java @@ -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; @@ -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 VARIABLE_SCOPE = Set.of(VariableScope.STORY); @Mock private UiContext uiContext; @Mock private ISoftAssert softAssert; @@ -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, @@ -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); } }