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) {
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" );
71 if ( StringUtils.contains(seleniumBrowser, "chrome")) {
74 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
75 DesiredCapabilities.chrome()
78 return new ChromeDriver( );
82 if (StringUtils.contains(seleniumBrowser, "safari")) {
85 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
86 DesiredCapabilities.safari()
89 return new SafariDriver();
93 if (StringUtils.contains(seleniumBrowser, "iexplore")) {
96 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
97 DesiredCapabilities.internetExplorer()
100 new InternetExplorerDriver( );
104 if (StringUtils.contains( seleniumBrowser, "firefox" ))
106 if ( seleniumRemote )
108 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
109 DesiredCapabilities.firefox()
114 return new FirefoxDriver();
118 if ( seleniumRemote )
120 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
121 DesiredCapabilities.htmlUnit()
126 DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
127 capabilities.setJavascriptEnabled( true );
128 capabilities.setVersion( "firefox-52" );
129 HtmlUnitDriver driver = new HtmlUnitDriver( capabilities ) {
131 protected WebClient modifyWebClient( WebClient client )
133 client.getOptions().setThrowExceptionOnFailingStatusCode( false );
134 client.getOptions().setThrowExceptionOnScriptError( false );
135 client.getOptions().setCssEnabled( true );
142 } catch (MalformedURLException e) {
143 throw new RuntimeException("Initializion of remote driver failed");
148 public static String getBaseUrl() {
150 if (System.getProperties().containsKey( "baseUrl" )) {
151 return System.getProperty("baseUrl");
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"))
158 Properties portProperties = new Properties();
159 try (InputStream inputStream = Files.newInputStream(Paths.get(System.getProperty("container.propertiesPortFilePath"))))
161 portProperties.load(inputStream);
163 catch ( IOException e )
165 log.error("Error during property loading with containger.propertiesPortFilePath");
167 if ( portProperties.containsKey( "tomcat.maven.http.port" ) )
169 containerPort = Integer.parseInt( portProperties.getProperty( "tomcat.maven.http.port" ) );
173 containerPort = Integer.parseInt( portProperties.getProperty( "container.http.port" ) );
176 return "http://localhost:" + containerPort+"/archiva";
179 public static Path takeScreenShot( String fileName, WebDriver driver) {
183 Path snapDir = Paths.get( "target", "errorshtmlsnap" );
184 Path screenShotDir = Paths.get("target","screenshots");
185 if ( !Files.exists( snapDir ) )
187 Files.createDirectories( snapDir );
189 Path htmlFile = snapDir.resolve( fileName + ".html" );
190 Path screenShotFile = screenShotDir.resolve( fileName );
191 String pageSource=null;
192 String encoding="ISO-8859-1";
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();
200 if (pageSource.contains("encoding=\"")) {
201 encoding = pageSource.replaceFirst( ".*encoding=\"([^\"]+)\".*", "$1" );
203 FileUtils.writeStringToFile( htmlFile.toFile(), pageSource, encoding);
206 File scrs = ((TakesScreenshot)driver).getScreenshotAs( OutputType.FILE );
207 result = scrs.toPath();
208 Files.copy(result, screenShotFile);
210 catch ( Exception e )
212 log.info( "Could not create screenshot: " + e.getMessage() );
216 catch ( IOException e )
218 log.info( "Creating screenshot failed " + e.getMessage() );