├── .github └── workflows │ ├── maven.build.master.yml │ ├── maven.yml │ └── maven2.yml ├── .gitignore ├── .idea ├── .gitignore ├── .name ├── compiler.xml ├── misc.xml └── vcs.xml ├── LICENSE ├── README.md ├── pom.xml ├── resources ├── mac │ └── chromedriver └── windows │ └── chromedriver.exe └── src └── test └── java ├── HelloSelenium2Test.java ├── HelloSelenium3Test.java ├── HelloSeleniumTest.java ├── SeleniumTest.java ├── advanced └── tips │ └── PageLoadTest.java ├── atda ├── AcceptanceTestDrivenAutomationTest.java ├── BasePage.java ├── LoginPage.java └── ProductsPage.java ├── best └── practices │ ├── Duplication.java │ ├── LoginPage.java │ ├── PersonalInfoPage.java │ ├── ProductsPage.java │ ├── ShoppingCartPage.java │ └── Synchronization.java ├── csvDataDriven ├── CSVDataReader.java ├── CSVExercise.java ├── Prices.csv ├── README.md └── Users.csv ├── enumDataDriven ├── EnumDataDrivenTests.java ├── EnumExercise.java ├── SortValue.java └── User.java ├── locating └── elements │ └── LocatingElementsTest.java ├── testNGDataDriven ├── TestNGDataDrivenExercise.java └── UserLoginTests.java └── testingClassData ├── DataProviderExercise.java ├── ProductDataProvider.java ├── UserDataProvider.java └── UserLoginTests.java /.github/workflows/maven.build.master.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Java CI with Maven 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Set up JDK 111 20 | uses: actions/setup-java@v1 21 | with: 22 | java-version: 11 23 | - name: Build with Maven 24 | run: mvn compile -X 25 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Java CI with Maven 5 | 6 | on: 7 | push: 8 | branches: 9 | - cross_browser_sauce_bindings 10 | 11 | jobs: 12 | build: 13 | 14 | runs-on: ubuntu-latest 15 | 16 | steps: 17 | - uses: actions/checkout@v2 18 | - name: Set up JDK 11 19 | uses: actions/setup-java@v1 20 | with: 21 | java-version: 11 22 | - name: Run acceptance tests 23 | env: 24 | SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }} 25 | SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }} 26 | run: | 27 | echo "$TEST_SECRET" 28 | mvn test -X -Dtest=AcceptanceTestDrivenAutomationTest 29 | -------------------------------------------------------------------------------- /.github/workflows/maven2.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Selenium Automation 5 | 6 | on: 7 | push: 8 | branches: 9 | - ci 10 | pull_request: 11 | branches: [ master ] 12 | 13 | jobs: 14 | build: 15 | 16 | runs-on: ubuntu-latest 17 | 18 | steps: 19 | - uses: actions/checkout@v2 20 | - name: Set up JDK 11 21 | uses: actions/setup-java@v1 22 | with: 23 | java-version: 11 24 | - name: Run acceptance tests 25 | env: 26 | SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }} 27 | SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }} 28 | run: mvn -X test 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | **/target 26 | 27 | ## IntelliJ ## 28 | .idea 29 | *.iml -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /workspace.xml -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | selenium.java -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020 Nikolay Advolodkin www.ultimateqa.com 2 | 3 | Permission to use, copy, modify, and distribute this software for any 4 | purpose with or without fee is hereby granted, provided that the above 5 | copyright notice and this permission notice appear in all copies. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Java CI with Maven](https://github.com/nadvolod/selenium-java/workflows/Java%20CI%20with%20Maven/badge.svg) 2 | [![Codacy Badge](https://api.codacy.com/project/badge/Grade/95a6a0b3fe3f418fb7ff035fac5d2f87)](https://app.codacy.com/manual/nadvolod/selenium-java?utm_source=github.com&utm_medium=referral&utm_content=nadvolod/selenium-java&utm_campaign=Badge_Grade_Dashboard) 3 | 4 | The Java Software Engineer in Test (SDET) curriculum is your ticket to a wonderful test automation career. 5 | 🌎 5 months of world-class training 6 | 7 | 💻 Gain real-world experience through automation with real customers 8 | 9 | 🚀 Build out your open source coding skills through group projects 10 | 11 | 💼 Interview training, on-the-job guidance, and live support to ensure your success in your new role. 12 | 13 | 📅 100% virtual education 14 | 15 | Join the ranks of the world's elite software developers and test automation experts 🌟 16 | 17 | 👉 Join now, be the first to know about the launch and enjoy exclusive benefits: https://ultimateqa.ck.page/academy-coming-soon 18 | 19 | # The Bootcamp that Will Make You Expert in 20 | 21 | 💪 Mastering automation testing from A to Z—be the go-to person to improve software quality 22 | 23 | 🚀 Unleashing the potential of using Selenium WebDriver for automation 24 | 25 | 🧠 Mastering Java for Software Automation Engineers—the way to definitely go pro 26 | 27 | 🏆 Implementing automation best practices established over decades—as the top professionals do 28 | 29 | 👍 Using ATDA—a secret automation technique to create top-quality tests 30 | 31 | 🦸‍♀️️Creating a test automation framework in less than 45 minutes 32 | 33 | ## Some Topics You Will Master 34 | 35 | ### How to achieve parallelization with JUnit and TestNg 36 | 37 | [![Parallel Test Execution with JUnit and TestNg](http://img.youtube.com/vi/ufccoaURMIc/0.jpg)](https://youtu.be/ufccoaURMIc "Parallel Test Execution with JUnit and TestNg") 38 | 39 | ### How to use the Page Object Model design pattern effectively 40 | 41 | ### Continuous integration with SauceLabs and GitHub 42 | 43 | ### Cross Browser and Cross Platform Automation using SauceLabs 44 | 45 | ### How to get awesome reports using the TestProject SDK 46 | 47 | ``` 48 | FIRST 150 PEOPLE GET 15% DISCOUNT! USE COUPON CODE github-15 AT CHECKOUT 49 | ``` 50 | [Complete Selenium WebDriver with Java Bootcamp](https://ultimateqa.com/selenium-webdriver-java-course/) 51 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | org.ultimateqa 7 | selenium.java 8 | 1.0-SNAPSHOT 9 | 10 | 11 | 12 | 13 | 14 | 15 | org.apache.maven.plugins 16 | maven-compiler-plugin 17 | 3.8.1 18 | 19 | 11 20 | 11 21 | 22 | 23 | 24 | org.apache.maven.plugins 25 | maven-surefire-plugin 26 | 3.0.0-M4 27 | 28 | all 29 | 10 30 | all 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.seleniumhq.selenium 39 | selenium-java 40 | 3.141.59 41 | test 42 | 43 | 44 | junit 45 | junit 46 | 4.13.1 47 | test 48 | 49 | 50 | 51 | io.github.bonigarcia 52 | webdrivermanager 53 | 4.0.0 54 | test 55 | 56 | 57 | org.apache.commons 58 | commons-lang3 59 | 3.10 60 | 61 | 62 | org.junit.jupiter 63 | junit-jupiter 64 | 5.5.2 65 | test 66 | 67 | 68 | au.com.bytecode 69 | opencsv 70 | 2.4 71 | test 72 | 73 | 74 | pl.pragmatists 75 | JUnitParams 76 | 1.1.0 77 | 78 | 79 | org.testng 80 | testng 81 | 7.4.0 82 | test 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /resources/mac/chromedriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nadvolod/selenium-java/dc0776d8e61811a1f2f53d5d0e4c3b223765f9b4/resources/mac/chromedriver -------------------------------------------------------------------------------- /resources/windows/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nadvolod/selenium-java/dc0776d8e61811a1f2f53d5d0e4c3b223765f9b4/resources/windows/chromedriver.exe -------------------------------------------------------------------------------- /src/test/java/HelloSelenium2Test.java: -------------------------------------------------------------------------------- 1 | import io.github.bonigarcia.wdm.WebDriverManager; 2 | import org.junit.BeforeClass; 3 | import org.junit.Test; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.chrome.ChromeDriver; 6 | 7 | 8 | public class HelloSelenium2Test { 9 | //This method will run once before all of the tests in our class 10 | @BeforeClass 11 | public static void setupClass() { 12 | WebDriverManager.chromedriver().setup(); 13 | } 14 | @Test 15 | public void smarterTest() 16 | { 17 | WebDriver driver = new ChromeDriver(); 18 | driver.get("https://www.saucedemo.com/"); 19 | driver.quit(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/HelloSelenium3Test.java: -------------------------------------------------------------------------------- 1 | import io.github.bonigarcia.wdm.WebDriverManager; 2 | import org.junit.Test; 3 | import org.openqa.selenium.WebDriver; 4 | import org.openqa.selenium.chrome.ChromeDriver; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | 9 | public class HelloSelenium3Test { 10 | @Test 11 | public void smarterTest() 12 | { 13 | //Use WebDriverManager Maven package to download the correct version of 14 | //a driver to automate Chrome browser 15 | WebDriverManager.chromedriver().setup(); 16 | //Set variable driver to an instance of ChromeDriver 17 | WebDriver driver = new ChromeDriver(); 18 | //Open the URL 19 | driver.get("https://www.saucedemo.com/"); 20 | //Assert that the title of the page = Swag Labs 21 | assertEquals("Swag Labs", driver.getTitle()); 22 | //Quit the browser 23 | driver.quit(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/HelloSeleniumTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | import org.openqa.selenium.WebDriver; 3 | import org.openqa.selenium.chrome.ChromeDriver; 4 | 5 | public class HelloSeleniumTest { 6 | @Test 7 | public void firstTest() 8 | { 9 | //Telling the system where to find chromedriver on Mac. 10 | //System.setProperty("webdriver.chrome.driver", "resources/chromedriver"); 11 | //Here's how to do it on Windows 12 | System.setProperty("webdriver.chrome.driver", "resources/windows/chromedriver.exe"); 13 | 14 | WebDriver driver = new ChromeDriver(); 15 | driver.get("https://www.saucedemo.com/"); 16 | driver.quit(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/test/java/SeleniumTest.java: -------------------------------------------------------------------------------- 1 | import io.github.bonigarcia.wdm.WebDriverManager; 2 | import org.junit.BeforeClass; 3 | import org.junit.Test; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.chrome.ChromeDriver; 6 | import org.openqa.selenium.safari.SafariDriver; 7 | 8 | 9 | public class SeleniumTest { 10 | 11 | // Navigate to this URL using another browser https://the-internet.herokuapp.com/ 12 | // Navigate to this URL https://example.cypress.io/ 13 | // Navigate to https://react-shopping-cart-67954.firebaseapp.com/ 14 | // Using WebDriverManager 15 | 16 | //This method will run once before all of the tests in our class 17 | // @BeforeClass 18 | // public static void setupClass() { 19 | // WebDriverManager.edgedriver().setup(); 20 | // } 21 | @Test 22 | public void safariTest() { 23 | WebDriver driver = new SafariDriver(); 24 | driver.get("https://the-internet.herokuapp.com/"); 25 | driver.quit(); 26 | } 27 | @Test 28 | public void cypressTest() throws InterruptedException { 29 | WebDriver driver = new SafariDriver(); 30 | driver.get("https://example.cypress.io/"); 31 | Thread.sleep(3000); 32 | driver.quit(); 33 | } 34 | @Test 35 | public void shoppingCartTest() throws InterruptedException { 36 | WebDriver driver = new SafariDriver(); 37 | driver.get("https://react-shopping-cart-67954.firebaseapp.com/"); 38 | Thread.sleep(3000); 39 | driver.quit(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/advanced/tips/PageLoadTest.java: -------------------------------------------------------------------------------- 1 | package advanced.tips; 2 | 3 | import io.github.bonigarcia.wdm.WebDriverManager; 4 | import org.apache.commons.lang3.SystemUtils; 5 | import org.junit.After; 6 | import org.junit.BeforeClass; 7 | import org.junit.Test; 8 | import org.openqa.selenium.WebDriver; 9 | import org.openqa.selenium.chrome.ChromeDriver; 10 | 11 | public class PageLoadTest { 12 | private WebDriver driver; 13 | 14 | //This method will run once before all of the tests in our class 15 | @BeforeClass 16 | public static void setupClass() { 17 | WebDriverManager.chromedriver().setup(); 18 | } 19 | 20 | @Test 21 | public void checkPageLoad() { 22 | driver = getDriver(); 23 | driver.navigate().to("https://www.saucedemo.com/"); 24 | } 25 | 26 | @After 27 | public void tearDown() { 28 | driver.quit(); 29 | } 30 | 31 | private WebDriver getDriver() { 32 | //Using WebDriverManager package, we are able to not worry about 33 | return new ChromeDriver(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/atda/AcceptanceTestDrivenAutomationTest.java: -------------------------------------------------------------------------------- 1 | package atda; 2 | 3 | import io.github.bonigarcia.wdm.WebDriverManager; 4 | import org.junit.After; 5 | import org.junit.Before; 6 | import org.junit.BeforeClass; 7 | import org.junit.Test; 8 | import org.openqa.selenium.WebDriver; 9 | import org.openqa.selenium.chrome.ChromeDriver; 10 | 11 | import static org.junit.Assert.assertTrue; 12 | 13 | public class AcceptanceTestDrivenAutomationTest { 14 | WebDriver driver; 15 | //This method will run once before all of the tests in our class 16 | @BeforeClass 17 | public static void setupClass() { 18 | WebDriverManager.chromedriver().setup(); 19 | } 20 | @Before 21 | public void setup() 22 | { 23 | driver = getDriver(); 24 | } 25 | @After 26 | public void cleanup() 27 | { 28 | driver.quit(); 29 | } 30 | @Test 31 | public void shouldOpen() 32 | { 33 | LoginPage loginPage = new LoginPage(driver); 34 | loginPage.open(); 35 | assertTrue(loginPage.isLoaded()); 36 | } 37 | 38 | @Test 39 | public void shouldLogin() 40 | { 41 | LoginPage loginPage = new LoginPage(driver); 42 | loginPage.open(); 43 | assertTrue(loginPage.isLoaded()); 44 | 45 | loginPage.login("standard_user", "secret_sauce"); 46 | assertTrue(new ProductsPage(driver).isLoaded()); 47 | } 48 | 49 | private WebDriver getDriver() { 50 | //Using WebDriverManager package, we dopn't have to not worry about 51 | //where the ChromeDriver comes from and if it's the latest 52 | return new ChromeDriver(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/atda/BasePage.java: -------------------------------------------------------------------------------- 1 | package atda; 2 | 3 | import org.openqa.selenium.WebDriver; 4 | import org.openqa.selenium.support.ui.WebDriverWait; 5 | 6 | public class BasePage { 7 | public final WebDriver driver; 8 | WebDriverWait wait; 9 | 10 | 11 | public BasePage(WebDriver driver) { 12 | this.driver = driver; 13 | wait = new WebDriverWait(driver, 10); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/atda/LoginPage.java: -------------------------------------------------------------------------------- 1 | package atda; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.support.ui.ExpectedConditions; 6 | 7 | public class LoginPage extends BasePage { 8 | 9 | public LoginPage(WebDriver driver) { 10 | super(driver); 11 | } 12 | 13 | public void open() { 14 | driver.get("http://www.saucedemo.com"); 15 | } 16 | 17 | public boolean isLoaded() { 18 | return wait.until(ExpectedConditions.presenceOfElementLocated(By.id("user-name"))).isDisplayed(); 19 | } 20 | 21 | public void login(String userName, String password) { 22 | driver.findElement(By.id("user-name")).sendKeys(userName); 23 | driver.findElement(By.id("password")).sendKeys(password); 24 | driver.findElement(By.className("btn_action")).submit(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/test/java/atda/ProductsPage.java: -------------------------------------------------------------------------------- 1 | package atda; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.support.ui.ExpectedConditions; 6 | import org.openqa.selenium.support.ui.WebDriverWait; 7 | 8 | public class ProductsPage extends BasePage{ 9 | public ProductsPage(WebDriver driver) { 10 | super(driver); 11 | } 12 | 13 | public boolean isLoaded() { 14 | return wait.until( 15 | ExpectedConditions.presenceOfElementLocated(By.id("inventory_filter_container"))).isDisplayed(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/best/practices/Duplication.java: -------------------------------------------------------------------------------- 1 | package best.practices; 2 | 3 | import org.junit.After; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.openqa.selenium.By; 7 | import org.openqa.selenium.WebDriver; 8 | import org.openqa.selenium.chrome.ChromeDriver; 9 | import org.openqa.selenium.support.ui.ExpectedConditions; 10 | import org.openqa.selenium.support.ui.WebDriverWait; 11 | 12 | import static org.junit.Assert.assertTrue; 13 | 14 | public class Duplication { 15 | WebDriver driver; 16 | @Before 17 | public void setup() 18 | { 19 | driver = getDriver(); 20 | } 21 | @After 22 | public void cleanup() 23 | { 24 | driver.quit(); 25 | } 26 | @Test 27 | public void test1() { 28 | driver.get("http://www.saucedemo.com"); 29 | driver.findElement(By.id("user-name")).sendKeys("standard_user"); 30 | driver.findElement(By.id("password")).sendKeys("secret_sauce"); 31 | driver.findElement(By.className("btn_action")).submit(); 32 | WebDriverWait wait = new WebDriverWait(driver, 10); 33 | boolean isDisplayed = wait.until( 34 | ExpectedConditions.presenceOfElementLocated(By.id("inventory_filter_container"))).isDisplayed(); 35 | assertTrue(isDisplayed); 36 | } 37 | @Test 38 | public void test2() { 39 | driver.get("http://www.saucedemo.com"); 40 | driver.findElement(By.id("user-name")).sendKeys("problem_user"); 41 | driver.findElement(By.id("password")).sendKeys("secret_sauce"); 42 | driver.findElement(By.className("btn_action")).submit(); 43 | WebDriverWait wait = new WebDriverWait(driver, 10); 44 | boolean isDisplayed = wait.until( 45 | ExpectedConditions.presenceOfElementLocated(By.id("inventory_filter_container"))).isDisplayed(); 46 | assertTrue(isDisplayed); 47 | } 48 | @Test 49 | public void test3() { 50 | open(); 51 | typeText(By.id("user-name"), "standard_user"); 52 | typeText(By.id("password"), "secret_sauce"); 53 | clickButton(By.className("btn_action")); 54 | boolean isDisplayed = waitUntilDisplayed(By.id("inventory_filter_container")); 55 | 56 | assertTrue(isDisplayed); 57 | } 58 | @Test 59 | public void test4() { 60 | open(); 61 | typeText(By.id("user-name"), "problem_user"); 62 | typeText(By.id("password"), "secret_sauce"); 63 | clickButton(By.className("btn_action")); 64 | boolean isDisplayed = waitUntilDisplayed(By.id("inventory_filter_container")); 65 | 66 | assertTrue(isDisplayed); 67 | } 68 | private boolean waitUntilDisplayed(By locator) { 69 | WebDriverWait wait = new WebDriverWait(driver, 10); 70 | return wait.until( 71 | ExpectedConditions.presenceOfElementLocated(locator)).isDisplayed(); 72 | } 73 | 74 | private void clickButton(By locator) { 75 | driver.findElement(locator).submit(); 76 | } 77 | 78 | private void typeText(By locator, String string) { 79 | driver.findElement(locator).sendKeys(string); 80 | } 81 | 82 | private void open() { 83 | driver.get("http://www.saucedemo.com"); 84 | } 85 | 86 | private WebDriver getDriver() { 87 | //Telling the system where to find chromedriver. On Windows you also need to add .exe 88 | System.setProperty("webdriver.chrome.driver", "resources/chromedriver"); 89 | return new ChromeDriver(); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/test/java/best/practices/LoginPage.java: -------------------------------------------------------------------------------- 1 | package best.practices; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | 7 | public class LoginPage { 8 | //Private driver for a page object so that it's never accessible to our tests 9 | private WebDriver driver; 10 | //Private element locator that is never open to our tests 11 | private WebElement getUserNameField() 12 | { 13 | return driver.findElement(By.id("user-name")); 14 | } 15 | //Public login method, what the user does! 16 | public void login(String userName, String password) { 17 | getUserNameField().sendKeys(userName); 18 | driver.findElement(By.id("password")).sendKeys(password); 19 | driver.findElement(By.className("btn_action")).submit(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/best/practices/PersonalInfoPage.java: -------------------------------------------------------------------------------- 1 | package best.practices; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | 7 | public class PersonalInfoPage { 8 | //Private driver for a page object so that it's never accessible to our tests 9 | private WebDriver driver; 10 | //Private element locator that is never open to our tests 11 | private WebElement getFirstNameField() 12 | { 13 | return driver.findElement(By.id("FAKE LOCATOR")); 14 | } 15 | //Public login method, what the user does! 16 | public void fillOutPersonalInformation() { 17 | getFirstNameField().sendKeys("firstName"); 18 | getLastNameField().sendKeys("firstName"); 19 | getZipCodeField().sendKeys("firstName"); 20 | } 21 | 22 | private WebElement getZipCodeField() { 23 | return driver.findElement(By.id("FAKE LOCATOR")); 24 | } 25 | private WebElement getLastNameField() { 26 | return driver.findElement(By.id("FAKE LOCATOR")); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/best/practices/ProductsPage.java: -------------------------------------------------------------------------------- 1 | package best.practices; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | 7 | public class ProductsPage { 8 | private WebDriver driver; 9 | private WebElement getShoppingCartElement() 10 | { 11 | return driver.findElement(By.id("BLABHLABHLJ")); 12 | } 13 | public void openShoppingCart() { 14 | getShoppingCartElement().click(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/best/practices/ShoppingCartPage.java: -------------------------------------------------------------------------------- 1 | package best.practices; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | 7 | public class ShoppingCartPage { 8 | //Private driver for a page object so that it's never accessible to our tests 9 | private WebDriver driver; 10 | //Private element locator that is never open to our tests 11 | private WebElement getCheckoutButton() 12 | { 13 | return driver.findElement(By.id("FAKE LOCATOR")); 14 | } 15 | //Public login method, what the user does! 16 | public void startCheckout() { 17 | getCheckoutButton().click(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/test/java/best/practices/Synchronization.java: -------------------------------------------------------------------------------- 1 | package best.practices; 2 | 3 | import org.junit.After; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.openqa.selenium.By; 7 | import org.openqa.selenium.WebDriver; 8 | import org.openqa.selenium.WebElement; 9 | import org.openqa.selenium.chrome.ChromeDriver; 10 | import org.openqa.selenium.support.ui.ExpectedConditions; 11 | import org.openqa.selenium.support.ui.WebDriverWait; 12 | 13 | import java.util.concurrent.TimeUnit; 14 | 15 | public class Synchronization { 16 | WebDriver driver; 17 | String elementExistsInDOM = "https://the-internet.herokuapp.com/dynamic_loading/1"; 18 | String elementRenderedAfter = "https://the-internet.herokuapp.com/dynamic_loading/2"; 19 | By locator = By.id("finish"); 20 | @Before 21 | public void setup() 22 | { 23 | driver = getDriver(); 24 | } 25 | @After 26 | public void cleanup() 27 | { 28 | driver.quit(); 29 | } 30 | @Test 31 | public void implicitWaitFindsHiddenElement() { 32 | driver.get(elementExistsInDOM); 33 | driver.findElement(locator); 34 | } 35 | @Test 36 | public void implicitWaitThrowsNoSuchElementException() { 37 | driver.get(elementRenderedAfter); 38 | driver.findElement(locator); 39 | } 40 | @Test 41 | public void configuredImplicitWait() { 42 | driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 43 | driver.get(elementRenderedAfter); 44 | driver.findElement(locator); 45 | } 46 | @Test(expected = org.openqa.selenium.TimeoutException.class) 47 | public void explicitWaitFixesImplicitWaitIssues() { 48 | driver.get(elementExistsInDOM); 49 | WebDriverWait wait = new WebDriverWait(driver, 5); 50 | wait.until(ExpectedConditions.visibilityOfElementLocated(locator)); 51 | } 52 | @Test 53 | public void explicitWaitWhenElementPresent() { 54 | driver.get(elementExistsInDOM); 55 | WebDriverWait wait = new WebDriverWait(driver, 5); 56 | wait.until(ExpectedConditions.presenceOfElementLocated(locator)); 57 | } 58 | @Test 59 | public void correctSynchronization() { 60 | driver.get(elementRenderedAfter); 61 | WebDriverWait wait = new WebDriverWait(driver, 5); 62 | WebElement startButton = 63 | wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("start"))).findElement(By.xpath("//button")); 64 | startButton.click(); 65 | WebElement helloWorldElement = 66 | wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("finish"))); 67 | } 68 | 69 | private WebDriver getDriver() { 70 | //Telling the system where to find chromedriver. On Windows you also need to add .exe 71 | System.setProperty("webdriver.chrome.driver", "resources/chromedriver"); 72 | return new ChromeDriver(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/csvDataDriven/CSVDataReader.java: -------------------------------------------------------------------------------- 1 | package csvDataDriven; 2 | 3 | import au.com.bytecode.opencsv.CSVReader; 4 | import io.github.bonigarcia.wdm.WebDriverManager; 5 | import org.junit.Assert; 6 | import org.junit.jupiter.api.AfterEach; 7 | import org.junit.jupiter.api.BeforeEach; 8 | import org.junit.jupiter.params.ParameterizedTest; 9 | import org.junit.jupiter.params.provider.MethodSource; 10 | import org.openqa.selenium.By; 11 | import org.openqa.selenium.WebDriver; 12 | import org.openqa.selenium.chrome.ChromeDriver; 13 | 14 | import java.io.FileReader; 15 | import java.io.IOException; 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | 19 | public class CSVDataReader { 20 | WebDriver driver; 21 | 22 | // We use throws IOException because this is the exception we might expect if the file is not found. 23 | // Alternatively, we can wrap all our code in a try-catch block, and cath the IOException. 24 | public static String[][] readCsvFile() throws IOException { 25 | // Here we actually read the CSV file. 26 | // We need to provide the file location, the separator used, the quotes character and the starting line. 27 | // The file is read as 0-based, and since we have a header line, we wil start at the second line, which means we pass 1 as parameter 28 | CSVReader csvReader = new CSVReader(new FileReader("src/test/java/csvDataDriven/Users.csv"), ',', '"', 1); 29 | String[] line; 30 | // Here we create a string list, which one element for each line of the CSV 31 | List userList = new ArrayList<>(); 32 | while ((line = csvReader.readNext()) != null) { 33 | userList.add(line); 34 | } 35 | // After we get the number of rows and columns, we create a new array which has 2 string values, 36 | // which will be the string data (username and password) from our file: 37 | int rows = userList.size(); 38 | int cols = userList.get(0).length; 39 | String[][] dataFromCSV = new String[rows][cols]; 40 | for (int i = 0; i < rows; i++) { 41 | String[] eachRow = userList.get(i); 42 | for (int j = 0; j < cols; j++) { 43 | dataFromCSV[i][j] = eachRow[j]; 44 | } 45 | } 46 | return dataFromCSV; 47 | } 48 | 49 | private WebDriver getDriver() { 50 | return new ChromeDriver(); 51 | } 52 | 53 | @BeforeEach 54 | public void setup() { 55 | WebDriverManager.chromedriver().setup(); 56 | driver = getDriver(); 57 | driver.get("https://www.saucedemo.com/"); 58 | } 59 | 60 | @AfterEach 61 | public void tearDown() { 62 | driver.quit(); 63 | } 64 | 65 | @MethodSource("readCsvFile") 66 | @ParameterizedTest 67 | public void loginTest(String userName, String password) { 68 | driver.findElement(By.id("user-name")).sendKeys(userName); 69 | driver.findElement(By.id("password")).sendKeys(password); 70 | driver.findElement(By.id("login-button")).click(); 71 | Assert.assertTrue(driver.findElement(By.className("title")).isDisplayed()); 72 | } 73 | } -------------------------------------------------------------------------------- /src/test/java/csvDataDriven/CSVExercise.java: -------------------------------------------------------------------------------- 1 | package csvDataDriven; 2 | 3 | import au.com.bytecode.opencsv.CSVReader; 4 | import io.github.bonigarcia.wdm.WebDriverManager; 5 | import org.junit.Assert; 6 | import org.junit.jupiter.api.AfterEach; 7 | import org.junit.jupiter.api.BeforeEach; 8 | import org.junit.jupiter.params.ParameterizedTest; 9 | import org.junit.jupiter.params.provider.MethodSource; 10 | import org.openqa.selenium.By; 11 | import org.openqa.selenium.WebDriver; 12 | import org.openqa.selenium.WebElement; 13 | import org.openqa.selenium.chrome.ChromeDriver; 14 | 15 | import java.io.FileReader; 16 | import java.io.IOException; 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | public class CSVExercise { 21 | WebDriver driver; 22 | 23 | public static String[][] readCsvFile() throws IOException { 24 | CSVReader csvReader = new CSVReader(new FileReader("src/test/java/csvDataDriven/Prices.csv"), ',', '"', 1); 25 | String[] line; 26 | List userList = new ArrayList<>(); 27 | while ((line = csvReader.readNext()) != null) { 28 | userList.add(line); 29 | } 30 | int rows = userList.size(); 31 | int cols = userList.get(0).length; 32 | String[][] dataFromCSV = new String[rows][cols]; 33 | for (int i = 0; i < rows; i++) { 34 | String[] eachRow = userList.get(i); 35 | for (int j = 0; j < cols; j++) { 36 | dataFromCSV[i][j] = eachRow[j]; 37 | } 38 | } 39 | return dataFromCSV; 40 | } 41 | 42 | private WebDriver getDriver() { 43 | return new ChromeDriver(); 44 | } 45 | 46 | @BeforeEach 47 | public void setup() { 48 | WebDriverManager.chromedriver().setup(); 49 | driver = getDriver(); 50 | driver.get("https://www.saucedemo.com/"); 51 | driver.findElement(By.id("user-name")).sendKeys("standard_user"); 52 | driver.findElement(By.id("password")).sendKeys("secret_sauce"); 53 | driver.findElement(By.id("login-button")).click(); 54 | } 55 | 56 | @AfterEach 57 | public void tearDown() { 58 | driver.quit(); 59 | } 60 | 61 | @MethodSource("readCsvFile") 62 | @ParameterizedTest 63 | public void loginTest(String itemName, String price) { 64 | // The xPath for the element is a bit complex. Basically what we are looking for is the div that contains the product price 65 | // and has the same ancestor as the div that contains the itemName (so we know we are checking the price for our specific product) 66 | String xPath = String.format 67 | ("//div[contains(text(),'%s')]/ancestor::div[@class='inventory_item_description']//div[@class='inventory_item_price']", 68 | itemName); 69 | WebElement priceElement = driver.findElement(By.xpath(xPath)); 70 | System.out.println(priceElement.getText()); 71 | Assert.assertEquals(priceElement.getText(), price); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/csvDataDriven/Prices.csv: -------------------------------------------------------------------------------- 1 | Item Name,Price 2 | Sauce Labs Backpack,$29.99 3 | Sauce Labs Bike Light,$9.99 4 | Sauce Labs Bolt T-Shirt,$15.99 5 | Sauce Labs Fleece Jacket,$49.99 6 | Sauce Labs Onesie,$7.99 7 | Test.allTheThings() T-Shirt (Red),$15.99 -------------------------------------------------------------------------------- /src/test/java/csvDataDriven/README.md: -------------------------------------------------------------------------------- 1 | # Data Driven Testing 2 | 3 | ## How to run the tests 4 | 5 | ```bash 6 | cd selenium-java 7 | mvn test -Dtest=CSVDataReader -X 8 | ``` 9 | -------------------------------------------------------------------------------- /src/test/java/csvDataDriven/Users.csv: -------------------------------------------------------------------------------- 1 | Username, Password 2 | standard_user,secret_sauce 3 | problem_user,secret_sauce 4 | performance_glitch_user,secret_sauce -------------------------------------------------------------------------------- /src/test/java/enumDataDriven/EnumDataDrivenTests.java: -------------------------------------------------------------------------------- 1 | package enumDataDriven; 2 | 3 | import io.github.bonigarcia.wdm.WebDriverManager; 4 | import org.junit.Assert; 5 | import org.junit.jupiter.api.AfterEach; 6 | import org.junit.jupiter.api.BeforeEach; 7 | import org.junit.jupiter.params.ParameterizedTest; 8 | import org.junit.jupiter.params.provider.EnumSource; 9 | import org.openqa.selenium.By; 10 | import org.openqa.selenium.WebDriver; 11 | import org.openqa.selenium.chrome.ChromeDriver; 12 | 13 | public class EnumDataDrivenTests { 14 | WebDriver driver; 15 | 16 | private WebDriver getDriver() { 17 | return new ChromeDriver(); 18 | } 19 | 20 | @BeforeEach 21 | public void setup() { 22 | WebDriverManager.chromedriver().setup(); 23 | driver = getDriver(); 24 | driver.get("https://www.saucedemo.com/"); 25 | } 26 | 27 | @AfterEach 28 | public void tearDown() { 29 | driver.quit(); 30 | } 31 | 32 | // Using the @ParameterizedTest annotation, we let our test know it will run multiple times with different parameters 33 | // The @EnumSource lets the test know that the sourse is the Enum class provided as a parameter 34 | @ParameterizedTest 35 | @EnumSource(User.class) 36 | public void loginTest(User user) { 37 | driver.findElement(By.id("user-name")).sendKeys(user.toString()); 38 | driver.findElement(By.id("password")).sendKeys("secret_sauce"); 39 | driver.findElement(By.id("login-button")).click(); 40 | Assert.assertTrue(driver.findElement(By.className("title")).isDisplayed()); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/enumDataDriven/EnumExercise.java: -------------------------------------------------------------------------------- 1 | package enumDataDriven; 2 | 3 | import io.github.bonigarcia.wdm.WebDriverManager; 4 | import org.junit.jupiter.api.AfterEach; 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.params.ParameterizedTest; 7 | import org.junit.jupiter.params.provider.EnumSource; 8 | import org.openqa.selenium.By; 9 | import org.openqa.selenium.WebDriver; 10 | import org.openqa.selenium.chrome.ChromeDriver; 11 | import org.openqa.selenium.support.ui.Select; 12 | 13 | public class EnumExercise { 14 | WebDriver driver; 15 | 16 | private WebDriver getDriver() { 17 | return new ChromeDriver(); 18 | } 19 | 20 | @BeforeEach 21 | public void setup() { 22 | WebDriverManager.chromedriver().setup(); 23 | driver = getDriver(); 24 | driver.get("https://www.saucedemo.com/"); 25 | driver.findElement(By.id("user-name")).sendKeys("standard_user"); 26 | driver.findElement(By.id("password")).sendKeys("secret_sauce"); 27 | driver.findElement(By.id("login-button")).click(); 28 | } 29 | 30 | @AfterEach 31 | public void tearDown() { 32 | driver.quit(); 33 | } 34 | 35 | @ParameterizedTest 36 | @EnumSource(SortValue.class) 37 | public void sortingTest(SortValue value) { 38 | Select sortDropdown = new Select (driver.findElement(By.className("product_sort_container"))); 39 | sortDropdown.selectByValue(value.toString()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/enumDataDriven/SortValue.java: -------------------------------------------------------------------------------- 1 | package enumDataDriven; 2 | 3 | public enum SortValue { 4 | az, 5 | za, 6 | lohi, 7 | hilo 8 | } 9 | -------------------------------------------------------------------------------- /src/test/java/enumDataDriven/User.java: -------------------------------------------------------------------------------- 1 | package enumDataDriven; 2 | 3 | // This class provides the values for our users 4 | public enum User { 5 | standard_user, 6 | problem_user, 7 | performance_glitch_user 8 | } 9 | -------------------------------------------------------------------------------- /src/test/java/locating/elements/LocatingElementsTest.java: -------------------------------------------------------------------------------- 1 | package locating.elements; 2 | 3 | import io.github.bonigarcia.wdm.WebDriverManager; 4 | import org.junit.BeforeClass; 5 | import org.junit.Test; 6 | import org.openqa.selenium.By; 7 | import org.openqa.selenium.WebDriver; 8 | import org.openqa.selenium.WebElement; 9 | import org.openqa.selenium.chrome.ChromeDriver; 10 | import org.openqa.selenium.support.ui.ExpectedConditions; 11 | import org.openqa.selenium.support.ui.WebDriverWait; 12 | 13 | import static org.junit.Assert.assertTrue; 14 | 15 | public class LocatingElementsTest { 16 | @BeforeClass 17 | public static void setupClass() { 18 | WebDriverManager.chromedriver().setup(); 19 | } 20 | 21 | @Test 22 | public void elementsQuiz1() 23 | { 24 | //1. Instantiate the driver 25 | WebDriver driver = new ChromeDriver(); 26 | //2. navigate to the URL 27 | driver.get("https://www.saucedemo.com/"); 28 | //3. Find element //4. check the state 29 | WebDriverWait wait = new WebDriverWait(driver, 10); 30 | WebElement element = wait.until( 31 | ExpectedConditions.presenceOfElementLocated(By.id("user-name"))); 32 | //5. take action //6. record the result 33 | assertTrue(element.isDisplayed()); 34 | //7. quit the driver 35 | driver.quit(); 36 | } 37 | @Test 38 | public void typesOfLocators() 39 | { 40 | //1. Instantiate the driver 41 | WebDriver driver = new ChromeDriver(); 42 | //2. navigate to the URL 43 | driver.get("https://www.saucedemo.com/"); 44 | //3. Find element 45 | WebElement element; 46 | //ID 47 | element = driver.findElement(By.id("user-name")); 48 | //Name 49 | //driver.findElement(By.name("name of locator")); 50 | //Class name 51 | driver.findElement(By.className("form_input")); 52 | //Tag name 53 | driver.findElement(By.tagName("input")); 54 | //Css selector 55 | //#user-name 56 | driver.findElement(By.cssSelector("#user-name")); 57 | //Xpath 58 | // //*[@id="user-name"] 59 | driver.findElement(By.xpath("//*[@id=\"user-name\"]")); 60 | driver.quit(); 61 | 62 | //1. Instantiate the driver 63 | driver = new ChromeDriver(); 64 | //2. navigate to the URL 65 | driver.get("https://ultimateqa.com/simple-html-elements-for-automation/"); 66 | //Link text 67 | driver.findElement(By.linkText("Click me using this link text!")); 68 | //Partial link text 69 | driver.findElement(By.partialLinkText("link text!")); 70 | //7. quit the driver 71 | driver.quit(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/testNGDataDriven/TestNGDataDrivenExercise.java: -------------------------------------------------------------------------------- 1 | package testNGDataDriven; 2 | 3 | import io.github.bonigarcia.wdm.WebDriverManager; 4 | 5 | import org.openqa.selenium.By; 6 | import org.openqa.selenium.WebDriver; 7 | import org.openqa.selenium.WebElement; 8 | import org.openqa.selenium.chrome.ChromeDriver; 9 | import org.testng.Assert; 10 | import org.testng.annotations.AfterMethod; 11 | import org.testng.annotations.BeforeMethod; 12 | import org.testng.annotations.Test; 13 | 14 | public class TestNGDataDrivenExercise { 15 | 16 | WebDriver driver; 17 | private WebDriver getDriver() { 18 | return new ChromeDriver(); 19 | } 20 | 21 | @BeforeMethod 22 | public void setup() { 23 | WebDriverManager.chromedriver().setup(); 24 | driver = getDriver(); 25 | driver.get("https://www.saucedemo.com/"); 26 | driver.findElement(By.id("user-name")).sendKeys("standard_user"); 27 | driver.findElement(By.id("password")).sendKeys("secret_sauce"); 28 | driver.findElement(By.id("login-button")).click(); 29 | } 30 | 31 | @AfterMethod 32 | public void tearDown() { 33 | driver.quit(); 34 | } 35 | 36 | // The test method is the same as in the previous examples, 37 | // except with TestNG we pass the data provider method like this: 38 | @Test (dataProvider = "provideProductData") 39 | public void priceCheckTest(String itemName, String price) { 40 | // The xPath for the element is a bit complex. Basically what we are looking for is the div that contains the product price 41 | // and has the same ancestor as the div that contains the itemName (so we know we are checking the price for our specific product) 42 | String xPath = String.format 43 | ("//div[contains(text(),'%s')]/ancestor::div[@class='inventory_item_description']//div[@class='inventory_item_price']", 44 | itemName); 45 | WebElement priceElement = driver.findElement(By.xpath(xPath)); 46 | System.out.println(priceElement.getText()); 47 | Assert.assertEquals(priceElement.getText(), price); 48 | } 49 | 50 | // This time the objects returned have 2 values: 51 | // One for the product name, and one for the price 52 | @org.testng.annotations.DataProvider(name = "provideProductData") 53 | public static Object[][] provideProductData() { 54 | return new Object[][]{ 55 | new Object[]{"Sauce Labs Backpack", "$29.99"}, 56 | new Object[]{"Sauce Labs Bike Light", "$9.99"}, 57 | new Object[]{"Sauce Labs Bolt T-Shirt", "$15.99"}, 58 | new Object[]{"Sauce Labs Fleece Jacket", "$49.99"}, 59 | new Object[]{"Sauce Labs Onesie", "$7.99"}, 60 | new Object[]{"Test.allTheThings() T-Shirt (Red)", "$15.99"} 61 | }; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/test/java/testNGDataDriven/UserLoginTests.java: -------------------------------------------------------------------------------- 1 | package testNGDataDriven; 2 | 3 | import io.github.bonigarcia.wdm.WebDriverManager; 4 | import org.openqa.selenium.By; 5 | import org.openqa.selenium.WebDriver; 6 | import org.openqa.selenium.chrome.ChromeDriver; 7 | import org.testng.Assert; 8 | import org.testng.annotations.AfterMethod; 9 | import org.testng.annotations.BeforeMethod; 10 | import org.testng.annotations.Test; 11 | 12 | public class UserLoginTests { 13 | WebDriver driver; 14 | 15 | private WebDriver getDriver() { 16 | return new ChromeDriver(); 17 | } 18 | 19 | // Pay extra attention to the annotations, which are a bit different than the JUnit ones 20 | @BeforeMethod 21 | public void setup() { 22 | WebDriverManager.chromedriver().setup(); 23 | driver = getDriver(); 24 | driver.get("https://www.saucedemo.com/"); 25 | } 26 | 27 | @AfterMethod 28 | public void tearDown() { 29 | driver.quit(); 30 | } 31 | 32 | // The test is the same as before 33 | // We pass 2 string parameters for username and password 34 | // which are returned by the data provider method 35 | @Test (dataProvider = "DataProvider") 36 | public void loginTest(String userName, String password) { 37 | driver.findElement(By.id("user-name")).sendKeys(userName); 38 | driver.findElement(By.id("password")).sendKeys(password); 39 | driver.findElement(By.id("login-button")).click(); 40 | Assert.assertTrue(driver.findElement(By.className("title")).isDisplayed()); 41 | } 42 | 43 | // This is the data provider method, which returns user objects 44 | // with 2 string values: username and password 45 | @org.testng.annotations.DataProvider(name = "DataProvider") 46 | public Object[][] provideUserData() { 47 | return new Object[][] { 48 | new Object[] {"standard_user", "secret_sauce"}, 49 | new Object[] {"performance_glitch_user", "secret_sauce"}, 50 | new Object[] {"problem_user", "secret_sauce"} 51 | }; 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /src/test/java/testingClassData/DataProviderExercise.java: -------------------------------------------------------------------------------- 1 | package testingClassData; 2 | 3 | import io.github.bonigarcia.wdm.WebDriverManager; 4 | import junitparams.JUnitParamsRunner; 5 | import junitparams.Parameters; 6 | import org.junit.After; 7 | import org.junit.Assert; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.openqa.selenium.By; 12 | import org.openqa.selenium.WebDriver; 13 | import org.openqa.selenium.WebElement; 14 | import org.openqa.selenium.chrome.ChromeDriver; 15 | 16 | @RunWith(JUnitParamsRunner.class) 17 | public class DataProviderExercise { 18 | WebDriver driver; 19 | 20 | private WebDriver getDriver() { 21 | return new ChromeDriver(); 22 | } 23 | 24 | @Before 25 | public void setup() { 26 | WebDriverManager.chromedriver().setup(); 27 | driver = getDriver(); 28 | driver.get("https://www.saucedemo.com/"); 29 | driver.findElement(By.id("user-name")).sendKeys("standard_user"); 30 | driver.findElement(By.id("password")).sendKeys("secret_sauce"); 31 | driver.findElement(By.id("login-button")).click(); 32 | } 33 | 34 | @After 35 | public void tearDown() { 36 | driver.quit(); 37 | } 38 | 39 | @Test 40 | @Parameters(source = ProductDataProvider.class) 41 | public void priceCheckTest(String itemName, String price) { 42 | // The xPath for the element is a bit complex. Basically what we are looking for is the div that contains the product price 43 | // and has the same ancestor as the div that contains the itemName (so we know we are checking the price for our specific product) 44 | String xPath = String.format 45 | ("//div[contains(text(),'%s')]/ancestor::div[@class='inventory_item_description']//div[@class='inventory_item_price']", 46 | itemName); 47 | WebElement priceElement = driver.findElement(By.xpath(xPath)); 48 | System.out.println(priceElement.getText()); 49 | Assert.assertEquals(priceElement.getText(), price); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/testingClassData/ProductDataProvider.java: -------------------------------------------------------------------------------- 1 | package testingClassData; 2 | 3 | public class ProductDataProvider { 4 | public static Object[] provideUserData() { 5 | return new Object[] { 6 | new Object[] {"Sauce Labs Backpack", "$29.99"}, 7 | new Object[] {"Sauce Labs Bike Light", "$9.99"}, 8 | new Object[] {"Sauce Labs Bolt T-Shirt", "$15.99"}, 9 | new Object[] {"Sauce Labs Fleece Jacket", "$49.99"}, 10 | new Object[] {"Sauce Labs Onesie", "$7.99"}, 11 | new Object[] {"Test.allTheThings() T-Shirt (Red)", "$15.99"} 12 | }; 13 | } 14 | } -------------------------------------------------------------------------------- /src/test/java/testingClassData/UserDataProvider.java: -------------------------------------------------------------------------------- 1 | package testingClassData; 2 | 3 | // This class provides the data for our tests 4 | // It returns 3 different objects, with 2 String attributes (in our case, the 2 attributes are the username and the password) 5 | public class UserDataProvider { 6 | public static Object[] provideUserData() { 7 | return new Object[] { 8 | new Object[] {"standard_user", "secret_sauce"}, 9 | new Object[] {"performance_glitch_user", "secret_sauce"}, 10 | new Object[] {"problem_user", "secret_sauce"} 11 | }; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/testingClassData/UserLoginTests.java: -------------------------------------------------------------------------------- 1 | package testingClassData; 2 | 3 | import io.github.bonigarcia.wdm.WebDriverManager; 4 | import junitparams.JUnitParamsRunner; 5 | import junitparams.Parameters; 6 | import org.junit.After; 7 | import org.junit.Assert; 8 | import org.junit.Before; 9 | import org.junit.Test; 10 | import org.junit.runner.RunWith; 11 | import org.openqa.selenium.By; 12 | import org.openqa.selenium.WebDriver; 13 | import org.openqa.selenium.chrome.ChromeDriver; 14 | 15 | // We need to add an annotation to the class to instruct it that the tests will run using parameters 16 | @RunWith(JUnitParamsRunner.class) 17 | public class UserLoginTests { 18 | 19 | WebDriver driver; 20 | 21 | private WebDriver getDriver() { 22 | return new ChromeDriver(); 23 | } 24 | 25 | @Before 26 | public void setup() { 27 | WebDriverManager.chromedriver().setup(); 28 | driver = getDriver(); 29 | driver.get("https://www.saucedemo.com/"); 30 | } 31 | 32 | @After 33 | public void tearDown() { 34 | driver.quit(); 35 | } 36 | 37 | // In our test, we simply need to pass the data provider class to let the test know where the parameters come from: 38 | @Test 39 | @Parameters(source = UserDataProvider.class) 40 | public void loginTest(String userName, String password) { 41 | driver.findElement(By.id("user-name")).sendKeys(userName); 42 | driver.findElement(By.id("password")).sendKeys(password); 43 | driver.findElement(By.id("login-button")).click(); 44 | Assert.assertTrue(driver.findElement(By.className("title")).isDisplayed()); 45 | } 46 | } 47 | --------------------------------------------------------------------------------