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.config.AludraTestConfig;
19  import org.aludratest.config.ConfigProperty;
20  import org.aludratest.exception.AutomationException;
21  import org.aludratest.exception.TechnicalException;
22  import org.aludratest.service.Implementation;
23  import org.aludratest.service.gui.web.AludraWebGUI;
24  import org.aludratest.service.gui.web.WebGUICondition;
25  import org.aludratest.service.gui.web.WebGUIInteraction;
26  import org.aludratest.service.gui.web.WebGUIVerification;
27  import org.aludratest.service.gui.web.selenium.AbstractSeleniumService;
28  import org.aludratest.service.gui.web.selenium.SeleniumResourceService;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Implements the interface {@link AludraWebGUI} using Selenium 1 functionality
34   * to access the web GUI.
35   * 
36   * @author Marcel Malitz
37   * @author Volker Bergmann
38   * @author Joerg Langnickel
39   */
40  @Implementation({ AludraWebGUI.class })
41  @ConfigProperty(name = "browser", type = String.class, description = "The Selenium 1 browser name to use.", defaultValue = "*firefoxproxy")
42  public class AludraSelenium1 extends AbstractSeleniumService implements AludraWebGUI {
43  
44      /** The class' Logger. */
45      private static final Logger LOGGER = LoggerFactory.getLogger(AludraSelenium1.class);
46  
47      /** The {@link SeleniumWrapper} to perform the actual invocations. */
48      private SeleniumWrapper seleniumWrapper;
49  
50      private Selenium1Interaction interaction;
51      private Selenium1Verification verification;
52      private Selenium1Condition condition;
53  
54      private State state;
55  
56      /** Default constructor as required by the framework */
57      public AludraSelenium1() {
58          this.state = State.CREATED;
59      }
60  
61      /** Used by the framework to configure the service */
62      @Override
63      public void initService() {
64          assertState(State.CREATED, "init()");
65          try {
66              this.seleniumWrapper = new SeleniumWrapper(aludraServiceContext.getInstanceName(),
67                      aludraServiceContext.newComponentInstance(SeleniumResourceService.class), configuration);
68  
69              int hostCount = seleniumWrapper.getHostCount();
70              int threadCount = aludraServiceContext.newComponentInstance(AludraTestConfig.class).getNumberOfThreads();
71              if (hostCount != threadCount) {
72                  throw new AutomationException("Number of execution hosts (" + hostCount + ") "
73                          + "does not match the AludraTest number of threads (" + threadCount + ")");
74              }
75  
76              this.interaction = new Selenium1Interaction(seleniumWrapper);
77              this.verification = new Selenium1Verification(seleniumWrapper);
78              this.condition = new Selenium1Condition(seleniumWrapper);
79              this.state = State.INITIALIZED;
80              LOGGER.info("Opened " + getDescription());
81          }
82          catch (Exception e) { // NOSONAR
83              this.state = State.CLOSED;
84              String message = "Failed to initialize " + getClass() + "(" + getDescription() + ")";
85              throw new TechnicalException(message, e);
86          }
87      }
88  
89      @Override
90      public String getDescription() {
91          if (seleniumWrapper != null && seleniumWrapper.getConfiguration() != null) {
92              return "Using Selenium host: " + seleniumWrapper.getUsedSeleniumHost() + ", AUT: "
93                      + seleniumWrapper.getConfiguration().getUrlOfAut();
94          }
95          else {
96              return "[not available]";
97          }
98      }
99  
100     @Override
101     public WebGUIInteraction perform() {
102         assertState(State.INITIALIZED, "perform()");
103         return this.interaction;
104     }
105 
106     @Override
107     public WebGUIVerification verify() {
108         assertState(State.INITIALIZED, "verify()");
109         return this.verification;
110     }
111 
112     @Override
113     public WebGUICondition check() {
114         assertState(State.INITIALIZED, "check()");
115         return this.condition;
116     }
117 
118     @Override
119     public void close() {
120         if (this.state == State.INITIALIZED) {
121             this.state = State.CLOSED;
122             LOGGER.info("Closed " + getDescription());
123             try {
124                 seleniumWrapper.tearDown();
125             }
126             catch (Exception e) { // NOSONAR
127                 LOGGER.error("Error closing " + getClass().getName(), e);
128             }
129         }
130     }
131 
132     // private helpers ---------------------------------------------------------
133     private void assertState(State expectedState, String operation) {
134         if (this.state != expectedState) {
135             throw new TechnicalException("Operation '" + operation + "' " +
136                     "expects state '" + expectedState + "', but found " + this.state);
137         }
138     }
139 
140     static enum State {
141         CREATED, INITIALIZED, CLOSED
142     }
143 
144 }