Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit f39bd3b

Browse files
committed
Refactored JavaScriptExecutor test to be more readable
1 parent 54efea5 commit f39bd3b

File tree

2 files changed

+87
-77
lines changed

2 files changed

+87
-77
lines changed

webtester-core/src/main/java/info/novatec/testit/webtester/browser/operations/JavaScriptExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void execute(String script, PageFragment fragment, Object... parameters)
4949
* @param script the JavaScript code to be executed on the current page
5050
* @param fragment the target {@link PageFragment}
5151
* @param parameters any of Boolean, Long, String, List, WebElement or null.
52-
* @param <T> the type of the return value is cast to before returning
52+
* @param <T> the expected type of the return value
5353
* @return the return value of the JavaScript
5454
* @see JavascriptExecutor#executeScript(String, Object...)
5555
* @since 2.0

webtester-core/src/test/java/info/novatec/testit/webtester/browser/operations/JavaScriptExecutorTest.java

Lines changed: 86 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,104 +4,114 @@
44
import static org.mockito.Mockito.doReturn;
55
import static org.mockito.Mockito.mock;
66
import static org.mockito.Mockito.verify;
7-
import static org.mockito.Mockito.withSettings;
87

8+
import org.junit.Before;
99
import org.junit.Test;
10+
import org.junit.experimental.runners.Enclosed;
1011
import org.junit.runner.RunWith;
12+
import org.mockito.InjectMocks;
13+
import org.mockito.Mock;
1114
import org.mockito.runners.MockitoJUnitRunner;
1215
import org.openqa.selenium.JavascriptExecutor;
1316
import org.openqa.selenium.WebDriver;
1417
import org.openqa.selenium.WebElement;
1518

19+
import utils.MockFactory;
20+
1621
import info.novatec.testit.webtester.browser.Browser;
1722
import info.novatec.testit.webtester.pagefragments.PageFragment;
1823

1924

20-
@RunWith(MockitoJUnitRunner.class)
25+
@RunWith(Enclosed.class)
2126
public class JavaScriptExecutorTest {
2227

23-
static final String JAVA_SCRIPT = "alert('Hello World!')";
24-
25-
@Test
26-
public void javaScriptIsExecutedIfWebDriverSupportsIt() {
27-
WebDriver webDriver = javaScriptExecutingWebDriver();
28-
javaScriptFor(webDriver).execute(JAVA_SCRIPT);
29-
verify(( JavascriptExecutor ) webDriver).executeScript(JAVA_SCRIPT);
30-
}
31-
32-
@Test
33-
public void justParametersWithoutReturn() {
34-
WebDriver webDriver = javaScriptExecutingWebDriver();
35-
javaScriptFor(webDriver).execute(JAVA_SCRIPT, "param1", "param2");
36-
verify(( JavascriptExecutor ) webDriver).executeScript(JAVA_SCRIPT, "param1", "param2");
37-
}
38-
39-
@Test
40-
public void justParametersWithReturn() {
41-
WebDriver webDriver = javaScriptExecutingWebDriver();
42-
doReturn("returnValue").when(( JavascriptExecutor ) webDriver).executeScript(JAVA_SCRIPT, "param");
43-
Object returnValue = javaScriptFor(webDriver).executeWithReturn(JAVA_SCRIPT, "param");
44-
assertThat(returnValue).isEqualTo("returnValue");
45-
}
46-
47-
@Test
48-
public void pageFragmentAndParametersWithoutReturn() {
49-
50-
WebDriver webDriver = javaScriptExecutingWebDriver();
51-
PageFragment fragment = pageFragment();
52-
53-
javaScriptFor(webDriver).execute(JAVA_SCRIPT, fragment, "param1", "param2");
54-
55-
WebElement webElement = fragment.webElement();
56-
verify(( JavascriptExecutor ) webDriver).executeScript(JAVA_SCRIPT, webElement, "param1", "param2");
28+
@RunWith(MockitoJUnitRunner.class)
29+
public static class JavaScriptEnabledDriver {
30+
31+
@Mock
32+
Browser browser;
33+
@InjectMocks
34+
JavaScriptExecutor cut;
35+
36+
@Mock(extraInterfaces = WebDriver.class)
37+
JavascriptExecutor webDriver;
38+
39+
@Before
40+
public void stubBrowsersWebDriver() {
41+
doReturn(webDriver).when(browser).webDriver();
42+
}
43+
44+
@Test
45+
public void scriptsCanBeExecuted() {
46+
cut.execute("alert('Hello World!')");
47+
verify(webDriver).executeScript("alert('Hello World!')");
48+
}
49+
50+
@Test
51+
public void scriptsWithParametersCanBeExecuted() {
52+
cut.execute("alert('arguments[0]')", "Hello World!");
53+
verify(webDriver).executeScript("alert('arguments[0]')", "Hello World!");
54+
}
55+
56+
@Test
57+
public void scriptsWithReturnCanBeExecuted() {
58+
doReturn(true).when(webDriver).executeScript("return true");
59+
Boolean returnValue = cut.executeWithReturn("return true");
60+
assertThat(returnValue).isEqualTo(true);
61+
}
62+
63+
@Test
64+
public void scriptsWithReturnAndParametersCanBeExecuted() {
65+
doReturn(true).when(webDriver).executeScript("return !arguments[0]", false);
66+
Boolean returnValue = cut.executeWithReturn("return !arguments[0]", false);
67+
assertThat(returnValue).isEqualTo(true);
68+
}
69+
70+
@Test
71+
public void scriptsWithPageFragmentsUseCorrectWebElement() {
72+
PageFragment fragment = MockFactory.fragment().build();
73+
cut.execute("arguments[0].value = 'arguments[1]')", fragment, "new value");
74+
verify(webDriver).executeScript("arguments[0].value = 'arguments[1]')", fragment.webElement(), "new value");
75+
}
76+
77+
@Test
78+
public void scriptsWithReturnAndPageFragmentsUseCorrectWebElement() {
79+
80+
PageFragment fragment = MockFactory.fragment().build();
81+
WebElement webElement = fragment.webElement();
82+
doReturn("old value").when(webDriver).executeScript("return arguments[0].arguments[1])", webElement, "value");
83+
84+
String returnValue = cut.executeWithReturn("return arguments[0].arguments[1])", fragment, "value");
85+
assertThat(returnValue).isEqualTo("old value");
86+
87+
}
5788

5889
}
5990

60-
@Test
61-
public void pageFragmentAndParametersWithReturn() {
91+
@RunWith(MockitoJUnitRunner.class)
92+
public static class NonJavaScriptEnabledDriver {
6293

63-
WebDriver webDriver = javaScriptExecutingWebDriver();
64-
PageFragment fragment = pageFragment();
94+
@Mock
95+
Browser browser;
96+
@InjectMocks
97+
JavaScriptExecutor cut;
6598

66-
WebElement webElement = fragment.webElement();
67-
doReturn("returnValue").when(( JavascriptExecutor ) webDriver).executeScript(JAVA_SCRIPT, webElement, "param");
99+
@Before
100+
public void setUpBrowser() {
101+
WebDriver nonJavaScriptWebDriver = mock(WebDriver.class);
102+
doReturn(nonJavaScriptWebDriver).when(browser).webDriver();
103+
}
68104

69-
Object returnValue = javaScriptFor(webDriver).executeWithReturn(JAVA_SCRIPT, fragment, "param");
70-
assertThat(returnValue).isEqualTo("returnValue");
105+
@Test(expected = UnsupportedOperationException.class)
106+
public void cantExecuteScript() {
107+
cut.execute("alert('Hello World!')");
108+
}
71109

72-
}
73-
74-
@Test(expected = UnsupportedOperationException.class)
75-
public void executingJavaScriptThrowsExceptionIfBrowserDoesNotSupportIt() {
76-
WebDriver webDriver = nonJavaScriptExecutingWebDriver();
77-
javaScriptFor(webDriver).execute(JAVA_SCRIPT);
78-
}
79-
80-
/* utilities */
81-
82-
JavaScriptExecutor javaScriptFor(WebDriver webDriver) {
83-
return new JavaScriptExecutor(browserFor(webDriver));
84-
}
85-
86-
Browser browserFor(WebDriver webDriver) {
87-
Browser browser = mock(Browser.class);
88-
doReturn(webDriver).when(browser).webDriver();
89-
return browser;
90-
}
91-
92-
WebDriver javaScriptExecutingWebDriver() {
93-
return mock(WebDriver.class, withSettings().extraInterfaces(JavascriptExecutor.class));
94-
}
95-
96-
WebDriver nonJavaScriptExecutingWebDriver() {
97-
return mock(WebDriver.class);
98-
}
110+
@Test(expected = UnsupportedOperationException.class)
111+
public void cantExecuteScriptWithReturn() {
112+
cut.executeWithReturn("return true");
113+
}
99114

100-
PageFragment pageFragment() {
101-
PageFragment fragment = mock(PageFragment.class);
102-
WebElement webElement = mock(WebElement.class);
103-
doReturn(webElement).when(fragment).webElement();
104-
return fragment;
105115
}
106116

107117
}

0 commit comments

Comments
 (0)