├── .gitignore ├── LICENSE ├── README.md ├── pom.xml ├── renovate.json └── src └── test └── java └── org └── seleniumdemo ├── common ├── Base.java ├── BrowserFactory.java └── Wait.java ├── pageobject ├── CategoryPage.java ├── CheckoutPage.java ├── HomePage.java ├── ProductDetailPage.java ├── SearchResultPage.java └── SubCategoryPage.java └── test ├── SearchTest.java └── ShoppingTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | target/ 3 | *.iml 4 | .DS_STORE -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 German Potes 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 | # selenium-demo 2 | 3 | This repository contains an example of how to build a selenium project with java using good practices. 4 | The different stages of the project from the most basic test cases until a more sophisticated version can be 5 | followed up by checking out the next tags: 6 | 7 | ``git checkout `` 8 | 9 | * v0.0.1: First commit, basic test copied from selenium IDE (http://www.seleniumhq.org/) 10 | * v0.0.2: Add asserts to previews test case 11 | * v0.0.3: Add chrome support 12 | * v0.0.4: Delete previews test case and add new test (IDE) case based on a more complex page (http://automationpractice.com) 13 | * v0.0.5: Fix previews test case and add assertions 14 | * v0.0.6: Add page object model 15 | * v0.0.7: Add search test and chrome support 16 | * v0.0.8: Do some refactors (base test, browser factory), add explicit waits and enable parallel runs 17 | * v0.0.9: Add PageFactory, use ChromeDriverManager 18 | * v0.0.10: Enable executions on remote server 19 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.seleniumdemo 8 | selenium-demo 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | org.seleniumhq.selenium 14 | selenium-java 15 | 3.141.59 16 | test 17 | 18 | 19 | io.github.bonigarcia 20 | webdrivermanager 21 | 3.7.1 22 | test 23 | 24 | 25 | junit 26 | junit 27 | 4.12 28 | test 29 | 30 | 31 | com.saucelabs 32 | sauce_junit 33 | 2.1.25 34 | test 35 | 36 | 37 | 38 | 39 | 40 | 41 | org.apache.maven.plugins 42 | maven-surefire-plugin 43 | 44 | 45 | none 46 | 47 | 48 | 49 | 50 | org.apache.maven.plugins 51 | maven-failsafe-plugin 52 | 2.22.2 53 | 54 | 55 | *Test.java 56 | 57 | classes 58 | 10 59 | 60 | 61 | 62 | 63 | integration-test 64 | verify 65 | 66 | 67 | 68 | 69 | 70 | org.apache.maven.plugins 71 | maven-surefire-plugin 72 | 2.22.2 73 | 74 | true 75 | 76 | 77 | 78 | 79 | 80 | 81 | org.apache.maven.plugins 82 | maven-compiler-plugin 83 | 84 | 1.8 85 | 1.8 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base" 4 | ] 5 | } 6 | -------------------------------------------------------------------------------- /src/test/java/org/seleniumdemo/common/Base.java: -------------------------------------------------------------------------------- 1 | package org.seleniumdemo.common; 2 | 3 | import com.saucelabs.common.SauceOnDemandSessionIdProvider; 4 | import com.saucelabs.common.Utils; 5 | import org.junit.After; 6 | import org.junit.Before; 7 | import org.junit.Rule; 8 | import org.junit.rules.TestName; 9 | import org.openqa.selenium.WebDriver; 10 | import org.openqa.selenium.remote.DesiredCapabilities; 11 | import org.openqa.selenium.remote.RemoteWebDriver; 12 | 13 | import java.net.URL; 14 | import java.util.concurrent.TimeUnit; 15 | 16 | public class Base implements SauceOnDemandSessionIdProvider { 17 | private final static String USER_NAME = Utils.readPropertyOrEnv("SAUCE_USERNAME", ""); 18 | private final static String ACCESS_KEY = Utils.readPropertyOrEnv("SAUCE_ACCESS_KEY", ""); 19 | private static final String SELENIUM_URI = "@ondemand.saucelabs.com:443/wd/hub"; 20 | private static final long SAUCELABS_ID = System.currentTimeMillis() % 100000; 21 | 22 | private String sessionId; 23 | protected WebDriver driver; 24 | 25 | @Rule 26 | public final TestName name = new TestName() { 27 | public String getMethodName() { 28 | return String.format("%s", super.getMethodName()); 29 | } 30 | }; 31 | 32 | @Before 33 | public void setUp() throws Exception { 34 | String browserName = System.getProperty("browserName"); 35 | 36 | driver = BrowserFactory.getBrowser(browserName); 37 | 38 | this.sessionId = (((RemoteWebDriver) driver).getSessionId()).toString(); 39 | driver.manage().timeouts().implicitlyWait(Wait.EXPLICIT_WAIT, TimeUnit.SECONDS); 40 | } 41 | 42 | @After 43 | public void tearDown() throws Exception { 44 | driver.quit(); 45 | } 46 | 47 | @Override 48 | public String getSessionId() { 49 | return this.sessionId; 50 | } 51 | } -------------------------------------------------------------------------------- /src/test/java/org/seleniumdemo/common/BrowserFactory.java: -------------------------------------------------------------------------------- 1 | package org.seleniumdemo.common; 2 | 3 | import io.github.bonigarcia.wdm.ChromeDriverManager; 4 | import io.github.bonigarcia.wdm.FirefoxDriverManager; 5 | import org.openqa.selenium.WebDriver; 6 | import org.openqa.selenium.chrome.ChromeDriver; 7 | import org.openqa.selenium.firefox.FirefoxDriver; 8 | 9 | class BrowserFactory { 10 | static WebDriver getBrowser(String browserName) { 11 | WebDriver driver; 12 | if (browserName != null && browserName.equals("chrome")) { 13 | ChromeDriverManager.getInstance().setup(); 14 | driver = new ChromeDriver(); 15 | } else { 16 | FirefoxDriverManager.getInstance().setup(); 17 | driver = new FirefoxDriver(); 18 | } 19 | return driver; 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /src/test/java/org/seleniumdemo/common/Wait.java: -------------------------------------------------------------------------------- 1 | package org.seleniumdemo.common; 2 | 3 | public interface Wait { 4 | int EXPLICIT_WAIT = 30; 5 | int SHORT_WAIT = 1; 6 | } 7 | -------------------------------------------------------------------------------- /src/test/java/org/seleniumdemo/pageobject/CategoryPage.java: -------------------------------------------------------------------------------- 1 | package org.seleniumdemo.pageobject; 2 | 3 | import org.openqa.selenium.WebDriver; 4 | import org.openqa.selenium.WebElement; 5 | import org.openqa.selenium.support.FindBy; 6 | import org.openqa.selenium.support.PageFactory; 7 | 8 | import java.util.Objects; 9 | 10 | public class CategoryPage { 11 | @FindBy(linkText = "Tops") 12 | private WebElement topCategory; 13 | 14 | public CategoryPage(final WebDriver driver) { 15 | Objects.requireNonNull(driver); 16 | PageFactory.initElements(driver, this); 17 | } 18 | 19 | public void goToTopSubCategory() { 20 | topCategory.click(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/test/java/org/seleniumdemo/pageobject/CheckoutPage.java: -------------------------------------------------------------------------------- 1 | package org.seleniumdemo.pageobject; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import org.openqa.selenium.interactions.Actions; 7 | import org.openqa.selenium.support.FindBy; 8 | import org.openqa.selenium.support.PageFactory; 9 | import org.openqa.selenium.support.ui.WebDriverWait; 10 | 11 | import java.util.Objects; 12 | 13 | public class CheckoutPage { 14 | @FindBy(id = "total_price_container") 15 | private WebElement totalPriceLabel; 16 | 17 | @FindBy(css = "a[title='View my shopping cart']") 18 | private WebElement shoppingCartOption; 19 | 20 | @FindBy(css = "a.ajax_cart_block_remove_link") 21 | private WebElement removeElementIcon; 22 | 23 | @FindBy(css = ".ajax_cart_no_product") 24 | private WebElement shoppingCartEmptyLabel; 25 | private static final By SHOPPING_CART_EMPTY_LABEL = By.cssSelector(".ajax_cart_no_product"); 26 | 27 | private final WebDriver driver; 28 | 29 | 30 | public CheckoutPage(final WebDriver driver) { 31 | Objects.requireNonNull(driver); 32 | this.driver = driver; 33 | PageFactory.initElements(this.driver, this); 34 | } 35 | 36 | public String getTotalPrice() { 37 | return totalPriceLabel.getText(); 38 | } 39 | 40 | public void cleanShoppingCart() throws Exception { 41 | Actions actions = new Actions(driver); 42 | actions.moveToElement(shoppingCartOption); 43 | actions.perform(); 44 | 45 | removeElementIcon.click(); 46 | } 47 | 48 | public boolean isShoppingCartEmpty() { 49 | new WebDriverWait(driver, 1) 50 | .until((WebDriver driver1) -> driver1.findElement(SHOPPING_CART_EMPTY_LABEL).isDisplayed()); 51 | return driver.findElement(SHOPPING_CART_EMPTY_LABEL).isDisplayed(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/org/seleniumdemo/pageobject/HomePage.java: -------------------------------------------------------------------------------- 1 | package org.seleniumdemo.pageobject; 2 | 3 | import org.openqa.selenium.Keys; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import org.openqa.selenium.support.FindBy; 7 | import org.openqa.selenium.support.PageFactory; 8 | 9 | import java.util.Objects; 10 | 11 | public class HomePage { 12 | @FindBy(linkText = "Women") 13 | private WebElement womenMenu; 14 | 15 | @FindBy(name = "search_query") 16 | private WebElement searchField; 17 | 18 | private final WebDriver driver; 19 | 20 | public HomePage(final WebDriver driver) { 21 | Objects.requireNonNull(driver); 22 | this.driver = driver; 23 | PageFactory.initElements(this.driver, this); 24 | } 25 | 26 | public void goToWomenSubcategory() { 27 | womenMenu.click(); 28 | } 29 | 30 | public void loadHomePage() { 31 | driver.get("http://automationpractice.com/index.php"); 32 | } 33 | 34 | public void search(String searchText) { 35 | Objects.requireNonNull(searchText); 36 | searchField.sendKeys(searchText); 37 | searchField.sendKeys(Keys.ENTER); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/seleniumdemo/pageobject/ProductDetailPage.java: -------------------------------------------------------------------------------- 1 | package org.seleniumdemo.pageobject; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import org.openqa.selenium.support.FindBy; 7 | import org.openqa.selenium.support.PageFactory; 8 | import org.openqa.selenium.support.ui.WebDriverWait; 9 | import org.seleniumdemo.common.Wait; 10 | 11 | import java.util.Objects; 12 | 13 | public class ProductDetailPage { 14 | @FindBy(name = "Submit") 15 | private WebElement submitButton; 16 | 17 | private static final By CHECKOUT_BUTTON = By.cssSelector("a[title='Proceed to checkout']"); 18 | 19 | private final WebDriver driver; 20 | 21 | public ProductDetailPage(final WebDriver driver) { 22 | Objects.requireNonNull(driver); 23 | this.driver = driver; 24 | PageFactory.initElements(this.driver, this); 25 | } 26 | 27 | public void addProductToCart() { 28 | submitButton.click(); 29 | 30 | new WebDriverWait(driver, Wait.SHORT_WAIT) 31 | .until((WebDriver driver1) -> { 32 | WebElement checkoutButton = driver1.findElement(CHECKOUT_BUTTON); 33 | return checkoutButton != null && checkoutButton.isDisplayed(); 34 | }); 35 | 36 | driver.findElement(CHECKOUT_BUTTON).click(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/seleniumdemo/pageobject/SearchResultPage.java: -------------------------------------------------------------------------------- 1 | package org.seleniumdemo.pageobject; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | 6 | import java.util.Objects; 7 | 8 | public class SearchResultPage { 9 | private final WebDriver driver; 10 | 11 | public SearchResultPage(final WebDriver driver) { 12 | Objects.requireNonNull(driver); 13 | this.driver = driver; 14 | } 15 | 16 | public boolean isResultItemDisplayed(String itemName) { 17 | return driver.findElement(By.cssSelector(String.format("img[alt='%s']", itemName))).isDisplayed(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/org/seleniumdemo/pageobject/SubCategoryPage.java: -------------------------------------------------------------------------------- 1 | package org.seleniumdemo.pageobject; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import org.openqa.selenium.interactions.Actions; 7 | 8 | import java.util.Objects; 9 | 10 | public class SubCategoryPage { 11 | private final WebDriver driver; 12 | 13 | public SubCategoryPage(final WebDriver driver) { 14 | Objects.requireNonNull(driver); 15 | this.driver = driver; 16 | } 17 | 18 | public void goToProductDetails(String productName) { 19 | Objects.requireNonNull(productName); 20 | WebElement productImage = driver.findElement(By.cssSelector(String.format("img[title='%s']", productName))); 21 | 22 | Actions actions = new Actions(driver); 23 | actions.moveToElement(productImage); 24 | actions.perform(); 25 | productImage.click(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/org/seleniumdemo/test/SearchTest.java: -------------------------------------------------------------------------------- 1 | package org.seleniumdemo.test; 2 | 3 | import org.junit.Test; 4 | import org.seleniumdemo.common.Base; 5 | import org.seleniumdemo.pageobject.HomePage; 6 | import org.seleniumdemo.pageobject.SearchResultPage; 7 | 8 | import static org.junit.Assert.assertTrue; 9 | 10 | public class SearchTest extends Base { 11 | 12 | @Test 13 | public void searchDressAndValidateResults() { 14 | HomePage homePage = new HomePage(driver); 15 | SearchResultPage searchResultPage = new SearchResultPage(driver); 16 | 17 | homePage.loadHomePage(); 18 | homePage.search("dress"); 19 | 20 | boolean isDressDisplayed = searchResultPage.isResultItemDisplayed("Printed Chiffon Dress"); 21 | assertTrue(isDressDisplayed); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/org/seleniumdemo/test/ShoppingTest.java: -------------------------------------------------------------------------------- 1 | package org.seleniumdemo.test; 2 | 3 | import org.junit.Test; 4 | import org.seleniumdemo.common.Base; 5 | import org.seleniumdemo.pageobject.*; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | public class ShoppingTest extends Base { 10 | 11 | @Test 12 | public void addShortsToCartValidatePriceAndCleanCart() throws Exception { 13 | HomePage homePage = new HomePage(driver); 14 | CategoryPage categoryPage = new CategoryPage(driver); 15 | SubCategoryPage subCategoryPage = new SubCategoryPage(driver); 16 | ProductDetailPage productDetailPage = new ProductDetailPage(driver); 17 | CheckoutPage checkoutPage = new CheckoutPage(driver); 18 | 19 | homePage.loadHomePage(); 20 | homePage.goToWomenSubcategory(); 21 | 22 | categoryPage.goToTopSubCategory(); 23 | 24 | String productName = "Faded Short Sleeve T-shirts"; 25 | subCategoryPage.goToProductDetails(productName); 26 | 27 | productDetailPage.addProductToCart(); 28 | String totalPrice = checkoutPage.getTotalPrice(); 29 | assertEquals("$18.51", totalPrice); 30 | 31 | checkoutPage.cleanShoppingCart(); 32 | boolean isCartEmpty = checkoutPage.isShoppingCartEmpty(); 33 | assertTrue(isCartEmpty); 34 | } 35 | } 36 | --------------------------------------------------------------------------------