├── suite ├── search-module.xml └── order-module.xml ├── Dockerfile ├── Jenkinsfile ├── src ├── test │ └── java │ │ └── com │ │ └── testautomationguru │ │ └── container │ │ └── test │ │ ├── BaseTest.java │ │ ├── SearchTest.java │ │ └── OrderTest.java └── main │ └── java │ └── com │ └── testautomationguru │ └── container │ └── pages │ ├── SearchPage.java │ └── OrderPage.java └── pom.xml /suite/search-module.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /suite/order-module.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk:8-jre-slim 2 | 3 | #A Directory in the base image to copy our depedencies 4 | WORKDIR /usr/share/tag 5 | 6 | # Add the project jar & copy dependencies 7 | ADD target/container-test.jar container-test.jar 8 | ADD target/libs libs 9 | 10 | # Add the suite xmls 11 | ADD suite/order-module.xml order-module.xml 12 | ADD suite/search-module.xml search-module.xml 13 | 14 | # Command line to execute the test 15 | # Expects below ennvironment variables 16 | # BROWSER = chrome / firefox 17 | # MODULE = order-module / search-module 18 | # SELENIUM_HUB = selenium hub hostname / ipaddress 19 | 20 | ENTRYPOINT java -cp container-test.jar:libs/* -DseleniumHubHost=$SELENIUM_HUB -Dbrowser=$BROWSER org.testng.TestNG $MODULE 21 | -------------------------------------------------------------------------------- /Jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | agent { 3 | node { 4 | label 'docker' && 'maven' 5 | } 6 | } 7 | stages { 8 | stage('Build Jar') { 9 | steps { 10 | sh 'mvn clean package -DskipTests' 11 | } 12 | } 13 | stage('Build Image') { 14 | steps { 15 | script { 16 | app = docker.build("vinsdocker/containertest") 17 | } 18 | } 19 | } 20 | stage('Push Image') { 21 | steps { 22 | script { 23 | docker.withRegistry('https://registry.hub.docker.com', 'dockerhub') { 24 | app.push("${BUILD_NUMBER}") 25 | app.push("latest") 26 | } 27 | } 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /src/test/java/com/testautomationguru/container/test/BaseTest.java: -------------------------------------------------------------------------------- 1 | package com.testautomationguru.container.test; 2 | 3 | import org.testng.annotations.BeforeTest; 4 | 5 | import java.net.MalformedURLException; 6 | import java.net.URL; 7 | 8 | import org.openqa.selenium.WebDriver; 9 | import org.openqa.selenium.remote.DesiredCapabilities; 10 | import org.openqa.selenium.remote.RemoteWebDriver; 11 | import org.testng.annotations.AfterTest; 12 | 13 | public class BaseTest { 14 | 15 | protected WebDriver driver; 16 | 17 | @BeforeTest 18 | public void setUp() throws MalformedURLException { 19 | 20 | DesiredCapabilities dc = DesiredCapabilities.chrome(); 21 | 22 | if (System.getProperty("browser").equals("firefox")) 23 | dc = DesiredCapabilities.firefox(); 24 | 25 | String host = System.getProperty("seleniumHubHost"); 26 | 27 | driver = new RemoteWebDriver(new URL("http://" + host + ":4444/wd/hub"), dc); 28 | 29 | } 30 | 31 | @AfterTest 32 | public void tearDown() throws InterruptedException { 33 | driver.quit(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/com/testautomationguru/container/test/SearchTest.java: -------------------------------------------------------------------------------- 1 | package com.testautomationguru.container.test; 2 | 3 | import java.net.MalformedURLException; 4 | 5 | import org.testng.Assert; 6 | import org.testng.annotations.BeforeTest; 7 | import org.testng.annotations.DataProvider; 8 | import org.testng.annotations.Test; 9 | 10 | import com.testautomationguru.container.pages.SearchPage; 11 | 12 | public class SearchTest extends BaseTest { 13 | 14 | private SearchPage google; 15 | 16 | @BeforeTest 17 | public void setUp() throws MalformedURLException { 18 | super.setUp(); 19 | google = new SearchPage(driver); 20 | } 21 | 22 | @Test(dataProvider = "search-keywords") 23 | public void googleTest(String searchKeyword) { 24 | google.goTo(); 25 | google.searchFor(searchKeyword); 26 | Assert.assertTrue(google.getResults().size()>0); 27 | } 28 | 29 | @DataProvider(name = "search-keywords") 30 | public static Object[][] credentials() { 31 | return new Object[][] { 32 | { "test automation guru" }, 33 | { "selenium webdriver" }, 34 | { "dockerized selenium grid" }, 35 | { "test automation blog" }, 36 | { "jmeter docker" } , 37 | { "test automation guru" }, 38 | { "selenium webdriver" }, 39 | { "dockerized selenium grid" }, 40 | { "test automation blog" }, 41 | { "jmeter docker" } 42 | }; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/com/testautomationguru/container/test/OrderTest.java: -------------------------------------------------------------------------------- 1 | package com.testautomationguru.container.test; 2 | 3 | 4 | import java.net.MalformedURLException; 5 | import java.util.List; 6 | 7 | import org.testng.Assert; 8 | import org.testng.annotations.BeforeTest; 9 | import org.testng.annotations.DataProvider; 10 | import org.testng.annotations.Test; 11 | 12 | import com.testautomationguru.container.pages.OrderPage; 13 | 14 | public class OrderTest extends BaseTest{ 15 | 16 | private OrderPage store; 17 | 18 | @BeforeTest 19 | public void setUp() throws MalformedURLException { 20 | super.setUp(); 21 | store = new OrderPage(driver); 22 | } 23 | 24 | @Test(dataProvider = "category") 25 | public void googleTest(String category) { 26 | store.goTo(); 27 | store.goToCategory(category); 28 | List itemPrice = store.getListOfItems(); 29 | itemPrice.stream() 30 | .forEach(System.out::println); 31 | System.out.println("----------------------------"); 32 | Assert.assertTrue(itemPrice.size()>0); 33 | } 34 | 35 | @DataProvider(name = "category") 36 | public static Object[][] credentials() { 37 | return new Object[][] { 38 | { "Accessories" }, 39 | { "iMacs" }, 40 | { "iPads" }, 41 | { "iPhones" }, 42 | { "iPods" }, 43 | { "MacBooks" }, 44 | { "Accessories" }, 45 | { "iMacs" }, 46 | { "iPads" }, 47 | { "iPhones" }, 48 | { "iPods" }, 49 | { "MacBooks" } 50 | }; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/testautomationguru/container/pages/SearchPage.java: -------------------------------------------------------------------------------- 1 | package com.testautomationguru.container.pages; 2 | 3 | import java.util.List; 4 | 5 | import org.openqa.selenium.By; 6 | import org.openqa.selenium.WebDriver; 7 | import org.openqa.selenium.WebElement; 8 | import org.openqa.selenium.support.FindBy; 9 | import org.openqa.selenium.support.PageFactory; 10 | import org.openqa.selenium.support.ui.ExpectedConditions; 11 | import org.openqa.selenium.support.ui.WebDriverWait; 12 | 13 | public class SearchPage { 14 | 15 | private final WebDriver driver; 16 | private final WebDriverWait wait; 17 | 18 | @FindBy(name = "q") 19 | private WebElement searchBox; 20 | 21 | @FindBy(css = "input.lsb") 22 | private WebElement searchButton; 23 | 24 | @FindBy(className = "rc") 25 | private List searchResults; 26 | 27 | @FindBy(id = "foot") 28 | private WebElement footer; 29 | 30 | public SearchPage(final WebDriver driver) { 31 | this.driver = driver; 32 | PageFactory.initElements(driver, this); 33 | this.wait = new WebDriverWait(driver, 30); 34 | } 35 | 36 | public void goTo() { 37 | this.driver.get("https://www.google.com"); 38 | System.out.println("Browser launched and navigated to Google"); 39 | } 40 | 41 | public void searchFor(String text) { 42 | this.searchBox.sendKeys(text); 43 | System.out.println(text + " - entered for search"); 44 | wait.until(ExpectedConditions.elementToBeClickable(this.searchButton)); 45 | System.out.println("Search button clicked"); 46 | this.searchButton.click(); 47 | wait.until(ExpectedConditions.presenceOfElementLocated(By.className("rc"))); 48 | System.out.println("Results appeared"); 49 | } 50 | 51 | public List getResults() { 52 | System.out.println("Results Count : " + this.searchResults.size()); 53 | System.out.println("---------------------------------------------"); 54 | return this.searchResults; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.testautomationguru.container 5 | docker-selenium-jenkinsfile 6 | 0.0.1-SNAPSHOT 7 | 8 | 9 | org.seleniumhq.selenium 10 | selenium-java 11 | 3.5.2 12 | 13 | 14 | org.testng 15 | testng 16 | 6.11 17 | 18 | 19 | 20 | container-test 21 | src 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-compiler-plugin 26 | 3.5.1 27 | 28 | 1.8 29 | 1.8 30 | 1.8 31 | 32 | 33 | 34 | org.apache.maven.plugins 35 | maven-dependency-plugin 36 | 37 | 38 | copy-dependencies 39 | prepare-package 40 | 41 | copy-dependencies 42 | 43 | 44 | 45 | ${project.build.directory}/libs 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.apache.maven.plugins 53 | maven-jar-plugin 54 | 55 | 56 | 57 | true 58 | libs/ 59 | 60 | 61 | 62 | 63 | 64 | org.apache.maven.plugins 65 | maven-surefire-plugin 66 | 2.20 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /src/main/java/com/testautomationguru/container/pages/OrderPage.java: -------------------------------------------------------------------------------- 1 | package com.testautomationguru.container.pages; 2 | 3 | import java.util.List; 4 | import java.util.function.Consumer; 5 | import java.util.stream.Collectors; 6 | 7 | import org.openqa.selenium.By; 8 | import org.openqa.selenium.WebDriver; 9 | import org.openqa.selenium.WebElement; 10 | import org.openqa.selenium.interactions.Actions; 11 | import org.openqa.selenium.support.PageFactory; 12 | import org.openqa.selenium.support.ui.ExpectedConditions; 13 | import org.openqa.selenium.support.ui.WebDriverWait; 14 | 15 | public class OrderPage { 16 | 17 | private WebDriver driver; 18 | private WebDriverWait wait; 19 | private Actions action; 20 | 21 | Consumer hover = (By by) -> { 22 | action.moveToElement(driver.findElement(by)) 23 | .perform(); 24 | }; 25 | 26 | public OrderPage(final WebDriver driver) { 27 | this.driver = driver; 28 | PageFactory.initElements(driver, this); 29 | this.wait = new WebDriverWait(driver, 30); 30 | this.action = new Actions(driver); 31 | } 32 | 33 | public void goTo() { 34 | this.driver.get("http://store.demoqa.com/products-page/product-category/?view_type=default"); 35 | System.out.println("Browser launched and navigated to DemoQA page"); 36 | } 37 | 38 | public void goToCategory(String category){ 39 | hover.accept(By.linkText("Product Category")); 40 | wait.until(ExpectedConditions.elementToBeClickable(By.linkText(category))); 41 | 42 | //click on the category 43 | driver.findElement(By.linkText(category)).click(); 44 | 45 | //wait for the list of products to be available 46 | wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[text()='"+ category +"']"))); 47 | } 48 | 49 | public List getListOfItems() { 50 | 51 | return driver.findElements(By.cssSelector("div.productcol")) 52 | .stream() 53 | .map(this::getItemAndPrice) 54 | .collect(Collectors.toList()); 55 | 56 | } 57 | 58 | private String getItemAndPrice(WebElement element) { 59 | String title = element.findElement(By.cssSelector("h2.prodtitle")).getText().trim(); 60 | String price = element.findElement(By.cssSelector("span.currentprice")).getText().trim(); 61 | return title + " - " + price; 62 | } 63 | 64 | 65 | } 66 | --------------------------------------------------------------------------------