Skip to content

Commit

Permalink
New web steps for tabs and frames. (EmbedITCZ#127)
Browse files Browse the repository at this point in the history
* New web steps for tabs and frames.
FORCE_CLICK web action.
Documentation for web steps amended.
  • Loading branch information
xsmrcek authored and Daniel.Smrcek committed Aug 5, 2019
1 parent ca05a2e commit 33b9141
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protected void configure(HttpSecurity http) throws Exception {
.authorizeRequests()
.antMatchers("/rest/secure/**").fullyAuthenticated()
.and()
.httpBasic();
.httpBasic()
.and().headers().frameOptions().sameOrigin();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ <h1>WebAction test page</h1>
</button>
<span id="double-click-result"></span>
</p>
<p>
<button id="force-click-btn" onclick="getElementById('force-click-result').innerHTML = 'OK'" style="display: none;">FORCE_CLICK</button>
<span id="force-click-result">AAA</span>
</p>
<p>
<button id="press-btn" onkeydown="getElementById('press-result').innerHTML = 'OK'">PRESS</button>
<span id="press-result"></span>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Marvelous Frames</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1>Page with marvelous title and iframes</h1>
<div>
<p><span>Frame number: </span><span data-selector="number">0</span></p>
<iframe title="firstFrame" src="/iframes/first-frame.html">
</iframe>
</div>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE HTML>
<html>
<head>
<title>firstFrame</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<p><span>Frame number: </span><span data-selector="number">1</span></p>
<iframe title="secondFrame" src="/iframes/second-frame.html">
</iframe>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE HTML>
<html>
<head>
<title>secondFrame</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<p><span>Frame number: </span><span data-selector="number">2</span></p>
<iframe title="thirdFrame" src="/iframes/third-frame.html">
</iframe>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE HTML>
<html>
<head>
<title>thirdFrame</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<p><span>Frame number: </span><span data-selector="number">3</span></p>
<p><span>This is last frame</span></p>
</div>
</body>
</html>
31 changes: 31 additions & 0 deletions jbehave-support-core/docs/Web-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,36 @@ When navigated forward
Then navigate forward
```

To focus iframes and return to main frame.
```
Given on page [home] frame [iframe] is focused
Then on page [home] frame [iframe] is focused
```
```
Given main frame is focused
Then main frame is focused
```

To open a new tab. (works only in browsers with javascript enabled)
```
Given open and focus new tab
Then open and focus new tab
```

To focus any opened tab using part of its URL or title
```
Given tab with [url] containing [google] is focused
Then tab with [title] containing [google] is focused
```

To close current tab or whole browser
```
When current tab is closed
```
```
Given browser is closed
```

#### Performing an action on HTML elements

To perform an action on a page use the following step.
Expand All @@ -161,6 +191,7 @@ There are the following actions available at the moment:
- DISMISS - for dismissing alert dialog
- CLICK
- DOUBLE_CLICK
- FORCE_CLICK - for clicking on enabled but invisible elements (works only in browsers with javascript enabled)
- FILL - for inserting text value
- CLEAR - for clearing text value of input/textarea
- PRESS - for pressing special keys from the org.openqa.selenium.Keys enum
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jbehavesupport.core.internal.web.action;

import org.jbehavesupport.core.web.WebActionContext;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Component;

@Component
public class ForceClickWebAction extends AbstractWebAction {

@Override
public String name() {
return "FORCE_CLICK";
}

@Override
public void perform(WebActionContext ctx) {
if (!(driver instanceof JavascriptExecutor)) {
throw new AssertionError("WebDriver must implement javascript to use FORCE_CLICK");
}
WebElement element = elementLocator.findElement(ctx.getPage(), ctx.getElement());
JavascriptExecutor jse2 = (JavascriptExecutor) driver;
jse2.executeScript("arguments[0].click();", element);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Map;
import java.util.Set;

import org.assertj.core.api.Assertions;
import org.jbehavesupport.core.TestContext;
import org.jbehavesupport.core.expression.ExpressionEvaluatingParameter;
import org.jbehavesupport.core.internal.verification.VerifierNames;
Expand All @@ -32,7 +33,10 @@
import org.jbehave.core.steps.Row;
import org.jbehavesupport.core.internal.ExampleTableConstraints;
import org.jbehavesupport.core.internal.MetadataUtil;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -60,6 +64,9 @@ public static WebSetting getCurrentSetting() {
return CURRENT_SETTING.get();
}

@Autowired
private WebElementRegistry elementRegistry;

@BeforeScenario
public void beforeScenario() {
if (!givenStoryHelper.isInGivenStory()) {
Expand Down Expand Up @@ -185,6 +192,12 @@ public void waitUntilCondition(String page, String element, ExpressionEvaluating
waitCondition.evaluate(waitConditionCtx);
}

/**
* @deprecated because it's not opening new tab, only focus tab opened by another action
* getLastOpenedWindowHandler() may not work correctly on all browsers (https://developer.mozilla.org/en-US/docs/Web/WebDriver/Commands/GetWindowHandles)
* findTabWithUrlOrTitle() should be used instead
*/
@Deprecated
@Then("new tab is opened and focused")
public void switchToNewTab() {
assertThat(driver.getWindowHandles().size())
Expand All @@ -193,6 +206,56 @@ public void switchToNewTab() {
driver.switchTo().window(getLastOpenedWindowHandler());
}

@Given("open and focus new tab")
@Then("open and focus new tab")
public void openAndFocusNewTab() {
Set<String> handlesBefore = driver.getWindowHandles();
if (driver instanceof JavascriptExecutor) {
((JavascriptExecutor) driver).executeScript("window.open()");
Set<String> handlesAfter = driver.getWindowHandles();
if (handlesAfter.size() == handlesBefore.size() + 1) {
handlesAfter.removeAll(handlesBefore);
driver.switchTo().window(handlesAfter.iterator().next());
return;
}
}
throw new AssertionError("Opening new tab failed");
}

@Given("tab with [$urlTitle] containing [$text] is focused")
@Then("tab with [$urlTitle] containing [$text] is focused")
public void findTabWithUrlOrTitle(String urlTitle, ExpressionEvaluatingParameter<String> text) {
Assertions.assertThat(urlTitle).matches("url|title").as("Must be url or title");
driver.getWindowHandles().stream()
.filter(handle -> {
try {
driver.switchTo().window(handle);
} catch (Exception e) {
return urlTitlecontainsText(urlTitle, text.getValue());
}
return urlTitlecontainsText(urlTitle, text.getValue());
})
.findAny()
.orElseThrow(IllegalStateException::new);
}

private boolean urlTitlecontainsText(String urlTitle, String text) {
return urlTitle.contains("url") ? driver.getCurrentUrl().contains(text) : driver.getTitle().contains(text);
}

@Given("on page [$page] frame [$frame] is focused")
@Then("on page [$page] frame [$frame] is focused")
public void focusNamedFrame(ExpressionEvaluatingParameter<String> page, ExpressionEvaluatingParameter<String> frame) {
WebElement iFrame = driver.findElement(elementRegistry.getLocator(page.getValue(), frame.getValue()));
driver.switchTo().frame(iFrame);
}

@Given("main frame is focused")
@Then("main frame is focused")
public void focusMainFrame() {
driver.switchTo().defaultContent();
}

@When("current tab is closed")
public void closeTab() {
driver.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ When on [home] page these actions are performed:
| element | action | data |
| #click-btn | CLICK | |
| #double-click-btn | DOUBLE_CLICK | |
| #force-click-btn | FORCE_CLICK | |
| #press-btn | PRESS | UP |
| #clear-input | CLEAR | |
| #fill-input | FILL | foo |
Expand All @@ -19,6 +20,7 @@ Then on [home] page these conditions are verified:
| element | property | data |
| #click-result | TEXT | OK |
| #double-click-result | TEXT | OK |
| #force-click-result | TEXT | OK |
| #press-result | TEXT | OK |
| #clear-input | VALUE | {EMPTY_STRING} |
| #fill-input | VALUE | foo |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,42 @@ Then on [home] page wait until [@title] has text Marvelous

When navigated forward
Then on [search] page wait until [@url] has text search.html

Then open and focus new tab

Given [TEST]/[frames.html] url is open
Then on [frames] page wait until [@url] has text frames.html

Then open and focus new tab

Given [TEST]/[index.html] url is open

Then tab with [url] containing [search] is focused
Then on [search] page wait until [@url] has text search.html

Then tab with [title] containing [Marvelous Frames] is focused
Then on [frames] page wait until [@url] has text frames.html

Given on page [frames] frame [firstFrame] is focused

Then on [frames] page these conditions are verified:
| element | property | data |
| numberOfFrame | TEXT | 1 |

Given on page [frames] frame [secondFrame] is focused

Then on [frames] page these conditions are verified:
| element | property | data |
| numberOfFrame | TEXT | 2 |

Given on page [frames] frame [thirdFrame] is focused

Then on [frames] page these conditions are verified:
| element | property | data |
| numberOfFrame | TEXT | 3 |

Given main frame is focused

Then on [frames] page these conditions are verified:
| element | property | data |
| numberOfFrame | TEXT | 0 |
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ search-result:
firstName: "#first-name"
lastName: "#last-name"

frames:
text: "#text"
firstFrame: "*[title='firstFrame']"
secondFrame: "*[title='secondFrame']"
thirdFrame: "*[title='thirdFrame']"
numberOfFrame: "*[data-selector='number']"

0 comments on commit 33b9141

Please sign in to comment.