├── README.md ├── pom.xml ├── src ├── main │ └── java │ │ └── com │ │ └── udemy │ │ ├── calculator │ │ ├── Calculator.java │ │ ├── MathOperation.java │ │ └── TestMain.java │ │ └── java │ │ ├── TestMain.java │ │ ├── assignment │ │ ├── FirstNameAssignment.java │ │ └── PriceTable.java │ │ ├── compare │ │ └── Student.java │ │ ├── datatype │ │ ├── PrimitiveType.java │ │ └── ReferenceType.java │ │ ├── interfacepolymorphism │ │ ├── Alarm.java │ │ ├── Clock.java │ │ ├── GoogleMini.java │ │ └── IPhone.java │ │ ├── lambda │ │ ├── GreetingService.java │ │ └── StringOperations.java │ │ ├── pages │ │ └── TableDemoPage.java │ │ ├── polymorphism │ │ ├── Animal.java │ │ ├── AreaCalculator.java │ │ ├── Cat.java │ │ ├── Dog.java │ │ └── Horse.java │ │ ├── predicate │ │ └── Rules.java │ │ ├── supplier │ │ └── DriverFactory.java │ │ └── util │ │ └── LinkUtil.java └── test │ └── java │ └── com │ └── udemy │ └── java │ └── test │ ├── BrokenLinkTest.java │ ├── CheckboxSelectionTest.java │ ├── DriverTest.java │ ├── HoverableDropdownTest.java │ ├── PriceTableTest.java │ └── SearchCriteriaFactory.java └── udemy-java.iml /README.md: -------------------------------------------------------------------------------- 1 | # java-8-and-beyond 2 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.udemy.java 8 | udemy-java 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | 14 | org.seleniumhq.selenium 15 | selenium-java 16 | 3.141.59 17 | 18 | 19 | 20 | org.testng 21 | testng 22 | 6.14.3 23 | test 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/calculator/Calculator.java: -------------------------------------------------------------------------------- 1 | package com.udemy.calculator; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class Calculator { 7 | 8 | private static final Map map = new HashMap<>(); 9 | 10 | static { 11 | map.put("+", (a, b) -> a + b); 12 | map.put("-", (a, b) -> a - b); 13 | map.put("*", (a, b) -> a * b); 14 | map.put("/", (a, b) -> a / b); 15 | } 16 | 17 | public static void addOperation(String key, MathOperation mathOperation){ 18 | map.put(key, mathOperation); 19 | } 20 | 21 | public static int calculate(String expression){ 22 | String[] exps = expression.split(" "); 23 | int onScreenNumber = Integer.parseInt(exps[0]); 24 | for (int i = 1; i < exps.length; i = i + 2) { 25 | MathOperation op = map.get(exps[i]); 26 | int enteredNumber = Integer.parseInt(exps[i+1]); 27 | onScreenNumber = calculate(onScreenNumber, op, enteredNumber); 28 | } 29 | return onScreenNumber; 30 | } 31 | 32 | private static int calculate(int onScreenNumber, MathOperation mathOperation, int enteredNumber){ 33 | return mathOperation.operate(onScreenNumber, enteredNumber); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/calculator/MathOperation.java: -------------------------------------------------------------------------------- 1 | package com.udemy.calculator; 2 | 3 | @FunctionalInterface 4 | public interface MathOperation { 5 | int operate(int a, int b); 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/calculator/TestMain.java: -------------------------------------------------------------------------------- 1 | package com.udemy.calculator; 2 | 3 | 4 | import java.util.List; 5 | import java.util.function.Consumer; 6 | 7 | public class TestMain { 8 | 9 | @FunctionalInterface 10 | public interface TriConsumer{ 11 | void accept(String a, String b, int c); 12 | } 13 | 14 | public interface Person{} 15 | 16 | public static void main(String[] args) { 17 | 18 | String exp = "9 * 9 - 1 * 7 / 8 + 30 ^ 2 % 3"; 19 | 20 | Calculator.addOperation("^", (a, b) -> (int) Math.pow(a, b)); 21 | Calculator.addOperation("%", (a, b) -> a % b); 22 | 23 | System.out.println( 24 | 25 | 26 | Calculator.calculate(exp) 27 | 28 | 29 | ); 30 | 31 | 32 | TriConsumer personProperties = (firstName, lastName, age) -> { }; 33 | 34 | Consumer person = (p) -> { }; 35 | 36 | 37 | 38 | 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/TestMain.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java; 2 | 3 | 4 | public class TestMain { 5 | 6 | public static void main(String[] args) { 7 | 8 | // to quickly test 9 | 10 | 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/assignment/FirstNameAssignment.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.assignment; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.Files; 5 | import java.nio.file.Path; 6 | import java.nio.file.Paths; 7 | import java.util.Comparator; 8 | import java.util.List; 9 | import java.util.stream.Collectors; 10 | 11 | public class FirstNameAssignment { 12 | 13 | public static void main(String[] args) throws IOException { 14 | 15 | // /home/qa/Downloads/udemy-java/first-names.txt 16 | Path path = Paths.get("/home/qa/Downloads/udemy-java/first-names.txt"); 17 | List list = Files.readAllLines(path); 18 | 19 | // print the count of names which start with B 20 | System.out.println( 21 | 22 | list.stream() 23 | .filter(name -> name.startsWith("B")) 24 | .count() 25 | 26 | ); 27 | 28 | // create a list of names which start with C and contains 's' in it 29 | List names = list.stream() 30 | .filter(name -> name.startsWith("C")) 31 | .filter(name -> name.toLowerCase().contains("s")) 32 | .collect(Collectors.toList()); 33 | 34 | System.out.println(names.size()); 35 | 36 | 37 | // print the total number of chars for all the names starting with M 38 | System.out.println( 39 | 40 | list.stream() 41 | .filter(name -> name.startsWith("M")) 42 | .map(String::trim) 43 | .map(String::length) 44 | .mapToInt(i -> i) 45 | .sum() 46 | 47 | 48 | ); 49 | 50 | 51 | // Jo-Ann => Jo Ann 52 | // find the names containing - in it and replace it with a space; collect them into a list 53 | System.out.println( 54 | 55 | list.stream() 56 | .filter(name -> name.contains("-")) 57 | .map(name -> name.replaceAll("-", " ")) 58 | .collect(Collectors.toList()) 59 | 60 | 61 | ); 62 | 63 | 64 | // find the name which has more number of chars 65 | 66 | System.out.println( 67 | 68 | list.stream() 69 | .max(Comparator.comparing(s -> s.length())) 70 | .get() 71 | 72 | ); 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/assignment/PriceTable.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.assignment; 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 | 9 | import java.util.Comparator; 10 | import java.util.List; 11 | import java.util.Optional; 12 | 13 | public class PriceTable { 14 | 15 | private final WebDriver driver; 16 | 17 | @FindBy(css = "table#prods tr") 18 | private List rows; 19 | 20 | @FindBy(id = "result") 21 | private WebElement verifyButton; 22 | 23 | @FindBy(id = "status") 24 | private WebElement status; 25 | 26 | public PriceTable(WebDriver driver){ 27 | this.driver = driver; 28 | this.driver.get("https://vins-udemy.s3.amazonaws.com/java/html/java8-stream-table-price.html"); 29 | PageFactory.initElements(driver, this); 30 | } 31 | 32 | //100 2 33 | public void selectMinPriceRow(){ 34 | Optional> minRow = rows.stream() //tr 35 | .skip(1) 36 | .map(tr -> tr.findElements(By.tagName("td"))) //List 37 | .min(Comparator.comparing(tdList -> Integer.parseInt(tdList.get(2).getText()))); 38 | if(minRow.isPresent()){ 39 | List cells = minRow.get(); 40 | cells.get(3).findElement(By.tagName("input")).click(); 41 | } 42 | this.verifyButton.click(); 43 | } 44 | 45 | public String getStatus(){ 46 | return this.status.getText().trim(); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/compare/Student.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.compare; 2 | 3 | public class Student { 4 | 5 | private String name; 6 | private int score; 7 | private int height; 8 | 9 | public Student(String name, int score, int height){ 10 | this.name = name; 11 | this.score = score; 12 | this.height = height; 13 | } 14 | 15 | public String getName() { 16 | return name; 17 | } 18 | 19 | public void setName(String name) { 20 | this.name = name; 21 | } 22 | 23 | public int getScore() { 24 | return score; 25 | } 26 | 27 | public void setScore(int score) { 28 | this.score = score; 29 | } 30 | 31 | public int getHeight() { 32 | return height; 33 | } 34 | 35 | public void setHeight(int height) { 36 | this.height = height; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "Student{" + 42 | "name='" + name + '\'' + 43 | ", score=" + score + 44 | ", height=" + height + 45 | '}'; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/datatype/PrimitiveType.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.datatype; 2 | 3 | public class PrimitiveType { 4 | 5 | public static void main(String[] args) { 6 | 7 | int i = 5; 8 | 9 | System.out.println("Before :: " + i); 10 | change(i); 11 | System.out.println("After :: " + i); 12 | 13 | 14 | 15 | } 16 | 17 | //5 18 | private static void change(int a){ 19 | //6 20 | a++; //a=a+1 21 | } 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/datatype/ReferenceType.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.datatype; 2 | 3 | import java.util.Arrays; 4 | 5 | public class ReferenceType { 6 | public static void main(String[] args) { 7 | 8 | //abc123 9 | int[] arr = new int[] {1, 2, 3}; 10 | 11 | 12 | predict(arr); 13 | 14 | totalSale(arr); 15 | 16 | } 17 | 18 | //abc123 19 | private static void predict(int[] a){ 20 | //def23 21 | a = Arrays.copyOf(a, a.length); 22 | a[0]++; 23 | a[1]++; 24 | System.out.println("Prediction for next month :: " + (a[0] + a[1] + a[2])); 25 | } 26 | 27 | //abc123 28 | private static void totalSale(int[] a){ 29 | 30 | //abc123 31 | System.out.println("Total Sale for this month :: " + (a[0] + a[1] + a[2])); 32 | 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/interfacepolymorphism/Alarm.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.interfacepolymorphism; 2 | 3 | public interface Alarm { 4 | 5 | void setAlarm(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/interfacepolymorphism/Clock.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.interfacepolymorphism; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | public class Clock implements Alarm { 6 | 7 | public void showTime(){ 8 | System.out.println("It is " + LocalDateTime.now()); 9 | } 10 | 11 | public void setAlarm(){ 12 | System.out.println("Clock - Setting an alarm @7:30AM"); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/interfacepolymorphism/GoogleMini.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.interfacepolymorphism; 2 | 3 | public class GoogleMini implements Alarm{ 4 | 5 | public void ask(){ 6 | System.out.println("Hows the weather outside?"); 7 | } 8 | 9 | public void setAlarm(){ 10 | System.out.println("Google Mini - Setting an alarm @7:30AM"); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/interfacepolymorphism/IPhone.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.interfacepolymorphism; 2 | 3 | public class IPhone implements Alarm { 4 | 5 | public void dial(){ 6 | System.out.println("Caling 123..."); 7 | } 8 | 9 | public void answerPhone(){ 10 | System.out.println("Hello"); 11 | } 12 | 13 | public void setAlarm(){ 14 | System.out.println("IPhone - Setting an alarm @7:30AM"); 15 | } 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/lambda/GreetingService.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.lambda; 2 | 3 | // SAM - single abstract method 4 | 5 | @FunctionalInterface 6 | public interface GreetingService { 7 | String greet(String firstName); 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/lambda/StringOperations.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.lambda; 2 | 3 | @FunctionalInterface 4 | public interface StringOperations { 5 | void accept(String s); 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/pages/TableDemoPage.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.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 | import java.util.function.Predicate; 9 | 10 | public class TableDemoPage { 11 | 12 | private final WebDriver driver; 13 | 14 | public TableDemoPage(WebDriver driver){ 15 | this.driver = driver; 16 | } 17 | 18 | public void goTo(){ 19 | this.driver.get("https://vins-udemy.s3.amazonaws.com/java/html/java8-stream-table-1.html"); 20 | } 21 | 22 | public void selectRows(Predicate> predicate){ 23 | this.driver.findElements(By.tagName("tr")) // rows 24 | .stream() 25 | .skip(1) 26 | .map(tr -> tr.findElements(By.tagName("td"))) // td List 27 | .filter(tdList -> tdList.size() == 4) 28 | .filter(predicate) 29 | .map(tdList -> tdList.get(3)) // td containing chkbox 30 | .map(td -> td.findElement(By.tagName("input"))) 31 | .forEach(WebElement::click); 32 | } 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/polymorphism/Animal.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.polymorphism; 2 | 3 | public abstract class Animal { 4 | 5 | private String name; 6 | 7 | public void makeSound(){ 8 | System.out.println("I am animal.. Grrrrr....."); 9 | } 10 | 11 | public void walk(){ 12 | System.out.println("I am walking"); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/polymorphism/AreaCalculator.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.polymorphism; 2 | 3 | public class AreaCalculator { 4 | 5 | //square 6 | public int getArea(int side){ 7 | return side * side; 8 | } 9 | 10 | public double getArea(double side){ 11 | return side * side; 12 | } 13 | 14 | public int getArea(int length, int width){ 15 | return length * width; 16 | } 17 | 18 | public double getArea(double length, int width){ 19 | return length * width; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/polymorphism/Cat.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.polymorphism; 2 | 3 | //IS A/AN 4 | public class Cat extends Animal { 5 | 6 | 7 | @Override 8 | public void makeSound(){ 9 | System.out.println("I am cat. meow...."); 10 | } 11 | 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/polymorphism/Dog.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.polymorphism; 2 | 3 | public class Dog extends Animal{ 4 | 5 | @Override 6 | public void makeSound(){ 7 | System.out.println("I am dog. barking...."); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/polymorphism/Horse.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.polymorphism; 2 | 3 | public class Horse extends Animal { 4 | 5 | @Override 6 | public void walk(){ 7 | System.out.println("I am galloping...."); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/predicate/Rules.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.predicate; 2 | 3 | import org.openqa.selenium.WebElement; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.function.Predicate; 8 | 9 | public class Rules { 10 | 11 | private static Predicate isBlank = (e) -> e.getText().trim().length() == 0; 12 | private static Predicate hasS = (e) -> e.getText().toLowerCase().contains("s"); 13 | 14 | public static List> get(){ 15 | List> r = new ArrayList<>(); 16 | r.add(isBlank); 17 | r.add(hasS); 18 | return r; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/supplier/DriverFactory.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.supplier; 2 | 3 | import com.google.common.base.Supplier; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.chrome.ChromeDriver; 6 | import org.openqa.selenium.firefox.FirefoxDriver; 7 | 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | public class DriverFactory { 12 | 13 | private static final Supplier chromeSupplier = () -> { 14 | System.setProperty("webdriver.chrome.driver", "/home/qa/.arquillian/drone/chrome/2.38/chromedriver"); 15 | return new ChromeDriver(); 16 | }; 17 | 18 | private static final Supplier firefoxSupplier = () -> { 19 | System.setProperty("webdriver.gecko.driver", "/home/qa/.arquillian/drone/firefox/geckodriver"); 20 | return new FirefoxDriver(); 21 | }; 22 | 23 | private static final Map> MAP = new HashMap<>(); 24 | 25 | static { 26 | MAP.put("chrome", chromeSupplier); 27 | MAP.put("firefox", firefoxSupplier); 28 | } 29 | 30 | 31 | public static WebDriver getDriver(String browser){ 32 | return MAP.get(browser).get(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/udemy/java/util/LinkUtil.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.util; 2 | 3 | import java.net.HttpURLConnection; 4 | import java.net.URL; 5 | 6 | public class LinkUtil { 7 | 8 | // hits the given url and returns the HTTP response code 9 | public static int getResponseCode(String link) { 10 | URL url; 11 | HttpURLConnection con = null; 12 | Integer responsecode = 0; 13 | try { 14 | url = new URL(link); 15 | con = (HttpURLConnection) url.openConnection(); 16 | responsecode = con.getResponseCode(); 17 | } catch (Exception e) { 18 | // skip 19 | } finally { 20 | if (null != con) 21 | con.disconnect(); 22 | } 23 | return responsecode; 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /src/test/java/com/udemy/java/test/BrokenLinkTest.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.test; 2 | 3 | import com.udemy.java.supplier.DriverFactory; 4 | import com.udemy.java.util.LinkUtil; 5 | import org.openqa.selenium.By; 6 | import org.openqa.selenium.WebDriver; 7 | import org.openqa.selenium.WebElement; 8 | import org.testng.Assert; 9 | import org.testng.annotations.*; 10 | 11 | import java.time.LocalDateTime; 12 | import java.util.List; 13 | import java.util.stream.Collectors; 14 | 15 | public class BrokenLinkTest { 16 | 17 | private WebDriver driver; 18 | 19 | @BeforeTest 20 | @Parameters("browser") 21 | public void setDriver(@Optional("chrome") String browser){ 22 | this.driver = DriverFactory.getDriver(browser); 23 | } 24 | 25 | // https://the-internet.herokuapp.com/broken_images 26 | 27 | @Test 28 | public void linkTest(){ 29 | this.driver.get("https://www.google.com"); 30 | 31 | System.out.println("Before :: " + LocalDateTime.now()); 32 | 33 | List list = this.driver.findElements(By.xpath("//*[@href]")) 34 | .stream() 35 | .parallel() 36 | .map(e -> e.getAttribute("href")) 37 | .filter(src -> LinkUtil.getResponseCode(src) != 200) 38 | .collect(Collectors.toList()); 39 | 40 | System.out.println("After :: " + LocalDateTime.now()); 41 | } 42 | 43 | @AfterTest 44 | public void quitDriver(){ 45 | this.driver.quit(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/com/udemy/java/test/CheckboxSelectionTest.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.test; 2 | 3 | import com.google.common.util.concurrent.Uninterruptibles; 4 | import com.udemy.java.pages.TableDemoPage; 5 | import com.udemy.java.supplier.DriverFactory; 6 | import org.openqa.selenium.By; 7 | import org.openqa.selenium.WebDriver; 8 | import org.openqa.selenium.WebElement; 9 | import org.testng.annotations.*; 10 | 11 | import java.util.List; 12 | import java.util.concurrent.TimeUnit; 13 | import java.util.function.Predicate; 14 | 15 | public class CheckboxSelectionTest { 16 | 17 | private WebDriver driver; 18 | private TableDemoPage tableDemoPage; 19 | 20 | @BeforeTest 21 | @Parameters("browser") 22 | public void setDriver(@Optional("chrome") String browser){ 23 | this.driver = DriverFactory.getDriver(browser); 24 | this.tableDemoPage = new TableDemoPage(driver); 25 | } 26 | 27 | @Test(dataProvider = "criteriaProvider") 28 | public void tableRowTest(Predicate> searchCriteria){ 29 | tableDemoPage.goTo(); 30 | tableDemoPage.selectRows(searchCriteria); 31 | Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); 32 | } 33 | 34 | @DataProvider(name = "criteriaProvider") 35 | public Object[] testdata(){ 36 | return new Object[] { 37 | SearchCriteriaFactory.getCriteria("allMale"), 38 | SearchCriteriaFactory.getCriteria("allFemale"), 39 | SearchCriteriaFactory.getCriteria("allGender"), 40 | SearchCriteriaFactory.getCriteria("allAU"), 41 | SearchCriteriaFactory.getCriteria("allFemaleAU") 42 | }; 43 | } 44 | 45 | @AfterTest 46 | public void quitDriver(){ 47 | this.driver.quit(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/com/udemy/java/test/DriverTest.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.test; 2 | 3 | import com.udemy.java.supplier.DriverFactory; 4 | import org.openqa.selenium.By; 5 | import org.openqa.selenium.WebDriver; 6 | import org.openqa.selenium.WebElement; 7 | import org.testng.annotations.*; 8 | 9 | public class DriverTest { 10 | 11 | private WebDriver driver; 12 | 13 | @BeforeTest 14 | @Parameters("browser") 15 | public void setDriver(@Optional("chrome") String browser){ 16 | this.driver = DriverFactory.getDriver(browser); 17 | } 18 | 19 | // https://the-internet.herokuapp.com/broken_images 20 | 21 | @Test 22 | public void googleTest(){ 23 | this.driver.get("https://google.com/"); 24 | this.driver.findElements(By.tagName("a")) 25 | .stream() 26 | .map(WebElement::getText) 27 | .map(String::trim) 28 | .filter(e -> e.length() > 0) 29 | .filter(e -> !e.toLowerCase().contains("s")) 30 | .map(String::toUpperCase) 31 | .forEach(System.out::println); 32 | } 33 | 34 | @AfterTest 35 | public void quitDriver(){ 36 | this.driver.quit(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/com/udemy/java/test/HoverableDropdownTest.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.test; 2 | 3 | import com.google.common.util.concurrent.Uninterruptibles; 4 | import com.udemy.java.supplier.DriverFactory; 5 | import org.openqa.selenium.By; 6 | import org.openqa.selenium.WebDriver; 7 | import org.openqa.selenium.WebElement; 8 | import org.openqa.selenium.interactions.Actions; 9 | import org.testng.annotations.*; 10 | 11 | import java.util.Arrays; 12 | import java.util.concurrent.TimeUnit; 13 | 14 | public class HoverableDropdownTest { 15 | 16 | private WebDriver driver; 17 | private Actions actions; 18 | 19 | @BeforeTest 20 | @Parameters("browser") 21 | public void setDriver(@Optional("chrome") String browser){ 22 | this.driver = DriverFactory.getDriver(browser); 23 | this.actions = new Actions(driver); 24 | } 25 | 26 | @Test(dataProvider = "linkProvider") 27 | public void dropdownTest(String path){ 28 | this.driver.get("https://www.bootply.com/render/6FC76YQ4Nh#"); 29 | String[] split = path.split("=>"); 30 | 31 | Arrays.stream(split) 32 | .map(s -> s.trim()) 33 | .map(s -> By.linkText(s)) 34 | .map(by -> driver.findElement(by)) 35 | .map(ele -> actions.moveToElement(ele)) 36 | .forEach(a -> a.perform()); 37 | 38 | Uninterruptibles.sleepUninterruptibly(3, TimeUnit.SECONDS); 39 | 40 | } 41 | 42 | @DataProvider(name = "linkProvider") 43 | public Object[] testdata(){ 44 | return new Object[] { 45 | "Dropdown => Dropdown Link 2", 46 | "Dropdown => Dropdown Link 5 => Dropdown Submenu Link 5.1", 47 | "Dropdown => Dropdown Link 5 => Dropdown Submenu Link 5.4 => Dropdown Submenu Link 5.4.2", 48 | }; 49 | } 50 | 51 | @AfterTest 52 | public void quitDriver(){ 53 | this.driver.quit(); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/com/udemy/java/test/PriceTableTest.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.test; 2 | 3 | import com.google.common.util.concurrent.Uninterruptibles; 4 | import com.udemy.java.assignment.PriceTable; 5 | import com.udemy.java.supplier.DriverFactory; 6 | import org.openqa.selenium.By; 7 | import org.openqa.selenium.WebDriver; 8 | import org.openqa.selenium.WebElement; 9 | import org.testng.Assert; 10 | import org.testng.annotations.*; 11 | 12 | import java.util.concurrent.TimeUnit; 13 | 14 | public class PriceTableTest { 15 | 16 | private WebDriver driver; 17 | 18 | @BeforeTest 19 | @Parameters("browser") 20 | public void setDriver(@Optional("chrome") String browser){ 21 | this.driver = DriverFactory.getDriver(browser); 22 | } 23 | 24 | @Test 25 | public void minPriceTest(){ 26 | PriceTable p = new PriceTable(driver); 27 | p.selectMinPriceRow(); 28 | String status = p.getStatus(); 29 | 30 | Assert.assertEquals("PASS", status); 31 | 32 | Uninterruptibles.sleepUninterruptibly(3, TimeUnit.SECONDS); 33 | } 34 | 35 | @AfterTest 36 | public void quitDriver(){ 37 | this.driver.quit(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/com/udemy/java/test/SearchCriteriaFactory.java: -------------------------------------------------------------------------------- 1 | package com.udemy.java.test; 2 | 3 | import com.google.common.base.Supplier; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | 7 | import java.util.HashMap; 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.function.Predicate; 11 | 12 | public class SearchCriteriaFactory { 13 | 14 | private static Predicate> allMale = (l) -> l.get(1).getText().equalsIgnoreCase("male"); 15 | private static Predicate> allFemale = (l) -> l.get(1).getText().equalsIgnoreCase("female"); 16 | private static Predicate> allGender = allMale.or(allFemale); 17 | private static Predicate> allAU = (l) -> l.get(2).getText().equalsIgnoreCase("AU"); 18 | private static Predicate> allFemaleAU = allFemale.and(allAU); 19 | 20 | private static final Map>> MAP = new HashMap<>(); 21 | 22 | static { 23 | MAP.put("allMale", allMale); 24 | MAP.put("allFemale", allFemale); 25 | MAP.put("allGender", allGender); 26 | MAP.put("allAU", allAU); 27 | MAP.put("allFemaleAU", allFemaleAU); 28 | } 29 | 30 | public static Predicate> getCriteria(String criteriaName){ 31 | return MAP.get(criteriaName); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /udemy-java.iml: -------------------------------------------------------------------------------- 1 | 2 | --------------------------------------------------------------------------------