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

Fix parametrization for FindBy and Description annotation. #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<compiler.version>1.8</compiler.version>

<selenium.version>2.53.0</selenium.version>
<handlebars.version>4.0.6</handlebars.version>
</properties>

<dependencies>
Expand All @@ -57,11 +58,16 @@
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.1.0</version>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
<version>${handlebars.version}</version>
<exclusions>
<exclusion>
<groupId>org.mozilla</groupId>
<artifactId>rhino</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/io/qameta/htmlelements/util/HandlebarsUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.qameta.htmlelements.util;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;

import java.util.Map;

/**
* @author nogert <nogert@yandex-team.ru>
*/
public class HandlebarsUtils {

private static Handlebars handlebars = new Handlebars();

private HandlebarsUtils() {
}

public static String resolveVars(String original, Map<String, String> parameters) {
try {
Template template = handlebars.compileInline(original);
original = template.apply(parameters);
} catch (Exception ignored) {
//do nothing
}
return original;
}
}
10 changes: 2 additions & 8 deletions src/main/java/io/qameta/htmlelements/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,14 @@ public static String getDescription(Class<?> clazz) {
public static String getSelector(Method method, Object[] args) {
Map<String, String> parameters = getParameters(method, args);
String selector = method.getAnnotation(FindBy.class).value();
for (String key : parameters.keySet()) {
selector = selector.replaceAll("\\{\\{ " + key + " \\}\\}", parameters.get(key));
}
return selector;
return HandlebarsUtils.resolveVars(selector, parameters);
}

public static String getDescription(Method method, Object[] args) {
if (method.isAnnotationPresent(Description.class)) {
Map<String, String> parameters = getParameters(method, args);
String name = method.getAnnotation(Description.class).value();
for (String key : parameters.keySet()) {
name = name.replaceAll("\\{\\{ " + key + " \\}\\}", parameters.get(key));
}
return name;
return HandlebarsUtils.resolveVars(name, parameters);
} else {
return splitCamelCase(method.getName());
}
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/io/qameta/htmlelements/HandleBarsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.qameta.htmlelements;

import io.qameta.htmlelements.util.HandlebarsUtils;
import org.junit.Test;

import java.util.HashMap;

import static org.junit.Assert.assertEquals;

/**
* @author nogert <nogert@yandex-team.ru>
*/
public class HandleBarsTest {

@Test
public void resolveCorrectTemplate() {
String original = "{{template}} is correct";
HashMap<String, String> parameters = new HashMap<String, String>() {{
put("template", "Template");
}};

assertEquals("Template is correct", HandlebarsUtils.resolveVars(original, parameters));
}

@Test
public void resolveCorrectTemplateWithSpaces() {
String original = "{{ template }} is correct";
HashMap<String, String> parameters = new HashMap<String, String>() {{
put("template", "Template");
}};

assertEquals("Template is correct", HandlebarsUtils.resolveVars(original, parameters));
}

@Test
public void notResolveNotCorrectTemplate() {
String original = "{{template is not correct";
HashMap<String, String> parameters = new HashMap<String, String>() {{
put("template", "Template");
}};

assertEquals("{{template is not correct", HandlebarsUtils.resolveVars(original, parameters));
}
}
41 changes: 41 additions & 0 deletions src/test/java/io/qameta/htmlelements/ParametrizationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.qameta.htmlelements;

import io.qameta.htmlelements.example.element.SearchArrow;
import io.qameta.htmlelements.util.ReflectionUtils;
import org.junit.Test;

import java.lang.reflect.Method;

import static org.junit.Assert.assertEquals;

/**
* @author nogert <nogert@yandex-team.ru>
*/
public class ParametrizationTest {

private static final String TEST_FORM_NAME = "test form name";
private static final String[] SEARCH_FORM_PARAMETERS = {TEST_FORM_NAME};

@Test
public void elementWithParametrizedDescriptionShouldHasCorrectName() throws NoSuchMethodException {
Method searchForm = getSearchFormMethod();
String description = ReflectionUtils.getDescription(searchForm, new String[]{TEST_FORM_NAME});
assertEquals(String.format("Форма %s", TEST_FORM_NAME), description);

}

@Test
public void elementWithParametrizedFindByShouldHasCorrectXpath() throws NoSuchMethodException {
Method searchForm = getSearchFormMethod();
String selector = ReflectionUtils.getSelector(searchForm, SEARCH_FORM_PARAMETERS);

assertEquals(String.format("//div[@class='%s']", TEST_FORM_NAME), selector);
}

private Method getSearchFormMethod() throws NoSuchMethodException {
return ReflectionUtils.getMethods(SearchArrow.class)
.stream()
.filter(m -> m.getName().equals("form"))
.findFirst().orElseThrow(NoSuchMethodException::new);
}
}
2 changes: 1 addition & 1 deletion src/test/java/io/qameta/htmlelements/example/TestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class TestData {

public static final String SEARCH_FORM_XPATH = "//div[@class='form']";

public static final String SEARCH_FORM_TEMPLATE = "//div[@class='{{ className }}']";
public static final String SEARCH_FORM_TEMPLATE = "//div[@class='{{className}}']";

public static final String REQUEST_INPUT_XPATH = "//div[@class='input']";

Expand Down