diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..412eeda --- /dev/null +++ b/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore index 63323bd..b7c249c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /target /.project /.settings -/.classpath \ No newline at end of file +/.classpath +/.idea +/*.iml diff --git a/NOTICE b/NOTICE index 2aa9092..d26506d 100644 --- a/NOTICE +++ b/NOTICE @@ -1,5 +1,5 @@ -Selenium2 Java QuickStart Archetype -Copyright 2011 sebarmeli +WebDriver Java QuickStart Archetype +Copyright 2013-2014 Alexei Barantsev barancev@gmail.com This product includes software developed by Sebastiano Armeli-Battana diff --git a/README.md b/README.md index 1409bd3..e81c74a 100644 --- a/README.md +++ b/README.md @@ -1,79 +1 @@ -Introduction -============ - -This archetype generates a small Maven project with Selenium 2 and TestNG embedded to make it easy to get started testing with Selenium Web Driver. - -This project provides just a starting point to get up to speed with Selenium 2 infrastructure. - -To install the archetype in your local repo: - - git clone git://github.com/sebarmeli/Selenium2-Java-QuickStart-Archetype.git - cd Selenium2-Java-Quickstart-Archetype - mvn install - -Now, you can use the archetype in a new project typing: - - mvn archetype:generate -DarchetypeGroupId=com.sebarmeli -DarchetypeArtifactId=selenium2-java-quickstart-archetype -DarchetypeVersion=0.2 -DgroupId= -DartifactId= - -where *mygroupId* : group id of the project you are creating; *myartifactId* : artifact id of the project you are creating - -It uses Java bindings for Selenium version 2.12.0, OperaDriver version 0.7.3 and TestNG version 6.3. - - -Project Structure ------------------------------------ - -The project follows the standard Maven structure, so all the tests go in the *src/test/java* folder. Tests should inherit from the **TestBase** class. In this class a factory method -from **WebDriverFactory** class is in charge of generating the instance of the WebDriver interface you need. Different parameters are passed into the factory: - -* base URL : base URL of the AUT (application under test) -* Grid 2 hub URL : URL of the hub (if using Grid 2) -* browser fatures: a) name b) version c) platform -* username / password : in case of BASIC authenticated site - -Those parameters are retrieved from the *src/main/resources/application.properties* file. You can also populate the properties file from command line (through -D:@ondemand.saucelabs.com:80/wd/hub* - -Further Notes -------- -The project is just a starting point, feel free to modify it according to your needs. - -Credits -------- -The selenium2-java-quickstart-archetype project is an open source project licensed under the Apache License 2.0. \ No newline at end of file +Deprecated in favor of https://github.com/barancev/webdriver-testng-archetype and https://github.com/barancev/webdriver-junit-archetype diff --git a/changelog.txt b/changelog.txt deleted file mode 100644 index 9caea3c..0000000 --- a/changelog.txt +++ /dev/null @@ -1,8 +0,0 @@ -Version 0.2 -------------- - * Upgrade to Archetype Plugin version 2 - * Mobile testing supported - -Version 0.1 -------------- - * Initial release \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2b189e5..5e832aa 100644 --- a/pom.xml +++ b/pom.xml @@ -1,44 +1,72 @@ - - 4.0.0 - com.sebarmeli - selenium2-java-quickstart-archetype - 0.2 - Selenium2 Java QuickStart Archetype - maven-archetype - Generates a QuickStart project with Selenium 2 and TestNG - - 2.1 - - - - The Apache Software License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - - - - - - org.apache.maven.archetype - archetype-packaging - ${archetype.version} - - - - - - maven-archetype-plugin - ${archetype.version} - true - - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + ru.stqa.selenium + webdriver-java-quickstart-archetype + 0.8 + maven-archetype + + WebDriver Java QuickStart Archetype + Generates a QuickStart project with WebDriver and TestNG + https://github.com/barancev/webdriver-java-quickstart-archetype + + + UTF-8 + 2.2 + + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + scm:git:https://github.com/barancev/webdriver-java-quickstart-archetype + scm:git:https://github.com/barancev/webdriver-java-quickstart-archetype + https://github.com/barancev/webdriver-java-quickstart-archetype + + + + + barancev + Alexei Barantsev + + Owner + + + + + + + + org.apache.maven.archetype + archetype-packaging + ${archetype.version} + + + + + + maven-archetype-plugin + ${archetype.version} + true + + + + + + + + sonatype-nexus-snapshots + http://oss.sonatype.org/content/repositories/snapshots + + + sonatype-nexus-staging + http://oss.sonatype.org/service/local/staging/deploy/maven2 + + + diff --git a/src/main/resources/META-INF/maven/archetype-metadata.xml b/src/main/resources/META-INF/maven/archetype-metadata.xml index 47faca4..aef48b1 100644 --- a/src/main/resources/META-INF/maven/archetype-metadata.xml +++ b/src/main/resources/META-INF/maven/archetype-metadata.xml @@ -22,9 +22,6 @@ src/main/resources - - src/main/resources/drivers/chrome/ - src/main/resources/grid2 diff --git a/src/main/resources/archetype-resources/pom.xml b/src/main/resources/archetype-resources/pom.xml index c167ee5..790dc41 100644 --- a/src/main/resources/archetype-resources/pom.xml +++ b/src/main/resources/archetype-resources/pom.xml @@ -6,119 +6,120 @@ ${version} jar - - - org.testng - testng - 6.3 - - - org.seleniumhq.selenium - selenium-java - 2.12.0 - - - com.opera - operadriver - 0.7.3 - - - - - - src/main/resources - true - - - - - org.apache.maven.plugins - maven-resources-plugin - 2.3 - - UTF-8 - - - - maven-surefire-plugin - - - src/test/resources/testng.xml - - - - - maven-compiler-plugin - 2.3.2 - - 1.6 - 1.6 - - - - - - - - local - - local - - - - - - - - - - true - - - - dev - - dev - - - - - - - - - - - test - - test - - - - - - - - - - - prod - - prod - - - - - - - - - - - force-local-driver - - - - - + + + org.testng + testng + 6.9.4 + + + junit + junit + 4.12 + + + ru.stqa.selenium + webdriver-factory + 1.1.46 + + + + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.6 + + UTF-8 + + + + maven-surefire-plugin + 2.16 + + + src/test/resources/testng.xml + + + + + maven-compiler-plugin + 3.1 + + 1.6 + 1.6 + + + + + + + + local + + local + + + + http://localhost/ + firefox + + + + + true + + + + dev + + dev + + + + http://devhost/ + firefox + + + + + + test + + test + + + + http://testhost/ + firefox + + + + + + prod + + prod + + + + http://prodhost/ + firefox + + + + + + force-local-driver + + + + + diff --git a/src/main/resources/archetype-resources/src/main/java/pages/HomePage.java b/src/main/resources/archetype-resources/src/main/java/pages/HomePage.java index 2866f9a..88957d7 100644 --- a/src/main/resources/archetype-resources/src/main/java/pages/HomePage.java +++ b/src/main/resources/archetype-resources/src/main/java/pages/HomePage.java @@ -9,10 +9,8 @@ import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.How; -/* +/** * Sample page - * - * @author Sebastiano Armeli-Battana */ public class HomePage extends Page { diff --git a/src/main/resources/archetype-resources/src/main/java/pages/Page.java b/src/main/resources/archetype-resources/src/main/java/pages/Page.java index 0821b25..28d7529 100644 --- a/src/main/resources/archetype-resources/src/main/java/pages/Page.java +++ b/src/main/resources/archetype-resources/src/main/java/pages/Page.java @@ -1,34 +1,32 @@ -#set( $symbol_pound = '#' ) -#set( $symbol_dollar = '$' ) -#set( $symbol_escape = '\' ) -package ${groupId}.pages; - -import org.openqa.selenium.WebDriver; - -/* - * Abstract class representation of a Page in the UI. Page object pattern - * - * @author Sebastiano Armeli-Battana - */ -public abstract class Page { - - protected WebDriver webDriver; - - /* - * Constructor injecting the WebDriver interface - * - * @param webDriver - */ - public Page(WebDriver webDriver) { - this.webDriver = webDriver; - } - - public WebDriver getWebDriver() { - return webDriver; - } - - public String getTitle() { - return webDriver.getTitle(); - } - -} +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) +package ${groupId}.pages; + +import org.openqa.selenium.WebDriver; + +/** + * Abstract class representation of a Page in the UI. Page object pattern + */ +public abstract class Page { + + protected WebDriver webDriver; + + /* + * Constructor injecting the WebDriver interface + * + * @param webDriver + */ + public Page(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public WebDriver getWebDriver() { + return webDriver; + } + + public String getTitle() { + return webDriver.getTitle(); + } + +} diff --git a/src/main/resources/archetype-resources/src/main/java/util/Browser.java b/src/main/resources/archetype-resources/src/main/java/util/Browser.java deleted file mode 100644 index 8771941..0000000 --- a/src/main/resources/archetype-resources/src/main/java/util/Browser.java +++ /dev/null @@ -1,41 +0,0 @@ -#set( $symbol_pound = '#' ) -#set( $symbol_dollar = '$' ) -#set( $symbol_escape = '\' ) -package ${groupId}.util; - -/* - * Bean representing a browser. It contains name, version and platform fields. - * - * @author Sebastiano Armeli-Battana - */ -public class Browser { - - private String name; - private String version; - private String platform; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - - public String getPlatform() { - return platform; - } - - public void setPlatform(String platform) { - this.platform = platform; - } - -} \ No newline at end of file diff --git a/src/main/resources/archetype-resources/src/main/java/util/PropertyLoader.java b/src/main/resources/archetype-resources/src/main/java/util/PropertyLoader.java index 52256f7..a06b6bb 100644 --- a/src/main/resources/archetype-resources/src/main/java/util/PropertyLoader.java +++ b/src/main/resources/archetype-resources/src/main/java/util/PropertyLoader.java @@ -6,10 +6,8 @@ import java.io.IOException; import java.util.Properties; -/* +/** * Class that extracts properties from the prop file. - * - * @author Sebastiano Armeli-Battana */ public class PropertyLoader { @@ -23,7 +21,7 @@ public static String loadProperty(String name) { e.printStackTrace(); } - String value = ""; + String value = null; if (name != null) { value = props.getProperty(name); diff --git a/src/main/resources/archetype-resources/src/main/java/webdriver/AuthenticatedHtmlUnitDriver.java b/src/main/resources/archetype-resources/src/main/java/webdriver/AuthenticatedHtmlUnitDriver.java deleted file mode 100644 index 4958005..0000000 --- a/src/main/resources/archetype-resources/src/main/java/webdriver/AuthenticatedHtmlUnitDriver.java +++ /dev/null @@ -1,39 +0,0 @@ -#set( $symbol_pound = '#' ) -#set( $symbol_dollar = '$' ) -#set( $symbol_escape = '\' ) -package ${groupId}.webdriver; - -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.htmlunit.HtmlUnitDriver; - -import com.gargoylesoftware.htmlunit.BrowserVersion; -import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider; -import com.gargoylesoftware.htmlunit.WebClient; - -/* - * Class extending HtmlUnitDriver to support BASIC authentication - * - * @author Sebastiano Armeli-Battana - */ -public class AuthenticatedHtmlUnitDriver extends HtmlUnitDriver { - private static String USERNAME; - private static String PASSWORD; - - public AuthenticatedHtmlUnitDriver() { - } - - public static WebDriver create(String username, String password) { - USERNAME = username; - PASSWORD = password; - return new AuthenticatedHtmlUnitDriver(); - } - - @Override - protected WebClient newWebClient(BrowserVersion browserVersion) { - WebClient client = super.newWebClient(browserVersion); - DefaultCredentialsProvider provider = new DefaultCredentialsProvider(); - provider.addCredentials(USERNAME, PASSWORD); - client.setCredentialsProvider(provider); - return client; - } -} diff --git a/src/main/resources/archetype-resources/src/main/java/webdriver/WebDriverFactory.java b/src/main/resources/archetype-resources/src/main/java/webdriver/WebDriverFactory.java deleted file mode 100644 index 17f4935..0000000 --- a/src/main/resources/archetype-resources/src/main/java/webdriver/WebDriverFactory.java +++ /dev/null @@ -1,242 +0,0 @@ -#set( $symbol_pound = '#' ) -#set( $symbol_dollar = '$' ) -#set( $symbol_escape = '\' ) -package ${groupId}.webdriver; - -import java.net.MalformedURLException; -import java.net.URL; - -import org.openqa.selenium.Platform; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.android.AndroidDriver; -import org.openqa.selenium.chrome.ChromeDriver; -import org.openqa.selenium.firefox.FirefoxDriver; -import org.openqa.selenium.firefox.FirefoxProfile; -import org.openqa.selenium.htmlunit.HtmlUnitDriver; -import org.openqa.selenium.ie.InternetExplorerDriver; -import org.openqa.selenium.iphone.IPhoneDriver; -import org.openqa.selenium.remote.CapabilityType; -import org.openqa.selenium.remote.DesiredCapabilities; -import org.openqa.selenium.remote.RemoteWebDriver; - -import com.opera.core.systems.OperaDriver; -import ${groupId}.util.Browser; -import ${groupId}.webdriver.AuthenticatedHtmlUnitDriver; - -import ${groupId}.util.Browser; - -/* - * Factory to instantiate a WebDriver object. It returns an instance of the driver (local invocation) or an instance of RemoteWebDriver - * - * @author Sebastiano Armeli-Battana - */ -public class WebDriverFactory { - - /* Browsers constants */ - public static final String CHROME = "chrome"; - public static final String FIREFOX = "firefox"; - public static final String OPERA = "opera"; - public static final String INTERNET_EXPLORER = "ie"; - public static final String HTML_UNIT = "htmlunit"; - public static final String IPHONE = "iphone"; - - /* Platform constants */ - public static final String WINDOWS = "windows"; - public static final String ANDROID = "android"; - public static final String XP = "xp"; - public static final String VISTA = "vista"; - public static final String MAC = "mac"; - public static final String LINUX = "linux"; - - /* - * Factory method to return a RemoteWebDriver instance given the url of the - * Grid hub and a Browser instance. - * - * @param gridHubUrl : grid hub URI - * - * @param browser : Browser object containing info around the browser to hit - * - * @param username : username for BASIC authentication on the page to test - * - * @param password : password for BASIC authentication on the page to test - * - * @return RemoteWebDriver - */ - public static WebDriver getInstance(String gridHubUrl, Browser browser, - String username, String password) { - - WebDriver webDriver = null; - - DesiredCapabilities capability = new DesiredCapabilities(); - String browserName = browser.getName(); - capability.setJavascriptEnabled(true); - - // In case there is no Hub - if (gridHubUrl == null || gridHubUrl.length() == 0) { - return getInstance(browserName, username, password); - } - - if (CHROME.equals(browserName)) { - capability = DesiredCapabilities.chrome(); - } else if (FIREFOX.equals(browserName)) { - capability = DesiredCapabilities.firefox(); - - FirefoxProfile ffProfile = new FirefoxProfile(); - - // Authenication Hack for Firefox - if (username != null && password != null) { - ffProfile.setPreference("network.http.phishy-userpass-length", - 255); - capability.setCapability(FirefoxDriver.PROFILE, ffProfile); - } - - capability.setCapability(CapabilityType.TAKES_SCREENSHOT, true); - } else if (INTERNET_EXPLORER.equals(browserName)) { - - capability = DesiredCapabilities.internetExplorer(); - capability - .setCapability( - InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, - true); - } else if (OPERA.equals(browserName)) { - capability = DesiredCapabilities.opera(); - } else if (ANDROID.equals(browserName)) { - capability = DesiredCapabilities.android(); - } else if (IPHONE.equals(browserName)) { - capability = DesiredCapabilities.iphone(); - } else { - - capability = DesiredCapabilities.htmlUnit(); - // HTMLunit Check - if (username != null && password != null) { - webDriver = (HtmlUnitDriver) AuthenticatedHtmlUnitDriver - .create(username, password); - } else { - webDriver = new HtmlUnitDriver(true); - } - - return webDriver; - } - - capability = setVersionAndPlatform(capability, browser.getVersion(), - browser.getPlatform()); - - // Create Remote WebDriver - try { - webDriver = new RemoteWebDriver(new URL(gridHubUrl), capability); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - - return webDriver; - } - - /* - * Factory method to return a WebDriver instance given the browser to hit - * - * @param browser : String representing the local browser to hit - * - * @param username : username for BASIC authentication on the page to test - * - * @param password : password for BASIC authentication on the page to test - * - * @return WebDriver instance - */ - public static WebDriver getInstance(String browser, String username, - String password) { - - WebDriver webDriver = null; - - if (CHROME.equals(browser)) { - setChromeDriver(); - - webDriver = new ChromeDriver(); - } else if (FIREFOX.equals(browser)) { - - FirefoxProfile ffProfile = new FirefoxProfile(); - - // Authenication Hack for Firefox - if (username != null && password != null) { - ffProfile.setPreference("network.http.phishy-userpass-length", - 255); - } - - webDriver = new FirefoxDriver(ffProfile); - - } else if (INTERNET_EXPLORER.equals(browser)) { - webDriver = new InternetExplorerDriver(); - - } else if (OPERA.equals(browser)) { - webDriver = new OperaDriver(); - - } else if (IPHONE.equals(browser)) { - try { - webDriver = new IPhoneDriver(); - } catch (Exception e) { - e.printStackTrace(); - } - - } else if (ANDROID.equals(browser)) { - webDriver = new AndroidDriver(); - - } else { - - // HTMLunit Check - if (username != null && password != null) { - webDriver = (HtmlUnitDriver) AuthenticatedHtmlUnitDriver - .create(username, password); - } else { - webDriver = new HtmlUnitDriver(true); - } - } - - return webDriver; - } - - /* - * Helper method to set version and platform for a specific browser - * - * @param capability : DesiredCapabilities object coming from the selected - * browser - * - * @param version : browser version - * - * @param platform : browser platform - * - * @return DesiredCapabilities - */ - private static DesiredCapabilities setVersionAndPlatform( - DesiredCapabilities capability, String version, String platform) { - if (MAC.equalsIgnoreCase(platform)) { - capability.setPlatform(Platform.MAC); - } else if (LINUX.equalsIgnoreCase(platform)) { - capability.setPlatform(Platform.LINUX); - } else if (XP.equalsIgnoreCase(platform)) { - capability.setPlatform(Platform.XP); - } else if (VISTA.equalsIgnoreCase(platform)) { - capability.setPlatform(Platform.VISTA); - } else if (WINDOWS.equalsIgnoreCase(platform)) { - capability.setPlatform(Platform.WINDOWS); - } else if (ANDROID.equalsIgnoreCase(platform)) { - capability.setPlatform(Platform.ANDROID); - } else { - capability.setPlatform(Platform.ANY); - } - - if (version != null) { - capability.setVersion(version); - } - return capability; - } - - /* - * Helper method to set ChromeDriver location into the right ststem property - */ - private static void setChromeDriver() { - String os = System.getProperty("os.name").toLowerCase().substring(0, 3); - String chromeBinary = "src/main/resources/drivers/chrome/chromedriver" - + (os.equals("win") ? ".exe" : ""); - System.setProperty("webdriver.chrome.driver", chromeBinary); - } - -} \ No newline at end of file diff --git a/src/main/resources/archetype-resources/src/main/resources/drivers/chrome/README.txt b/src/main/resources/archetype-resources/src/main/resources/drivers/chrome/README.txt deleted file mode 100644 index 1e240e9..0000000 --- a/src/main/resources/archetype-resources/src/main/resources/drivers/chrome/README.txt +++ /dev/null @@ -1,4 +0,0 @@ -#set( $symbol_pound = '#' ) -#set( $symbol_dollar = '$' ) -#set( $symbol_escape = '\' ) -Place in this folder the chrome driver for your OS (chromedriver.exe for Win or chromedriver for Mac or Linux) if you need it. \ No newline at end of file diff --git a/src/main/resources/archetype-resources/src/test/java/pages/HomePageTest.java b/src/main/resources/archetype-resources/src/test/java/HomePageTest.java similarity index 80% rename from src/main/resources/archetype-resources/src/test/java/pages/HomePageTest.java rename to src/main/resources/archetype-resources/src/test/java/HomePageTest.java index 2567afc..ce4144b 100644 --- a/src/main/resources/archetype-resources/src/test/java/pages/HomePageTest.java +++ b/src/main/resources/archetype-resources/src/test/java/HomePageTest.java @@ -1,34 +1,36 @@ -#set( $symbol_pound = '#' ) -#set( $symbol_dollar = '$' ) -#set( $symbol_escape = '\' ) -package ${groupId}.pages; - -import org.openqa.selenium.support.PageFactory; -import org.testng.Assert; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Parameters; -import org.testng.annotations.Test; - -public class HomePageTest extends TestBase { - - HomePage homepage; - - @Parameters({ "path" }) - @BeforeClass - public void testInit(String path) { - - // Load the page in the browser - webDriver.get(websiteUrl + path); - homepage = PageFactory.initElements(webDriver, HomePage.class); - } - - @Test - public void testH1Existing() throws InterruptedException { - Assert.assertTrue(homepage.getH1() != null); - } - - @Test - public void test2() throws InterruptedException { - Assert.assertTrue(true); - } -} +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) +package ${groupId}; + +import org.openqa.selenium.support.PageFactory; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; + +import ${groupId}.pages.HomePage; + +public class HomePageTest extends TestBase { + + HomePage homepage; + + @Parameters({ "path" }) + @BeforeClass + public void testInit(String path) { + + // Load the page in the browser + driver.get(baseUrl + path); + homepage = PageFactory.initElements(driver, HomePage.class); + } + + @Test + public void testH1Existing() throws InterruptedException { + Assert.assertTrue(homepage.getH1() != null); + } + + @Test + public void test2() throws InterruptedException { + Assert.assertTrue(true); + } +} diff --git a/src/main/resources/archetype-resources/src/test/java/TestBase.java b/src/main/resources/archetype-resources/src/test/java/TestBase.java new file mode 100644 index 0000000..2e6d434 --- /dev/null +++ b/src/main/resources/archetype-resources/src/test/java/TestBase.java @@ -0,0 +1,56 @@ +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) +package ${groupId}; + +import java.util.concurrent.TimeUnit; + +import org.openqa.selenium.Platform; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.remote.DesiredCapabilities; + +import org.testng.annotations.AfterSuite; +import org.testng.annotations.BeforeClass; + +import ru.stqa.selenium.factory.WebDriverFactory; + +import ${groupId}.util.PropertyLoader; + +/** + * Base class for all the TestNG-based test classes + */ +public class TestBase { + protected WebDriver driver; + + protected String gridHubUrl; + + protected String baseUrl; + + @BeforeClass + public void init() { + baseUrl = PropertyLoader.loadProperty("site.url"); + gridHubUrl = PropertyLoader.loadProperty("grid2.hub"); + + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setBrowserName(PropertyLoader.loadProperty("browser.name")); + capabilities.setVersion(PropertyLoader.loadProperty("browser.version")); + String platform = PropertyLoader.loadProperty("browser.platform"); + if (!(null == platform || "".equals(platform))) { + capabilities.setPlatform(Platform.valueOf(PropertyLoader.loadProperty("browser.platform"))); + } + + if (!(null == gridHubUrl || "".equals(gridHubUrl))) { + driver = WebDriverFactory.getDriver(gridHubUrl, capabilities); + } else { + driver = WebDriverFactory.getDriver(capabilities); + } + driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); + } + + @AfterSuite(alwaysRun = true) + public void tearDown() { + if (driver != null) { + WebDriverFactory.dismissDriver(driver); + } + } +} diff --git a/src/main/resources/archetype-resources/src/test/java/pages/TestBase.java b/src/main/resources/archetype-resources/src/test/java/pages/TestBase.java deleted file mode 100644 index 167a43a..0000000 --- a/src/main/resources/archetype-resources/src/test/java/pages/TestBase.java +++ /dev/null @@ -1,88 +0,0 @@ -#set( $symbol_pound = '#' ) -#set( $symbol_dollar = '$' ) -#set( $symbol_escape = '\' ) -package ${groupId}.pages; - -import java.io.File; -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import org.apache.commons.io.FileUtils; -import org.openqa.selenium.OutputType; -import org.openqa.selenium.TakesScreenshot; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.remote.Augmenter; -import org.openqa.selenium.remote.ScreenshotException; -import org.testng.ITestResult; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.AfterSuite; -import org.testng.annotations.BeforeClass; - -import ${groupId}.util.PropertyLoader; -import ${groupId}.util.Browser; -import ${groupId}.webdriver.WebDriverFactory; - -/* - * Base class for all the test classes - * - * @author Sebastiano Armeli-Battana - */ - -public class TestBase { - private static final String SCREENSHOT_FOLDER = "target/screenshots/"; - private static final String SCREENSHOT_FORMAT = ".png"; - - protected WebDriver webDriver; - - protected String gridHubUrl; - - protected String websiteUrl; - - protected Browser browser; - - @BeforeClass - public void init() { - websiteUrl = PropertyLoader.loadProperty("site.url"); - gridHubUrl = PropertyLoader.loadProperty("grid2.hub"); - - browser = new Browser(); - browser.setName(PropertyLoader.loadProperty("browser.name")); - browser.setVersion(PropertyLoader.loadProperty("browser.version")); - browser.setPlatform(PropertyLoader.loadProperty("browser.platform")); - - String username = PropertyLoader.loadProperty("user.username"); - String password = PropertyLoader.loadProperty("user.password"); - - webDriver = WebDriverFactory.getInstance(gridHubUrl, browser, username, - password); - webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); - } - - @AfterSuite(alwaysRun = true) - public void tearDown() { - if (webDriver != null) { - webDriver.quit(); - } - } - -// @AfterMethod -// public void setScreenshot(ITestResult result) { -// if (!result.isSuccess()) { -// try { -// WebDriver returned = new Augmenter().augment(webDriver); -// if (returned != null) { -// File f = ((TakesScreenshot) returned) -// .getScreenshotAs(OutputType.FILE); -// try { -// FileUtils.copyFile(f, new File(SCREENSHOT_FOLDER -// + result.getName() + SCREENSHOT_FORMAT)); -// } catch (IOException e) { -// e.printStackTrace(); -// } -// } -// } catch (ScreenshotException se) { -// se.printStackTrace(); -// } -// } -// } -} diff --git a/src/main/resources/archetype-resources/src/test/resources/testng.xml b/src/main/resources/archetype-resources/src/test/resources/testng.xml index a0e5b10..c5a8444 100644 --- a/src/main/resources/archetype-resources/src/test/resources/testng.xml +++ b/src/main/resources/archetype-resources/src/test/resources/testng.xml @@ -1,13 +1,13 @@ -#set( $symbol_pound = '#' ) -#set( $symbol_dollar = '$' ) -#set( $symbol_escape = '\' ) - - - - - - - - - +#set( $symbol_pound = '#' ) +#set( $symbol_dollar = '$' ) +#set( $symbol_escape = '\' ) + + + + + + + + + \ No newline at end of file