1 package com.atlassian.webdriver.rule;
2
3 import com.atlassian.webdriver.testing.annotation.WindowSize;
4 import com.atlassian.webdriver.testing.rule.WindowSizeRule;
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.junit.runner.Description;
8 import org.junit.runner.RunWith;
9 import org.junit.runners.model.Statement;
10 import org.mockito.Mock;
11 import org.mockito.runners.MockitoJUnitRunner;
12 import org.openqa.selenium.Dimension;
13 import org.openqa.selenium.WebDriver;
14
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.verifyNoMoreInteractions;
17 import static org.mockito.Mockito.when;
18
19
20
21
22
23
24 @RunWith(MockitoJUnitRunner.class)
25 public class TestWindowSizeRule
26 {
27
28 @Mock private Statement mockTest;
29 @Mock private WebDriver webDriver;
30 @Mock private WebDriver.Options wedDriverOptions;
31 @Mock private WebDriver.Window wedDriverWindow;
32 @Mock private WindowSize mockWindowSize;
33
34 @Before
35 public void stubWebDriverOptions()
36 {
37 when(webDriver.manage()).thenReturn(wedDriverOptions);
38 when(wedDriverOptions.window()).thenReturn(wedDriverWindow);
39 }
40
41 @Test
42 public void shouldMaximizeWindowIfAnnotationNotPresent()
43 {
44 final Description description = Description.createTestDescription(TestWindowSizeRule.class, "testMethod");
45 new SafeStatementInvoker(createRule().apply(mockTest, description)).invokeSafely();
46 verify(wedDriverWindow).maximize();
47 }
48
49 @Test
50 public void shouldMaximizeWindowIfMaximizeIsTrueOnAnnotation()
51 {
52 when(mockWindowSize.annotationType()).thenReturn((Class)WindowSize.class);
53 when(mockWindowSize.maximize()).thenReturn(true);
54 when(mockWindowSize.width()).thenReturn(1024);
55 when(mockWindowSize.height()).thenReturn(768);
56 final Description description = Description.createTestDescription(TestWindowSizeRule.class, "testMethod", mockWindowSize);
57 new SafeStatementInvoker(createRule().apply(mockTest, description)).invokeSafely();
58 verify(wedDriverWindow).maximize();
59 verifyNoMoreInteractions(wedDriverWindow);
60 }
61
62 @Test
63 public void shouldSetWindowSizeIfMaximizeIsFalseOnAnnotation()
64 {
65 when(mockWindowSize.annotationType()).thenReturn((Class)WindowSize.class);
66 when(mockWindowSize.maximize()).thenReturn(false);
67 when(mockWindowSize.width()).thenReturn(1024);
68 when(mockWindowSize.height()).thenReturn(768);
69 final Description description = Description.createTestDescription(TestWindowSizeRule.class, "testMethod", mockWindowSize);
70 new SafeStatementInvoker(createRule().apply(mockTest, description)).invokeSafely();
71 verify(wedDriverWindow).setSize(new Dimension(1024,768));
72 verifyNoMoreInteractions(wedDriverWindow);
73 }
74
75 @Test
76 public void shouldSetWindowSizeIfMaximizeIsFalseOnClassLevelAnnotation()
77 {
78 final Description description = Description.createTestDescription(ClassAnnotatedWithWindowSize.class, "testMethod");
79 new SafeStatementInvoker(createRule().apply(mockTest, description)).invokeSafely();
80 verify(wedDriverWindow).setSize(new Dimension(1024,768));
81 verifyNoMoreInteractions(wedDriverWindow);
82 }
83
84 @Test
85 public void shouldPreferMethodLevelAnnotationOverClassLevel()
86 {
87 when(mockWindowSize.annotationType()).thenReturn((Class)WindowSize.class);
88 when(mockWindowSize.maximize()).thenReturn(true);
89 final Description description = Description.createTestDescription(ClassAnnotatedWithWindowSize.class, "testMethod", mockWindowSize);
90 new SafeStatementInvoker(createRule().apply(mockTest, description)).invokeSafely();
91 verify(wedDriverWindow).maximize();
92 verifyNoMoreInteractions(wedDriverWindow);
93 }
94
95 @Test(expected = IllegalStateException.class)
96 public void shouldThrowExceptionIfHeightInvalid() throws Throwable
97 {
98 when(mockWindowSize.annotationType()).thenReturn((Class)WindowSize.class);
99 when(mockWindowSize.maximize()).thenReturn(false);
100 when(mockWindowSize.width()).thenReturn(1024);
101 when(mockWindowSize.height()).thenReturn(0);
102 final Description description = Description.createTestDescription(TestWindowSizeRule.class, "testMethod", mockWindowSize);
103 createRule().apply(mockTest, description).evaluate();
104 }
105
106
107 private WindowSizeRule createRule()
108 {
109 return new WindowSizeRule(webDriver);
110 }
111
112 @WindowSize(width = 1024, height = 768)
113 public static class ClassAnnotatedWithWindowSize
114 {
115
116 }
117
118 }