View Javadoc

1   /**
2    * 
3    */
4   package org.sirius.server.web;
5   
6   import java.util.HashMap;
7   
8   import javax.jws.WebService;
9   
10  import org.openqa.selenium.By;
11  import org.openqa.selenium.Dimension;
12  import org.openqa.selenium.Point;
13  import org.openqa.selenium.WebDriver;
14  import org.openqa.selenium.WebElement;
15  import org.openqa.selenium.chrome.ChromeDriver;
16  import org.openqa.selenium.firefox.FirefoxDriver;
17  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
18  import org.openqa.selenium.ie.InternetExplorerDriver;
19  
20  /**
21   * @author Myk Kolisnyk
22   * 
23   */
24  @WebService
25  public class WebCore extends WebHelper {
26  
27  	public final String IE = "ie";
28  	public final String FIREFOX = "firefox";
29  	public final String FIREFOX_UNSECURED = "chrome";
30  	public final String CHROME = "googlechrome";
31  	public final String OPERA = "opera";
32  	public final String HTMLUNIT = "htmlunit";
33  
34  	/**
35  	 * 
36  	 */
37  	public WebCore() {
38  	}
39  
40  	public String start(String browser) {
41  		WebDriver driver = null;
42  		if (browser.equalsIgnoreCase(IE)) {
43  			driver = new InternetExplorerDriver();
44  		} else if (browser.equalsIgnoreCase(FIREFOX)) {
45  			driver = new FirefoxDriver();
46  		} else if (browser.equalsIgnoreCase(CHROME)) {
47  			driver = new ChromeDriver();
48  		} else if (browser.equalsIgnoreCase(HTMLUNIT)) {
49  			driver = new HtmlUnitDriver();
50  		}
51  
52  		if (driver != null) {
53  			DriverMap.drivers.put(driver.toString(), driver);
54  			return driver.toString();
55  		}
56  
57  		return null;
58  	}
59  
60  	public void stop(String token) {
61  		driver(token).close();
62  		DriverMap.drivers.remove(token);
63  	}
64  
65  	public void open(String token, String url) {
66  		driver(token).navigate().to(url);
67  	}
68  
69  	public void back(String token) {
70  		driver(token).navigate().back();
71  	}
72  
73  	public void forward(String token) {
74  		driver(token).navigate().forward();
75  	}
76  
77  	public void refresh(String token) {
78  		driver(token).navigate().refresh();
79  	}
80  
81  	public String getURL(String token) {
82  		return driver(token).getCurrentUrl();
83  	}
84  
85  	public String getPageSource(String token) {
86  		return driver(token).getPageSource();
87  	}
88  
89  	public String getTitle(String token) {
90  		return driver(token).getTitle();
91  	}
92  
93  	public String getWindowHandle(String token) {
94  		return driver(token).getWindowHandle();
95  	}
96  
97  	public void selectFrameByIndex(String token, int index) {
98  		driver(token).switchTo().frame(index);
99  	}
100 
101 	public void selectFrameByName(String token, String name) {
102 		driver(token).switchTo().frame(name);
103 	}
104 
105 	public void selectWindow(String token, String name) {
106 		driver(token).switchTo().window(name);
107 	}
108 
109 	public void selectDefaultContent(String token) {
110 		driver(token).switchTo().defaultContent();
111 	}
112 
113 	public void selectAlert(String token) {
114 		driver(token).switchTo().alert();
115 	}
116 
117 	public void clear(String token, String startFrom, String locator) {
118 		getElement(token, startFrom, locator).clear();
119 	}
120 
121 	public void click(String token, String startFrom, String locator) {
122 		getElement(token, startFrom, locator).click();
123 	}
124 
125 	public String getAttribute(String token, String startFrom, String locator,
126 			String attribute) {
127 		return getElement(token, startFrom, locator).getAttribute(attribute);
128 	}
129 
130 	public String getCssValue(String token, String startFrom, String locator,
131 			String value) {
132 		return getElement(token, startFrom, locator).getCssValue(value);
133 	}
134 
135 	public Point getLocation(String token, String startFrom, String locator) {
136 		return getElement(token, startFrom, locator).getLocation();
137 	}
138 
139 	public Dimension getSize(String token, String startFrom, String locator) {
140 		return getElement(token, startFrom, locator).getSize();
141 	}
142 
143 	public String getTagName(String token, String startFrom, String locator) {
144 		return getElement(token, startFrom, locator).getTagName();
145 	}
146 
147 	public String getText(String token, String startFrom, String locator) {
148 		return getElement(token, startFrom, locator).getText();
149 	}
150 
151 	public boolean isDisplayed(String token, String startFrom, String locator) {
152 		return getElement(token, startFrom, locator).isDisplayed();
153 	}
154 
155 	public boolean isEnabled(String token, String startFrom, String locator) {
156 		return getElement(token, startFrom, locator).isEnabled();
157 	}
158 
159 	public boolean isSelected(String token, String startFrom, String locator) {
160 		return getElement(token, startFrom, locator).isSelected();
161 	}
162 
163 	public void sendKeys(String token, String startFrom, String locator,
164 			String text) {
165 		getElement(token, startFrom, locator).sendKeys(text);
166 	}
167 
168 	public void submit(String token, String startFrom, String locator) {
169 		getElement(token, startFrom, locator).submit();
170 	}
171 }