Skip to content

Instantly share code, notes, and snippets.

@lfryc
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfryc/504a477b0e78731f7edf to your computer and use it in GitHub Desktop.
Save lfryc/504a477b0e78731f7edf to your computer and use it in GitHub Desktop.
Graphene / Angular integration
package io.graphene.ng;
import java.lang.annotation.Annotation;
import java.util.List;
import org.jboss.arquillian.graphene.findby.ByJQuery;
import org.jboss.arquillian.graphene.spi.findby.LocationStrategy;
import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
public class ByNG extends By {
private ByJQuery by;
/**
* Instantiates new locator with given jQuery selector
*
* @param selector the jQuery selector
*/
private ByNG(String jquerySelector) {
by = ByJQuery.selector(jquerySelector);
}
/**
* Instantiates new locator with given jQuery selector
*
* @param model the jQuery selector
*/
public static ByNG model(String model) {
return new ByNG(String.format(":visible[ng-model*='%s']", model));
}
public static ByNG action(String action) {
return new ByNG(String.format(":visible[ng-click*='%s']", action));
}
public static ByNG repeat(String action) {
return new ByNG(String.format(":visible[ng-repeat*=' in %s']", action));
}
/*
* (non-Javadoc)
*
* @see org.openqa.selenium.By#findElement(org.openqa.selenium.SearchContext)
*/
@Override
public WebElement findElement(SearchContext context) {
return by.findElement(context);
}
/*
* (non-Javadoc)
*
* @see org.openqa.selenium.By#findElements(org.openqa.selenium.SearchContext)
*/
@Override
public List<WebElement> findElements(SearchContext context) {
return by.findElements(context);
}
/*
* (non-Javadoc)
*
* @see org.openqa.selenium.By#toString()
*/
@Override
public String toString() {
return by.toString();
}
/**
* Location strategy for searching for elements using jQuery selectors
*
* @author Lukas Fryc
*/
public static class NgLocationStrategy implements LocationStrategy {
@Override
public ByNG fromAnnotation(Annotation annotation) {
FindByNG findBy = (FindByNG) annotation;
if (!findBy.model().isEmpty()) {
return ByNG.model(findBy.model());
}
if (!findBy.action().isEmpty()) {
return ByNG.action(findBy.action());
}
if (!findBy.repeat().isEmpty()) {
return ByNG.repeat(findBy.repeat());
}
throw new IllegalStateException(String.format("Can't determine the selector from annotation %s", findBy));
}
}
}
package io.graphene.ng;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.jboss.arquillian.graphene.spi.findby.ImplementsLocationStrategy;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@ImplementsLocationStrategy(ByNG.NgLocationStrategy.class)
public @interface FindByNG {
String model() default "";
String action() default "";
String repeat() default "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment