1 package org.apache.archiva.web.test.tools;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
36 import java.io.FileInputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.net.MalformedURLException;
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;
49 * Created by martin_s on 04.06.17.
51 public class WebdriverUtility
54 static final Logger log = LoggerFactory.getLogger( WebdriverUtility.class );
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);
64 public static WebDriver newWebDriver(String seleniumBrowser, String seleniumHost, int seleniumPort, boolean seleniumRemote) {
67 if ( StringUtils.contains(seleniumBrowser, "chrome")) {
70 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
71 DesiredCapabilities.chrome()
74 return new ChromeDriver( );
78 if (StringUtils.contains(seleniumBrowser, "safari")) {
81 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
82 DesiredCapabilities.safari()
85 return new SafariDriver();
89 if (StringUtils.contains(seleniumBrowser, "iexplore")) {
92 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
93 DesiredCapabilities.internetExplorer()
96 new InternetExplorerDriver( );
100 if (StringUtils.contains( seleniumBrowser, "firefox" ))
102 if ( seleniumRemote )
104 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
105 DesiredCapabilities.firefox()
110 return new FirefoxDriver();
114 if ( seleniumRemote )
116 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
117 DesiredCapabilities.htmlUnit()
122 DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
123 capabilities.setJavascriptEnabled( true );
124 capabilities.setVersion( "firefox-52" );
125 HtmlUnitDriver driver = new HtmlUnitDriver( capabilities ) {
127 protected WebClient modifyWebClient( WebClient client )
129 client.getOptions().setThrowExceptionOnFailingStatusCode( false );
130 client.getOptions().setThrowExceptionOnScriptError( false );
131 client.getOptions().setCssEnabled( true );
138 } catch (MalformedURLException e) {
139 throw new RuntimeException("Initializion of remote driver failed");
144 public static String getBaseUrl() {
146 if (System.getProperties().containsKey( "baseUrl" )) {
147 return System.getProperty("baseUrl");
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"))
154 Properties portProperties = new Properties();
155 try (InputStream inputStream = Files.newInputStream(Paths.get(System.getProperty("container.propertiesPortFilePath"))))
157 portProperties.load(inputStream);
159 catch ( IOException e )
161 log.error("Error during property loading with containger.propertiesPortFilePath");
163 if ( portProperties.containsKey( "tomcat.maven.http.port" ) )
165 containerPort = Integer.parseInt( portProperties.getProperty( "tomcat.maven.http.port" ) );
169 containerPort = Integer.parseInt( portProperties.getProperty( "container.http.port" ) );
172 return "http://localhost:" + containerPort+"/archiva";
175 public static Path takeScreenShot( String fileName, WebDriver driver) {
179 Path snapDir = Paths.get( "target", "errorshtmlsnap" );
180 Path screenShotDir = Paths.get("target","screenshots");
181 if ( !Files.exists( snapDir ) )
183 Files.createDirectories( snapDir );
185 Path htmlFile = snapDir.resolve( fileName + ".html" );
186 Path screenShotFile = screenShotDir.resolve( fileName );
187 String pageSource=null;
188 String encoding="ISO-8859-1";
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();
196 if (pageSource.contains("encoding=\"")) {
197 encoding = pageSource.replaceFirst( ".*encoding=\"([^\"]+)\".*", "$1" );
199 FileUtils.writeStringToFile( htmlFile.toFile(), pageSource, encoding);
202 File scrs = ((TakesScreenshot)driver).getScreenshotAs( OutputType.FILE );
203 result = scrs.toPath();
204 Files.copy(result, screenShotFile);
206 catch ( Exception e )
208 log.info( "Could not create screenshot: " + e.getMessage() );
212 catch ( IOException e )
214 log.info( "Creating screenshot failed " + e.getMessage() );