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

Users can now change browsers mid-story #150

Merged
merged 3 commits into from
Jul 31, 2019
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/1e5c39dfaa6240b8b448d0df114c0d8e)](https://www.codacy.com/app/jbehavesupport/jbehave-support?utm_source=github.com&utm_medium=referral&utm_content=EmbedITCZ/jbehave-support&utm_campaign=Badge_Grade)
[![Gitter](https://badges.gitter.im/jbehave-support/community.svg)](https://gitter.im/jbehave-support/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
# jbehave-support
Light extension to JBehave.
Light extension to [JBehave](https://jbehave.org).

## how to build
- checkout project to local disk
Expand Down
7 changes: 7 additions & 0 deletions jbehave-support-core/docs/Web-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ When current tab is closed
Given browser is closed
```

To switch to another browser (midstory, this will close the current browser automatically)
```
When browser is changed to [$browserName]
```
```
Given browser is changed to [$browserName]
```
#### Performing an action on HTML elements

To perform an action on a page use the following step.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,9 @@ public VerifierResolver verifierResolver(List<Verifier> verifiers) {

@Bean
public WebDriver webDriver(WebDriverFactoryResolver webDriverFactoryResolver) {
WebDriverFactory webDriverFactory = webDriverFactoryResolver.resolveWebDriverFactory();
ProxyFactory proxyFactory = new ProxyFactory(WebDriver.class, new WebDriverDelegatingInterceptor(webDriverFactory));
ProxyFactory proxyFactory = new ProxyFactory(WebDriver.class, new WebDriverDelegatingInterceptor(webDriverFactoryResolver));
proxyFactory.setProxyTargetClass(true);
proxyFactory.setTargetClass(webDriverFactory.getProxyClass());
proxyFactory.setTargetClass(webDriverFactoryResolver.resolveWebDriverFactory().getProxyClass());
return (WebDriver) proxyFactory.getProxy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.aopalliance.intercept.MethodInvocation;
import org.jbehavesupport.core.web.WebDriverFactory;
import org.jbehavesupport.core.web.WebDriverFactoryResolver;
import org.openqa.selenium.WebDriver;
import org.springframework.aop.DynamicIntroductionAdvice;
import org.springframework.aop.IntroductionInterceptor;
Expand All @@ -18,16 +19,22 @@
public class WebDriverDelegatingInterceptor extends IntroductionInfoSupport
implements IntroductionInterceptor {

private WebDriverFactory webDriverFactory;

private WebDriver driver = null;
private transient WebDriverFactoryResolver webDriverFactoryResolver;

public WebDriverDelegatingInterceptor(WebDriverFactory webDriverFactory) {
this.webDriverFactory = webDriverFactory;
private transient WebDriver driver = null;
Hebcak marked this conversation as resolved.
Show resolved Hide resolved

public WebDriverDelegatingInterceptor(WebDriverFactoryResolver webDriverFactoryResolver) {
this.webDriverFactoryResolver = webDriverFactoryResolver;
init();
}

private void init() {
initInterfaces(webDriverFactoryResolver.resolveWebDriverFactory());
}

private void initInterfaces(WebDriverFactory webDriverFactory) {
publishedInterfaces.clear();
publishedInterfaces.addAll(webDriverFactory.getProxyInterfaces());
publishedInterfaces.addAll(ClassUtils.getAllInterfacesForClassAsSet(webDriverFactory.getProxyClass()));

Expand Down Expand Up @@ -56,6 +63,8 @@ public Object invoke(MethodInvocation mi) throws Throwable {

private Object doProceed(final MethodInvocation mi) throws Throwable {
if (driver == null) {
WebDriverFactory webDriverFactory = webDriverFactoryResolver.resolveWebDriverFactory();
initInterfaces(webDriverFactory);
driver = webDriverFactory.createWebDriver();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import lombok.Setter;
import org.jbehavesupport.core.web.WebDriverFactory;
import org.jbehavesupport.core.web.WebDriverFactoryResolver;

Expand All @@ -13,6 +14,7 @@ public class WebDriverFactoryResolverImpl implements WebDriverFactoryResolver {

private final List<WebDriverFactory> webDriverFactories;

@Setter
@Value("${web.browser:chrome}")
private String browserName;

Expand All @@ -23,5 +25,4 @@ public WebDriverFactory resolveWebDriverFactory() {
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("No WebDriverFactory found for given browser [" + browserName + "]."));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface WebDriverFactoryResolver {
*/
WebDriverFactory resolveWebDriverFactory();

void setBrowserName(String browserName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public final class WebSteps {
private final VerifierResolver verifierResolver;
private final GivenStoryHelper givenStoryHelper;
private final WebElementRegistry elementRegistry;
private final WebDriverFactoryResolver webDriverFactoryResolver;

public static WebSetting getCurrentSetting() {
return CURRENT_SETTING.get();
Expand Down Expand Up @@ -276,6 +277,13 @@ public void navigateForward() {
driver.navigate().forward();
}

@When("browser is changed to [$browserName]")
@Given("browser is changed to [$browserName]")
public void changeBrowser(String browserName) {
driver.quit();
webDriverFactoryResolver.setBrowserName(browserName);
}

private String parseConditionValue(String condition) {
String[] conditionParts = condition.split(" ");
return conditionParts.length >= 3 ? join(" ", asList(conditionParts).subList(2,conditionParts.length)) : null;
Expand Down