diff --git a/docs/modules/plugins/pages/plugin-web-app.adoc b/docs/modules/plugins/pages/plugin-web-app.adoc index 9006ecf445..a60308257d 100644 --- a/docs/modules/plugins/pages/plugin-web-app.adoc +++ b/docs/modules/plugins/pages/plugin-web-app.adoc @@ -760,3 +760,32 @@ Then `${info.duration}` is > `1000` Then `${info.networkState}` is = `2` Then `${info.src}` matches `.+youtube.+` ---- + +=== Perform steps for all elements + +Executes the steps against all elements found by locator. + +[source,gherkin] +---- +When I find $comparisonRule `$number` elements by `$locator` and for each element do$stepsToExecute +---- + +Alias: +[source,gherkin] +---- +When I find $comparisonRule '$number' elements by $locator and for each element do$stepsToExecute +---- + +* `$comparisonRule` - xref:parameters:comparison-rule.adoc[The comparison rule]. +* `$number` - The number of elements to find. +* `$locator` - <<_locator>>. +* `$stepsToExecute` - The `ExamplesTable` with a single column containing the steps to execute. + +.Script type check +[source,gherkin] +---- +When I find = `5` elements by `By.xpath(//script):a` and for each element do +|step | +|When I set 'type' attribute value of the context element to the 'scenario' variable 'type'| +|Then `${type}` is equal to `text/javascript` | +---- diff --git a/vividus-plugin-web-app/src/main/java/org/vividus/steps/ui/web/NestedSteps.java b/vividus-plugin-web-app/src/main/java/org/vividus/steps/ui/web/NestedSteps.java index 74ebc627ef..4eb3fbcec5 100644 --- a/vividus-plugin-web-app/src/main/java/org/vividus/steps/ui/web/NestedSteps.java +++ b/vividus-plugin-web-app/src/main/java/org/vividus/steps/ui/web/NestedSteps.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -29,6 +29,7 @@ import org.vividus.steps.SubSteps; import org.vividus.steps.ui.validation.IBaseValidations; import org.vividus.ui.action.search.Locator; +import org.vividus.ui.action.search.SearchParameters; import org.vividus.ui.context.IUiContext; import org.vividus.ui.context.SearchContextSetter; import org.vividus.ui.web.action.ICssSelectorFactory; @@ -97,9 +98,11 @@ public void performAllStepsForElementIfFound(ComparisonRule comparisonRule, int stepsToExecute.execute(Optional.empty()); }); IntStream.range(1, cssSelectors.size()).forEach(i -> { + SearchParameters searchParameters = new SearchParameters(cssSelectors.get(i), + locator.getSearchParameters().getVisibility()); WebElement element = baseValidations .assertIfElementExists("An element for iteration " + (i + 1), - new Locator(WebLocatorType.CSS_SELECTOR, cssSelectors.get(i))); + new Locator(WebLocatorType.CSS_SELECTOR, searchParameters)); runStepsWithContextReset(() -> { uiContext.putSearchContext(element, () -> { }); diff --git a/vividus-plugin-web-app/src/test/java/org/vividus/steps/ui/web/NestedStepsTests.java b/vividus-plugin-web-app/src/test/java/org/vividus/steps/ui/web/NestedStepsTests.java index 882afe6500..fc8c99a490 100644 --- a/vividus-plugin-web-app/src/test/java/org/vividus/steps/ui/web/NestedStepsTests.java +++ b/vividus-plugin-web-app/src/test/java/org/vividus/steps/ui/web/NestedStepsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2022 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. @@ -42,6 +42,8 @@ import org.vividus.steps.SubSteps; import org.vividus.steps.ui.validation.IBaseValidations; import org.vividus.ui.action.search.Locator; +import org.vividus.ui.action.search.SearchParameters; +import org.vividus.ui.action.search.Visibility; import org.vividus.ui.context.IUiContext; import org.vividus.ui.context.SearchContextSetter; import org.vividus.ui.web.action.ICssSelectorFactory; @@ -72,8 +74,9 @@ void testPerformAllStepsForElementIfFound() ComparisonRule.EQUAL_TO)).thenReturn(Arrays.asList(first, second)); when(cssSelectorFactory.getCssSelectors(List.of(first, second))) .thenReturn(List.of(FIRST_XPATH, SECOND_XPATH).stream()); - Locator secondLocator = new Locator(WebLocatorType.CSS_SELECTOR, - SECOND_XPATH); + SearchParameters searchParameters = new SearchParameters(SECOND_XPATH, Visibility.ALL); + when(locator.getSearchParameters()).thenReturn(searchParameters); + Locator secondLocator = new Locator(WebLocatorType.CSS_SELECTOR, searchParameters); when(baseValidations.assertIfElementExists("An element for iteration 2", secondLocator)).thenReturn(second); SearchContextSetter searchContextSetter = mockSearchContextSetter(); diff --git a/vividus-tests/src/main/resources/story/integration/GenericNestedSteps.story b/vividus-tests/src/main/resources/story/integration/GenericNestedSteps.story new file mode 100644 index 0000000000..728a56a6c0 --- /dev/null +++ b/vividus-tests/src/main/resources/story/integration/GenericNestedSteps.story @@ -0,0 +1,14 @@ +Description: Integration tests for GenericNestedSteps class. + +Meta: + @epic vividus-extension-selenium + + +Scenario: Verify step: When I find $comparisonRule `$number` elements `$locator` and while they exist do up to $iterationLimit iteration of$stepsToExecute +Meta: + @requirementId 2054 + +Given I am on a page with the URL '${vividus-test-site-url}/elementState.html' +When I find > `0` elements `id(element-to-hide)` and while they exist do up to 10 iteration of +|step | +|When I click on element located `id(button-hide)`| diff --git a/vividus-tests/src/main/resources/story/integration/NestedSteps.story b/vividus-tests/src/main/resources/story/integration/NestedSteps.story index 728a56a6c0..b5e549987d 100644 --- a/vividus-tests/src/main/resources/story/integration/NestedSteps.story +++ b/vividus-tests/src/main/resources/story/integration/NestedSteps.story @@ -1,14 +1,9 @@ -Description: Integration tests for GenericNestedSteps class. - Meta: - @epic vividus-extension-selenium - - -Scenario: Verify step: When I find $comparisonRule `$number` elements `$locator` and while they exist do up to $iterationLimit iteration of$stepsToExecute -Meta: - @requirementId 2054 - -Given I am on a page with the URL '${vividus-test-site-url}/elementState.html' -When I find > `0` elements `id(element-to-hide)` and while they exist do up to 10 iteration of -|step | -|When I click on element located `id(button-hide)`| + @epic vividus-plugin-web-app + +Scenario: Verify step "When I find $comparisonRule '$number' elements by $locator and for each element do$stepsToExecute" +Given I am on a page with the URL 'https://demo.guru99.com/' +When I find = `5` elements by `By.xpath(//head/link[contains(@href,'V4/css')]):a` and for each element do +|step | +|When I set 'rel' attribute value of the context element to the 'scenario' variable 'relValue' | +|Then `${relValue}` is equal to `stylesheet` |