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.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;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.net.MalformedURLException;
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;
50 * Created by martin_s on 04.06.17.
52 public class WebdriverUtility
55 static final Logger log = LoggerFactory.getLogger( WebdriverUtility.class );
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);
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" );
72 if ( StringUtils.contains(seleniumBrowser, "chrome")) {
73 ChromeOptions options = new ChromeOptions();
74 options.addArguments("start-maximized");
77 DesiredCapabilities capabilities = DesiredCapabilities.chrome();
78 capabilities.setCapability( ChromeOptions.CAPABILITY, options );
79 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
83 return new ChromeDriver( options );
87 if (StringUtils.contains(seleniumBrowser, "safari")) {
90 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
91 DesiredCapabilities.safari()
94 return new SafariDriver();
98 if (StringUtils.contains(seleniumBrowser, "iexplore")) {
101 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
102 DesiredCapabilities.internetExplorer()
105 new InternetExplorerDriver( );
109 if (StringUtils.contains( seleniumBrowser, "firefox" ))
111 if ( seleniumRemote )
113 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
114 DesiredCapabilities.firefox()
119 return new FirefoxDriver();
123 if ( seleniumRemote )
125 return new RemoteWebDriver( new URL( "http://" + seleniumHost + ":" + seleniumPort + "/wd/hub" ),
126 DesiredCapabilities.htmlUnit()
131 DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
132 capabilities.setJavascriptEnabled( true );
133 capabilities.setVersion( "firefox-52" );
134 HtmlUnitDriver driver = new HtmlUnitDriver( capabilities ) {
136 protected WebClient modifyWebClient( WebClient client )
138 client.getOptions().setThrowExceptionOnFailingStatusCode( false );
139 client.getOptions().setThrowExceptionOnScriptError( false );
140 client.getOptions().setCssEnabled( true );
147 } catch (MalformedURLException e) {
148 throw new RuntimeException("Initializion of remote driver failed");
153 public static String getBaseUrl() {
155 if (System.getProperties().containsKey( "baseUrl" )) {
156 return System.getProperty("baseUrl");
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"))
163 Properties portProperties = new Properties();
164 try (InputStream inputStream = Files.newInputStream(Paths.get(System.getProperty("container.propertiesPortFilePath"))))
166 portProperties.load(inputStream);
168 catch ( IOException e )
170 log.error("Error during property loading with containger.propertiesPortFilePath");
172 if ( portProperties.containsKey( "tomcat.maven.http.port" ) )
174 containerPort = Integer.parseInt( portProperties.getProperty( "tomcat.maven.http.port" ) );
178 containerPort = Integer.parseInt( portProperties.getProperty( "container.http.port" ) );
181 return "http://localhost:" + containerPort+"/archiva";
184 public static Path takeScreenShot( String fileName, WebDriver driver) {
188 Path snapDir = Paths.get( "target", "errorshtmlsnap" );
189 Path screenShotDir = Paths.get("target","screenshots");
190 if ( !Files.exists( snapDir ) )
192 Files.createDirectories( snapDir );
194 Path htmlFile = snapDir.resolve( fileName + ".html" );
195 Path screenShotFile = screenShotDir.resolve( fileName );
196 String pageSource=null;
197 String encoding="ISO-8859-1";
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();
205 if (pageSource.contains("encoding=\"")) {
206 encoding = pageSource.replaceFirst( ".*encoding=\"([^\"]+)\".*", "$1" );
208 FileUtils.writeStringToFile( htmlFile.toFile(), pageSource, encoding);
211 File scrs = ((TakesScreenshot)driver).getScreenshotAs( OutputType.FILE );
212 result = scrs.toPath();
213 Files.copy(result, screenShotFile);
215 catch ( Exception e )
217 log.info( "Could not create screenshot: " + e.getMessage() );
221 catch ( IOException e )
223 log.info( "Creating screenshot failed " + e.getMessage() );