├── .gitignore ├── .travis.yml ├── .vscode └── settings.json ├── License.txt ├── README.md ├── env ├── default │ ├── default.properties │ ├── java.properties │ └── user.properties ├── edge │ └── browser.properties └── firefox │ └── browser.properties ├── manifest.json ├── pom.xml ├── specs ├── CustomerLogout.spec ├── CustomerSignup.spec ├── PlaceOrder.spec └── concepts │ ├── Authentication.cpt │ ├── CustomerSignup.cpt │ ├── GoToStoreWebsite.cpt │ └── PlaceOrder.cpt └── src └── test └── java ├── .DS_Store ├── CustomerSignup.java ├── LogIn.java ├── LogOut.java ├── PlaceOrder.java └── utils ├── AppLauncher.java ├── FlashMessages.java └── driver ├── Driver.java └── DriverFactory.java /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | .gauge/ 3 | reports/ 4 | out/ 5 | logs/ 6 | gauge_bin/ 7 | libs/ 8 | .idea 9 | .gradle/ 10 | *.iml 11 | build/ 12 | *.log 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | os: 2 | - osx 3 | 4 | language: java 5 | 6 | before_install: 7 | - brew update 8 | - npm install --global surge 9 | 10 | install: 11 | - brew cask install google-chrome 12 | - brew cask install chromedriver 13 | - brew install gauge 14 | - gauge install java 15 | - gauge install html-report 16 | 17 | cache: 18 | directories: 19 | - $HOME/.m2 20 | 21 | script: 22 | - curl -L https://bintray.com/artifact/download/gauge/activeadmin-demo/activeadmin-demo.war -O 23 | - nohup java -jar activeadmin-demo.war >/dev/null 2>&1 & 24 | - sleep 30 25 | - mvn clean 26 | - mvn test 27 | 28 | deploy: 29 | provider: surge 30 | project: ./reports/html-report/ 31 | domain: gauge-java-maven-selenium.surge.sh 32 | skip_cleanup: true 33 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "*.spec": "gauge", 4 | "*.cpt": "gauge" 5 | }, 6 | "java.configuration.updateBuildConfiguration": "automatic" 7 | } -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 ThoughtWorks 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gauge example in Java 2 | 3 | [![Build Status](https://travis-ci.org/getgauge-examples/java-maven-selenium.svg?branch=master)](https://travis-ci.org/getgauge-examples/java-maven-selenium) 4 | 5 | This is an example project for doing web automation testing with [Gauge](http://getgauge.io). This project tests some of the functionalities of the [active admin demo](https://github.com/getgauge/activeadmin-demo) app. This app is hosted as a Java WAR (with embedded Jetty). 6 | 7 | ## Running this example 8 | The tests are run on Chrome by default. 9 | 10 | ### Prerequisites 11 | 12 | This example requires the following softwares to run. 13 | * [Java 1.7](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) or above 14 | * Note that Gauge works with Java 1.6 and above. But this particular example uses Java 1.7 15 | * [Gauge](https://docs.gauge.org/getting_started/installing-gauge.html) 16 | * Gauge Java plugin 17 | * can be installed using `gauge install java` 18 | * Chrome 19 | 20 | ### Setting up the System Under Test (SUT) 21 | 22 | * Download [activeadmin-demo.war](https://github.com/getgauge-examples/activeadmin-demo/releases/tag/untagged-f0befd5494efa4baabd2) 23 | * Bring up the SUT by executing the below command 24 | ``` 25 | java -jar activeadmin-demo.war 26 | ``` 27 | * The SUT should now be available at [http://localhost:8080/](http://localhost:8080) 28 | 29 | ### Setting up Maven 30 | 31 | * [Maven installation instructions](http://maven.apache.org/install.html) 32 | 33 | ## Run specs 34 | 35 | If you already have Maven installed, you can execute specs as `mvn test` 36 | This runs Gauge specs with [Maven](http://maven.apache.org/index.html). 37 | 38 | This uses Chrome as default browser for specs execution. Make sure Chrome is installed in your machine and [chromedriver](https://sites.google.com/a/chromium.org/chromedriver/) is in PATH. 39 | 40 | If you want to use Firefox/IE as browser, pass the corresponding argument to set browser environment as follows: 41 | 42 | ``` 43 | mvn gauge:execute -DspecsDir=specs -Denv="firefox" 44 | or 45 | mvn gauge:execute -DspecsDir=specs -Denv="ie" 46 | ``` 47 | 48 | Note: 49 | * Gauge can also be used with other [build tools](https://docs.gauge.org/latest/configuration.html#build-tools). 50 | * You can use Gauge even without a build script! 51 | 52 | ## Topics covered in the example 53 | 54 | * Use [Webdriver](http://docs.seleniumhq.org/projects/webdriver/) as base of implementation 55 | * [Concepts](https://docs.gauge.org/latest/writing-specifications.html#concept) 56 | * [Specification](https://docs.gauge.org/latest/writing-specifications.html#specifications-spec), [Scenario](https://docs.gauge.org/latest/writing-specifications.html#longstart-scenarios) & [Step](https://docs.gauge.org/latest/writing-specifications.html#longstart-steps) usage 57 | * [Table driven execution](https://docs.gauge.org/latest/execution.html#data-driven-execution) 58 | * [External datasource (special param)](https://docs.gauge.org/latest/execution.html#external-csv-for-data-table) 59 | * Using Gauge with [Selenium Webdriver](http://docs.seleniumhq.org/projects/webdriver/) 60 | * Running Gauge specs with [Maven](https://maven.apache.org/) 61 | 62 | # Copyright 63 | Copyright 2016, ThoughtWorks Inc. 64 | -------------------------------------------------------------------------------- /env/default/default.properties: -------------------------------------------------------------------------------- 1 | # default.properties 2 | # properties set here will be available to the test execution as environment variables 3 | 4 | # sample_key = sample_value 5 | 6 | #The path to the gauge reports directory. Should be either relative to the project directory or an absolute path 7 | gauge_reports_dir = reports 8 | 9 | #Set as false if gauge reports should not be overwritten on each execution. A new time-stamped directory will be created on each execution. 10 | overwrite_reports = true 11 | 12 | # Set to false to disable screenshots on failure in reports. 13 | screenshot_on_failure = true 14 | 15 | # The path to the gauge logs directory. Should be either relative to the project directory or an absolute path 16 | logs_directory = logs 17 | 18 | # The path the gauge specifications directory. Takes a comma separated list of specification files/directories. 19 | gauge_specs_dir = specs 20 | 21 | # The default delimiter used read csv files. 22 | csv_delimiter = , 23 | 24 | # The port for flash plugin 25 | flash_server_port = 8000 26 | -------------------------------------------------------------------------------- /env/default/java.properties: -------------------------------------------------------------------------------- 1 | 2 | # Specify an alternate Java home if you want to use a custom version 3 | gauge_java_home = 4 | 5 | # IntelliJ and Eclipse out directory will be usually autodetected 6 | # Use the below property if you need to override the build path 7 | gauge_custom_build_path = 8 | 9 | # specify the directory where additional libs are kept 10 | # you can specify multiple directory names separated with a comma (,) 11 | gauge_additional_libs = libs/* 12 | 13 | # JVM argument passed to java while launching 14 | gauge_jvm_args = 15 | 16 | # specify the directory containing java files to be compiled 17 | # you can specify multiple directory names separated with a comma (,) 18 | gauge_custom_compile_dir = 19 | 20 | # specify the level at which the objects should be cleared 21 | # Possible values are suite, spec and scenario. Default value is suite. 22 | gauge_clear_state_level = scenario 23 | 24 | -------------------------------------------------------------------------------- /env/default/user.properties: -------------------------------------------------------------------------------- 1 | 2 | # user.properties 3 | APP_URL = http://localhost:8080 -------------------------------------------------------------------------------- /env/edge/browser.properties: -------------------------------------------------------------------------------- 1 | BROWSER = EDGE 2 | -------------------------------------------------------------------------------- /env/firefox/browser.properties: -------------------------------------------------------------------------------- 1 | BROWSER = FIREFOX 2 | -------------------------------------------------------------------------------- /manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "Language": "java", 3 | "Plugins": [ 4 | "html-report", 5 | "spectacle", 6 | "json-report", 7 | "flash", 8 | "xml-report" 9 | ] 10 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | gauge_example 6 | active_admin 7 | 1.0-SNAPSHOT 8 | 9 | 10 | 11 | com.thoughtworks.gauge 12 | gauge-java 13 | 0.7.13 14 | test 15 | 16 | 17 | junit 18 | junit 19 | 4.12 20 | test 21 | 22 | 23 | org.seleniumhq.selenium 24 | selenium-java 25 | 3.141.59 26 | test 27 | 28 | 29 | io.github.bonigarcia 30 | webdrivermanager 31 | 3.7.1 32 | 33 | 34 | com.google.guava 35 | guava 36 | 30.1.1-jre 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.apache.maven.plugins 44 | maven-compiler-plugin 45 | 3.8.1 46 | 47 | 48 | com.thoughtworks.gauge.maven 49 | gauge-maven-plugin 50 | 1.4.2 51 | 52 | 53 | test 54 | 55 | specs 56 | 57 | 58 | execute 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 15 67 | 15 68 | 69 | 70 | -------------------------------------------------------------------------------- /specs/CustomerLogout.spec: -------------------------------------------------------------------------------- 1 | # Customer Log out 2 | 3 | |name|email |password| 4 | |----|--------------------|--------| 5 | |John|john.doe@example.com|password| 6 | |Jane|jane.doe@example.com|password| 7 | 8 | * Go to active admin store 9 | 10 | ## Customer must be able to log out 11 | * Sign up as with email and 12 | * Log out 13 | -------------------------------------------------------------------------------- /specs/CustomerSignup.spec: -------------------------------------------------------------------------------- 1 | # Customer sign-up 2 | 3 | * Go to active admin store 4 | 5 | ## Customer Sign-up 6 | 7 | tags: sign-up, customer 8 | 9 | * Sign up a new customer with name "Jann" email "jann.doe@example.com" and "password" 10 | * Check if the user "Jann" is logged in 11 | * See items available for purchase. 12 | -------------------------------------------------------------------------------- /specs/PlaceOrder.spec: -------------------------------------------------------------------------------- 1 | # Place an Order 2 | 3 | * Go to active admin store 4 | 5 | ## Buy a book 6 | 7 | tags: customer 8 | 9 | * Log in with customer name "ScroogeMcduck" and "password" 10 | * Place order for "Beginning Ruby: From Novice to Professional". The cart should now contain "1" items 11 | * Log out 12 | 13 | ## Cart retains items until order is placed 14 | 15 | tags: customer 16 | 17 | * Log in with customer name "ScroogeMcduck" and "password" 18 | * Add "Beginning Ruby: From Novice to Professional" and the cart will now contain "1" item(s) 19 | * Log out 20 | 21 | * Log in with customer name "ScroogeMcduck" and "password" 22 | * Add "The Well-Grounded Rubyist" and the cart will now contain "2" item(s) 23 | * Log out 24 | -------------------------------------------------------------------------------- /specs/concepts/Authentication.cpt: -------------------------------------------------------------------------------- 1 | 2 | # Check if the user is logged in 3 | * Show the log in status for user 4 | * Give an option to Log Out 5 | 6 | # Log out 7 | * Log out the customer 8 | * Show a message "You have been logged out." 9 | * Give an option to Log In 10 | 11 | # Log in with customer name and 12 | * Login as name and 13 | * Show the log in status for user 14 | * See items available for purchase. -------------------------------------------------------------------------------- /specs/concepts/CustomerSignup.cpt: -------------------------------------------------------------------------------- 1 | 2 | # Sign up a new customer with name email and 3 | * Sign up as with email and 4 | * Show a message "Thank you for signing up! You are now logged in." -------------------------------------------------------------------------------- /specs/concepts/GoToStoreWebsite.cpt: -------------------------------------------------------------------------------- 1 | 2 | # Go to active admin store 3 | * Go to the store website 4 | * Clear previous login -------------------------------------------------------------------------------- /specs/concepts/PlaceOrder.cpt: -------------------------------------------------------------------------------- 1 | Created by sswaroop on 5/15/17 2 | 3 | This is a concept file with following syntax for each concept. 4 | # Checkout 5 | * Checkout now 6 | * Show a message "Thank you for your purchase! We will ship it shortly!" 7 | 8 | # Add and the cart will now contain item(s) 9 | * See items available for purchase. 10 | * Add item to the cart. 11 | * Cart now contains number of items 12 | 13 | # Place order for . The cart should now contain items 14 | * See items available for purchase. 15 | * Add and the cart will now contain item(s) 16 | * Checkout -------------------------------------------------------------------------------- /src/test/java/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/getgauge-examples/java-maven-selenium/1828ce9888b3db0407ed57f8fc72ec14c7aac4e2/src/test/java/.DS_Store -------------------------------------------------------------------------------- /src/test/java/CustomerSignup.java: -------------------------------------------------------------------------------- 1 | import com.thoughtworks.gauge.Step; 2 | import org.openqa.selenium.By; 3 | import org.openqa.selenium.WebDriver; 4 | import org.openqa.selenium.WebElement; 5 | import utils.driver.Driver; 6 | import java.util.List; 7 | import static org.hamcrest.core.Is.is; 8 | import static org.hamcrest.core.IsNot.not; 9 | import static org.junit.Assert.assertThat; 10 | 11 | public class CustomerSignup { 12 | 13 | @Step("Sign up as with email and ") 14 | public void registerCustomerWith(String customer, String email, String password) { 15 | WebDriver webDriver = Driver.webDriver; 16 | webDriver.findElement(By.linkText("Sign up")).click(); 17 | WebElement form = webDriver.findElement(By.id("new_user")); 18 | form.findElement(By.name("user[username]")).sendKeys(customer); 19 | form.findElement(By.name("user[email]")).sendKeys(email); 20 | form.findElement(By.name("user[password]")).sendKeys(password); 21 | form.findElement(By.name("user[password_confirmation]")).sendKeys(password); 22 | form.findElement(By.name("commit")).click(); 23 | } 24 | 25 | @Step("See items available for purchase.") 26 | public void seeItemsAvailableForPurchase() { 27 | WebDriver webDriver = Driver.webDriver; 28 | List products = webDriver.findElements(By.className("product")); 29 | assertThat(products.size(), is(not(0))); 30 | } 31 | } -------------------------------------------------------------------------------- /src/test/java/LogIn.java: -------------------------------------------------------------------------------- 1 | import com.thoughtworks.gauge.Step; 2 | import org.openqa.selenium.By; 3 | import org.openqa.selenium.WebDriver; 4 | import org.openqa.selenium.WebElement; 5 | import utils.driver.Driver; 6 | 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public class LogIn { 10 | 11 | private static By submitLogIn = By.linkText("Log in"); 12 | 13 | @Step("Give an option to Log In") 14 | public void giveAnOptionToLogIn() { 15 | WebDriver webDriver = Driver.webDriver; 16 | WebElement login = webDriver.findElement(submitLogIn); 17 | assertTrue(login.isDisplayed()); 18 | } 19 | 20 | @Step("Show the log in status for user ") 21 | public void showTheLogInStatusForUser(String customer) { 22 | WebDriver webDriver = Driver.webDriver; 23 | WebElement authenticatedInfo = webDriver.findElement(By.id("auth")); 24 | assertTrue(authenticatedInfo.isDisplayed()); 25 | assertTrue(authenticatedInfo.getText().contains("Welcome " + customer + "! Not you?")); 26 | } 27 | 28 | @Step("Login as name and ") 29 | public void LoginAsCustomerDetails(String name, String password) { 30 | WebDriver webDriver = Driver.webDriver; 31 | webDriver.findElement(submitLogIn).click(); 32 | webDriver.findElement(By.name("login")).sendKeys(name); 33 | webDriver.findElement(By.name("password")).sendKeys(password); 34 | webDriver.findElement(By.name("commit")).click(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/LogOut.java: -------------------------------------------------------------------------------- 1 | import com.thoughtworks.gauge.Step; 2 | import org.openqa.selenium.By; 3 | import org.openqa.selenium.WebDriver; 4 | import org.openqa.selenium.WebElement; 5 | import org.openqa.selenium.support.ui.ExpectedConditions; 6 | import org.openqa.selenium.support.ui.WebDriverWait; 7 | import utils.driver.Driver; 8 | import static org.junit.Assert.assertTrue; 9 | 10 | public class LogOut { 11 | 12 | private static By submitLogOut = By.linkText("Log out"); 13 | 14 | @Step("Log out the customer") 15 | public void logOutTheCustomer() { 16 | logOut(); 17 | } 18 | 19 | @Step("Clear previous login") 20 | public void clearPreviousLogin() { 21 | try { 22 | logOut(); 23 | } catch (Exception ex) { 24 | System.out.println("no previously logged in Customers"); 25 | } 26 | } 27 | 28 | @Step("Give an option to Log Out") 29 | public void giveAnOptionToLogOut() { 30 | WebDriver webDriver = Driver.webDriver; 31 | WebElement logOut = webDriver.findElement(submitLogOut); 32 | assertTrue(logOut.isDisplayed()); 33 | } 34 | 35 | private void logOut() { 36 | WebDriver webDriver = Driver.webDriver; 37 | 38 | WebDriverWait wait = new WebDriverWait(webDriver, 5); 39 | wait.until(ExpectedConditions.visibilityOfElementLocated(submitLogOut)); 40 | WebElement logOut = webDriver.findElement(submitLogOut); 41 | 42 | logOut.click(); 43 | } 44 | } -------------------------------------------------------------------------------- /src/test/java/PlaceOrder.java: -------------------------------------------------------------------------------- 1 | import com.thoughtworks.gauge.Step; 2 | import org.openqa.selenium.By; 3 | import org.openqa.selenium.WebDriver; 4 | import org.openqa.selenium.WebElement; 5 | import utils.driver.Driver; 6 | 7 | import java.util.List; 8 | 9 | import static org.junit.Assert.assertEquals; 10 | 11 | public class PlaceOrder { 12 | 13 | @Step("Add item to the cart.") 14 | public void addItemToTheBasket(String item) { 15 | WebDriver webDriver = Driver.webDriver; 16 | webDriver.findElement(By.linkText(item)).click(); 17 | webDriver.findElement(By.linkText("Add to Card")).click(); 18 | } 19 | 20 | @Step("Checkout now") 21 | public void placeTheOrder() { 22 | WebDriver webDriver = Driver.webDriver; 23 | webDriver.findElement(By.xpath("//input[@value='Checkout Now!']")).click(); 24 | } 25 | 26 | @Step("Cart now contains number of items") 27 | public void cartNowContains(int numberOfItems) { 28 | WebDriver webDriver = Driver.webDriver; 29 | List products = webDriver.findElements(By.xpath("//table/tbody/tr")); 30 | assertEquals(numberOfItems,products.size()-2); 31 | } 32 | } -------------------------------------------------------------------------------- /src/test/java/utils/AppLauncher.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import com.thoughtworks.gauge.Step; 4 | import utils.driver.Driver; 5 | 6 | public class AppLauncher { 7 | 8 | public static String APP_URL = System.getenv("APP_URL"); 9 | 10 | @Step("Go to the store website") 11 | public void launchTheApplication() { 12 | Driver.webDriver.get(APP_URL); 13 | } 14 | } -------------------------------------------------------------------------------- /src/test/java/utils/FlashMessages.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import com.thoughtworks.gauge.Step; 4 | import org.openqa.selenium.By; 5 | import org.openqa.selenium.WebDriver; 6 | import org.openqa.selenium.WebElement; 7 | import utils.driver.Driver; 8 | import static org.junit.Assert.assertTrue; 9 | 10 | public class FlashMessages { 11 | @Step("Show a message ") 12 | public void showAWelcomeMessage(String message) { 13 | WebDriver webDriver = Driver.webDriver; 14 | WebElement flashNoticeElement = webDriver.findElement(By.xpath(String.format("//div[@id = 'flash_notice' and text() = '%s']", message))); 15 | assertTrue(flashNoticeElement.isDisplayed()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/utils/driver/Driver.java: -------------------------------------------------------------------------------- 1 | package utils.driver; 2 | 3 | import com.thoughtworks.gauge.AfterSuite; 4 | import com.thoughtworks.gauge.BeforeSuite; 5 | import org.openqa.selenium.WebDriver; 6 | 7 | public class Driver { 8 | 9 | // Holds the WebDriver instance 10 | public static WebDriver webDriver; 11 | 12 | // Initialize a webDriver instance of required browser 13 | // Since this does not have a significance in the application's business domain, the BeforeSuite hook is used to instantiate the webDriver 14 | @BeforeSuite 15 | public void initializeDriver(){ 16 | webDriver = DriverFactory.getDriver(); 17 | } 18 | 19 | // Close the webDriver instance 20 | @AfterSuite 21 | public void closeDriver(){ 22 | webDriver.quit(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/utils/driver/DriverFactory.java: -------------------------------------------------------------------------------- 1 | package utils.driver; 2 | 3 | import io.github.bonigarcia.wdm.WebDriverManager; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.chrome.ChromeDriver; 6 | 7 | class DriverFactory { 8 | 9 | // Get a new WebDriver Instance. 10 | // There are various implementations for this depending on browser. The required browser can be set as an environment variable. 11 | // Refer http://getgauge.io/documentation/user/current/managing_environments/README.html 12 | static WebDriver getDriver() { 13 | WebDriverManager.chromedriver().setup(); 14 | return new ChromeDriver(); 15 | } 16 | } 17 | --------------------------------------------------------------------------------