]> source.dussan.org Git - archiva.git/blob
0f3af86bbad94eca705b24409ba485658d5c44c4
[archiva.git] /
1 package org.apache.archiva.web.test.tools;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import com.gargoylesoftware.htmlunit.WebClient;
22 import org.apache.commons.io.FileUtils;
23 import org.apache.commons.lang3.StringUtils;
24 import org.openqa.selenium.*;
25 import org.openqa.selenium.chrome.ChromeDriver;
26 import org.openqa.selenium.firefox.FirefoxDriver;
27 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
28 import org.openqa.selenium.ie.InternetExplorerDriver;
29 import org.openqa.selenium.remote.DesiredCapabilities;
30 import org.openqa.selenium.remote.RemoteWebDriver;
31 import org.openqa.selenium.safari.SafariDriver;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.net.MalformedURLException;
40 import java.net.URL;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.util.Properties;
45 import java.util.function.Consumer;
46 import java.util.function.Function;
47
48 /**
49  * Created by martin_s on 04.06.17.
50  */
51 public class WebdriverUtility
52 {
53
54     static final Logger log = LoggerFactory.getLogger( WebdriverUtility.class );
55
56     public static WebDriver newWebDriver() {
57         String seleniumBrowser = System.getProperty("selenium.browser");
58         String seleniumHost = System.getProperty("seleniumHost", "localhost");
59         int seleniumPort = Integer.getInteger("seleniumPort", 4444);
60         boolean seleniumRemote = Boolean.parseBoolean(System.getProperty("seleniumRemote","false"));
61         return newWebDriver(seleniumBrowser,seleniumHost, seleniumPort,seleniumRemote);
62     }
63
64     public static WebDriver newWebDriver(String seleniumBrowser, String seleniumHost, int seleniumPort, boolean seleniumRemote) {
65         try {
66
67             if ( StringUtils.contains(seleniumBrowser, "chrome")) {
68                 if (seleniumRemote)
69                 {
70                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
71                         DesiredCapabilities.chrome()
72                     );
73                 } else {
74                     return new ChromeDriver(  );
75                 }
76             }
77
78             if (StringUtils.contains(seleniumBrowser, "safari")) {
79                 if (seleniumRemote)
80                 {
81                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
82                         DesiredCapabilities.safari()
83                     );
84                 } else {
85                     return new SafariDriver();
86                 }
87             }
88
89             if (StringUtils.contains(seleniumBrowser, "iexplore")) {
90                 if (seleniumRemote)
91                 {
92                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
93                         DesiredCapabilities.internetExplorer()
94                     );
95                 } else {
96                     new InternetExplorerDriver(  );
97                 }
98             }
99
100             if (StringUtils.contains( seleniumBrowser, "firefox" ))
101             {
102                 if ( seleniumRemote )
103                 {
104                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
105                         DesiredCapabilities.firefox()
106                     );
107                 }
108                 else
109                 {
110                     return new FirefoxDriver();
111                 }
112             }
113
114             if ( seleniumRemote )
115             {
116                 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
117                     DesiredCapabilities.htmlUnit()
118                 );
119             }
120             else
121             {
122                 DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
123                 capabilities.setJavascriptEnabled( true );
124                 capabilities.setVersion( "firefox-52" );
125                 HtmlUnitDriver driver = new HtmlUnitDriver( capabilities  ) {
126                     @Override
127                     protected WebClient modifyWebClient( WebClient client )
128                     {
129                         client.getOptions().setThrowExceptionOnFailingStatusCode( false );
130                         client.getOptions().setThrowExceptionOnScriptError( false );
131                         client.getOptions().setCssEnabled( true );
132                         return client;
133                     }
134                 };
135                 return driver;
136             }
137
138         } catch (MalformedURLException e) {
139             throw new RuntimeException("Initializion of remote driver failed");
140         }
141
142     }
143
144     public static String getBaseUrl() {
145
146         if (System.getProperties().containsKey( "baseUrl" )) {
147             return System.getProperty("baseUrl");
148         }
149         int containerPort = 7777;
150         if (System.getProperties().containsKey("container.http.port")) {
151             containerPort = Integer.parseInt(System.getProperty("container.http.port"));
152         } else if (System.getProperties().containsKey("container.propertiesPortFilePath"))
153         {
154             Properties portProperties = new Properties();
155             try (InputStream inputStream = Files.newInputStream(Paths.get(System.getProperty("container.propertiesPortFilePath"))))
156             {
157                 portProperties.load(inputStream);
158             }
159             catch ( IOException e )
160             {
161                 log.error("Error during property loading with containger.propertiesPortFilePath");
162             }
163             if ( portProperties.containsKey( "tomcat.maven.http.port" ) )
164             {
165                 containerPort = Integer.parseInt( portProperties.getProperty( "tomcat.maven.http.port" ) );
166             }
167             else
168             {
169                 containerPort = Integer.parseInt( portProperties.getProperty( "container.http.port" ) );
170             }
171         }
172         return "http://localhost:" + containerPort+"/archiva";
173     }
174
175     public static Path takeScreenShot( String fileName, WebDriver driver) {
176         Path result = null;
177         try
178         {
179             Path snapDir = Paths.get( "target", "errorshtmlsnap" );
180             Path screenShotDir = Paths.get("target","screenshots");
181             if ( !Files.exists( snapDir ) )
182             {
183                 Files.createDirectories( snapDir );
184             }
185             Path htmlFile = snapDir.resolve( fileName + ".html" );
186             Path screenShotFile = screenShotDir.resolve( fileName );
187             String pageSource=null;
188             String encoding="ISO-8859-1";
189             try
190             {
191                 pageSource = ( (JavascriptExecutor) driver ).executeScript( "return document.documentElement.outerHTML;" ).toString();
192             } catch (Exception e) {
193                 log.info("Could not create html source by javascript");
194                 pageSource = driver.getPageSource();
195             }
196             if (pageSource.contains("encoding=\"")) {
197                 encoding = pageSource.replaceFirst( ".*encoding=\"([^\"]+)\".*", "$1" );
198             }
199             FileUtils.writeStringToFile( htmlFile.toFile(), pageSource, encoding);
200             try
201             {
202                 File scrs = ((TakesScreenshot)driver).getScreenshotAs( OutputType.FILE );
203                 result = scrs.toPath();
204                 Files.copy(result, screenShotFile);
205             }
206             catch ( Exception e )
207             {
208                 log.info( "Could not create screenshot: " + e.getMessage() );
209             }
210
211         }
212         catch ( IOException e )
213         {
214             log.info( "Creating screenshot failed " + e.getMessage() );
215         }
216         return result;
217     }
218 }