Skip to content

Commit

Permalink
[extension-selenium] Add step to execute JavaScript (#5120)
Browse files Browse the repository at this point in the history
Co-authored-by: draker94 <noreply@github.com>
  • Loading branch information
draker94 and web-flow authored Jun 11, 2024
1 parent 0346009 commit ad8380b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
23 changes: 21 additions & 2 deletions docs/modules/plugins/partials/generic-ui-steps.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,28 @@ When I find >= `0` elements `xpath(//*[@class='menu enabled'])` and while they e
|When I click on element located by `id(disable)`|
----

:javascript-executor-link: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html[JavascriptExecutor]

=== Execute JavaScript

Executes passed JavaScript code on the opened page via {javascript-executor-link}.

[source,gherkin]
----
When I execute javascript `$jsCode`
----
* `$jsCode` - The JavaScript code.

.Click on the link using JavaScript
[source,gherkin]
----
Given I am on page with URL `https://vividus-test-site-a92k.onrender.com`
When I execute javascript `document.querySelector('a').click()`
----

=== Execute JavaScript with arguments

Executes JavaScript via https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html[JavascriptExecutor]. Step executes the script either without any arguments or with `String` or `Object` argument types.
Executes JavaScript via {javascript-executor-link}. Step executes the script either without any arguments or with `String` or `Object` argument types.

[source,gherkin]
----
Expand Down Expand Up @@ -417,7 +436,7 @@ When I execute javascript `sauce:throttleCPU` with arguments:
| {"rate" : 4} | object |
----

==== Execute JavaScript and save result.
=== Execute JavaScript and save result.

https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html[Executes JavaScript] code and saves result into variable.

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-2024 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 Down Expand Up @@ -38,6 +38,18 @@ public ExecuteScriptSteps(JavascriptActions javascriptActions, VariableContext v
this.javascriptActions = javascriptActions;
}

/**
* Executes passed JavaScript code on the opened page
*
* @param jsCode JavaScript code
* (e.g. "document.querySelector('[name="vividus-logo"]').remove()")
*/
@When("I execute javascript `$jsCode`")
public void executeJavascript(String jsCode)
{
javascriptActions.executeScript(jsCode);
}

/**
* Executes passed JavaScript code on the opened page
* and saves returned value into the <b>variable</b>
Expand All @@ -54,7 +66,7 @@ public ExecuteScriptSteps(JavascriptActions javascriptActions, VariableContext v
* @param jsCode Code in javascript that returns some value as result
* (e.g. var a=1; return a;)
*/
@When("I execute javascript `$jsCode` and save result to $scopes variable `$variableName`")
@When(value = "I execute javascript `$jsCode` and save result to $scopes variable `$variableName`", priority = 1)
public void saveValueFromJS(String jsCode, Set<VariableScope> scopes, String variableName)
{
assertAndSaveResult(() -> javascriptActions.executeScript(jsCode), scopes, variableName);
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-2024 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 Down Expand Up @@ -67,6 +67,14 @@ class ExecuteScriptStepsTests
@InjectMocks
private ExecuteScriptSteps executeScriptSteps;

@Test
void shouldExecuteJavascript()
{
String jsCode = "document.querySelector('[name=\"vividus-logo\"]').remove()";
executeScriptSteps.executeJavascript(jsCode);
verify(javascriptActions).executeScript(jsCode);
}

static Stream<Arguments> executeJavascriptWithArguments()
{
// CHECKSTYLE:OFF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ Scenario: Step verification: "When I execute javascript `$jsCode` with arguments
Then number of elements found by `xpath(//img)` is = `1`
When I execute javascript `document.querySelector('[name="vividus-logo"]').remove()` with arguments:
Then number of elements found by `xpath(//img)` is = `0`

Scenario: Step verification: "When I execute javascript `$jsCode`"
Then number of elements found by `xpath(//a)` is = `1`
When I execute javascript `document.querySelector('a').remove()`
Then number of elements found by `xpath(//a)` is = `0`

0 comments on commit ad8380b

Please sign in to comment.