Skip to content

Commit

Permalink
Merge pull request #2 from ddavison/fix-static-initializer
Browse files Browse the repository at this point in the history
v2.1 changes
  • Loading branch information
ddavison committed Oct 10, 2014
2 parents e369edc + f24e9e4 commit 6c2b850
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@ local.properties

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
target/
Binary file added chromedriver.linux
Binary file not shown.
File renamed without changes.
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<name>getting-started-with-selenium-framework</name>
<url>http://github.com/ddavison/getting-started-with-selenium-framework</url>
<version>1.0</version>
<version>1.1</version>
<description>A quick and easy start-up browser automation framework using Selenium</description>

<parent>
Expand Down Expand Up @@ -151,5 +151,17 @@
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>

<!-- logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
</project>
131 changes: 91 additions & 40 deletions src/main/java/io/ddavison/selenium/AutomationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,10 @@

package io.ddavison.selenium;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.After;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
Expand All @@ -35,7 +23,18 @@
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.support.ui.Select;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.*;

/**
* the base test that includes all Selenium 2 functionality that you will need
Expand All @@ -45,6 +44,8 @@
*/
public class AutomationTest {

public static final Logger log = LogManager.getLogger(AutomationTest.class);

public WebDriver driver;

// max seconds before failing a script.
Expand All @@ -54,7 +55,6 @@ public class AutomationTest {

public Actions actions;

public Map<String, By> props = new HashMap<String, By>();
private Map<String, String> vars = new HashMap<String, String>();

/**
Expand All @@ -77,17 +77,26 @@ public AutomationTest() {
switch (configuration.browser()) {
case CHROME:
capabilities = DesiredCapabilities.chrome();
if (isLocal) driver = new ChromeDriver(capabilities);
if (isLocal) try {
driver = new ChromeDriver(capabilities);
} catch (Exception x) {
logFatal("chromedriver not found. See https://github.com/ddavison/getting-started-with-selenium-framework/wiki/WebDriver-Executables for more information.");
System.exit(1);
}
break;
case FIREFOX:
capabilities = DesiredCapabilities.firefox();
if (isLocal) driver = new FirefoxDriver(capabilities);
break;
case INTERNET_EXPLORER:
logFatal("iedriver not found. See https://github.com/ddavison/getting-started-with-selenium-framework/wiki/WebDriver-Executables for more information.");
System.exit(1);
capabilities = DesiredCapabilities.internetExplorer();
if (isLocal) driver = new InternetExplorerDriver(capabilities);
break;
case SAFARI:
logFatal("safaridriver not found. See https://github.com/ddavison/getting-started-with-selenium-framework/wiki/WebDriver-Executables for more information.");
System.exit(1);
capabilities = DesiredCapabilities.safari();
if (isLocal) driver = new SafariDriver(capabilities);
break;
Expand All @@ -108,20 +117,6 @@ public AutomationTest() {
System.err.println("Couldn't connect to hub: " + configuration.hub());
return;
}

// load the properties.
Properties properties = new Properties();
try {
properties.load(getClass().getResourceAsStream(getClass().getSimpleName().concat(".properties")));

for (String key : properties.stringPropertyNames())
// css is arbitrary here.. USE IT! It rocks!
props.put(key, By.cssSelector(properties.getProperty(key)));
}
catch (Exception x) {
// x.printStackTrace();
System.err.println("WARN: No css properties file for this test was found. You can create one under /src/tests/resources/<package(s)>/" + getClass().getSimpleName().concat(".properties"));
}

actions = new Actions(driver);
driver.navigate().to(baseUrl);
Expand All @@ -130,13 +125,20 @@ public AutomationTest() {
static {
// Set the webdriver env vars.
if (System.getProperty("os.name").toLowerCase().contains("mac")) {
System.setProperty("webdriver.chrome.driver", findFile("chromedriver"));
// System.setProperty("webdriver.ie.driver", null); // mac doesn't have ie
// System.setProperty("webdriver.firefox.driver", ""); // if using firefox, uncomment this, and replace "" with findFile("firefoxDriver")
} else {
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.mac"));
System.setProperty("webdriver.firefox.driver", "");
} else if (System.getProperty("os.name").toLowerCase().contains("nix") ||
System.getProperty("os.name").toLowerCase().contains("nux") ||
System.getProperty("os.name").toLowerCase().contains("aix")
) {
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.linux"));
System.setProperty("webdriver.firefox.driver", "");
} else if (System.getProperty("os.name").toLowerCase().contains("win")) {
System.setProperty("webdriver.chrome.driver", findFile("chromedriver.exe"));
// System.setProperty("webdriver.ie.driver", findFile("iedriver.exe")); // if using IE, uncomment this.
// System.setProperty("webdriver.firefox.driver", ""); // if using firefox, uncomment this, and replace "" with findFile("firefoxDriver.exe")
System.setProperty("webdriver.ie.driver", findFile("iedriver.exe"));
System.setProperty("webdriver.firefox.driver", "");
} else {

}
}

Expand All @@ -146,7 +148,7 @@ static public String findFile(String filename) {
if (new File(path + filename).exists())
return path + filename;
}
return null;
return "";
}

@After
Expand Down Expand Up @@ -887,12 +889,61 @@ public String get(String key, String defaultValue) {
}

/**
* Log something to 'out'
* Log something as information
* @param object What to log.
* @return <code>AutomationTest</code> (for fluency)
*/
public AutomationTest log(Object object) {
System.out.println(object);
return logInfo(object);
}

/**
* Log something as information
* @param object What to log
* @return <code>AutomationTest</code> (for fluency)
*/
public AutomationTest logInfo(Object object) {
log.info(object);
return this;
}

/**
* Log something as a warning
* @param object What to log
* @return <code>AutomationTest</code> (for fluency)
*/
public AutomationTest logWarn(Object object) {
log.warn(object);
return this;
}

/**
* Log something as an error
* @param object What to log
* @return <code>AutomationTest</code> (for fluency)
*/
public AutomationTest logError(Object object) {
log.error(object);
return this;
}

/**
* Log something as debug
* @param object What to log
* @return <code>AutomationTest</code> (for fluency)
*/
public AutomationTest logDebug(Object object) {
log.debug(object);
return this;
}

/**
* Log something as fatal
* @param object What to log
* @return <code>AutomationTest</code> (for fluency)
*/
public AutomationTest logFatal(Object object) {
log.fatal(object);
return this;
}
}
3 changes: 2 additions & 1 deletion src/test/java/io/ddavison/selenium/FrameworkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
public class FrameworkTest extends AutomationTest {
@Test
public void testClick() throws Exception {
click("#click")
log("Test started")
.click("#click")
.validatePresent("#click.success"); // adds the .success class after click
}

Expand Down
22 changes: 22 additions & 0 deletions src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2014 Daniel Davison (http://github.com/ddavison)
~
~ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-->

<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

0 comments on commit 6c2b850

Please sign in to comment.