|
4 | 4 | import static org.mockito.Mockito.doReturn; |
5 | 5 | import static org.mockito.Mockito.mock; |
6 | 6 | import static org.mockito.Mockito.verify; |
7 | | -import static org.mockito.Mockito.withSettings; |
8 | 7 |
|
| 8 | +import org.junit.Before; |
9 | 9 | import org.junit.Test; |
| 10 | +import org.junit.experimental.runners.Enclosed; |
10 | 11 | import org.junit.runner.RunWith; |
| 12 | +import org.mockito.InjectMocks; |
| 13 | +import org.mockito.Mock; |
11 | 14 | import org.mockito.runners.MockitoJUnitRunner; |
12 | 15 | import org.openqa.selenium.JavascriptExecutor; |
13 | 16 | import org.openqa.selenium.WebDriver; |
14 | 17 | import org.openqa.selenium.WebElement; |
15 | 18 |
|
| 19 | +import utils.MockFactory; |
| 20 | + |
16 | 21 | import info.novatec.testit.webtester.browser.Browser; |
17 | 22 | import info.novatec.testit.webtester.pagefragments.PageFragment; |
18 | 23 |
|
19 | 24 |
|
20 | | -@RunWith(MockitoJUnitRunner.class) |
| 25 | +@RunWith(Enclosed.class) |
21 | 26 | public class JavaScriptExecutorTest { |
22 | 27 |
|
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 | + } |
57 | 88 |
|
58 | 89 | } |
59 | 90 |
|
60 | | - @Test |
61 | | - public void pageFragmentAndParametersWithReturn() { |
| 91 | + @RunWith(MockitoJUnitRunner.class) |
| 92 | + public static class NonJavaScriptEnabledDriver { |
62 | 93 |
|
63 | | - WebDriver webDriver = javaScriptExecutingWebDriver(); |
64 | | - PageFragment fragment = pageFragment(); |
| 94 | + @Mock |
| 95 | + Browser browser; |
| 96 | + @InjectMocks |
| 97 | + JavaScriptExecutor cut; |
65 | 98 |
|
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 | + } |
68 | 104 |
|
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 | + } |
71 | 109 |
|
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 | + } |
99 | 114 |
|
100 | | - PageFragment pageFragment() { |
101 | | - PageFragment fragment = mock(PageFragment.class); |
102 | | - WebElement webElement = mock(WebElement.class); |
103 | | - doReturn(webElement).when(fragment).webElement(); |
104 | | - return fragment; |
105 | 115 | } |
106 | 116 |
|
107 | 117 | } |
0 commit comments