Skip to content

Commit

Permalink
[extension-selenium] Unify syntax of steps changing context (#3517)
Browse files Browse the repository at this point in the history
  • Loading branch information
vkepin authored Jan 18, 2023
1 parent 8105002 commit a2024df
Show file tree
Hide file tree
Showing 26 changed files with 161 additions and 86 deletions.
2 changes: 1 addition & 1 deletion docs/modules/plugins/pages/plugin-accessibility.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The step based on the contextual approach and when it's necessary it could be us
[source,gherkin]
----
Given I am on page with URL `https://vividus-test-site.onrender.com/`
When I change context to element located `xpath(//body)`
When I change context to element located by `xpath(//body)`
Then I test accessibility:
|standard|level |elementsToIgnore |elementsToCheck|violationsToIgnore |
|WCAG2AAA|NOTICE|By.id(ignore), By.cssSelector(#errors > h1 > img)| |WCAG2AAA.Principle1.Guideline1_3.1_3_1.H42.2,WCAG2AAA.Principle2.Guideline2_4.2_4_9.H30|
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/plugins/pages/plugin-mobile-app.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ Then number of elements found by `accessibilityId(end-of-screen)` is equal to `1
[source,gherkin]
----
Scenario: Switch slides
When I change context to element located `accessibilityId(carousel)`
When I change context to element located by `accessibilityId(carousel)`
Then number of elements found by `accessibilityId(slide 2)` is equal to `0`
When I swipe LEFT to element located `accessibilityId(slide 2)` with duration PT1S
Then number of elements found by `accessibilityId(slide 2)` is equal to `1`
Expand Down Expand Up @@ -1084,7 +1084,7 @@ When I scan barcode from context and save result to $scopes variable `$variableN
.Scan the barcode
[source,gherkin]
----
When I change context to element located `xpath(//android.view.ViewGroup[@resource-id='qrContainer'])`
When I change context to element located by `xpath(//android.view.ViewGroup[@resource-id='qrContainer'])`
When I scan barcode from context and save result to scenario variable `qrCodeLink`
Then `${qrCodeLink}` is equal to `https://www.example.com`
----
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/plugins/pages/plugin-web-app.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ When I scan barcode from context and save result to $scopes variable `$variableN
.Scan the barcode
[source,gherkin]
----
When I change context to element located `By.xpath(//div[@id='qrCode'])`
When I change context to element located by `xpath(//div[@id='qrCode'])`
When I scan barcode from context and save result to scenario variable `qrCodeLink`
Then `${qrCodeLink}` is equal to `https://www.example.com`
----
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/plugins/partials/dynamic-variables-ui.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ WARNING: In case of missing search context the error will be logged and `-1` wil
.Check the size and the location of the image
[source,gherkin]
----
When I change context to element located `id(userpic)`
When I change context to element located by `id(userpic)`
Then `${context-height}` is > `0`
Then `${context-width}` is > `0`
Then `${context-x-coordinate}` is > `0`
Expand Down
22 changes: 17 additions & 5 deletions docs/modules/plugins/partials/generic-ui-steps.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ When I save text of context element to $scopes variable `$variableName`
.Save the text of the context element
[source,gherkin]
----
When I change context to element located `By.id(username)`
When I change context to element located by `id(username)`
When I save text of context element to SCENARIO variable `username`
----

Expand All @@ -172,7 +172,7 @@ When I save `$attributeName` attribute value of context element to $scopes varia
.Save the attribute value of the context element
[source,gherkin]
----
When I change context to element located `By.id(username)`
When I change context to element located by `id(username)`
When I save `innerText` attribute value of context element to SCENARIO variable `username`
----

Expand Down Expand Up @@ -223,6 +223,12 @@ Then `${numberOfLinks}` is equal to `1`

Changes the context to an element located by locator for limiting area of subsequent UI interactions.

[source,gherkin]
----
When I change context to element located by `$locator` in scope of current context
----

_Deprecated syntax (will be removed in VIVIDUS 0.7.0)_:
[source,gherkin]
----
When I change context to element located `$locator` in scope of current context
Expand All @@ -234,14 +240,20 @@ When I change context to element located `$locator` in scope of current context
[source,gherkin]
----
Then number of elements found by `By.xpath(html)` is equal to `1`
When I change context to element located `By.xpath(//body)` in scope of current context
When I change context to element located by `xpath(//body)` in scope of current context
Then number of elements found by `By.xpath(html)` is equal to `0`
----

=== Reset and change context

Resets current context and changes the context to an element located by locator for limiting area of subsequent UI interactions.

[source,gherkin]
----
When I change context to element located by `$locator`
----

_Deprecated syntax (will be removed in VIVIDUS 0.7.0)_:
[source,gherkin]
----
When I change context to element located `$locator`
Expand All @@ -253,7 +265,7 @@ When I change context to element located `$locator`
[source,gherkin]
----
Then number of elements found by `By.xpath(html)` is equal to `1`
When I change context to element located `By.xpath(//body)`
When I change context to element located by `xpath(//body)`
Then number of elements found by `By.xpath(html)` is equal to `0`
----

Expand All @@ -271,7 +283,7 @@ When I reset context
[source,gherkin]
----
Then number of elements found by `By.xpath(html)` is equal to `1`
When I change context to element located `By.xpath(//body)`
When I change context to element located by `xpath(//body)`
Then number of elements found by `By.xpath(html)` is equal to `0`
When I reset context
Then number of elements found by `By.xpath(html)` is equal to `1`
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/plugins/partials/plugin-web-app-steps.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ If the context is not set, the whole page will be scrolled
.Scroll login to a bottom
[source,gherkin]
----
When I change context to element located `id(login)`
When I change context to element located by `id(login)`
When I scroll context to BOTTOM edge
----

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2023 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.
Expand All @@ -20,6 +20,8 @@

import org.jbehave.core.annotations.When;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vividus.steps.ui.validation.IBaseValidations;
import org.vividus.ui.action.search.Locator;
import org.vividus.ui.context.IUiContext;
Expand All @@ -30,6 +32,7 @@
public class GenericSetContextSteps
{
public static final String ELEMENT_TO_SET_CONTEXT = "Element to set context";
private static final Logger LOGGER = LoggerFactory.getLogger(GenericSetContextSteps.class);
private final IUiContext uiContext;
private final IBaseValidations baseValidations;

Expand All @@ -52,21 +55,39 @@ public void resetContext()
* Resets currently set context and
* sets the context for further localization of elements to an <b>element</b> located by <b>locator</b>
* @param locator locator to find an element
* @deprecated Use step: "When I change context to element located by `$locator`"
*/
@Deprecated(since = "0.5.4", forRemoval = true)
@When("I change context to element located `$locator`")
public void resetAndChangeContextToElement(Locator locator)
{
LOGGER.warn("The step: \"When I change context to element located `$locator`\" is deprecated "
+ "and will be removed in VIVIDUS 0.7.0. "
+ "Use step: \"When I change context to element located by `$locator`\"");
resetContext();
changeContext(() -> getBaseValidations().assertIfElementExists(ELEMENT_TO_SET_CONTEXT, locator),
() -> resetAndChangeContextToElement(locator));
}

/**
* Resets currently set context and
* sets the context for further localization of elements to an <b>element</b> located by <b>locator</b>
* @param locator locator to find an element
*/
@When("I change context to element located by `$locator`")
public void resetAndSetContextToElement(Locator locator)
{
resetContext();
changeContext(() -> getBaseValidations().assertElementExists(ELEMENT_TO_SET_CONTEXT, locator).orElse(null),
() -> resetAndSetContextToElement(locator));
}

/**
* Sets the context for further localization of elements to an <b>element</b> located by <b>locator</b>
* in scope of the current context.
* @param locator locator to find an element
*/
@When("I change context to element located `$locator` in scope of current context")
@When("I change context to element located by `$locator` in scope of current context")
public void changeContextToElement(Locator locator)
{
changeContext(() -> getBaseValidations().assertElementExists(ELEMENT_TO_SET_CONTEXT, locator).orElse(null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ When I wait until element located by `<locator>` disappears
Composite: Then element located `$locator` exists for `$duration` duration
!-- WARNING: The step "Then element located `$locator` exists for `$duration` duration" is deprecated and will be removed in VIVIDUS 0.6.0
Then element located by `<locator>` exists for `<duration>` duration

Composite: When I change context to element located `$locator` in scope of current context
!-- WARNING: The step "When I change context to element located `$locator` in scope of current context` duration" is deprecated and will be removed in VIVIDUS 0.7.0
When I change context to element located by `<locator>` in scope of current context
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 the original author or authors.
* Copyright 2019-2023 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.
Expand All @@ -16,6 +16,9 @@

package org.vividus.steps.ui;

import static com.github.valfirst.slf4jtest.LoggingEvent.warn;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
Expand All @@ -26,8 +29,14 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Optional;

import com.github.valfirst.slf4jtest.LoggingEvent;
import com.github.valfirst.slf4jtest.TestLogger;
import com.github.valfirst.slf4jtest.TestLoggerFactory;
import com.github.valfirst.slf4jtest.TestLoggerFactoryExtension;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -42,11 +51,13 @@
import org.vividus.ui.context.IUiContext;
import org.vividus.ui.context.SearchContextSetter;

@ExtendWith(MockitoExtension.class)
@ExtendWith({ MockitoExtension.class, TestLoggerFactoryExtension.class })
class GenericSetContextStepsTests
{
private static final String ELEMENT_TO_SET_CONTEXT = "Element to set context";

private final TestLogger logger = TestLoggerFactory.getTestLogger(GenericSetContextSteps.class);

@Mock private IUiContext uiContext;
@Mock private IBaseValidations baseValidations;
@InjectMocks private GenericSetContextSteps genericSetContextSteps;
Expand Down Expand Up @@ -83,6 +94,33 @@ void shouldResetAndChangeContextToElement()
verify(uiContext, times(2)).reset();
verify(uiContext, times(2)).putSearchContext(eq(webElement), any(SearchContextSetter.class));
verify(baseValidations, times(2)).assertIfElementExists(ELEMENT_TO_SET_CONTEXT, locator);
LoggingEvent expectedLoggingEvent = warn(
"The step: \"When I change context to element located `$locator`\" is deprecated "
+ "and will be removed in VIVIDUS 0.7.0. "
+ "Use step: \"When I change context to element located by `$locator`\"");
assertThat(logger.getLoggingEvents(), is(List.of(expectedLoggingEvent, expectedLoggingEvent)));
}

@Test
void shouldResetAndSetContextToElement()
{
Locator locator = mock(Locator.class);
WebElement webElement = mock(WebElement.class);
Optional<WebElement> webElementOpt = Optional.of(webElement);
GenericSetContextSteps spy = Mockito.spy(genericSetContextSteps);
ArgumentCaptor<SearchContextSetter> setterCaptor = ArgumentCaptor.forClass(SearchContextSetter.class);

when(baseValidations.assertElementExists(ELEMENT_TO_SET_CONTEXT, locator)).thenReturn(webElementOpt);
doNothing().when(uiContext).putSearchContext(eq(webElement), setterCaptor.capture());

spy.resetAndSetContextToElement(locator);

SearchContextSetter setter = setterCaptor.getValue();
setter.setSearchContext();
verify(spy, times(2)).resetAndSetContextToElement(locator);
verify(uiContext, times(2)).reset();
verify(uiContext, times(2)).putSearchContext(eq(webElement), any(SearchContextSetter.class));
verify(baseValidations, times(2)).assertElementExists(ELEMENT_TO_SET_CONTEXT, locator);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Then the text '${expectedText}' exists

Scenario: Action verification CLICK_AND_HOLD
Given I am on page with URL `${vividus-test-site-url}/mouseEvents.html`
When I change context to element located `By.id(target)`
When I change context to element located by `id(target)`
Then the context element has the CSS property 'background-color'='rgba(255, 255, 255, 1)'
When I execute sequence of actions:
|type |argument |
Expand All @@ -53,7 +53,7 @@ Then the context element has the CSS property 'background-color'='rgba(255, 0, 0

Scenario: Action verification CLICK_AND_HOLD with no argument
Given I am on page with URL `${vividus-test-site-url}/mouseEvents.html`
When I change context to element located `By.id(target)`
When I change context to element located by `id(target)`
Then the context element has the CSS property 'background-color'='rgba(255, 255, 255, 1)'
When I execute sequence of actions:
|type |argument |
Expand All @@ -63,7 +63,7 @@ Then the context element has the CSS property 'background-color'='rgba(255, 0, 0

Scenario: Action verification RELEASE
Given I am on page with URL `${vividus-test-site-url}/mouseEvents.html`
When I change context to element located `By.id(target)`
When I change context to element located by `id(target)`
Then the context element has the CSS property 'background-color'='rgba(255, 255, 255, 1)'
When I execute sequence of actions:
|type |argument |
Expand All @@ -73,7 +73,7 @@ Then the context element has the CSS property 'background-color'='rgba(0, 128, 0

Scenario: Action verification RELEASE with no argument
Given I am on page with URL `${vividus-test-site-url}/mouseEvents.html`
When I change context to element located `By.id(target)`
When I change context to element located by `id(target)`
Then the context element has the CSS property 'background-color'='rgba(255, 255, 255, 1)'
When I execute sequence of actions:
|type |argument |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ Meta:
@requirementId 2687
Given I am on page with URL `${vividus-test-site-url}/qrModal.html`
When I click on element located by `id(modalButton)`
When I change context to element located `By.xpath(//div[@class='modal-content'])` in scope of current context
When I change context to element located by `xpath(//div[@class='modal-content'])` in scope of current context
When I scan barcode from context and save result to scenario variable `qrCodeLink`
Then `${qrCodeLink}` is = `https://github.com/vividus-framework/vividus`
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ Scope: STORY
Given I am on page with URL `${vividus-test-site-url}/checkboxes.html`

Scenario: Validation of step: 'When I $checkboxAction checkbox located by `$searchAttributes`'
When I change context to element located `id(single)`
When I change context to element located by `id(single)`
Then number of not selected elements found by `checkboxName(One)` is equal to `1`
When I check checkbox located by `id(one)`
Then number of selected elements found by `checkboxName(One)` is equal to `1`
When I refresh the page

Scenario: Validation of step 'When I $checkboxState all checkboxes located by `$checkboxesLocator`'
When I change context to element located `id(double)`
When I change context to element located by `id(double)`
Then number of not selected elements found by `checkboxName(Two)` is equal to `1`
Then number of not selected elements found by `checkboxName(Three)` is equal to `1`
When I check all checkboxes located by `xpath(.//input)`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Meta:
Scenario: Step verification 'When I drag element located `$origin` and drop it at $location of element located `$target`'
Given I am on page with URL `https://4qp6vjp319.codesandbox.io/`
When I wait until element located by `xpath(//div[@id='root']/ul)` appears
When I change context to element located `By.xpath(//div[@id='root']/ul)`
When I change context to element located by `xpath(//div[@id='root']/ul)`
Then the text matches 'item 0.*item 1.*item 2.*item 3.*'
When I drag element located `By.xpath(//li[contains(., 'item 0')])` and drop it at top of element located `By.xpath(//li[contains(., 'item 3')])`
Then the text matches 'item 1.*item 2.*item 0.*item 3.*'
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Given I am on page with URL `${vividus-test-site-url}`
Scenario: Verify context' rectangle dynamic variables
Meta:
@requirementId 802
When I change context to element located `tagName(img)`
When I change context to element located by `tagName(img)`
Then `${context-height}` is > `0`
Then `${context-width}` is > `0`
Then `${context-x-coordinate}` is > `0`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Then the page has the relative URL '/mouseEvents.html'

Scenario: Step verification Then the context element has a width of '$widthInPerc'% relative to the parent element
Given I am on page with URL `${vividus-test-site-url}/dropdowns.html`
When I change context to element located `By.xpath(.//*[@for='colors'])`
When I change context to element located by `xpath(.//*[@for='colors'])`
Then the context element has a width of '13'% relative to the parent element

Scenario: Step verification When I perform right click on an element located `$locator`
Expand All @@ -56,12 +56,12 @@ Then number of elements found by `elementName(Bar)` is = `1`

Scenario: Step verification Then the context element has the CSS property '$cssName'='$cssValue'
Given I am on page with URL `${vividus-test-site-url}/inputs.html`
When I change context to element located `By.xpath(.//*[@title='Text input section'])`
When I change context to element located by `xpath(.//*[@title='Text input section'])`
Then the context element has the CSS property 'color'='rgba(0, 0, 0, 1)'

Scenario: Step verification Then the context element has the CSS property '$cssName' containing '$cssValue'
Given I am on page with URL `${vividus-test-site-url}/inputs.html`
When I change context to element located `By.xpath(.//*[@title='Text input section'])`
When I change context to element located by `xpath(.//*[@title='Text input section'])`
Then the context element has the CSS property 'color' containing '(0, 0, 0, 1)'

Scenario: Step verification When I select an element '$locator' and upload the file '$filePath'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Examples:

Scenario: Step verification Then context contains list of link items with the text: $expectedLinkItems
Given I am on page with URL `<linksUrl>`
When I change context to element located `By.xpath(//body)`
When I change context to element located by `xpath(//body)`
Then context contains list of link items with the text:
|text |
|Link to an element|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Meta:
When I open URL `${vividus-test-site-url}/delayedLoading?imageTimeout=1000` in new window
When I stop page loading
When I execute async javascript `setTimeout(() => arguments[0]('wait for page to load'), 1000)` and save result to scenario variable `timeout`
When I change context to element located `cssSelector(img)`
When I change context to element located by `cssSelector(img)`
Then `${context-height}` is < `100`
Then `${context-width}` is < `100`
Expand Down
Loading

0 comments on commit a2024df

Please sign in to comment.