|
7 | 7 | import static org.apache.commons.lang3.StringUtils.EMPTY; |
8 | 8 | import static org.hamcrest.core.Is.is; |
9 | 9 | import static org.hamcrest.core.IsCollectionContaining.hasItems; |
10 | | -import static org.junit.Assert.assertNotNull; |
11 | 10 | import static org.junit.Assert.assertThat; |
| 11 | +import static org.junit.Assert.assertTrue; |
12 | 12 |
|
13 | 13 | import io.appium.java_client.events.listeners.AlertListener; |
14 | 14 | import io.appium.java_client.events.listeners.AlertListener2; |
|
24 | 24 | import org.openqa.selenium.security.Credentials; |
25 | 25 |
|
26 | 26 | import java.util.Set; |
| 27 | +import java.util.function.Predicate; |
27 | 28 |
|
28 | 29 | public class ListenableObjectTest { |
29 | 30 | private static final String PREFIX = "Externally defined listener: "; |
30 | 31 |
|
| 32 | + private final EmptyWebDriver emptyWebDriver = new EmptyWebDriver(); |
| 33 | + private final ContextListener2 contextListener = new ContextListener2(); |
| 34 | + private final AlertListener2 alertListener = new AlertListener2(); |
| 35 | + private final SearchingListener2 searchingListener = new SearchingListener2(); |
| 36 | + |
| 37 | + private final ContextAware contextAware = new ContextAware() { |
| 38 | + @Override |
| 39 | + public WebDriver context(String name) { |
| 40 | + return emptyWebDriver; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Set<String> getContextHandles() { |
| 45 | + return of(EMPTY); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String getContext() { |
| 50 | + return EMPTY; |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + private final Credentials credentials = new Credentials() { |
| 55 | + @Override |
| 56 | + public int hashCode() { |
| 57 | + return super.hashCode(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String toString() { |
| 62 | + return "Test credentials 1"; |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + private final Credentials credentials2 = new Credentials() { |
| 67 | + @Override |
| 68 | + public int hashCode() { |
| 69 | + return super.hashCode(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public String toString() { |
| 74 | + return "Test credentials 2"; |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + private final Predicate<ContextAware> contextAwarePredicate = (contextAware) -> { |
| 79 | + contextAware.context("WEB_VIEW"); |
| 80 | + |
| 81 | + assertThat(contextListener.messages, |
| 82 | + hasItems(PREFIX + "Attempt to change current context to NATIVE_APP", |
| 83 | + PREFIX + "The previous context has been changed to NATIVE_APP", |
| 84 | + PREFIX + "Attempt to change current context to WEB_VIEW", |
| 85 | + PREFIX + "The previous context has been changed to WEB_VIEW")); |
| 86 | + assertThat(contextListener.messages.size(), is(4)); |
| 87 | + |
| 88 | + ContextListener singleContextListener = (ContextListener) |
| 89 | + listeners.get(ContextListener.class); |
| 90 | + assertThat(singleContextListener.messages, |
| 91 | + hasItems("Attempt to change current context to NATIVE_APP", |
| 92 | + "The previous context has been changed to NATIVE_APP", |
| 93 | + "Attempt to change current context to WEB_VIEW", |
| 94 | + "The previous context has been changed to WEB_VIEW")); |
| 95 | + assertThat(singleContextListener.messages.size(), is(4)); |
| 96 | + return true; |
| 97 | + }; |
| 98 | + |
| 99 | + private final Predicate<Alert> alertPredicate = alert -> { |
| 100 | + alert.accept(); |
| 101 | + alert.dismiss(); |
| 102 | + alert.sendKeys("Keys"); |
| 103 | + |
| 104 | + alert.setCredentials(credentials); |
| 105 | + alert.authenticateUsing(credentials2); |
| 106 | + |
| 107 | + assertThat(alertListener.messages, |
| 108 | + hasItems(PREFIX + "Attempt to accept alert", |
| 109 | + PREFIX + "The alert was accepted", |
| 110 | + PREFIX + "Attempt to dismiss alert", |
| 111 | + PREFIX + "The alert was dismissed", |
| 112 | + PREFIX + "Attempt to send keys to alert", |
| 113 | + PREFIX + "Keys were sent to alert", |
| 114 | + PREFIX + "Attempt to send credentials " + credentials.toString() + " to alert", |
| 115 | + PREFIX + "Credentials " + credentials.toString() + " were sent to alert", |
| 116 | + PREFIX + "Attempt to send credentials " + credentials2.toString() + " to alert", |
| 117 | + PREFIX + "Credentials " + credentials2.toString() + " were sent to alert")); |
| 118 | + assertThat(alertListener.messages.size(), is(10)); |
| 119 | + |
| 120 | + AlertListener singleAlertListener = (AlertListener) |
| 121 | + listeners.get(AlertListener.class); |
| 122 | + |
| 123 | + assertThat(singleAlertListener.messages, |
| 124 | + hasItems("Attempt to accept alert", |
| 125 | + "The alert was accepted", |
| 126 | + "Attempt to dismiss alert", |
| 127 | + "The alert was dismissed", |
| 128 | + "Attempt to send keys to alert", |
| 129 | + "Keys were sent to alert", |
| 130 | + "Attempt to send credentials " + credentials.toString() + " to alert", |
| 131 | + "Credentials " + credentials.toString() + " were sent to alert", |
| 132 | + "Attempt to send credentials " + credentials2.toString() + " to alert", |
| 133 | + "Credentials " + credentials2.toString() + " were sent to alert")); |
| 134 | + assertThat(singleAlertListener.messages.size(), is(10)); |
| 135 | + return true; |
| 136 | + }; |
| 137 | + |
| 138 | + private final Predicate<WebDriver> webDriverPredicate = driver -> { |
| 139 | + driver.findElement(By.id("someId")); |
| 140 | + assertThat(searchingListener.messages, |
| 141 | + hasItems(PREFIX + "Attempt to find something using By.id: someId. The root element is null", |
| 142 | + PREFIX + "The searching for something using By.id: someId has beed finished. " |
| 143 | + + "The root element was null")); |
| 144 | + assertThat(searchingListener.messages.size(), is(2)); |
| 145 | + |
| 146 | + SearchingListener singleSearchingListener = (SearchingListener) |
| 147 | + listeners.get(SearchingListener.class); |
| 148 | + |
| 149 | + assertThat(singleSearchingListener.messages, |
| 150 | + hasItems("Attempt to find something using By.id: someId. The root element is null", |
| 151 | + "The searching for something using By.id: someId has beed finished. " |
| 152 | + + "The root element was null")); |
| 153 | + assertThat(singleSearchingListener.messages.size(), is(2)); |
| 154 | + return true; |
| 155 | + }; |
| 156 | + |
| 157 | + |
31 | 158 | @Test public void listenableObjectSample() { |
32 | 159 | try { |
33 | | - EmptyWebDriver emptyWebDriver = new EmptyWebDriver(); |
34 | | - ContextAware contextAware = new ContextAware() { |
35 | | - @Override |
36 | | - public WebDriver context(String name) { |
37 | | - return emptyWebDriver; |
38 | | - } |
39 | | - |
40 | | - @Override |
41 | | - public Set<String> getContextHandles() { |
42 | | - return of(EMPTY); |
43 | | - } |
44 | | - |
45 | | - @Override |
46 | | - public String getContext() { |
47 | | - return EMPTY; |
48 | | - } |
49 | | - }; |
50 | | - |
51 | | - ContextListener2 contextListener = new ContextListener2(); |
52 | | - AlertListener2 alertListener = new AlertListener2(); |
53 | | - contextAware = getEventFiringObject(contextAware, emptyWebDriver, contextListener, alertListener); |
54 | | - |
55 | | - EmptyWebDriver webDriver = (EmptyWebDriver) contextAware.context("NATIVE_APP"); |
56 | | - contextAware.context("WEB_VIEW"); |
57 | | - assertNotNull(webDriver); |
58 | | - |
59 | | - assertThat(contextListener.messages, |
60 | | - hasItems(PREFIX + "Attempt to change current context to NATIVE_APP", |
61 | | - PREFIX + "The previous context has been changed to NATIVE_APP", |
62 | | - PREFIX + "Attempt to change current context to WEB_VIEW", |
63 | | - PREFIX + "The previous context has been changed to WEB_VIEW")); |
64 | | - assertThat(contextListener.messages.size(), is(4)); |
65 | | - |
66 | | - ContextListener singleContextListener = (ContextListener) listeners.get(ContextListener.class); |
67 | | - |
68 | | - assertThat(singleContextListener.messages, |
69 | | - hasItems("Attempt to change current context to NATIVE_APP", |
70 | | - "The previous context has been changed to NATIVE_APP", |
71 | | - "Attempt to change current context to WEB_VIEW", |
72 | | - "The previous context has been changed to WEB_VIEW")); |
73 | | - assertThat(singleContextListener.messages.size(), is(4)); |
| 160 | + ContextAware listenableContextAware = |
| 161 | + getEventFiringObject(contextAware, emptyWebDriver, contextListener, alertListener); |
| 162 | + WebDriver webDriver = listenableContextAware.context("NATIVE_APP"); |
| 163 | + assertTrue(contextAwarePredicate.test(listenableContextAware)); |
74 | 164 |
|
75 | 165 | Alert alert = webDriver.switchTo().alert(); |
76 | | - alert.accept(); |
77 | | - alert.dismiss(); |
78 | | - alert.sendKeys("Keys"); |
79 | | - Credentials credentials = new Credentials() { |
80 | | - @Override |
81 | | - public int hashCode() { |
82 | | - return super.hashCode(); |
83 | | - } |
84 | | - |
85 | | - @Override |
86 | | - public String toString() { |
87 | | - return "Test credentials 1"; |
88 | | - } |
89 | | - }; |
90 | | - |
91 | | - Credentials credentials2 = new Credentials() { |
92 | | - @Override |
93 | | - public int hashCode() { |
94 | | - return super.hashCode(); |
95 | | - } |
96 | | - |
97 | | - @Override |
98 | | - public String toString() { |
99 | | - return "Test credentials 2"; |
100 | | - } |
101 | | - }; |
102 | | - |
103 | | - alert.setCredentials(credentials); |
104 | | - alert.authenticateUsing(credentials2); |
105 | | - |
106 | | - assertThat(alertListener.messages, |
107 | | - hasItems(PREFIX + "Attempt to accept alert", |
108 | | - PREFIX + "The alert was accepted", |
109 | | - PREFIX + "Attempt to dismiss alert", |
110 | | - PREFIX + "The alert was dismissed", |
111 | | - PREFIX + "Attempt to send keys to alert", |
112 | | - PREFIX + "Keys were sent to alert", |
113 | | - PREFIX + "Attempt to send credentials " + credentials.toString() + " to alert", |
114 | | - PREFIX + "Credentials " + credentials.toString() + " were sent to alert", |
115 | | - PREFIX + "Attempt to send credentials " + credentials2.toString() + " to alert", |
116 | | - PREFIX + "Credentials " + credentials2.toString() + " were sent to alert")); |
117 | | - assertThat(alertListener.messages.size(), is(10)); |
118 | | - |
119 | | - AlertListener singleAlertListener = (AlertListener) listeners.get(AlertListener.class); |
120 | | - assertThat(singleAlertListener.messages, |
121 | | - hasItems("Attempt to accept alert", |
122 | | - "The alert was accepted", |
123 | | - "Attempt to dismiss alert", |
124 | | - "The alert was dismissed", |
125 | | - "Attempt to send keys to alert", |
126 | | - "Keys were sent to alert", |
127 | | - "Attempt to send credentials " + credentials.toString() + " to alert", |
128 | | - "Credentials " + credentials.toString() + " were sent to alert", |
129 | | - "Attempt to send credentials " + credentials2.toString() + " to alert", |
130 | | - "Credentials " + credentials2.toString() + " were sent to alert")); |
131 | | - assertThat(singleAlertListener.messages.size(), is(10)); |
132 | | - |
133 | | - SearchingListener2 searchingListener = new SearchingListener2(); |
134 | | - webDriver = getEventFiringWebDriver(webDriver, searchingListener); |
135 | | - |
136 | | - webDriver.findElement(By.id("someId")); |
137 | | - assertThat(searchingListener.messages, |
138 | | - hasItems(PREFIX + "Attempt to find something using By.id: someId. The root element is null", |
139 | | - PREFIX + "The searching for something using By.id: someId has beed finished. " |
140 | | - + "The root element was null")); |
141 | | - assertThat(searchingListener.messages.size(), is(2)); |
142 | | - |
143 | | - SearchingListener singleSearchingListener = (SearchingListener) |
144 | | - listeners.get(SearchingListener.class); |
145 | | - assertThat(singleSearchingListener.messages, |
146 | | - hasItems("Attempt to find something using By.id: someId. The root element is null", |
147 | | - "The searching for something using By.id: someId has beed finished. " |
148 | | - + "The root element was null")); |
149 | | - assertThat(singleSearchingListener.messages.size(), is(2)); |
| 166 | + assertTrue(alertPredicate.test(alert)); |
| 167 | + |
| 168 | + assertTrue(webDriverPredicate.test(getEventFiringWebDriver(webDriver, searchingListener))); |
150 | 169 | } finally { |
151 | 170 | listeners.get(ContextListener.class).messages.clear(); |
152 | 171 | listeners.get(AlertListener.class).messages.clear(); |
|
0 commit comments