Skip to content

Commit

Permalink
Users can now change browsers mid-story (EmbedITCZ#150)
Browse files Browse the repository at this point in the history
[FEAT] Users can now change browsers mid-story
  • Loading branch information
Hebcak authored and Daniel.Smrcek committed Aug 5, 2019
1 parent 6602a67 commit df91dc8
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
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;

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 @@ -60,6 +60,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 @@ -300,6 +301,13 @@ public void takeScreenShot(){
screenshotCreator.createScreenshot(Type.MANUAL);
}

@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

0 comments on commit df91dc8

Please sign in to comment.