1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36
37
38
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
45 private static final Logger LOGGER = LoggerFactory.getLogger(AludraSelenium1.class);
46
47
48 private SeleniumWrapper seleniumWrapper;
49
50 private Selenium1Interaction interaction;
51 private Selenium1Verification verification;
52 private Selenium1Condition condition;
53
54 private State state;
55
56
57 public AludraSelenium1() {
58 this.state = State.CREATED;
59 }
60
61
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) {
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) {
127 LOGGER.error("Error closing " + getClass().getName(), e);
128 }
129 }
130 }
131
132
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 }