Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[extension-selenium] Add set variable steps #1030

Merged
merged 1 commit into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions docs/modules/plugins/partials/generic-ui-steps.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,95 @@ When I wait until element located `$locator` disappears
Scenario: Wait for element disappearance
When I wait until element located `name(welcome-image)` disappears
----

=== Save text of context element

==== *_Info_*

Saves text of a context element into a variable

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

==== *_Wording_*

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

==== *_Parameters_*

. `$scopes` - xref:parameters:variable-scope.adoc[The set of variable's scope]
. `$variableName` - name of a variable

==== *_Usage_*

.SaveText.story
[source,gherkin]
----
Scenario: Save text of context element
When I change context to element located `By.id(username)`
When I save text of context element to SCENARIO variable `username`
----

=== Save attribute value of context element

==== *_Info_*

Saves attribute value of a context element into a variable

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

==== *_Wording_*

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

==== *_Parameters_*

. `$attributeName` - name of an element's attribute
. `$scopes` - xref:parameters:variable-scope.adoc[The set of variable's scope]
. `$variableName` - name of a variable

==== *_Usage_*

.SaveAttributeValue.story
[source,gherkin]
----
Scenario: Save attribute value of context element
When I change context to element located `By.id(username)`
When I save `innerText` attribute value of context element to SCENARIO variable `username`
----

=== Save attribute value of element

==== *_Info_*

Saves attribute value of an element located by locator into a variable

==== *_Wording_*

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

==== *_Parameters_*

. `$attributeName` - name of an element's attribute
. `$locator` - <<_locator>>
. `$scopes` - xref:parameters:variable-scope.adoc[The set of variable's scope]
. `$variableName` - name of a variable

==== *_Usage_*

.SaveAttributeValue.story
[source,gherkin]
----
Scenario: Save attribute value of element
When I save `innerText` attribute value of element located `By.id(username)` to SCENARIO variable `username`
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright 2019-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.bdd.steps.ui;

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

import org.jbehave.core.annotations.When;
import org.openqa.selenium.WebElement;
import org.vividus.bdd.context.IBddVariableContext;
import org.vividus.bdd.monitor.TakeScreenshotOnFailure;
import org.vividus.bdd.steps.ui.validation.IBaseValidations;
import org.vividus.bdd.variable.VariableScope;
import org.vividus.softassert.ISoftAssert;
import org.vividus.ui.action.ElementActions;
import org.vividus.ui.action.search.Locator;
import org.vividus.ui.action.search.Visibility;
import org.vividus.ui.context.IUiContext;

@TakeScreenshotOnFailure
public class GenericSetVariableSteps
{
private final ISoftAssert softAssert;
private final IBaseValidations baseValidations;
private final IBddVariableContext bddVariableContext;
private final ElementActions elementActions;
private final IUiContext uiContext;

public GenericSetVariableSteps(ISoftAssert softAssert, IBaseValidations baseValidations,
IBddVariableContext bddVariableContext, ElementActions elementActions, IUiContext uiContext)
{
this.softAssert = softAssert;
this.baseValidations = baseValidations;
this.bddVariableContext = bddVariableContext;
this.elementActions = elementActions;
this.uiContext = uiContext;
}

/**
* 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
*/
@When("I save text of context element to $scopes variable `$variableName`")
public void saveContextElementTextToVariable(Set<VariableScope> scopes, String variableName)
{
saveVariableIfContextElementPresent(elementActions::getElementText, scopes, variableName);
}

/**
* 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 A name under which the 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)
{
saveVariableIfContextElementPresent(contextElement -> getAssertedAttributeValue(contextElement, attributeName),
scopes, variableName);
}

/**
* Extracts the value of <b>attribute</b> of element found 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 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 A name under which the value should be saved
*/
@When("I save `$attributeName` attribute value of element located `$locator` to $scopes variable `$variableName`")
public void saveAttributeValueOfElementByLocatorToVariable(String attributeName, Locator locator,
Set<VariableScope> scopes, String variableName)
{
locator.getSearchParameters().setVisibility(Visibility.ALL);
Optional<Object> value = baseValidations.assertElementExists("The element to extract the attribute", locator)
.map(element -> getAssertedAttributeValue(element, attributeName));
putVariable(scopes, variableName, value);
}

private void saveVariableIfContextElementPresent(Function<WebElement, Object> contextElementProcessor,
Set<VariableScope> scopes, String variableName)
{
Optional<Object> value = Optional.ofNullable(uiContext.getSearchContext())
.map(context -> uiContext.getSearchContext(WebElement.class))
.map(contextElementProcessor);
putVariable(scopes, variableName, value);
}

private void putVariable(Set<VariableScope> scopes, String variableName, Optional<Object> value)
{
bddVariableContext.putVariable(scopes, variableName, value.orElse(null));
}

private String getAssertedAttributeValue(WebElement element, String attributeName)
{
String value = element.getAttribute(attributeName);
softAssert.assertNotNull("The '" + attributeName + "' attribute value was found", value);
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2019-2020 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.ui.action;

import org.openqa.selenium.WebElement;

public interface ElementActions
{
/**
* Gets the text content from element
* @param element element to get text from
* @return text of the element
* or empty string if no content found
*/
String getElementText(WebElement element);
}
2 changes: 2 additions & 0 deletions vividus-extension-selenium/src/main/resources/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@

<bean id="genericElementSteps" class="org.vividus.bdd.steps.ui.GenericElementSteps" />
<bean id="genericWaitSteps" class="org.vividus.bdd.steps.ui.GenericWaitSteps" />
<bean id="genericSetVariableSteps" class="org.vividus.bdd.steps.ui.GenericSetVariableSteps" />

<util:list id="stepBeanNames-UI" value-type="java.lang.String">
<idref bean="genericElementSteps" />
<idref bean="genericWaitSteps" />
<idref bean="genericSetVariableSteps" />
</util:list>

<util:map id="propertyEditors-UI" key-type="java.lang.Class">
Expand Down
Loading