]> source.dussan.org Git - archiva.git/blob
12546c340e6d1621d75aaa7bf3f4424d30999681
[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         log.info("WebDriver {}, {}, {}, {}", seleniumBrowser, seleniumHost, seleniumPort, seleniumRemote);
66         if (seleniumRemote && StringUtils.isEmpty( seleniumHost )) {
67             throw new IllegalArgumentException( "seleniumHost must be set, when seleniumRemote=true" );
68         }
69         try {
70
71             if ( StringUtils.contains(seleniumBrowser, "chrome")) {
72                 if (seleniumRemote)
73                 {
74                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
75                         DesiredCapabilities.chrome()
76                     );
77                 } else {
78                     return new ChromeDriver(  );
79                 }
80             }
81
82             if (StringUtils.contains(seleniumBrowser, "safari")) {
83                 if (seleniumRemote)
84                 {
85                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
86                         DesiredCapabilities.safari()
87                     );
88                 } else {
89                     return new SafariDriver();
90                 }
91             }
92
93             if (StringUtils.contains(seleniumBrowser, "iexplore")) {
94                 if (seleniumRemote)
95                 {
96                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
97                         DesiredCapabilities.internetExplorer()
98                     );
99                 } else {
100                     new InternetExplorerDriver(  );
101                 }
102             }
103
104             if (StringUtils.contains( seleniumBrowser, "firefox" ))
105             {
106                 if ( seleniumRemote )
107                 {
108                     return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
109                         DesiredCapabilities.firefox()
110                     );
111                 }
112                 else
113                 {
114                     return new FirefoxDriver();
115                 }
116             }
117
118             if ( seleniumRemote )
119             {
120                 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
121                     DesiredCapabilities.htmlUnit()
122                 );
123             }
124             else
125             {
126                 DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
127                 capabilities.setJavascriptEnabled( true );
128                 capabilities.setVersion( "firefox-52" );
129                 HtmlUnitDriver driver = new HtmlUnitDriver( capabilities  ) {
130                     @Override
131                     protected WebClient modifyWebClient( WebClient client )
132                     {
133                         client.getOptions().setThrowExceptionOnFailingStatusCode( false );
134                         client.getOptions().setThrowExceptionOnScriptError( false );
135                         client.getOptions().setCssEnabled( true );
136                         return client;
137                     }
138                 };
139                 return driver;
140             }
141
142         } catch (MalformedURLException e) {
143             throw new RuntimeException("Initializion of remote driver failed");
144         }
145
146     }
147
148     public static String getBaseUrl() {
149
150         if (System.getProperties().containsKey( "baseUrl" )) {
151             return System.getProperty("baseUrl");
152         }
153         int containerPort = 7777;
154         if (System.getProperties().containsKey("container.http.port")) {
155             containerPort = Integer.parseInt(System.getProperty("container.http.port"));
156         } else if (System.getProperties().containsKey("container.propertiesPortFilePath"))
157         {
158             Properties portProperties = new Properties();
159             try (InputStream inputStream = Files.newInputStream(Paths.get(System.getProperty("container.propertiesPortFilePath"))))
160             {
161                 portProperties.load(inputStream);
162             }
163             catch ( IOException e )
164             {
165                 log.error("Error during property loading with containger.propertiesPortFilePath");
166             }
167             if ( portProperties.containsKey( "tomcat.maven.http.port" ) )
168             {
169                 containerPort = Integer.parseInt( portProperties.getProperty( "tomcat.maven.http.port" ) );
170             }
171             else
172             {
173                 containerPort = Integer.parseInt( portProperties.getProperty( "container.http.port" ) );
174             }
175         }
176         return "http://localhost:" + containerPort+"/archiva";
177     }
178
179     public static Path takeScreenShot( String fileName, WebDriver driver) {
180         Path result = null;
181         try
182         {
183             Path snapDir = Paths.get( "target", "errorshtmlsnap" );
184             Path screenShotDir = Paths.get("target","screenshots");
185             if ( !Files.exists( snapDir ) )
186             {
187                 Files.createDirectories( snapDir );
188             }
189             Path htmlFile = snapDir.resolve( fileName + ".html" );
190             Path screenShotFile = screenShotDir.resolve( fileName );
191             String pageSource=null;
192             String encoding="ISO-8859-1";
193             try
194             {
195                 pageSource = ( (JavascriptExecutor) driver ).executeScript( "return document.documentElement.outerHTML;" ).toString();
196             } catch (Exception e) {
197                 log.info("Could not create html source by javascript");
198                 pageSource = driver.getPageSource();
199             }
200             if (pageSource.contains("encoding=\"")) {
201                 encoding = pageSource.replaceFirst( ".*encoding=\"([^\"]+)\".*", "$1" );
202             }
203             FileUtils.writeStringToFile( htmlFile.toFile(), pageSource, encoding);
204             try
205             {
206                 File scrs = ((TakesScreenshot)driver).getScreenshotAs( OutputType.FILE );
207                 result = scrs.toPath();
208                 Files.copy(result, screenShotFile);
209             }
210             catch ( Exception e )
211             {
212                 log.info( "Could not create screenshot: " + e.getMessage() );
213             }
214
215         }
216         catch ( IOException e )
217         {
218             log.info( "Creating screenshot failed " + e.getMessage() );
219         }
220         return result;
221     }
222 }