Changing webdriver tests to remotewebdriver

This commit is contained in:
Martin Stockhammer 2017-04-16 20:34:21 +02:00
parent f7b57a974d
commit c29a3426db
3 changed files with 106 additions and 72 deletions

View File

@ -83,6 +83,21 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>${selenium-server.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.24</version>
</dependency>
<dependency> <dependency>

View File

@ -30,11 +30,15 @@ import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver; import org.openqa.selenium.safari.SafariDriver;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -136,26 +140,36 @@ public class WebDriverBrowseTest
} }
@Override @Override
public WebDriver getDefaultDriver() public WebDriver getDefaultDriver() {
{ String seleniumBrowser = System.getProperty("selenium.browser");
String seleniumBrowser = System.getProperty( "selenium.browser" ); String seleniumHost = System.getProperty("seleniumHost", "localhost");
int seleniumPort = Integer.getInteger("seleniumPort", 4444);
try {
if ( StringUtils.contains( seleniumBrowser, "chrome" ) ) if (StringUtils.contains(seleniumBrowser, "chrome")) {
{ return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
return new ChromeDriver(); DesiredCapabilities.chrome()
} );
}
if ( StringUtils.contains( seleniumBrowser, "safari" ) ) if (StringUtils.contains(seleniumBrowser, "safari")) {
{ return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
return new SafariDriver(); DesiredCapabilities.safari()
} );
}
if ( StringUtils.contains( seleniumBrowser, "iexplore" ) ) if (StringUtils.contains(seleniumBrowser, "iexplore")) {
{ return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
return new InternetExplorerDriver(); DesiredCapabilities.internetExplorer()
);
}
return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
DesiredCapabilities.firefox()
);
} catch (MalformedURLException e) {
throw new RuntimeException("Initializion of remote driver failed");
} }
return new FirefoxDriver();
} }
} }

View File

@ -29,13 +29,18 @@ import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver; import org.openqa.selenium.safari.SafariDriver;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.fluentlenium.core.Fluent; import org.fluentlenium.core.Fluent;
import org.junit.Before; import org.junit.Before;
@ -44,86 +49,86 @@ import org.junit.Before;
* @author Olivier Lamy * @author Olivier Lamy
*/ */
public class WebDriverTest public class WebDriverTest
extends FluentTest extends FluentTest {
{
@Override @Override
public Fluent takeScreenShot( String fileName ) public Fluent takeScreenShot(String fileName) {
{ try {
try
{
// save html to have a minimum feedback if jenkins firefox not up // save html to have a minimum feedback if jenkins firefox not up
File fileNameHTML = new File( fileName + ".html" ); File fileNameHTML = new File(fileName + ".html");
FileUtils.writeStringToFile( fileNameHTML, getDriver().getPageSource() ); FileUtils.writeStringToFile(fileNameHTML, getDriver().getPageSource());
} } catch (IOException e) {
catch ( IOException e ) System.out.print(e.getMessage());
{
System.out.print( e.getMessage() );
e.printStackTrace(); e.printStackTrace();
} }
return super.takeScreenShot( fileName ); return super.takeScreenShot(fileName);
} }
@Before @Before
public void init() public void init() {
{ setSnapshotMode(Mode.TAKE_SNAPSHOT_ON_FAIL);
setSnapshotMode( Mode.TAKE_SNAPSHOT_ON_FAIL ); setSnapshotPath(new File("target", "errorshtmlsnap").getAbsolutePath());
setSnapshotPath( new File( "target", "errorshtmlsnap" ).getAbsolutePath() );
} }
@Test @Test
public void simpletest() public void simpletest()
throws Exception throws Exception {
{
Properties tomcatPortProperties = new Properties(); Properties tomcatPortProperties = new Properties();
tomcatPortProperties.load( tomcatPortProperties.load(
new FileInputStream( new File( System.getProperty( "tomcat.propertiesPortFilePath" ) ) ) ); new FileInputStream(new File(System.getProperty("tomcat.propertiesPortFilePath"))));
int tomcatPort = Integer.parseInt( tomcatPortProperties.getProperty( "tomcat.maven.http.port" ) ); int tomcatPort = Integer.parseInt(tomcatPortProperties.getProperty("tomcat.maven.http.port"));
goTo("http://localhost:" + tomcatPort + "/archiva/index.html?request_lang=en");
goTo( "http://localhost:" + tomcatPort + "/archiva/index.html?request_lang=en" );
// wait until topbar-menu-container is feeded // wait until topbar-menu-container is feeded
await().atMost(5, TimeUnit.SECONDS).until("#topbar-menu").isPresent(); await().atMost(5, TimeUnit.SECONDS).until("#topbar-menu").isPresent();
FluentList<FluentWebElement> elements = find( "#create-admin-link-a" );
if ( !elements.isEmpty() && elements.get( 0 ).isDisplayed() ) FluentList<FluentWebElement> elements = find("#create-admin-link-a");
{
WebElement webElement = elements.get( 0 ).getElement(); if (!elements.isEmpty() && elements.get(0).isDisplayed()) {
Assert.assertEquals( "Create Admin User", webElement.getText() ); WebElement webElement = elements.get(0).getElement();
} Assert.assertEquals("Create Admin User", webElement.getText());
else } else {
{ elements = find("#login-link-a");
elements = find( "#login-link-a" ); WebElement webElement = elements.get(0).getElement();
WebElement webElement = elements.get( 0 ).getElement(); Assert.assertEquals("LOGIN", webElement.getText());
Assert.assertEquals( "LOGIN", webElement.getText() );
} }
} }
@Override @Override
public WebDriver getDefaultDriver() public WebDriver getDefaultDriver() {
{ String seleniumBrowser = System.getProperty("selenium.browser");
String seleniumBrowser = System.getProperty( "selenium.browser" ); String seleniumHost = System.getProperty("seleniumHost", "localhost");
int seleniumPort = Integer.getInteger("seleniumPort", 4444);
try {
if ( StringUtils.contains( seleniumBrowser, "chrome" ) ) if (StringUtils.contains(seleniumBrowser, "chrome")) {
{ return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
return new ChromeDriver(); DesiredCapabilities.chrome()
);
}
if (StringUtils.contains(seleniumBrowser, "safari")) {
return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
DesiredCapabilities.safari()
);
}
if (StringUtils.contains(seleniumBrowser, "iexplore")) {
return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
DesiredCapabilities.internetExplorer()
);
}
return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"),
DesiredCapabilities.firefox()
);
} catch (MalformedURLException e) {
throw new RuntimeException("Initializion of remote driver failed");
} }
if ( StringUtils.contains( seleniumBrowser, "safari" ) )
{
return new SafariDriver();
}
if ( StringUtils.contains( seleniumBrowser, "iexplore" ) )
{
return new InternetExplorerDriver();
}
return new FirefoxDriver();
} }
} }