]> source.dussan.org Git - archiva.git/blob
a1fbca3bf0114d8abca3c3ab37c1c33224a8c800
[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.chrome.ChromeOptions;
27 import org.openqa.selenium.firefox.FirefoxDriver;
28 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
29 import org.openqa.selenium.ie.InternetExplorerDriver;
30 import org.openqa.selenium.remote.DesiredCapabilities;
31 import org.openqa.selenium.remote.RemoteWebDriver;
32 import org.openqa.selenium.safari.SafariDriver;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.net.MalformedURLException;
41 import java.net.URL;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.Properties;
46 import java.util.function.Consumer;
47 import java.util.function.Function;
48
49 /**
50  * Created by martin_s on 04.06.17.
51  */
52 public class WebdriverUtility
53 {
54
55     static final Logger log = LoggerFactory.getLogger( WebdriverUtility.class );
56
57     public static WebDriver newWebDriver() {
58         String seleniumBrowser = System.getProperty("selenium.browser");
59         String seleniumHost = System.getProperty("seleniumHost", "localhost");
60         int seleniumPort = Integer.getInteger("seleniumPort", 4444);
61         boolean seleniumRemote = Boolean.parseBoolean(System.getProperty("seleniumRemote","false"));
62         return newWebDriver(seleniumBrowser,seleniumHost, seleniumPort,seleniumRemote);
63     }
64
65     public static WebDriver newWebDriver(String seleniumBrowser, String seleniumHost, int seleniumPort, boolean seleniumRemote) {
66         log.info("WebDriver {}, {}, {}, {}", seleniumBrowser, seleniumHost, seleniumPort, seleniumRemote);
67         if (seleniumRemote && StringUtils.isEmpty( seleniumHost )) {
68             throw new IllegalArgumentException( "seleniumHost must be set, when seleniumRemote=true" );
69         }
70         try {
71
72             if ( StringUtils.contains(seleniumBrowser, "chrome")) {
73                 ChromeOptions options = new ChromeOptions();
74                 options.addArguments("start-maximized");
75                 if (seleniumRemote)
76                 {
77                     DesiredCapabilities capabilities = DesiredCapabilities.chrome();
78                     capabilities.setCapability( ChromeOptions.CAPABILITY, options );
79                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
80                         capabilities
81                     );
82                 } else {
83                     return new ChromeDriver( options );
84                 }
85             }
86
87             if (StringUtils.contains(seleniumBrowser, "safari")) {
88                 if (seleniumRemote)
89                 {
90                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
91                         DesiredCapabilities.safari()
92                     );
93                 } else {
94                     return new SafariDriver();
95                 }
96             }
97
98             if (StringUtils.contains(seleniumBrowser, "iexplore")) {
99                 if (seleniumRemote)
100                 {
101                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
102                         DesiredCapabilities.internetExplorer()
103                     );
104                 } else {
105                     new InternetExplorerDriver(  );
106                 }
107             }
108
109             if (StringUtils.contains( seleniumBrowser, "firefox" ))
110             {
111                 if ( seleniumRemote )
112                 {
113                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
114                         DesiredCapabilities.firefox()
115                     );
116                 }
117                 else
118                 {
119                     return new FirefoxDriver();
120                 }
121             }
122
123             if ( seleniumRemote )
124             {
125                 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
126                     DesiredCapabilities.htmlUnit()
127                 );
128             }
129             else
130             {
131                 DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
132                 capabilities.setJavascriptEnabled( true );
133                 capabilities.setVersion( "firefox-52" );
134                 HtmlUnitDriver driver = new HtmlUnitDriver( capabilities  ) {
135                     @Override
136                     protected WebClient modifyWebClient( WebClient client )
137                     {
138                         client.getOptions().setThrowExceptionOnFailingStatusCode( false );
139                         client.getOptions().setThrowExceptionOnScriptError( false );
140                         client.getOptions().setCssEnabled( true );
141                         return client;
142                     }
143                 };
144                 return driver;
145             }
146
147         } catch (MalformedURLException e) {
148             throw new RuntimeException("Initializion of remote driver failed");
149         }
150
151     }
152
153     public static String getBaseUrl() {
154
155         if (System.getProperties().containsKey( "baseUrl" )) {
156             return System.getProperty("baseUrl");
157         }
158         int containerPort = 7777;
159         if (System.getProperties().containsKey("container.http.port")) {
160             containerPort = Integer.parseInt(System.getProperty("container.http.port"));
161         } else if (System.getProperties().containsKey("container.propertiesPortFilePath"))
162         {
163             Properties portProperties = new Properties();
164             try (InputStream inputStream = Files.newInputStream(Paths.get(System.getProperty("container.propertiesPortFilePath"))))
165             {
166                 portProperties.load(inputStream);
167             }
168             catch ( IOException e )
169             {
170                 log.error("Error during property loading with containger.propertiesPortFilePath");
171             }
172             if ( portProperties.containsKey( "tomcat.maven.http.port" ) )
173             {
174                 containerPort = Integer.parseInt( portProperties.getProperty( "tomcat.maven.http.port" ) );
175             }
176             else
177             {
178                 containerPort = Integer.parseInt( portProperties.getProperty( "container.http.port" ) );
179             }
180         }
181         return "http://localhost:" + containerPort+"/archiva";
182     }
183
184     public static Path takeScreenShot( String fileName, WebDriver driver) {
185         Path result = null;
186         try
187         {
188             Path snapDir = Paths.get( "target", "errorshtmlsnap" );
189             Path screenShotDir = Paths.get("target","screenshots");
190             if ( !Files.exists( snapDir ) )
191             {
192                 Files.createDirectories( snapDir );
193             }
194             Path htmlFile = snapDir.resolve( fileName + ".html" );
195             Path screenShotFile = screenShotDir.resolve( fileName );
196             String pageSource=null;
197             String encoding="ISO-8859-1";
198             try
199             {
200                 pageSource = ( (JavascriptExecutor) driver ).executeScript( "return document.documentElement.outerHTML;" ).toString();
201             } catch (Exception e) {
202                 log.info("Could not create html source by javascript");
203                 pageSource = driver.getPageSource();
204             }
205             if (pageSource.contains("encoding=\"")) {
206                 encoding = pageSource.replaceFirst( ".*encoding=\"([^\"]+)\".*", "$1" );
207             }
208             FileUtils.writeStringToFile( htmlFile.toFile(), pageSource, encoding);
209             try
210             {
211                 File scrs = ((TakesScreenshot)driver).getScreenshotAs( OutputType.FILE );
212                 result = scrs.toPath();
213                 Files.copy(result, screenShotFile);
214             }
215             catch ( Exception e )
216             {
217                 log.info( "Could not create screenshot: " + e.getMessage() );
218             }
219
220         }
221         catch ( IOException e )
222         {
223             log.info( "Creating screenshot failed " + e.getMessage() );
224         }
225         return result;
226     }
227 }