├── .gitignore ├── .idea ├── .gitignore ├── vcs.xml ├── misc.xml ├── compiler.xml ├── jarRepositories.xml └── uiDesigner.xml ├── amazon-selenium-yt.iml ├── README.md ├── src ├── main │ └── java │ │ ├── pages │ │ ├── SearchBox.java │ │ ├── ProductDetailPage.java │ │ ├── CartPage.java │ │ ├── ProductsPage.java │ │ ├── BasePage.java │ │ └── HomePage.java │ │ └── logger │ │ └── Log.java └── test │ └── java │ ├── BaseTest.java │ ├── testlogger │ └── TestResultLogger.java │ └── Test_Add_Product_To_Cart.java ├── log4j.xml └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | logfile.log 3 | /target 4 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /amazon-selenium-yt.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YouTube example selenium project with POM design for beginners. 2 | 3 | ## Test Steps 4 | 5 | - Go to home page 6 | - Search for a product 7 | - Add product to cart 8 | - Go to cart and check if the product is added 9 | 10 | 11 | ## To run the tests 12 | 13 | - "mvn clean test" will be enough 14 | 15 | 16 | ## YouTube Video 17 | 18 | - https://www.youtube.com/watch?v=2naKQHbFQpY 19 | 20 | -------------------------------------------------------------------------------- /src/main/java/pages/SearchBox.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | 6 | public class SearchBox extends BasePage{ 7 | 8 | By searchBoxLocator = By.id("twotabsearchtextbox"); 9 | By submitButtonLocator = By.id("nav-search-submit-button"); 10 | 11 | 12 | public SearchBox(WebDriver driver) { 13 | super(driver); 14 | } 15 | 16 | public void search(String text) { 17 | type(searchBoxLocator , text); 18 | click(submitButtonLocator); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/pages/ProductDetailPage.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | 6 | public class ProductDetailPage extends BasePage{ 7 | 8 | By addToCartButtonLocator = By.id("add-to-cart-button"); 9 | 10 | public ProductDetailPage(WebDriver driver) { 11 | super(driver); 12 | } 13 | 14 | public boolean isOnProductDetailPage() { 15 | return isDisplayed(addToCartButtonLocator); 16 | } 17 | 18 | public void addToCart() { 19 | click(addToCartButtonLocator); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/logger/Log.java: -------------------------------------------------------------------------------- 1 | package logger; 2 | 3 | import org.apache.log4j.Logger; 4 | import org.apache.log4j.xml.DOMConfigurator; 5 | 6 | public class Log { 7 | 8 | static Logger logger = Logger.getLogger(Log.class); 9 | 10 | public Log(){ 11 | DOMConfigurator.configure("log4j.xml"); 12 | } 13 | 14 | public void info(String message){ 15 | logger.info(message); 16 | } 17 | 18 | public void warn(String message){ 19 | logger.warn(message); 20 | } 21 | 22 | public void error(String message){ 23 | logger.error(message); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/pages/CartPage.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import java.util.List; 7 | 8 | public class CartPage extends BasePage { 9 | 10 | By productNameLocator = new By.ByCssSelector("a.a-link-normal span.a-size-medium "); 11 | 12 | public CartPage(WebDriver driver) { 13 | super(driver); 14 | } 15 | 16 | public boolean checkIfProductAdded() { 17 | return getProducts().size() > 0 ; 18 | } 19 | 20 | private List getProducts(){ 21 | return findAll(productNameLocator); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/pages/ProductsPage.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | 7 | import java.util.List; 8 | 9 | public class ProductsPage extends BasePage { 10 | 11 | By shippingOptionLocator = By.id("p_n_free_shipping_eligible-title"); 12 | By productNameLocator = new By.ByCssSelector("span.a-size-base-plus"); 13 | 14 | public ProductsPage(WebDriver driver) { 15 | super(driver); 16 | } 17 | 18 | public boolean isOnProductPage() { 19 | return isDisplayed(shippingOptionLocator); 20 | } 21 | 22 | public void selectProduct(int i) { 23 | getAllProducts().get(i).click(); 24 | } 25 | 26 | private List getAllProducts(){ 27 | return findAll(productNameLocator); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/BaseTest.java: -------------------------------------------------------------------------------- 1 | import io.github.bonigarcia.wdm.WebDriverManager; 2 | import org.junit.jupiter.api.*; 3 | import org.junit.jupiter.api.extension.ExtendWith; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.chrome.ChromeDriver; 6 | import testlogger.TestResultLogger; 7 | 8 | @TestInstance(TestInstance.Lifecycle.PER_CLASS) 9 | @TestMethodOrder(MethodOrderer.OrderAnnotation.class) 10 | @ExtendWith(TestResultLogger.class) 11 | public class BaseTest { 12 | 13 | WebDriver driver ; 14 | 15 | @BeforeAll 16 | public void setUp(){ 17 | WebDriverManager.chromedriver().setup(); 18 | driver = new ChromeDriver(); 19 | driver.get("https://www.amazon.com.tr/"); 20 | driver.manage().window().maximize(); 21 | } 22 | 23 | @AfterAll 24 | public void tearDown(){ 25 | driver.quit(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/pages/BasePage.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | 7 | import java.util.List; 8 | 9 | public class BasePage { 10 | 11 | WebDriver driver ; 12 | 13 | public BasePage(WebDriver driver){ 14 | this.driver = driver ; 15 | } 16 | 17 | public WebElement find(By locator){ 18 | return driver.findElement(locator); 19 | } 20 | 21 | public List findAll(By locator){ 22 | return driver.findElements(locator); 23 | } 24 | 25 | public void click(By locator){ 26 | find(locator).click(); 27 | } 28 | 29 | public void type(By locator , String text){ 30 | find(locator).sendKeys(text); 31 | } 32 | 33 | public Boolean isDisplayed(By locator){ 34 | return find(locator).isDisplayed(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | -------------------------------------------------------------------------------- /log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/test/java/testlogger/TestResultLogger.java: -------------------------------------------------------------------------------- 1 | package testlogger; 2 | 3 | import logger.Log; 4 | import org.junit.jupiter.api.extension.ExtensionContext; 5 | import org.junit.jupiter.api.extension.TestWatcher; 6 | 7 | import java.util.Optional; 8 | 9 | public class TestResultLogger implements TestWatcher { 10 | 11 | Log log = new Log(); 12 | 13 | @Override 14 | public void testSuccessful(ExtensionContext context) { 15 | String testName = context.getDisplayName(); 16 | log.info(testName + " PASSED"); 17 | } 18 | 19 | @Override 20 | public void testFailed(ExtensionContext context, Throwable cause) { 21 | String testName = context.getDisplayName(); 22 | String testFailCause = cause.getMessage() ; 23 | log.error(testName + " FAILED with cause : " + testFailCause); 24 | } 25 | 26 | @Override 27 | public void testDisabled(ExtensionContext context, Optional reason) { 28 | 29 | } 30 | 31 | @Override 32 | public void testAborted(ExtensionContext context, Throwable cause) { 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/pages/HomePage.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | 6 | 7 | public class HomePage extends BasePage { 8 | 9 | SearchBox searchBox ; 10 | By cartCountLocator = By.id("nav-cart-count"); 11 | By cartContainerLocator = By.id("nav-cart-count-container"); 12 | By acceptCookiesLocator = By.id("sp-cc-accept"); 13 | 14 | public HomePage(WebDriver driver) { 15 | super(driver); 16 | searchBox = new SearchBox(driver); 17 | } 18 | 19 | public SearchBox searchBox(){ 20 | return this.searchBox; 21 | } 22 | 23 | public boolean isProductCountUp() { 24 | return getCartCount() > 0 ; 25 | } 26 | 27 | public void goToCart() { 28 | click(cartContainerLocator); 29 | } 30 | 31 | private int getCartCount(){ 32 | String count = find(cartCountLocator).getText(); 33 | return Integer.parseInt(count); 34 | } 35 | 36 | public void acceptCookies(){ 37 | if (isDisplayed(acceptCookiesLocator)){ 38 | click(acceptCookiesLocator); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/Test_Add_Product_To_Cart.java: -------------------------------------------------------------------------------- 1 | import org.junit.jupiter.api.Assertions; 2 | import org.junit.jupiter.api.Order; 3 | import org.junit.jupiter.api.Test; 4 | import pages.CartPage; 5 | import pages.HomePage; 6 | import pages.ProductDetailPage; 7 | import pages.ProductsPage; 8 | 9 | public class Test_Add_Product_To_Cart extends BaseTest { 10 | 11 | HomePage homePage ; 12 | ProductsPage productsPage ; 13 | ProductDetailPage productDetailPage ; 14 | CartPage cartPage ; 15 | 16 | @Test 17 | @Order(1) 18 | // @Disabled("Due to BUG-123 disabled") 19 | public void search_a_product(){ 20 | homePage = new HomePage(driver); 21 | homePage.acceptCookies(); 22 | productsPage = new ProductsPage(driver); 23 | homePage.searchBox().search("Laptop"); 24 | Assertions.assertTrue(productsPage.isOnProductPage() , 25 | "Not on products page!"); 26 | } 27 | 28 | @Test 29 | @Order(2) 30 | public void select_a_product(){ 31 | productDetailPage = new ProductDetailPage(driver); 32 | productsPage.selectProduct(1); 33 | Assertions.assertTrue(productDetailPage.isOnProductDetailPage(), 34 | "Not on product detail page!"); 35 | } 36 | 37 | @Test 38 | @Order(3) 39 | public void add_product_to_cart(){ 40 | productDetailPage.addToCart(); 41 | Assertions.assertTrue(homePage.isProductCountUp(), 42 | "Product count did not increase!"); 43 | } 44 | 45 | @Test 46 | @Order(4) 47 | public void go_to_cart(){ 48 | cartPage = new CartPage(driver); 49 | homePage.goToCart(); 50 | Assertions.assertTrue(cartPage.checkIfProductAdded() , 51 | "Product was not added to cart!"); 52 | } 53 | 54 | 55 | 56 | 57 | } 58 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.example 8 | amazon-selenium-yt 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 8 13 | 8 14 | 3.141.59 15 | 5.7.0 16 | 17 | 18 | 19 | 20 | org.seleniumhq.selenium 21 | selenium-java 22 | ${selenium.version} 23 | 24 | 25 | org.junit.jupiter 26 | junit-jupiter-engine 27 | ${junit.jupiter.version} 28 | compile 29 | 30 | 31 | io.github.bonigarcia 32 | webdrivermanager 33 | 4.4.3 34 | test 35 | 36 | 37 | log4j 38 | log4j 39 | 1.2.17 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.apache.maven.plugins 47 | maven-surefire-plugin 48 | 2.22.0 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | --------------------------------------------------------------------------------