View Javadoc
1   /*
2    * Copyright (C) 2010-2014 Hamburg Sud and the contributors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.aludratest.service.gui.web.selenium.selenium1;
17  
18  import org.aludratest.exception.AludraTestException;
19  import org.aludratest.service.gui.web.WebGUICondition;
20  import org.aludratest.service.locator.element.GUIElementLocator;
21  import org.aludratest.service.locator.window.WindowLocator;
22  import org.databene.commons.StringUtil;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * Selenium1 implementation of {@link WebGUICondition}
28   * 
29   * @author Marcel Malitz
30   * @author Joerg Langnickel
31   * @author Volker Bergmann
32   */
33  public class Selenium1Condition extends AbstractSeleniumAction implements WebGUICondition {
34  
35      private final static Logger LOGGER = LoggerFactory.getLogger(Selenium1Condition.class);
36  
37      /**
38       * Constructor
39       * 
40       * @param seleniumWrapper
41       *            - wrapper of Selenium on which the conditions will be executed
42       */
43      public Selenium1Condition(SeleniumWrapper seleniumWrapper) {
44          super(seleniumWrapper);
45      }
46  
47      @Override
48      public boolean isElementPresent(String elementType, String operation, GUIElementLocator locator) {
49          try {
50              wrapper.waitForElement(locator);
51              return true;
52          }
53          catch (AludraTestException e) {
54              return false;
55          }
56          catch (Exception e) { // NOSONAR
57              LOGGER.error("Unexpected exception during check which will be ignored", e);
58              return false;
59          }
60      }
61  
62      @Override
63      public boolean isElementVisible(String elementType, String operation, GUIElementLocator locator) {
64          try {
65              wrapper.waitForVisible(locator);
66              return true;
67          }
68          catch (AludraTestException e) {
69              return false;
70          }
71          catch (Exception e) { // NOSONAR
72              LOGGER.error("Unexpected exception during check which will be ignored", e);
73              return false;
74          }
75      }
76  
77      @Override
78      public boolean isElementEnabled(String elementType, String operation, GUIElementLocator locator) {
79          try {
80              wrapper.waitForEnabled(locator);
81              return true;
82          }
83          catch (AludraTestException e) {
84              return false;
85          }
86          catch (Exception e) { // NOSONAR
87              LOGGER.error("Unexpected exception during check which will be ignored", e);
88              return false;
89          }
90      }
91  
92      @Override
93      public boolean isElementEditable(String elementType, String elementName, GUIElementLocator locator) {
94          try {
95              wrapper.waitForEditable(locator);
96              return true;
97          }
98          catch (AludraTestException e) {
99              return false;
100         }
101         catch (Exception e) { // NOSONAR
102             LOGGER.error("Unexpected exception during check which will be ignored", e);
103             return false;
104         }
105     }
106 
107     @Override
108     public boolean isElementEditable(String elementType, String elementName, GUIElementLocator locator, long timeout) {
109         try {
110             wrapper.waitForEditable(locator, timeout);
111             return true;
112         }
113         catch (AludraTestException e) {
114             return false;
115         }
116         catch (Exception e) { // NOSONAR
117             LOGGER.error("Unexpected exception during check which will be ignored", e);
118             return false;
119         }
120     }
121 
122     @Override
123     public boolean isElementNotPresent(String elementType, String operation, GUIElementLocator locator) {
124         try {
125             wrapper.waitForElementNotPresent(locator);
126             return true;
127         }
128         catch (AludraTestException e) {
129             return false;
130         }
131         catch (Exception e) { // NOSONAR
132             LOGGER.error("Unexpected exception during check which will be ignored", e);
133             return false;
134         }
135     }
136 
137     @Override
138     public boolean isElementPresent(String elementType, String operation, GUIElementLocator locator, long timeout) {
139         try {
140             wrapper.waitForElement(locator, timeout);
141             return true;
142         }
143         catch (AludraTestException e) {
144             return false;
145         }
146         catch (Exception e) { // NOSONAR
147             LOGGER.error("Unexpected exception during check which will be ignored", e);
148             return false;
149         }
150     }
151 
152     @Override
153     public boolean isElementVisible(String elementType, String operation, GUIElementLocator locator, long timeout) {
154         try {
155             wrapper.waitForVisible(locator, timeout);
156             return true;
157         }
158         catch (AludraTestException e) {
159             return false;
160         }
161         catch (Exception e) { // NOSONAR
162             LOGGER.error("Unexpected exception during check which will be ignored", e);
163             return false;
164         }
165     }
166 
167     @Override
168     public boolean isElementEnabled(String elementType, String operation, GUIElementLocator locator, long timeout) {
169         try {
170             wrapper.waitForEnabled(locator, timeout);
171             return true;
172         }
173         catch (AludraTestException e) {
174             return false;
175         }
176         catch (Exception e) { // NOSONAR
177             LOGGER.error("Unexpected exception during check which will be ignored", e);
178             return false;
179         }
180     }
181 
182     @Override
183     public boolean isElementNotPresent(String elementType, String operation, GUIElementLocator locator, long timeout) {
184         try {
185             wrapper.waitForElementNotPresent(locator, timeout);
186             return true;
187         }
188         catch (AludraTestException e) {
189             return false;
190         }
191         catch (Exception e) { // NOSONAR
192             LOGGER.error("Unexpected exception during check which will be ignored", e);
193             return false;
194         }
195     }
196 
197     @Override
198     public boolean isWindowOpen(String elementType, String operation, WindowLocator locator) {
199         try {
200             wrapper.selectWindow(locator);
201             return true;
202         }
203         catch (AludraTestException e) {
204             return false;
205         }
206         catch (Exception e) { // NOSONAR
207             LOGGER.error("Unexpected exception during check which will be ignored", e);
208             return false;
209         }
210     }
211 
212     @Override
213     public boolean isElementPresentandInForeground(String elementType, String operation, GUIElementLocator locator) {
214         try {
215             wrapper.waitForElement(locator);
216             wrapper.waitForInForeground(locator);
217             return true;
218         }
219         catch (AludraTestException e) {
220             return false;
221         }
222         catch (Exception e) { // NOSONAR
223             LOGGER.error("Unexpected exception during check which will be ignored", e);
224             return false;
225         }
226     }
227 
228     @Override
229     public boolean isElementChecked(String elementType, String elementName, GUIElementLocator locator) {
230         try {
231             wrapper.waitForElement(locator);
232             wrapper.waitForInForeground(locator);
233             return wrapper.isChecked(locator);
234         }
235         catch (AludraTestException e) {
236             return false;
237         }
238         catch (Exception e) { // NOSONAR
239             LOGGER.error("Unexpected exception during check which will be ignored", e);
240             return false;
241         }
242     }
243 
244     @Override
245     public boolean containsLabels(String elementType, String elementName, GUIElementLocator locator, String... labels) {
246         CheckLabelCondition condition = new CheckLabelCondition(true, labels, wrapper, locator);
247         wrapper.retryUntilTimeout(condition);
248         return StringUtil.isEmpty(condition.getMismatches());
249     }
250 
251     @Override
252     public boolean equalsLabels(String elementType, String elementName, GUIElementLocator locator, String... labels) {
253         CheckLabelCondition condition = new CheckLabelCondition(false, labels, wrapper, locator);
254         wrapper.retryUntilTimeout(condition);
255         return StringUtil.isEmpty(condition.getMismatches());
256     }
257 }