├── Chapter01 ├── .gitignore ├── pom.xml └── src │ └── test │ ├── java │ └── com │ │ └── example │ │ ├── ElementTests.java │ │ ├── LocatorTest.java │ │ ├── NavigationTest.java │ │ └── SearchTest.java │ └── resources │ ├── drivers │ ├── chromedriver │ └── chromedriver.exe │ └── suites │ └── testng.xml ├── Chapter02 ├── .gitignore ├── pom.xml └── src │ └── test │ ├── java │ └── com │ │ └── example │ │ └── SearchTest.java │ └── resources │ ├── drivers │ ├── chromedriver │ └── chromedriver.exe │ └── suites │ └── testng.xml ├── Chapter03 ├── .gitignore ├── pom.xml └── src │ └── test │ ├── java │ └── com │ │ └── example │ │ ├── FrameHandlingTest.java │ │ ├── LoadCookieInfo.java │ │ ├── SearchAndNavigationTest.java │ │ ├── StoreCookieInfo.java │ │ └── WindowHandlingTest.java │ └── resources │ ├── drivers │ ├── chromedriver │ └── chromedriver.exe │ └── suites │ └── testng.xml ├── Chapter04 ├── .gitignore ├── pom.xml └── src │ └── test │ ├── java │ └── com │ │ └── example │ │ └── ActionsTest.java │ └── resources │ ├── drivers │ ├── chromedriver │ └── chromedriver.exe │ └── suites │ └── testng.xml ├── Chapter05 ├── .gitignore ├── pom.xml └── src │ └── test │ ├── java │ └── com │ │ └── example │ │ ├── IAmTheDriver.java │ │ ├── IAmTheEventListener.java │ │ ├── IAmTheEventListener2.java │ │ └── RegisteringMultipleListeners.java │ └── resources │ └── drivers │ ├── chromedriver │ └── chromedriver.exe ├── Chapter06 ├── .gitignore ├── pom.xml └── src │ └── test │ ├── java │ └── com │ │ └── example │ │ └── SearchTest.java │ └── resources │ ├── drivers │ ├── chromedriver │ └── chromedriver.exe │ └── suites │ └── testng.xml ├── Chapter07 ├── .gitignore ├── pom.xml └── src │ └── test │ ├── java │ └── com │ │ └── example │ │ ├── BmiCalculatorTest.java │ │ └── SearchTest.java │ └── resources │ ├── drivers │ ├── chromedriver │ └── chromedriver.exe │ └── suites │ └── testng.xml ├── Chapter08 ├── .gitignore ├── pom.xml └── src │ └── test │ ├── java │ └── com │ │ └── example │ │ ├── SearchTest.java │ │ ├── SearchTestWithCSVDataProvider.java │ │ ├── SearchTestWithDataProvider.java │ │ ├── SearchTestWithExcelDataProvider.java │ │ └── SpreadsheetData.java │ └── resources │ ├── data │ ├── data.csv │ └── data.xlsx │ ├── drivers │ ├── chromedriver │ └── chromedriver.exe │ └── suites │ └── testng.xml ├── Chapter18 ├── BrowserUtils.java ├── CreateDriver.java ├── ExtentTestNGIReporterListener.java ├── Global_VARS.java ├── JSONDataProvider.java ├── PassionTeaCo.json ├── PassionTeaCo.xml ├── PassionTeaCoBasePO.java ├── PassionTeaCoTest.java ├── PassionTeaCoWelcomePO.java ├── TestNG_ConsoleRunner.java ├── extent-config.xml ├── pom.xml └── selenium.properties ├── LICENSE └── README.md /Chapter01/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | *.iml 3 | /.idea 4 | test-output -------------------------------------------------------------------------------- /Chapter01/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.example 8 | chapter1 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 1.8 13 | 3.12.0 14 | 6.13.1 15 | 0.14 16 | 23.2-jre 17 | 3.0.7 18 | 1.3.1 19 | 3.8.0 20 | 3.7.0 21 | 3.7 22 | 2.6 23 | 24 | 25 | 26 | 27 | org.seleniumhq.selenium 28 | selenium-java 29 | ${selenium.version} 30 | 31 | 32 | org.testng 33 | testng 34 | ${testng.version} 35 | 36 | 37 | com.github.javafaker 38 | javafaker 39 | ${javafaker.version} 40 | 41 | 42 | com.google.guava 43 | guava 44 | ${guava.version} 45 | 46 | 47 | com.aventstack 48 | extentreports 49 | ${extentreports.version} 50 | 51 | 52 | com.vimalselvam 53 | testng-extentsreport 54 | ${extenttestng.version} 55 | 56 | 57 | org.assertj 58 | assertj-core 59 | ${assertj.version} 60 | 61 | 62 | org.apache.commons 63 | commons-lang3 64 | ${commons.version} 65 | 66 | 67 | commons-io 68 | commons-io 69 | ${commons.io.version} 70 | 71 | 72 | 73 | 74 | 75 | 76 | org.apache.maven.plugins 77 | maven-compiler-plugin 78 | ${maven.compiler.version} 79 | 80 | ${java.version} 81 | ${java.version} 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /Chapter01/src/test/java/com/example/ElementTests.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.Keys; 5 | import org.openqa.selenium.WebDriver; 6 | import org.openqa.selenium.WebElement; 7 | import org.openqa.selenium.chrome.ChromeDriver; 8 | import org.testng.annotations.*; 9 | 10 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 11 | 12 | /** 13 | * @author upgundecha 14 | */ 15 | public class ElementTests { 16 | 17 | WebDriver driver; 18 | 19 | @BeforeClass 20 | public void setup() { 21 | 22 | System.setProperty("webdriver.chrome.driver", 23 | "./src/test/resources/drivers/chromedriver"); 24 | driver = new ChromeDriver(); 25 | 26 | } 27 | 28 | @BeforeMethod 29 | public void navigate() { 30 | 31 | driver.get("http://demo-store.seleniumacademy.com/"); 32 | } 33 | 34 | @Test 35 | public void elementGetAttributesExample() { 36 | 37 | WebElement searchBox = driver.findElement(By.name("q")); 38 | 39 | System.out.println("Name of the box is: " 40 | + searchBox.getAttribute("name")); 41 | 42 | System.out.println("Id of the box is: " 43 | + searchBox.getAttribute("id")); 44 | 45 | System.out.println("Class of the box is: " 46 | + searchBox.getAttribute("class")); 47 | 48 | System.out.println("Placeholder of the box is: " 49 | + searchBox.getAttribute("placeholder")); 50 | } 51 | 52 | @Test 53 | public void elementSendKeysExample() { 54 | 55 | WebElement searchBox = driver.findElement(By.name("q")); 56 | 57 | searchBox.sendKeys("Phones"); 58 | searchBox.submit(); 59 | 60 | assertThat(driver.getTitle()) 61 | .isEqualTo("Search results for: 'Phones'"); 62 | } 63 | 64 | @Test 65 | public void elementSendKeysCompositeExample() { 66 | 67 | WebElement searchBox = driver.findElement(By.name("q")); 68 | 69 | searchBox.sendKeys(Keys.chord(Keys.SHIFT,"phones")); 70 | searchBox.submit(); 71 | 72 | assertThat(driver.getTitle()) 73 | .isEqualTo("Search results for: 'PHONES'"); 74 | } 75 | 76 | @Test 77 | public void elementClearExample() { 78 | 79 | WebElement searchBox = driver.findElement(By.name("q")); 80 | 81 | searchBox.sendKeys(Keys.chord(Keys.SHIFT,"phones")); 82 | searchBox.clear(); 83 | } 84 | 85 | @Test 86 | public void elementSubmitExample() { 87 | 88 | WebElement searchBox = driver.findElement(By.name("q")); 89 | 90 | searchBox.sendKeys(Keys.chord(Keys.SHIFT,"phones")); 91 | searchBox.submit(); 92 | } 93 | 94 | @Test 95 | public void elementGetCssValueExample() { 96 | 97 | WebElement searchBox = driver.findElement(By.name("q")); 98 | 99 | System.out.println("Font of the box is: " 100 | + searchBox.getCssValue("font-family")); 101 | } 102 | 103 | @Test 104 | public void elementLocationAndSizeExample() { 105 | 106 | WebElement searchBox = driver.findElement(By.name("q")); 107 | 108 | System.out.println("Location of the box is: " 109 | + searchBox.getLocation()); 110 | 111 | System.out.println("Size of the box is: " 112 | + searchBox.getSize()); 113 | } 114 | 115 | @Test 116 | public void elementGetTextExample() { 117 | 118 | WebElement siteNotice = driver.findElement(By 119 | .className("global-site-notice")); 120 | 121 | System.out.println("Complete text is: " 122 | + siteNotice.getText()); 123 | } 124 | 125 | @Test 126 | public void elementGetTagNameExample() { 127 | 128 | WebElement searchButton = driver.findElement(By.className("search-button")); 129 | 130 | System.out.println("Html tag of the button is: " 131 | + searchButton.getTagName()); 132 | } 133 | 134 | @Test 135 | public void elementStateExample() { 136 | 137 | WebElement searchBox = driver.findElement(By.name("q")); 138 | 139 | System.out.println("Search button is displayed: " 140 | + searchBox.isDisplayed()); 141 | 142 | System.out.println("Search button is enabled: " 143 | + searchBox.isEnabled()); 144 | 145 | System.out.println("Search button is selected: " 146 | + searchBox.isSelected()); 147 | } 148 | 149 | @AfterClass 150 | public void tearDown() { 151 | driver.quit(); 152 | } 153 | } -------------------------------------------------------------------------------- /Chapter01/src/test/java/com/example/LocatorTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import org.openqa.selenium.chrome.ChromeDriver; 7 | import org.openqa.selenium.interactions.SourceType; 8 | import org.testng.annotations.*; 9 | 10 | import javax.xml.bind.SchemaOutputResolver; 11 | import java.util.List; 12 | 13 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 14 | 15 | /** 16 | * @author upgundecha 17 | */ 18 | public class LocatorTest { 19 | 20 | WebDriver driver; 21 | 22 | @BeforeClass 23 | public void setup() { 24 | 25 | System.setProperty("webdriver.chrome.driver", 26 | "./src/test/resources/drivers/chromedriver"); 27 | driver = new ChromeDriver(); 28 | 29 | } 30 | 31 | @BeforeMethod 32 | public void navigate() { 33 | 34 | driver.get("http://demo-store.seleniumacademy.com/"); 35 | 36 | } 37 | 38 | @Test 39 | public void byIdLocatorExample() { 40 | 41 | WebElement searchBox = driver.findElement(By.id("search")); 42 | 43 | searchBox.sendKeys("Bags"); 44 | searchBox.submit(); 45 | 46 | assertThat(driver.getTitle()) 47 | .isEqualTo("Search results for: 'Bags'"); 48 | } 49 | 50 | @Test 51 | public void byClassNameLocatorExample() { 52 | 53 | WebElement searchBox = driver.findElement(By.id("search")); 54 | searchBox.sendKeys("Electronics"); 55 | 56 | WebElement searchButton = 57 | driver.findElement(By.className("search-button")); 58 | searchButton.click(); 59 | 60 | assertThat(driver.getTitle()) 61 | .isEqualTo("Search results for: 'Electronics'"); 62 | } 63 | 64 | @Test 65 | public void byLinkTextLocatorExample() { 66 | 67 | WebElement myAccountLink = 68 | driver.findElement(By.linkText("MY ACCOUNT")); 69 | 70 | myAccountLink.click(); 71 | 72 | assertThat(driver.getTitle()) 73 | .isEqualTo("Customer Login"); 74 | } 75 | 76 | @Test 77 | public void byPartialLinkTextLocatorExample() { 78 | 79 | WebElement orderAndReturns = 80 | driver.findElement(By.partialLinkText("PRIVACY")); 81 | 82 | orderAndReturns.click(); 83 | 84 | assertThat(driver.getTitle()) 85 | .isEqualTo("Privacy Policy"); 86 | } 87 | 88 | @Test 89 | public void byTagNameLocatorExample() { 90 | 91 | // get all links from the Home page 92 | List links = driver.findElements(By.tagName("a")); 93 | 94 | System.out.println("Found links:" + links.size()); 95 | 96 | // print links which have text using Java 8 Streams API 97 | links.stream() 98 | .filter(elem -> elem.getText().length() > 0) 99 | .forEach(elem -> System.out.println(elem.getText())); 100 | } 101 | 102 | @Test 103 | public void byXPathLocatorExample() { 104 | 105 | WebElement searchBox = 106 | driver.findElement(By.xpath("//*[@id='search']")); 107 | 108 | searchBox.sendKeys("Bags"); 109 | searchBox.submit(); 110 | 111 | assertThat(driver.getTitle()) 112 | .isEqualTo("Search results for: 'Bags'"); 113 | } 114 | 115 | @Test 116 | public void byCssSelectorLocatorExample() { 117 | 118 | WebElement searchBox = 119 | driver.findElement(By.cssSelector("#search")); 120 | 121 | searchBox.sendKeys("Bags"); 122 | searchBox.submit(); 123 | 124 | assertThat(driver.getTitle()) 125 | .isEqualTo("Search results for: 'Bags'"); 126 | } 127 | 128 | @AfterClass 129 | public void tearDown() { 130 | driver.quit(); 131 | } 132 | } 133 | 134 | -------------------------------------------------------------------------------- /Chapter01/src/test/java/com/example/NavigationTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.WebDriver; 4 | import org.openqa.selenium.chrome.ChromeDriver; 5 | import org.testng.Assert; 6 | import org.testng.annotations.*; 7 | 8 | 9 | /** 10 | * @author upgundecha 11 | */ 12 | public class NavigationTest { 13 | 14 | WebDriver driver; 15 | 16 | @BeforeMethod 17 | public void beforeMethod() { 18 | 19 | // set path of Chromedriver executable 20 | System.setProperty("webdriver.chrome.driver", 21 | "./src/test/resources/drivers/chromedriver"); 22 | 23 | // initialize new WebDriver session 24 | driver = new ChromeDriver(); 25 | } 26 | 27 | @Test 28 | public void navigateToAUrl() { 29 | 30 | // navigate to the web site 31 | driver.get("http://demo-store.seleniumacademy.com/"); 32 | 33 | // Validate page title 34 | Assert.assertEquals(driver.getTitle(), "Madison Island"); 35 | } 36 | 37 | @AfterMethod 38 | public void afterMethod() { 39 | 40 | // close and quit the browser 41 | driver.quit(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Chapter01/src/test/java/com/example/SearchTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import org.openqa.selenium.chrome.ChromeDriver; 7 | import org.testng.annotations.AfterMethod; 8 | import org.testng.annotations.BeforeMethod; 9 | import org.testng.annotations.Test; 10 | 11 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 12 | 13 | /** 14 | * @author upgundecha 15 | */ 16 | public class SearchTest { 17 | 18 | WebDriver driver; 19 | 20 | @BeforeMethod 21 | public void setup() { 22 | 23 | System.setProperty("webdriver.chrome.driver", 24 | "./src/test/resources/drivers/chromedriver"); 25 | driver = new ChromeDriver(); 26 | driver.get("http://demo-store.seleniumacademy.com/"); 27 | 28 | } 29 | 30 | @Test 31 | public void searchProduct() { 32 | 33 | // find search box and enter search string 34 | WebElement searchBox = driver.findElement(By.name("q")); 35 | 36 | searchBox.sendKeys("Phones"); 37 | 38 | WebElement searchButton = 39 | driver.findElement(By.className("search-button")); 40 | 41 | searchButton.click(); 42 | 43 | assertThat(driver.getTitle()) 44 | .isEqualTo("Search results for: 'Phones'"); 45 | } 46 | 47 | @AfterMethod 48 | public void tearDown() { 49 | driver.quit(); 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Chapter01/src/test/resources/drivers/chromedriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter01/src/test/resources/drivers/chromedriver -------------------------------------------------------------------------------- /Chapter01/src/test/resources/drivers/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter01/src/test/resources/drivers/chromedriver.exe -------------------------------------------------------------------------------- /Chapter01/src/test/resources/suites/testng.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 | -------------------------------------------------------------------------------- /Chapter02/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | *.iml 3 | /.idea 4 | test-output -------------------------------------------------------------------------------- /Chapter02/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.example 8 | chapter3 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 1.8 13 | 3.12.0 14 | 6.13.1 15 | 0.14 16 | 23.2-jre 17 | 3.0.7 18 | 1.3.1 19 | 3.8.0 20 | 3.7.0 21 | 3.7 22 | 2.6 23 | 24 | 25 | 26 | 27 | org.seleniumhq.selenium 28 | selenium-java 29 | ${selenium.version} 30 | 31 | 32 | org.testng 33 | testng 34 | ${testng.version} 35 | 36 | 37 | com.github.javafaker 38 | javafaker 39 | ${javafaker.version} 40 | 41 | 42 | com.google.guava 43 | guava 44 | ${guava.version} 45 | 46 | 47 | com.aventstack 48 | extentreports 49 | ${extentreports.version} 50 | 51 | 52 | com.vimalselvam 53 | testng-extentsreport 54 | ${extenttestng.version} 55 | 56 | 57 | org.assertj 58 | assertj-core 59 | ${assertj.version} 60 | 61 | 62 | org.apache.commons 63 | commons-lang3 64 | ${commons.version} 65 | 66 | 67 | commons-io 68 | commons-io 69 | ${commons.io.version} 70 | 71 | 72 | 73 | 74 | 75 | 76 | org.apache.maven.plugins 77 | maven-compiler-plugin 78 | ${maven.compiler.version} 79 | 80 | ${java.version} 81 | ${java.version} 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /Chapter02/src/test/java/com/example/SearchTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import org.openqa.selenium.chrome.ChromeDriver; 7 | import org.testng.annotations.AfterMethod; 8 | import org.testng.annotations.BeforeMethod; 9 | import org.testng.annotations.Test; 10 | 11 | import java.sql.SQLOutput; 12 | import java.util.ArrayList; 13 | import java.util.Comparator; 14 | import java.util.List; 15 | import java.util.stream.Collectors; 16 | 17 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 18 | 19 | /** 20 | * @author upgundecha 21 | */ 22 | public class SearchTest { 23 | 24 | WebDriver driver; 25 | 26 | @BeforeMethod 27 | public void setup() { 28 | 29 | System.setProperty("webdriver.chrome.driver", 30 | "./src/test/resources/drivers/chromedriver"); 31 | driver = new ChromeDriver(); 32 | driver.get("http://demo-store.seleniumacademy.com/"); 33 | 34 | } 35 | 36 | @Test 37 | public void searchProduct() { 38 | 39 | // find search box and enter search string 40 | WebElement searchBox = driver.findElement(By.name("q")); 41 | 42 | searchBox.sendKeys("Phones"); 43 | 44 | WebElement searchButton = 45 | driver.findElement(By.className("search-button")); 46 | 47 | searchButton.click(); 48 | 49 | assertThat(driver.getTitle()) 50 | .isEqualTo("Search results for: 'Phones'"); 51 | 52 | 53 | List searchItems = driver 54 | .findElements(By.cssSelector("h2.product-name a")); 55 | 56 | // List products = new ArrayList<>(); 57 | // 58 | // for(WebElement item : searchItems) { 59 | // products.add(item.getText()); 60 | // } 61 | // 62 | // System.out.println(products); 63 | 64 | List products = searchItems 65 | .stream() 66 | .map(WebElement::getText) 67 | .collect(Collectors.toList()); 68 | 69 | System.out.println(products); 70 | 71 | List languages = new ArrayList(); 72 | languages.add("English"); 73 | languages.add("German"); 74 | languages.add("French"); 75 | 76 | for(String language : languages) { 77 | System.out.println(language); 78 | } 79 | 80 | languages.stream().forEach(System.out::println); 81 | 82 | languages.stream().map(item -> item.toUpperCase()); 83 | 84 | languages.stream().sorted(); 85 | 86 | List sortedLanguages = languages.stream() 87 | .sorted() 88 | .collect(Collectors.toList()); 89 | 90 | System.out.println(sortedLanguages); 91 | 92 | List searchResult = new ArrayList<>(); 93 | searchResult.add(new Product("MADISON OVEREAR HEADPHONES", 125.00)); 94 | searchResult.add(new Product("MADISON EARBUDS", 35.00)); 95 | searchResult.add(new Product("MP3 PLAYER WITH AUDIO", 185.00)); 96 | 97 | Product product = searchResult.stream() 98 | .min(Comparator.comparing(item -> item.getPrice())) 99 | .get(); 100 | 101 | System.out.println("The product with lowest price is " + product.getName()); 102 | 103 | product = searchResult.stream() 104 | .max(Comparator.comparing(item -> item.getPrice())) 105 | .get(); 106 | 107 | System.out.println("The product with highest price is " + product.getName()); 108 | 109 | } 110 | 111 | @AfterMethod 112 | public void tearDown() { 113 | driver.quit(); 114 | } 115 | 116 | class Product { 117 | String name; 118 | Double price; 119 | 120 | public Product(String name, double price) { 121 | this.name = name; 122 | this.price = price; 123 | } 124 | 125 | public String getName() { 126 | return name; 127 | } 128 | 129 | public Double getPrice() { 130 | return price; 131 | } 132 | 133 | } 134 | } 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /Chapter02/src/test/resources/drivers/chromedriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter02/src/test/resources/drivers/chromedriver -------------------------------------------------------------------------------- /Chapter02/src/test/resources/drivers/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter02/src/test/resources/drivers/chromedriver.exe -------------------------------------------------------------------------------- /Chapter02/src/test/resources/suites/testng.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter03/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | *.iml 3 | /.idea 4 | test-output -------------------------------------------------------------------------------- /Chapter03/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.example 8 | chapter4 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 1.8 13 | 3.12.0 14 | 6.13.1 15 | 0.14 16 | 23.2-jre 17 | 3.0.7 18 | 1.3.1 19 | 3.8.0 20 | 3.7.0 21 | 3.7 22 | 2.6 23 | 0.14 24 | 25 | 26 | 27 | 28 | org.seleniumhq.selenium 29 | selenium-java 30 | ${selenium.version} 31 | 32 | 33 | org.testng 34 | testng 35 | ${testng.version} 36 | 37 | 38 | com.github.javafaker 39 | javafaker 40 | ${javafaker.version} 41 | 42 | 43 | com.google.guava 44 | guava 45 | ${guava.version} 46 | 47 | 48 | com.aventstack 49 | extentreports 50 | ${extentreports.version} 51 | 52 | 53 | com.vimalselvam 54 | testng-extentsreport 55 | ${extenttestng.version} 56 | 57 | 58 | org.assertj 59 | assertj-core 60 | ${assertj.version} 61 | 62 | 63 | org.apache.commons 64 | commons-lang3 65 | ${commons.version} 66 | 67 | 68 | commons-io 69 | commons-io 70 | ${commons.io.version} 71 | 72 | 73 | com.github.javafaker 74 | javafaker 75 | ${javafaker.version} 76 | 77 | 78 | 79 | 80 | 81 | 82 | org.apache.maven.plugins 83 | maven-compiler-plugin 84 | ${maven.compiler.version} 85 | 86 | ${java.version} 87 | ${java.version} 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /Chapter03/src/test/java/com/example/FrameHandlingTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import org.openqa.selenium.chrome.ChromeDriver; 7 | import org.testng.annotations.AfterMethod; 8 | import org.testng.annotations.BeforeMethod; 9 | import org.testng.annotations.Test; 10 | 11 | import java.io.IOException; 12 | 13 | public class FrameHandlingTest { 14 | 15 | WebDriver driver; 16 | 17 | @BeforeMethod 18 | public void setup() throws IOException { 19 | 20 | System.setProperty("webdriver.chrome.driver", 21 | "./src/test/resources/drivers/chromedriver"); 22 | 23 | driver = new ChromeDriver(); 24 | driver.get("http://guidebook.seleniumacademy.com/Frames.html"); 25 | } 26 | 27 | @Test 28 | public void switchBetweenFrames() { 29 | 30 | // First Frame 31 | driver.switchTo().frame(0); 32 | WebElement firstField = driver.findElement(By.name("1")); 33 | firstField.sendKeys("I'm Frame One"); 34 | driver.switchTo().defaultContent(); 35 | 36 | // Second Frame 37 | driver.switchTo().frame(1); 38 | WebElement secondField = driver.findElement(By.name("2")); 39 | secondField.sendKeys("I'm Frame Two"); 40 | } 41 | 42 | @AfterMethod 43 | public void tearDown() { 44 | driver.quit(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Chapter03/src/test/java/com/example/LoadCookieInfo.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | 4 | import org.openqa.selenium.By; 5 | import org.openqa.selenium.Cookie; 6 | import org.openqa.selenium.WebDriver; 7 | import org.openqa.selenium.chrome.ChromeDriver; 8 | import org.testng.annotations.AfterMethod; 9 | import org.testng.annotations.BeforeMethod; 10 | import org.testng.annotations.Test; 11 | 12 | import java.io.*; 13 | import java.text.SimpleDateFormat; 14 | import java.util.Date; 15 | import java.util.StringTokenizer; 16 | 17 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 18 | 19 | public class LoadCookieInfo { 20 | WebDriver driver; 21 | 22 | @BeforeMethod 23 | public void setup() throws IOException { 24 | System.setProperty("webdriver.chrome.driver", 25 | "./src/test/resources/drivers/chromedriver"); 26 | driver = new ChromeDriver(); 27 | driver.get("http://demo-store.seleniumacademy.com"); 28 | } 29 | 30 | @Test 31 | public void loadCookies() { 32 | try { 33 | File dataFile = new File("./target/browser.data"); 34 | FileReader fr = new FileReader(dataFile); 35 | BufferedReader br = new BufferedReader(fr); 36 | String line; 37 | while ((line = br.readLine()) != null) { 38 | StringTokenizer str = new StringTokenizer(line, ";"); 39 | while (str.hasMoreTokens()) { 40 | String name = str.nextToken(); 41 | String value = str.nextToken(); 42 | String domain = str.nextToken(); 43 | String path = str.nextToken(); 44 | Date expiry = null; 45 | String dt; 46 | if (!(dt = str.nextToken()).equals("null")) { 47 | SimpleDateFormat formatter = 48 | new SimpleDateFormat("E MMM d HH:mm:ss z yyyy"); 49 | expiry = formatter.parse(dt); 50 | } 51 | 52 | boolean isSecure = new Boolean(str.nextToken()). 53 | booleanValue(); 54 | Cookie ck = new Cookie(name, value, domain, path, expiry, isSecure); 55 | driver.manage().addCookie(ck); 56 | } 57 | } 58 | 59 | driver.get("http://demo-store.seleniumacademy.com/customer/account/index/"); 60 | assertThat(driver.findElement(By.cssSelector("div.page-title")).getText()) 61 | .isEqualTo("MY DASHBOARD"); 62 | 63 | } catch (Exception ex) { 64 | ex.printStackTrace(); 65 | } 66 | } 67 | 68 | @AfterMethod 69 | public void tearDown() { 70 | driver.quit(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Chapter03/src/test/java/com/example/SearchAndNavigationTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.apache.commons.io.FileUtils; 4 | import org.openqa.selenium.*; 5 | import org.openqa.selenium.chrome.ChromeDriver; 6 | import org.openqa.selenium.support.ui.ExpectedCondition; 7 | import org.openqa.selenium.support.ui.WebDriverWait; 8 | import org.testng.annotations.AfterMethod; 9 | import org.testng.annotations.BeforeMethod; 10 | import org.testng.annotations.Test; 11 | 12 | import java.io.File; 13 | import java.io.IOException; 14 | import java.util.concurrent.TimeUnit; 15 | 16 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 17 | 18 | /** 19 | * @author upgundecha 20 | */ 21 | public class SearchAndNavigationTest { 22 | 23 | WebDriver driver; 24 | 25 | @BeforeMethod 26 | public void setup() throws IOException { 27 | 28 | System.setProperty("webdriver.chrome.driver", 29 | "./src/test/resources/drivers/chromedriver"); 30 | 31 | driver = new ChromeDriver(); 32 | driver.navigate().to("http://demo-store.seleniumacademy.com/"); 33 | driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 34 | 35 | File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); 36 | FileUtils.copyFile(scrFile, new File("./target/screenshot.png")); 37 | 38 | } 39 | 40 | @Test 41 | public void searchProduct() { 42 | 43 | // find search box and enter search string 44 | WebElement searchBox = driver.findElement(By.name("q")); 45 | 46 | searchBox.sendKeys("Phones"); 47 | 48 | WebElement searchButton = 49 | driver.findElement(By.className("search-button")); 50 | 51 | searchButton.click(); 52 | 53 | assertThat(driver.getTitle()) 54 | .isEqualTo("Search results for: 'Phones'"); 55 | 56 | driver.navigate().back(); 57 | driver.navigate().forward(); 58 | driver.navigate().refresh(); 59 | 60 | } 61 | 62 | @AfterMethod 63 | public void tearDown() { 64 | driver.quit(); 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /Chapter03/src/test/java/com/example/StoreCookieInfo.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.Cookie; 5 | import org.openqa.selenium.WebDriver; 6 | import org.openqa.selenium.chrome.ChromeDriver; 7 | import org.testng.annotations.AfterMethod; 8 | import org.testng.annotations.BeforeMethod; 9 | import org.testng.annotations.Test; 10 | 11 | import java.io.BufferedWriter; 12 | import java.io.File; 13 | import java.io.FileWriter; 14 | import java.io.IOException; 15 | 16 | 17 | public class StoreCookieInfo { 18 | WebDriver driver; 19 | 20 | @BeforeMethod 21 | public void setup() throws IOException { 22 | System.setProperty("webdriver.chrome.driver", 23 | "./src/test/resources/drivers/chromedriver"); 24 | driver = new ChromeDriver(); 25 | driver.get("http://demo-store.seleniumacademy.com/customer/account/login/"); 26 | } 27 | 28 | @Test 29 | public void storeCookies() { 30 | 31 | driver.findElement(By.id("email")).sendKeys("email"); 32 | driver.findElement(By.id("pass")).sendKeys("password"); 33 | driver.findElement(By.id("send2")).submit(); 34 | 35 | File dataFile = new File("./target/browser.data"); 36 | try { 37 | dataFile.delete(); 38 | dataFile.createNewFile(); 39 | FileWriter fos = new FileWriter(dataFile); 40 | BufferedWriter bos = new BufferedWriter(fos); 41 | for (Cookie ck : driver.manage().getCookies()) { 42 | bos.write((ck.getName() + ";" + ck.getValue() + ";" + ck. 43 | getDomain() 44 | + ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck. 45 | isSecure())); 46 | bos.newLine(); 47 | } 48 | bos.flush(); 49 | bos.close(); 50 | fos.close(); 51 | } catch (Exception ex) { 52 | ex.printStackTrace(); 53 | } 54 | } 55 | 56 | @AfterMethod 57 | public void tearDown() { 58 | driver.quit(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Chapter03/src/test/java/com/example/WindowHandlingTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.*; 4 | import org.openqa.selenium.chrome.ChromeDriver; 5 | import org.testng.annotations.AfterMethod; 6 | import org.testng.annotations.BeforeMethod; 7 | import org.testng.annotations.Test; 8 | 9 | import java.io.IOException; 10 | 11 | public class WindowHandlingTest { 12 | 13 | WebDriver driver; 14 | 15 | @BeforeMethod 16 | public void setup() throws IOException { 17 | 18 | System.setProperty("webdriver.chrome.driver", 19 | "./src/test/resources/drivers/chromedriver"); 20 | 21 | driver = new ChromeDriver(); 22 | driver.get("http://guidebook.seleniumacademy.com/Window.html"); 23 | 24 | } 25 | 26 | @Test 27 | public void handleWindow() { 28 | 29 | String firstWindow = driver.getWindowHandle(); 30 | System.out.println("First Window Handle is: " + firstWindow); 31 | 32 | WebElement link = driver.findElement(By.linkText("Google Search")); 33 | link.click(); 34 | 35 | String secondWindow = driver.getWindowHandle(); 36 | System.out.println("Second Window Handle is: " + secondWindow); 37 | System.out.println("Number of Window Handles so for: " 38 | + driver.getWindowHandles().size()); 39 | 40 | driver.switchTo().window(firstWindow); 41 | } 42 | 43 | @AfterMethod 44 | public void tearDown() { 45 | driver.quit(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Chapter03/src/test/resources/drivers/chromedriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter03/src/test/resources/drivers/chromedriver -------------------------------------------------------------------------------- /Chapter03/src/test/resources/drivers/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter03/src/test/resources/drivers/chromedriver.exe -------------------------------------------------------------------------------- /Chapter03/src/test/resources/suites/testng.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 | -------------------------------------------------------------------------------- /Chapter04/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | *.iml 3 | /.idea 4 | test-output -------------------------------------------------------------------------------- /Chapter04/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.example 8 | chapter5 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 1.8 13 | 3.12.0 14 | 6.13.1 15 | 0.14 16 | 23.2-jre 17 | 3.0.7 18 | 1.3.1 19 | 3.8.0 20 | 3.7.0 21 | 3.7 22 | 2.6 23 | 0.14 24 | 25 | 26 | 27 | 28 | org.seleniumhq.selenium 29 | selenium-java 30 | ${selenium.version} 31 | 32 | 33 | org.testng 34 | testng 35 | ${testng.version} 36 | 37 | 38 | com.github.javafaker 39 | javafaker 40 | ${javafaker.version} 41 | 42 | 43 | com.google.guava 44 | guava 45 | ${guava.version} 46 | 47 | 48 | com.aventstack 49 | extentreports 50 | ${extentreports.version} 51 | 52 | 53 | com.vimalselvam 54 | testng-extentsreport 55 | ${extenttestng.version} 56 | 57 | 58 | org.assertj 59 | assertj-core 60 | ${assertj.version} 61 | 62 | 63 | org.apache.commons 64 | commons-lang3 65 | ${commons.version} 66 | 67 | 68 | commons-io 69 | commons-io 70 | ${commons.io.version} 71 | 72 | 73 | com.github.javafaker 74 | javafaker 75 | ${javafaker.version} 76 | 77 | 78 | 79 | 80 | 81 | 82 | org.apache.maven.plugins 83 | maven-compiler-plugin 84 | ${maven.compiler.version} 85 | 86 | ${java.version} 87 | ${java.version} 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /Chapter04/src/test/java/com/example/ActionsTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.Keys; 5 | import org.openqa.selenium.WebDriver; 6 | import org.openqa.selenium.WebElement; 7 | import org.openqa.selenium.chrome.ChromeDriver; 8 | import org.openqa.selenium.interactions.Action; 9 | import org.openqa.selenium.interactions.Actions; 10 | import org.testng.annotations.AfterMethod; 11 | import org.testng.annotations.BeforeMethod; 12 | import org.testng.annotations.Test; 13 | 14 | import java.io.IOException; 15 | 16 | public class ActionsTest { 17 | 18 | WebDriver driver; 19 | 20 | @BeforeMethod 21 | public void setup() throws IOException { 22 | 23 | System.setProperty("webdriver.chrome.driver", 24 | "./src/test/resources/drivers/chromedriver"); 25 | driver = new ChromeDriver(); 26 | } 27 | 28 | @Test 29 | public void shouldPerformCompositeAction() { 30 | 31 | driver.get("http://guidebook.seleniumacademy.com/Selectable.html"); 32 | 33 | WebElement one = driver.findElement(By.name("one")); 34 | WebElement three = driver.findElement(By.name("three")); 35 | WebElement five = driver.findElement(By.name("five")); 36 | 37 | // Add all the actions into the Actions actions. 38 | Actions actions = new Actions(driver); 39 | actions.keyDown(Keys.CONTROL) 40 | .click(one) 41 | .click(three) 42 | .click(five) 43 | .keyUp(Keys.CONTROL); 44 | 45 | // Generate the composite action. 46 | Action compositeAction = actions.build(); 47 | 48 | // Perform the composite action. 49 | compositeAction.perform(); 50 | } 51 | 52 | @Test 53 | public void shouldPerformAction() { 54 | 55 | driver.get("http://guidebook.seleniumacademy.com/Selectable.html"); 56 | 57 | WebElement one = driver.findElement(By.name("one")); 58 | WebElement three = driver.findElement(By.name("three")); 59 | WebElement five = driver.findElement(By.name("five")); 60 | 61 | // Add all the actions into the Actions actions. 62 | Actions actions = new Actions(driver); 63 | actions.keyDown(Keys.CONTROL) 64 | .click(one) 65 | .click(three) 66 | .click(five) 67 | .keyUp(Keys.CONTROL); 68 | 69 | // Perform the action 70 | actions.perform(); 71 | } 72 | 73 | @Test 74 | public void shouldMoveByOffSet() { 75 | 76 | driver.get("http://guidebook.seleniumacademy.com/Selectable.html"); 77 | 78 | WebElement three = driver.findElement(By.name("three")); 79 | System.out.println("X coordinate: " + three.getLocation().getX() 80 | + ", Y coordinate: " + three.getLocation().getY()); 81 | Actions actions = new Actions(driver); 82 | actions.moveByOffset(three.getLocation().getX() + 1, three. 83 | getLocation().getY() + 1); 84 | actions.perform(); 85 | } 86 | 87 | @Test 88 | public void shouldMoveByOffSetAndClick() { 89 | 90 | driver.get("http://guidebook.seleniumacademy.com/Selectable.html"); 91 | 92 | WebElement seven = driver.findElement(By.name("seven")); 93 | System.out.println("X coordinate: " + seven.getLocation().getX() + 94 | ", Y coordinate: " + seven.getLocation().getY()); 95 | Actions actions = new Actions(driver); 96 | actions.moveByOffset(seven.getLocation().getX() + 1, seven. 97 | getLocation().getY() + 1).click(); 98 | actions.perform(); 99 | } 100 | 101 | @Test 102 | public void shouldMoveByOffSetAndClickMultiple() { 103 | 104 | driver.get("http://guidebook.seleniumacademy.com/Selectable.html"); 105 | 106 | WebElement one = driver.findElement(By.name("one")); 107 | WebElement eleven = driver.findElement(By.name("eleven")); 108 | WebElement five = driver.findElement(By.name("five")); 109 | int border = 1; 110 | int tileWidth = 100; 111 | int tileHeight = 80; 112 | Actions actions = new Actions(driver); 113 | 114 | //Click on One 115 | actions.moveByOffset(one.getLocation().getX() + border, one. 116 | getLocation().getY() + border).click(); 117 | actions.build().perform(); 118 | 119 | // Click on Eleven 120 | actions.moveByOffset(2 * tileWidth + 4 * border, 2 * tileHeight + 4 * border). 121 | click(); 122 | actions.build().perform(); 123 | 124 | //Click on Five 125 | actions.moveByOffset(-2 * tileWidth - 4 * border, -tileHeight - 2 * border). 126 | click(); 127 | actions.build().perform(); 128 | } 129 | 130 | @Test 131 | public void shouldClickOnElement() { 132 | 133 | driver.get("http://guidebook.seleniumacademy.com/Selectable.html"); 134 | 135 | WebElement one = driver.findElement(By.name("one")); 136 | WebElement eleven = driver.findElement(By.name("eleven")); 137 | WebElement five = driver.findElement(By.name("five")); 138 | Actions actions = new Actions(driver); 139 | 140 | // //Click on One 141 | // actions.click(one); 142 | // actions.build().perform(); 143 | // 144 | // // Click on Eleven 145 | // actions.click(eleven); 146 | // actions.build().perform(); 147 | // 148 | // //Click on Five 149 | // actions.click(five); 150 | // actions.build().perform(); 151 | 152 | actions.click(one) 153 | .click(eleven) 154 | .click(five) 155 | .build().perform(); 156 | } 157 | 158 | @Test 159 | public void shouldClickAndHold() { 160 | 161 | driver.get("http://guidebook.seleniumacademy.com/Sortable.html"); 162 | 163 | Actions actions = new Actions(driver); 164 | 165 | //Move tile3 to the position of tile2 166 | actions.moveByOffset(200, 20) 167 | .clickAndHold() 168 | .moveByOffset(120, 0) 169 | .perform(); 170 | } 171 | 172 | @Test 173 | public void shouldClickAndHoldElement() { 174 | 175 | driver.get("http://guidebook.seleniumacademy.com/Sortable.html"); 176 | 177 | Actions actions = new Actions(driver); 178 | WebElement three = driver.findElement(By.name("three")); 179 | 180 | //Move tile3 to the position of tile2 181 | actions.clickAndHold(three) 182 | .moveByOffset(120, 0) 183 | .perform(); 184 | } 185 | 186 | @Test 187 | public void shouldClickAndHoldAndRelease() { 188 | 189 | driver.get("http://guidebook.seleniumacademy.com/Sortable.html"); 190 | 191 | WebElement three = driver.findElement(By.name("three")); 192 | Actions actions = new Actions(driver); 193 | 194 | //Move tile3 to the position of tile2 195 | actions.clickAndHold(three) 196 | .moveByOffset(120, 0) 197 | .release() 198 | .perform(); 199 | } 200 | 201 | @Test 202 | public void shouldClickAndHoldAndReleaseOnElement() { 203 | 204 | driver.get("http://guidebook.seleniumacademy.com/Sortable.html"); 205 | 206 | WebElement three = driver.findElement(By.name("three")); 207 | WebElement two = driver.findElement(By.name("two")); 208 | Actions actions = new Actions(driver); 209 | 210 | //Move tile3 to the position of tile2 211 | actions.clickAndHold(three) 212 | .release(two) 213 | .perform(); 214 | } 215 | 216 | @Test 217 | public void shouldClickAndHoldAndMove() { 218 | 219 | driver.get("http://guidebook.seleniumacademy.com/Sortable.html"); 220 | 221 | WebElement three = driver.findElement(By.name("three")); 222 | Actions actions = new Actions(driver); 223 | 224 | //Move tile3 to the position of tile2 225 | actions.moveToElement(three) 226 | .clickAndHold() 227 | .moveByOffset(120, 0) 228 | .perform(); 229 | } 230 | 231 | @Test 232 | public void shouldDrag() { 233 | 234 | driver.get("http://guidebook.seleniumacademy.com/DragMe.html"); 235 | 236 | WebElement dragMe = driver.findElement(By.id("draggable")); 237 | Actions actions = new Actions(driver); 238 | actions.dragAndDropBy(dragMe, 300, 200).perform(); 239 | } 240 | 241 | @Test 242 | public void shouldDragAndDrop() { 243 | 244 | driver.get("http://guidebook.seleniumacademy.com/DragAndDrop.html"); 245 | 246 | WebElement src = driver.findElement(By.id("draggable")); 247 | WebElement trgt = driver.findElement(By.id("droppable")); 248 | Actions actions = new Actions(driver); 249 | actions.dragAndDrop(src, trgt).perform(); 250 | } 251 | 252 | @Test 253 | public void shouldDoubleClick() { 254 | 255 | driver.get("http://guidebook.seleniumacademy.com/DoubleClick.html"); 256 | 257 | WebElement dblClick= driver.findElement(By.name("dblClick")); 258 | Actions actions = new Actions(driver); 259 | actions.moveToElement(dblClick).doubleClick().perform(); 260 | } 261 | 262 | @Test 263 | public void shouldDoubleClickElement() { 264 | 265 | driver.get("http://guidebook.seleniumacademy.com/DoubleClick.html"); 266 | 267 | WebElement dblClick = driver.findElement(By.name("dblClick")); 268 | Actions actions = new Actions(driver); 269 | actions.doubleClick(dblClick).perform(); 270 | } 271 | 272 | @Test 273 | public void shouldContextClick() { 274 | 275 | driver.get("http://guidebook.seleniumacademy.com/ContextClick.html"); 276 | 277 | WebElement contextMenu = driver.findElement(By.id("div-context")); 278 | Actions actions = new Actions(driver); 279 | actions.contextClick(contextMenu) 280 | .click(driver.findElement(By.name("Item 4"))) 281 | .perform(); 282 | } 283 | 284 | @Test 285 | public void shouldContextClickAtCurrentLocation() { 286 | 287 | driver.get("http://guidebook.seleniumacademy.com/ContextClick.html"); 288 | 289 | WebElement contextMenu = driver.findElement(By.id("div-context")); 290 | Actions actions = new Actions(driver); 291 | actions.moveToElement(contextMenu) 292 | .contextClick() 293 | .click(driver.findElement(By.name("Item 4"))) 294 | .perform(); 295 | } 296 | 297 | @AfterMethod 298 | public void tearDown() { 299 | driver.quit(); 300 | } 301 | } 302 | 303 | -------------------------------------------------------------------------------- /Chapter04/src/test/resources/drivers/chromedriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter04/src/test/resources/drivers/chromedriver -------------------------------------------------------------------------------- /Chapter04/src/test/resources/drivers/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter04/src/test/resources/drivers/chromedriver.exe -------------------------------------------------------------------------------- /Chapter04/src/test/resources/suites/testng.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Chapter05/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | *.iml 3 | /.idea 4 | test-output -------------------------------------------------------------------------------- /Chapter05/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.example 8 | chapter6 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 1.8 13 | 3.12.0 14 | 6.13.1 15 | 0.14 16 | 23.2-jre 17 | 3.0.7 18 | 1.3.1 19 | 3.8.0 20 | 3.7.0 21 | 3.7 22 | 2.6 23 | 0.14 24 | 25 | 26 | 27 | 28 | org.seleniumhq.selenium 29 | selenium-java 30 | ${selenium.version} 31 | 32 | 33 | org.testng 34 | testng 35 | ${testng.version} 36 | 37 | 38 | com.github.javafaker 39 | javafaker 40 | ${javafaker.version} 41 | 42 | 43 | com.google.guava 44 | guava 45 | ${guava.version} 46 | 47 | 48 | com.aventstack 49 | extentreports 50 | ${extentreports.version} 51 | 52 | 53 | com.vimalselvam 54 | testng-extentsreport 55 | ${extenttestng.version} 56 | 57 | 58 | org.assertj 59 | assertj-core 60 | ${assertj.version} 61 | 62 | 63 | org.apache.commons 64 | commons-lang3 65 | ${commons.version} 66 | 67 | 68 | commons-io 69 | commons-io 70 | ${commons.io.version} 71 | 72 | 73 | com.github.javafaker 74 | javafaker 75 | ${javafaker.version} 76 | 77 | 78 | 79 | 80 | 81 | 82 | org.apache.maven.plugins 83 | maven-compiler-plugin 84 | ${maven.compiler.version} 85 | 86 | ${java.version} 87 | ${java.version} 88 | 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /Chapter05/src/test/java/com/example/IAmTheDriver.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.WebDriver; 4 | import org.openqa.selenium.chrome.ChromeDriver; 5 | import org.openqa.selenium.support.events.EventFiringWebDriver; 6 | 7 | public class IAmTheDriver { 8 | public static void main(String... args){ 9 | 10 | System.setProperty("webdriver.chrome.driver", 11 | "./src/test/resources/drivers/chromedriver"); 12 | 13 | WebDriver driver = new ChromeDriver(); 14 | 15 | try { 16 | EventFiringWebDriver eventFiringDriver = new 17 | EventFiringWebDriver(driver); 18 | IAmTheEventListener eventListener = new IAmTheEventListener(); 19 | eventFiringDriver.register(eventListener); 20 | eventFiringDriver.get("http://www.google.com"); 21 | eventFiringDriver.get("http://www.facebook.com"); 22 | eventFiringDriver.navigate().back(); 23 | } finally { 24 | driver.close(); 25 | driver.quit(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter05/src/test/java/com/example/IAmTheEventListener.java: -------------------------------------------------------------------------------- 1 | package com.example; 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.events.WebDriverEventListener; 7 | 8 | public class IAmTheEventListener implements WebDriverEventListener { 9 | @Override 10 | public void beforeAlertAccept(WebDriver webDriver) { 11 | 12 | } 13 | 14 | @Override 15 | public void afterAlertAccept(WebDriver webDriver) { 16 | 17 | } 18 | 19 | @Override 20 | public void afterAlertDismiss(WebDriver webDriver) { 21 | 22 | } 23 | 24 | @Override 25 | public void beforeAlertDismiss(WebDriver webDriver) { 26 | 27 | } 28 | 29 | @Override 30 | public void beforeNavigateTo(String url, WebDriver driver) { 31 | System.out.println("Before Navigate To: " + url 32 | + " and Current url is: " + driver.getCurrentUrl()); 33 | } 34 | 35 | @Override 36 | public void afterNavigateTo(String url, WebDriver driver) { 37 | System.out.println("After Navigate To: " + url 38 | + " and Current url is: " + driver.getCurrentUrl()); 39 | } 40 | 41 | @Override 42 | public void beforeNavigateBack(WebDriver driver) { 43 | System.out.println("Before Navigate Back. Right now I'm at " 44 | + driver.getCurrentUrl()); 45 | } 46 | 47 | @Override 48 | public void afterNavigateBack(WebDriver driver) { 49 | System.out.println("After Navigate Back. Right now I'm at " 50 | + driver.getCurrentUrl()); 51 | } 52 | 53 | @Override 54 | public void beforeNavigateForward(WebDriver webDriver) { 55 | 56 | } 57 | 58 | @Override 59 | public void afterNavigateForward(WebDriver webDriver) { 60 | 61 | } 62 | 63 | @Override 64 | public void beforeNavigateRefresh(WebDriver webDriver) { 65 | 66 | } 67 | 68 | @Override 69 | public void afterNavigateRefresh(WebDriver webDriver) { 70 | 71 | } 72 | 73 | @Override 74 | public void beforeFindBy(By by, WebElement webElement, WebDriver webDriver) { 75 | 76 | } 77 | 78 | @Override 79 | public void afterFindBy(By by, WebElement webElement, WebDriver webDriver) { 80 | 81 | } 82 | 83 | @Override 84 | public void beforeClickOn(WebElement webElement, WebDriver webDriver) { 85 | 86 | } 87 | 88 | @Override 89 | public void afterClickOn(WebElement webElement, WebDriver webDriver) { 90 | 91 | } 92 | 93 | @Override 94 | public void beforeChangeValueOf(WebElement webElement, WebDriver webDriver, CharSequence[] charSequences) { 95 | 96 | } 97 | 98 | @Override 99 | public void afterChangeValueOf(WebElement webElement, WebDriver webDriver, CharSequence[] charSequences) { 100 | 101 | } 102 | 103 | @Override 104 | public void beforeScript(String s, WebDriver webDriver) { 105 | 106 | } 107 | 108 | @Override 109 | public void afterScript(String s, WebDriver webDriver) { 110 | 111 | } 112 | 113 | @Override 114 | public void onException(Throwable throwable, WebDriver webDriver) { 115 | 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Chapter05/src/test/java/com/example/IAmTheEventListener2.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.apache.commons.io.IOUtils; 4 | import org.openqa.selenium.JavascriptExecutor; 5 | import org.openqa.selenium.WebDriver; 6 | import org.openqa.selenium.support.events.AbstractWebDriverEventListener; 7 | 8 | import java.io.IOException; 9 | import java.net.MalformedURLException; 10 | import java.net.URL; 11 | import java.nio.charset.StandardCharsets; 12 | 13 | public class IAmTheEventListener2 extends AbstractWebDriverEventListener { 14 | 15 | @Override 16 | public void beforeNavigateTo(String url, WebDriver driver) { 17 | System.out.println("Before Navigate To "+ url); 18 | } 19 | 20 | @Override 21 | public void beforeNavigateBack(WebDriver driver) { 22 | System.out.println("Before Navigate Back. Right now I'm at " 23 | + driver.getCurrentUrl()); 24 | } 25 | 26 | @Override 27 | public void afterNavigateTo(String to, WebDriver driver) { 28 | try { 29 | 30 | JavascriptExecutor jsExecutor = (JavascriptExecutor) driver; 31 | URL url = new URL("https://raw.githubusercontent.com/GoogleChrome/" + 32 | "accessibility-developer-tools/stable/dist/js/axs_testing.js"); 33 | String script = IOUtils.toString(url.openStream(), StandardCharsets.UTF_8); 34 | jsExecutor.executeScript(script); 35 | String report = (String) jsExecutor.executeScript("var results = axs.Audit.run();" + 36 | "return axs.Audit.createReport(results);"); 37 | System.out.println("### Accessibility Report for " + driver.getTitle() + "####"); 38 | System.out.println(report); 39 | System.out.println("### END ####"); 40 | 41 | // Get the Load Event End 42 | long loadEventEnd = (Long) jsExecutor.executeScript("return window.performance.timing.loadEventEnd;"); 43 | // Get the Navigation Event Start 44 | long navigationStart = (Long) jsExecutor.executeScript("return window.performance.timing.navigationStart;"); 45 | // Difference between Load Event End and Navigation Event Start is // Page Load Time 46 | System.out.println("Page Load Time is " + (loadEventEnd - navigationStart)/1000 + " seconds."); 47 | 48 | } catch (MalformedURLException e) { 49 | e.printStackTrace(); 50 | } catch (IOException e) { 51 | e.printStackTrace(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Chapter05/src/test/java/com/example/RegisteringMultipleListeners.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.WebDriver; 4 | import org.openqa.selenium.chrome.ChromeDriver; 5 | import org.openqa.selenium.support.events.EventFiringWebDriver; 6 | 7 | public class RegisteringMultipleListeners { 8 | public static void main(String... args){ 9 | 10 | System.setProperty("webdriver.chrome.driver", 11 | "./src/test/resources/drivers/chromedriver"); 12 | 13 | WebDriver driver = new ChromeDriver(); 14 | 15 | try { 16 | EventFiringWebDriver eventFiringDriver = new 17 | EventFiringWebDriver(driver); 18 | IAmTheEventListener eventListener = new IAmTheEventListener(); 19 | IAmTheEventListener2 eventListener2 = new 20 | IAmTheEventListener2(); 21 | eventFiringDriver.register(eventListener); 22 | eventFiringDriver.register(eventListener2); 23 | eventFiringDriver.get("http://www.google.com"); 24 | eventFiringDriver.get("http://www.facebook.com"); 25 | eventFiringDriver.navigate().back(); 26 | } finally { 27 | driver.close(); 28 | driver.quit(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Chapter05/src/test/resources/drivers/chromedriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter05/src/test/resources/drivers/chromedriver -------------------------------------------------------------------------------- /Chapter05/src/test/resources/drivers/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter05/src/test/resources/drivers/chromedriver.exe -------------------------------------------------------------------------------- /Chapter06/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | *.iml 3 | /.idea 4 | test-output -------------------------------------------------------------------------------- /Chapter06/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.example 8 | chapter7 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 1.8 13 | 3.12.0 14 | 6.13.1 15 | 0.14 16 | 23.2-jre 17 | 3.0.7 18 | 1.3.1 19 | 3.8.0 20 | 3.7.0 21 | 3.7 22 | 2.6 23 | 24 | 25 | 26 | 27 | org.seleniumhq.selenium 28 | selenium-java 29 | ${selenium.version} 30 | 31 | 32 | org.testng 33 | testng 34 | ${testng.version} 35 | 36 | 37 | com.github.javafaker 38 | javafaker 39 | ${javafaker.version} 40 | 41 | 42 | com.google.guava 43 | guava 44 | ${guava.version} 45 | 46 | 47 | com.aventstack 48 | extentreports 49 | ${extentreports.version} 50 | 51 | 52 | com.vimalselvam 53 | testng-extentsreport 54 | ${extenttestng.version} 55 | 56 | 57 | org.assertj 58 | assertj-core 59 | ${assertj.version} 60 | 61 | 62 | org.apache.commons 63 | commons-lang3 64 | ${commons.version} 65 | 66 | 67 | commons-io 68 | commons-io 69 | ${commons.io.version} 70 | 71 | 72 | 73 | 74 | 75 | 76 | org.apache.maven.plugins 77 | maven-compiler-plugin 78 | ${maven.compiler.version} 79 | 80 | ${java.version} 81 | ${java.version} 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /Chapter06/src/test/java/com/example/SearchTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.apache.commons.io.FileUtils; 4 | import org.openqa.selenium.*; 5 | import org.openqa.selenium.remote.DesiredCapabilities; 6 | import org.openqa.selenium.remote.RemoteWebDriver; 7 | import org.testng.annotations.AfterMethod; 8 | import org.testng.annotations.BeforeMethod; 9 | import org.testng.annotations.Test; 10 | 11 | import java.io.File; 12 | import java.io.IOException; 13 | import java.net.MalformedURLException; 14 | import java.net.URL; 15 | 16 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 17 | 18 | /** 19 | * @author upgundecha 20 | */ 21 | public class SearchTest { 22 | 23 | WebDriver driver; 24 | 25 | @BeforeMethod 26 | public void setup() throws MalformedURLException { 27 | 28 | DesiredCapabilities caps = new DesiredCapabilities(); 29 | 30 | /* 31 | for Google Chrome 32 | */ 33 | 34 | caps.setBrowserName("chrome"); 35 | 36 | /* 37 | for Mozilla Firefox 38 | */ 39 | 40 | //caps.setBrowserName("firefox"); 41 | //caps.setCapability("marionette", true); 42 | 43 | /* 44 | for IE 45 | */ 46 | 47 | //caps.setBrowserName("internet explorer"); 48 | 49 | driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), caps); 50 | driver.get("http://demo-store.seleniumacademy.com/"); 51 | 52 | } 53 | 54 | @Test 55 | public void searchProduct() { 56 | 57 | // find search box and enter search string 58 | WebElement searchBox = driver.findElement(By.name("q")); 59 | 60 | searchBox.sendKeys("Phones"); 61 | 62 | WebElement searchButton = 63 | driver.findElement(By.className("search-button")); 64 | 65 | searchButton.click(); 66 | 67 | assertThat(driver.getTitle()) 68 | .isEqualTo("Search results for: 'Phones'"); 69 | } 70 | 71 | @AfterMethod 72 | public void tearDown() { 73 | driver.quit(); 74 | } 75 | } 76 | 77 | -------------------------------------------------------------------------------- /Chapter06/src/test/resources/drivers/chromedriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter06/src/test/resources/drivers/chromedriver -------------------------------------------------------------------------------- /Chapter06/src/test/resources/drivers/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter06/src/test/resources/drivers/chromedriver.exe -------------------------------------------------------------------------------- /Chapter06/src/test/resources/suites/testng.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter07/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | *.iml 3 | /.idea 4 | test-output -------------------------------------------------------------------------------- /Chapter07/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.example 8 | chapter8 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 1.8 13 | 3.12.0 14 | 6.13.1 15 | 0.14 16 | 23.2-jre 17 | 3.0.7 18 | 1.3.1 19 | 3.8.0 20 | 3.7.0 21 | 3.7 22 | 2.6 23 | 24 | 25 | 26 | 27 | org.seleniumhq.selenium 28 | selenium-java 29 | ${selenium.version} 30 | 31 | 32 | org.testng 33 | testng 34 | ${testng.version} 35 | 36 | 37 | com.github.javafaker 38 | javafaker 39 | ${javafaker.version} 40 | 41 | 42 | com.google.guava 43 | guava 44 | ${guava.version} 45 | 46 | 47 | com.aventstack 48 | extentreports 49 | ${extentreports.version} 50 | 51 | 52 | com.vimalselvam 53 | testng-extentsreport 54 | ${extenttestng.version} 55 | 56 | 57 | org.assertj 58 | assertj-core 59 | ${assertj.version} 60 | 61 | 62 | org.apache.commons 63 | commons-lang3 64 | ${commons.version} 65 | 66 | 67 | commons-io 68 | commons-io 69 | ${commons.io.version} 70 | 71 | 72 | 73 | 74 | 75 | 76 | org.apache.maven.plugins 77 | maven-compiler-plugin 78 | ${maven.compiler.version} 79 | 80 | ${java.version} 81 | ${java.version} 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /Chapter07/src/test/java/com/example/BmiCalculatorTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | 4 | import java.net.URL; 5 | import java.text.MessageFormat; 6 | 7 | 8 | import org.openqa.selenium.By; 9 | import org.openqa.selenium.WebDriver; 10 | import org.openqa.selenium.WebElement; 11 | import org.openqa.selenium.remote.DesiredCapabilities; 12 | import org.openqa.selenium.remote.RemoteWebDriver; 13 | import org.testng.annotations.AfterMethod; 14 | import org.testng.annotations.BeforeMethod; 15 | import org.testng.annotations.Test; 16 | 17 | import static org.testng.Assert.assertEquals; 18 | 19 | public class BmiCalculatorTest { 20 | 21 | WebDriver driver; 22 | 23 | @BeforeMethod 24 | public void setUp() throws Exception { 25 | 26 | String SAUCE_USER = "upgundecha"; 27 | String SAUCE_KEY = "5768f2a9-33be-4ebd-9a5f-3826d7c38ec9"; 28 | 29 | DesiredCapabilities caps = new DesiredCapabilities(); 30 | caps.setCapability("platform", "OS X 10.9"); 31 | caps.setCapability("browserName", "Safari"); 32 | caps.setCapability("name", "BMI Calculator Test"); 33 | driver = new RemoteWebDriver( 34 | new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub'", 35 | SAUCE_USER, SAUCE_KEY)), caps); 36 | driver.get("http://bit.ly/1zdNrFZ"); 37 | 38 | } 39 | 40 | @Test 41 | public void testBmiCalc() { 42 | WebElement height = driver.findElement(By.name("heightCMS")); 43 | height.sendKeys("181"); 44 | 45 | WebElement weight = driver.findElement(By.name("weightKg")); 46 | weight.sendKeys("80"); 47 | 48 | WebElement calculateButton = driver.findElement(By.id("Calculate")); 49 | calculateButton.click(); 50 | 51 | WebElement bmi = driver.findElement(By.name("bmi")); 52 | assertEquals(bmi.getAttribute("value"), "24.4"); 53 | 54 | WebElement bmi_category = driver.findElement(By.name("bmi_category")); 55 | assertEquals(bmi_category.getAttribute("value"), "Normal"); 56 | } 57 | 58 | @AfterMethod 59 | public void tearDown() throws Exception { 60 | driver.quit(); 61 | } 62 | } -------------------------------------------------------------------------------- /Chapter07/src/test/java/com/example/SearchTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.Platform; 5 | import org.openqa.selenium.WebDriver; 6 | import org.openqa.selenium.WebElement; 7 | import org.openqa.selenium.remote.DesiredCapabilities; 8 | import org.openqa.selenium.remote.RemoteWebDriver; 9 | import org.testng.annotations.AfterMethod; 10 | import org.testng.annotations.BeforeMethod; 11 | import org.testng.annotations.Test; 12 | 13 | import java.net.MalformedURLException; 14 | import java.net.URL; 15 | 16 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 17 | 18 | /** 19 | * @author upgundecha 20 | */ 21 | public class SearchTest { 22 | 23 | WebDriver driver; 24 | 25 | @BeforeMethod 26 | public void setup() throws MalformedURLException { 27 | 28 | DesiredCapabilities caps = new DesiredCapabilities(); 29 | 30 | // for Google Chrome 31 | 32 | caps.setBrowserName("chrome"); 33 | caps.setPlatform(Platform.MAC); 34 | 35 | // for Mozilla Firefox 36 | 37 | //caps.setBrowserName("firefox"); 38 | //caps.setCapability("marionette", true); 39 | 40 | // for IE 41 | 42 | //caps.setBrowserName("internet explorer"); 43 | 44 | driver = new RemoteWebDriver(new URL("http://192.168.0.101:1111/wd/hub"), caps); 45 | driver.get("http://demo-store.seleniumacademy.com/"); 46 | 47 | } 48 | 49 | @Test 50 | public void searchProduct() { 51 | 52 | // find search box and enter search string 53 | WebElement searchBox = driver.findElement(By.name("q")); 54 | 55 | searchBox.sendKeys("Phones"); 56 | 57 | WebElement searchButton = 58 | driver.findElement(By.className("search-button")); 59 | 60 | searchButton.click(); 61 | 62 | assertThat(driver.getTitle()) 63 | .isEqualTo("Search results for: 'Phones'"); 64 | } 65 | 66 | @AfterMethod 67 | public void tearDown() { 68 | driver.quit(); 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /Chapter07/src/test/resources/drivers/chromedriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter07/src/test/resources/drivers/chromedriver -------------------------------------------------------------------------------- /Chapter07/src/test/resources/drivers/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter07/src/test/resources/drivers/chromedriver.exe -------------------------------------------------------------------------------- /Chapter07/src/test/resources/suites/testng.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter08/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | *.iml 3 | /.idea 4 | test-output -------------------------------------------------------------------------------- /Chapter08/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.example 8 | chapter11 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 1.8 13 | 3.12.0 14 | 6.13.1 15 | 0.14 16 | 23.2-jre 17 | 3.0.7 18 | 1.3.1 19 | 3.8.0 20 | 3.7.0 21 | 3.7 22 | 2.6 23 | 24 | 25 | 26 | 27 | org.seleniumhq.selenium 28 | selenium-java 29 | ${selenium.version} 30 | 31 | 32 | org.testng 33 | testng 34 | ${testng.version} 35 | 36 | 37 | com.github.javafaker 38 | javafaker 39 | ${javafaker.version} 40 | 41 | 42 | com.google.guava 43 | guava 44 | ${guava.version} 45 | 46 | 47 | com.aventstack 48 | extentreports 49 | ${extentreports.version} 50 | 51 | 52 | com.vimalselvam 53 | testng-extentsreport 54 | ${extenttestng.version} 55 | 56 | 57 | org.assertj 58 | assertj-core 59 | ${assertj.version} 60 | 61 | 62 | org.apache.commons 63 | commons-lang3 64 | ${commons.version} 65 | 66 | 67 | commons-io 68 | commons-io 69 | ${commons.io.version} 70 | 71 | 72 | com.opencsv 73 | opencsv 74 | 3.4 75 | 76 | 77 | org.apache.poi 78 | poi 79 | 3.12 80 | test 81 | 82 | 83 | org.apache.poi 84 | poi-ooxml 85 | 3.12 86 | test 87 | 88 | 89 | 90 | 91 | 92 | 93 | org.apache.maven.plugins 94 | maven-compiler-plugin 95 | ${maven.compiler.version} 96 | 97 | ${java.version} 98 | ${java.version} 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /Chapter08/src/test/java/com/example/SearchTest.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import org.openqa.selenium.chrome.ChromeDriver; 7 | import org.testng.annotations.AfterMethod; 8 | import org.testng.annotations.BeforeMethod; 9 | import org.testng.annotations.Parameters; 10 | import org.testng.annotations.Test; 11 | 12 | import java.util.List; 13 | 14 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 15 | 16 | /** 17 | * @author upgundecha 18 | */ 19 | public class SearchTest { 20 | 21 | WebDriver driver; 22 | 23 | @BeforeMethod 24 | public void setup() { 25 | 26 | System.setProperty("webdriver.chrome.driver", 27 | "./src/test/resources/drivers/chromedriver"); 28 | driver = new ChromeDriver(); 29 | driver.get("http://demo-store.seleniumacademy.com/"); 30 | 31 | } 32 | 33 | @Parameters({"searchWord", "items"}) 34 | @Test 35 | public void searchProduct(String searchWord, int items) { 36 | 37 | // find search box and enter search string 38 | WebElement searchBox = driver.findElement(By.name("q")); 39 | 40 | searchBox.sendKeys(searchWord); 41 | 42 | WebElement searchButton = 43 | driver.findElement(By.className("search-button")); 44 | 45 | searchButton.click(); 46 | 47 | assertThat(driver.getTitle()) 48 | .isEqualTo("Search results for: '" + searchWord + "'"); 49 | 50 | List searchItems = driver 51 | .findElements(By.xpath("//h2[@class='product-name']/a")); 52 | 53 | assertThat(searchItems.size()) 54 | .isEqualTo(items); 55 | } 56 | 57 | @AfterMethod 58 | public void tearDown() { 59 | driver.quit(); 60 | } 61 | } -------------------------------------------------------------------------------- /Chapter08/src/test/java/com/example/SearchTestWithCSVDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import com.opencsv.CSVReader; 4 | import org.openqa.selenium.By; 5 | import org.openqa.selenium.WebDriver; 6 | import org.openqa.selenium.WebElement; 7 | import org.openqa.selenium.chrome.ChromeDriver; 8 | import org.testng.annotations.AfterMethod; 9 | import org.testng.annotations.BeforeMethod; 10 | import org.testng.annotations.DataProvider; 11 | import org.testng.annotations.Test; 12 | 13 | import java.io.BufferedReader; 14 | import java.io.FileReader; 15 | import java.io.IOException; 16 | import java.util.ArrayList; 17 | import java.util.Iterator; 18 | import java.util.List; 19 | 20 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 21 | 22 | /** 23 | * @author upgundecha 24 | */ 25 | public class SearchTestWithCSVDataProvider { 26 | 27 | WebDriver driver; 28 | 29 | @DataProvider(name = "searchWords") 30 | public Iterator provider() throws Exception { 31 | 32 | CSVReader reader = new CSVReader( 33 | new FileReader("./src/test/resources/data/data.csv") 34 | , ',', '\'', 1); 35 | 36 | List myEntries = new ArrayList(); 37 | String[] nextLine; 38 | while ((nextLine = reader.readNext()) != null) { 39 | myEntries.add(nextLine); 40 | } 41 | reader.close(); 42 | return myEntries.iterator(); 43 | } 44 | 45 | @BeforeMethod 46 | public void setup() { 47 | 48 | System.setProperty("webdriver.chrome.driver", 49 | "./src/test/resources/drivers/chromedriver"); 50 | driver = new ChromeDriver(); 51 | driver.get("http://demo-store.seleniumacademy.com/"); 52 | 53 | } 54 | 55 | @Test(dataProvider = "searchWords") 56 | public void searchProduct(String searchWord, String items) { 57 | 58 | // find search box and enter search string 59 | WebElement searchBox = driver.findElement(By.name("q")); 60 | 61 | searchBox.sendKeys(searchWord); 62 | 63 | WebElement searchButton = 64 | driver.findElement(By.className("search-button")); 65 | 66 | searchButton.click(); 67 | 68 | assertThat(driver.getTitle()) 69 | .isEqualTo("Search results for: '" + searchWord + "'"); 70 | 71 | List searchItems = driver 72 | .findElements(By.xpath("//h2[@class='product-name']/a")); 73 | 74 | assertThat(searchItems.size()) 75 | .isEqualTo(Integer.parseInt(items)); 76 | } 77 | 78 | @AfterMethod 79 | public void tearDown() { 80 | driver.quit(); 81 | } 82 | } -------------------------------------------------------------------------------- /Chapter08/src/test/java/com/example/SearchTestWithDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import org.openqa.selenium.chrome.ChromeDriver; 7 | import org.testng.annotations.*; 8 | 9 | import java.util.List; 10 | 11 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 12 | 13 | /** 14 | * @author upgundecha 15 | */ 16 | public class SearchTestWithDataProvider { 17 | 18 | WebDriver driver; 19 | 20 | @DataProvider(name = "searchWords") 21 | public Object[][] provider() { 22 | return new Object[][]{ 23 | {"phones", 3}, 24 | {"music", 5}, 25 | {"iphone 5s", 0} 26 | }; 27 | } 28 | 29 | @BeforeMethod 30 | public void setup() { 31 | 32 | System.setProperty("webdriver.chrome.driver", 33 | "./src/test/resources/drivers/chromedriver"); 34 | driver = new ChromeDriver(); 35 | driver.get("http://demo-store.seleniumacademy.com/"); 36 | 37 | } 38 | 39 | @Test(dataProvider = "searchWords") 40 | public void searchProduct(String searchWord, int items) { 41 | 42 | // find search box and enter search string 43 | WebElement searchBox = driver.findElement(By.name("q")); 44 | 45 | searchBox.sendKeys(searchWord); 46 | 47 | WebElement searchButton = 48 | driver.findElement(By.className("search-button")); 49 | 50 | searchButton.click(); 51 | 52 | assertThat(driver.getTitle()) 53 | .isEqualTo("Search results for: '" + searchWord + "'"); 54 | 55 | List searchItems = driver 56 | .findElements(By.xpath("//h2[@class='product-name']/a")); 57 | 58 | assertThat(searchItems.size()) 59 | .isEqualTo(items); 60 | } 61 | 62 | @AfterMethod 63 | public void tearDown() { 64 | driver.quit(); 65 | } 66 | } -------------------------------------------------------------------------------- /Chapter08/src/test/java/com/example/SearchTestWithExcelDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import org.openqa.selenium.By; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.WebElement; 6 | import org.openqa.selenium.chrome.ChromeDriver; 7 | import org.testng.annotations.AfterMethod; 8 | import org.testng.annotations.BeforeMethod; 9 | import org.testng.annotations.DataProvider; 10 | import org.testng.annotations.Test; 11 | 12 | import java.util.List; 13 | 14 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; 15 | 16 | /** 17 | * @author upgundecha 18 | */ 19 | public class SearchTestWithExcelDataProvider { 20 | 21 | WebDriver driver; 22 | 23 | @DataProvider(name = "searchWords") 24 | public Object[][] provider() throws Exception { 25 | SpreadsheetData spreadsheetData = new SpreadsheetData(); 26 | return spreadsheetData.getCellData("./src/test/resources/data/data.xlsx"); 27 | } 28 | 29 | @BeforeMethod 30 | public void setup() { 31 | 32 | System.setProperty("webdriver.chrome.driver", 33 | "./src/test/resources/drivers/chromedriver"); 34 | driver = new ChromeDriver(); 35 | driver.get("http://demo-store.seleniumacademy.com/"); 36 | 37 | } 38 | 39 | @Test(dataProvider = "searchWords") 40 | public void searchProduct(String searchWord, String items) { 41 | 42 | // find search box and enter search string 43 | WebElement searchBox = driver.findElement(By.name("q")); 44 | 45 | searchBox.sendKeys(searchWord); 46 | 47 | WebElement searchButton = 48 | driver.findElement(By.className("search-button")); 49 | 50 | searchButton.click(); 51 | 52 | assertThat(driver.getTitle()) 53 | .isEqualTo("Search results for: '" + searchWord + "'"); 54 | 55 | List searchItems = driver 56 | .findElements(By.xpath("//h2[@class='product-name']/a")); 57 | 58 | assertThat(searchItems.size()) 59 | .isEqualTo(Integer.parseInt(items)); 60 | } 61 | 62 | @AfterMethod 63 | public void tearDown() { 64 | driver.quit(); 65 | } 66 | } -------------------------------------------------------------------------------- /Chapter08/src/test/java/com/example/SpreadsheetData.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | import org.apache.poi.ss.usermodel.WorkbookFactory; 7 | import org.apache.poi.ss.usermodel.Workbook; 8 | import org.apache.poi.ss.usermodel.Sheet; 9 | import org.apache.poi.ss.usermodel.Row; 10 | import org.apache.poi.ss.usermodel.Cell; 11 | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 12 | 13 | public class SpreadsheetData { 14 | public String[][] getCellData(String path) throws InvalidFormatException, IOException { 15 | System.out.println(path); 16 | FileInputStream stream = new FileInputStream(path); 17 | Workbook workbook = WorkbookFactory.create(stream); 18 | System.out.println(workbook); 19 | Sheet s = workbook.getSheetAt(0); 20 | System.out.println(s); 21 | int rowcount = s.getLastRowNum(); 22 | int cellcount = s.getRow(0).getLastCellNum(); 23 | System.out.println(rowcount); 24 | String data[][] = new String[rowcount][cellcount]; 25 | for (int rowCnt = 1; rowCnt <= rowcount; rowCnt++) { 26 | Row row = s.getRow(rowCnt); 27 | for (int colCnt = 0; colCnt < cellcount; colCnt++) { 28 | Cell cell = row.getCell(colCnt); 29 | try { 30 | if (cell.getCellType() == cell.CELL_TYPE_STRING) { 31 | data[rowCnt - 1][colCnt] = cell.getStringCellValue(); 32 | } else { 33 | data[rowCnt - 1][colCnt] = String.valueOf(cell.getNumericCellValue()); 34 | } 35 | } catch (Exception e) { 36 | e.printStackTrace(); 37 | } 38 | } 39 | } 40 | return data; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Chapter08/src/test/resources/data/data.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter08/src/test/resources/data/data.csv -------------------------------------------------------------------------------- /Chapter08/src/test/resources/data/data.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter08/src/test/resources/data/data.xlsx -------------------------------------------------------------------------------- /Chapter08/src/test/resources/drivers/chromedriver: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter08/src/test/resources/drivers/chromedriver -------------------------------------------------------------------------------- /Chapter08/src/test/resources/drivers/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-Selenium/3176ee947b186443577f786f82d7986dc9dd01ab/Chapter08/src/test/resources/drivers/chromedriver.exe -------------------------------------------------------------------------------- /Chapter08/src/test/resources/suites/testng.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Chapter18/BrowserUtils.java: -------------------------------------------------------------------------------- 1 | package com.framework.ux.utils.chapter10; 2 | 3 | import org.openqa.selenium.*; 4 | import org.openqa.selenium.support.ui.ExpectedConditions; 5 | import org.openqa.selenium.support.ui.WebDriverWait; 6 | 7 | /** 8 | * @author Carl Cocchiaro 9 | * 10 | * Browser Utility Class 11 | * 12 | */ 13 | public class BrowserUtils { 14 | 15 | /** 16 | * waitFor method to poll page title 17 | * 18 | * @param title 19 | * @param timer 20 | * @throws Exception 21 | */ 22 | 23 | public static void waitFor(String title, 24 | int timer) 25 | throws Exception { 26 | 27 | WebDriver driver = CreateDriver.getInstance().getDriver(); 28 | WebDriverWait exists = new WebDriverWait(driver, timer); 29 | 30 | exists.until( ExpectedConditions.refreshed(ExpectedConditions.titleContains(title)) ); 31 | } 32 | 33 | /** 34 | * waitForURL method to poll page URL 35 | * 36 | * @param url 37 | * @param timer 38 | * @throws Exception 39 | */ 40 | public static void waitForURL(String url, 41 | int timer) 42 | throws Exception { 43 | 44 | WebDriver driver = CreateDriver.getInstance().getDriver(); 45 | WebDriverWait exists = new WebDriverWait(driver, timer); 46 | 47 | exists.until( ExpectedConditions.refreshed(ExpectedConditions.urlContains(url)) ); 48 | } 49 | 50 | /** 51 | * waitForClickable method to poll for clickable 52 | * 53 | * @param by 54 | * @param timer 55 | * @throws Exception 56 | */ 57 | public static void waitForClickable(By by, 58 | int timer) 59 | throws Exception { 60 | 61 | WebDriver driver = CreateDriver.getInstance().getDriver(); 62 | WebDriverWait exists = new WebDriverWait(driver, timer); 63 | 64 | exists.until( ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(by)) ); 65 | } 66 | 67 | /** 68 | * click method using JavaScript API click 69 | * 70 | * @param by 71 | * @throws Exception 72 | */ 73 | public static void click(By by) throws Exception { 74 | 75 | WebDriver driver = CreateDriver.getInstance().getDriver(); 76 | WebElement element = driver.findElement(by); 77 | 78 | JavascriptExecutor js = (JavascriptExecutor)driver; 79 | js.executeScript("arguments[0].click();", element); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /Chapter18/CreateDriver.java: -------------------------------------------------------------------------------- 1 | package com.framework.ux.utils.chapter10; 2 | 3 | import org.openqa.selenium.WebDriver; 4 | import org.openqa.selenium.chrome.ChromeDriver; 5 | import org.openqa.selenium.chrome.ChromeOptions; 6 | import org.openqa.selenium.firefox.FirefoxDriver; 7 | import org.openqa.selenium.firefox.FirefoxOptions; 8 | import org.openqa.selenium.firefox.FirefoxProfile; 9 | import org.openqa.selenium.ie.InternetExplorerDriver; 10 | import org.openqa.selenium.ie.InternetExplorerOptions; 11 | import org.openqa.selenium.remote.DesiredCapabilities; 12 | import org.openqa.selenium.remote.RemoteWebDriver; 13 | 14 | import java.io.FileInputStream; 15 | import java.util.*; 16 | import java.util.concurrent.TimeUnit; 17 | 18 | /** 19 | * @author Carl Cocchiaro 20 | * 21 | * Selenium Driver Class 22 | * 23 | */ 24 | public class CreateDriver { 25 | // local variables 26 | private static CreateDriver instance = null; 27 | private static final int IMPLICIT_TIMEOUT = 0; 28 | private ThreadLocal webDriver = new ThreadLocal(); 29 | private ThreadLocal sessionId = new ThreadLocal(); 30 | private ThreadLocal sessionBrowser = new ThreadLocal(); 31 | private ThreadLocal sessionPlatform = new ThreadLocal(); 32 | private ThreadLocal sessionVersion = new ThreadLocal(); 33 | private String getEnv = null; 34 | private Properties props = new Properties(); 35 | 36 | // constructor 37 | private CreateDriver() { 38 | } 39 | 40 | /** 41 | * getInstance method to retrieve active driver instance 42 | * 43 | * @return CreateDriver 44 | */ 45 | public static CreateDriver getInstance() { 46 | if ( instance == null ) { 47 | instance = new CreateDriver(); 48 | } 49 | 50 | return instance; 51 | } 52 | 53 | /** 54 | * setDriver method to create driver instance 55 | * 56 | * @param browser 57 | * @param environment 58 | * @param platform 59 | * @param optPreferences 60 | * @throws Exception 61 | */ 62 | @SafeVarargs 63 | public final void setDriver(String browser, 64 | String platform, 65 | String environment, 66 | Map... optPreferences) 67 | throws Exception { 68 | 69 | DesiredCapabilities caps = null; 70 | String getPlatform = null; 71 | props.load(new FileInputStream(Global_VARS.SE_PROPS)); 72 | 73 | switch (browser) { 74 | case "firefox": 75 | caps = DesiredCapabilities.firefox(); 76 | 77 | FirefoxOptions ffOpts = new FirefoxOptions(); 78 | FirefoxProfile ffProfile = new FirefoxProfile(); 79 | 80 | ffProfile.setPreference("browser.autofocus", true); 81 | ffProfile.setPreference("browser.tabs.remote.autostart.2", false); 82 | 83 | caps.setCapability(FirefoxDriver.PROFILE, ffProfile); 84 | caps.setCapability("marionette", true); 85 | 86 | // then pass them to the local WebDriver 87 | if ( environment.equalsIgnoreCase("local") ) { 88 | System.setProperty("webdriver.gecko.driver", props.getProperty("gecko.driver.windows.path")); 89 | webDriver.set(new FirefoxDriver(ffOpts.merge(caps))); 90 | } 91 | 92 | break; 93 | case "chrome": 94 | caps = DesiredCapabilities.chrome(); 95 | 96 | ChromeOptions chOptions = new ChromeOptions(); 97 | Map chromePrefs = new HashMap(); 98 | 99 | chromePrefs.put("credentials_enable_service", false); 100 | 101 | chOptions.setExperimentalOption("prefs", chromePrefs); 102 | chOptions.addArguments("--disable-plugins", "--disable-extensions", "--disable-popup-blocking"); 103 | 104 | caps.setCapability(ChromeOptions.CAPABILITY, chOptions); 105 | caps.setCapability("applicationCacheEnabled", false); 106 | 107 | if ( environment.equalsIgnoreCase("local") ) { 108 | System.setProperty("webdriver.chrome.driver", props.getProperty("chrome.driver.windows.path")); 109 | webDriver.set(new ChromeDriver(chOptions.merge(caps))); 110 | } 111 | 112 | break; 113 | case "internet explorer": 114 | caps = DesiredCapabilities.internetExplorer(); 115 | 116 | InternetExplorerOptions ieOpts = new InternetExplorerOptions(); 117 | 118 | ieOpts.requireWindowFocus(); 119 | ieOpts.merge(caps); 120 | 121 | caps.setCapability("requireWindowFocus", true); 122 | 123 | if ( environment.equalsIgnoreCase("local") ) { 124 | System.setProperty("webdriver.ie.driver", props.getProperty("ie.driver.windows.path")); 125 | webDriver.set(new InternetExplorerDriver(ieOpts.merge(caps))); 126 | } 127 | 128 | break; 129 | } 130 | 131 | getEnv = environment; 132 | getPlatform = platform; 133 | sessionId.set(((RemoteWebDriver) webDriver.get()).getSessionId().toString()); 134 | sessionBrowser.set(caps.getBrowserName()); 135 | sessionVersion.set(caps.getVersion()); 136 | sessionPlatform.set(getPlatform); 137 | 138 | System.out.println("\n*** TEST ENVIRONMENT = " 139 | + getSessionBrowser().toUpperCase() 140 | + "/" + getSessionPlatform().toUpperCase() 141 | + "/" + getEnv.toUpperCase() 142 | + "/Selenium Version=" + props.getProperty("selenium.revision") 143 | + "/Session ID=" + getSessionId() + "\n"); 144 | 145 | getDriver().manage().timeouts().implicitlyWait(IMPLICIT_TIMEOUT, TimeUnit.SECONDS); 146 | getDriver().manage().window().maximize(); 147 | } 148 | 149 | /** 150 | * getDriver method to retrieve active driver 151 | * 152 | * @return WebDriver 153 | */ 154 | public WebDriver getDriver() { 155 | return webDriver.get(); 156 | } 157 | 158 | /** 159 | * closeDriver method to close active driver 160 | * 161 | */ 162 | public void closeDriver() { 163 | try { 164 | getDriver().quit(); 165 | } 166 | 167 | catch ( Exception e ) { 168 | // do something 169 | } 170 | } 171 | 172 | /** 173 | * getSessionId method to retrieve active id 174 | * 175 | * @return String 176 | * @throws Exception 177 | */ 178 | public String getSessionId() throws Exception { 179 | return sessionId.get(); 180 | } 181 | 182 | /** 183 | * getSessionBrowser method to retrieve active browser 184 | * @return String 185 | * @throws Exception 186 | */ 187 | public String getSessionBrowser() throws Exception{ 188 | return sessionBrowser.get(); 189 | } 190 | 191 | /** 192 | * getSessionVersion method to retrieve active version 193 | * 194 | * @return String 195 | * @throws Exception 196 | */ 197 | public String getSessionVersion() throws Exception { 198 | return sessionVersion.get(); 199 | } 200 | 201 | /** 202 | * getSessionPlatform method to retrieve active platform 203 | * @return String 204 | * @throws Exception 205 | */ 206 | public String getSessionPlatform() throws Exception { 207 | return sessionPlatform.get(); 208 | } 209 | 210 | } -------------------------------------------------------------------------------- /Chapter18/ExtentTestNGIReporterListener.java: -------------------------------------------------------------------------------- 1 | package com.framework.ux.utils.chapter10; 2 | 3 | import com.aventstack.extentreports.ExtentReports; 4 | import com.aventstack.extentreports.ExtentTest; 5 | import com.aventstack.extentreports.MediaEntityBuilder; 6 | import com.aventstack.extentreports.Status; 7 | import com.aventstack.extentreports.reporter.ExtentHtmlReporter; 8 | import com.aventstack.extentreports.reporter.configuration.Protocol; 9 | import com.aventstack.extentreports.reporter.configuration.Theme; 10 | import org.testng.*; 11 | import org.testng.xml.XmlSuite; 12 | 13 | import java.io.File; 14 | import java.io.PrintWriter; 15 | import java.io.StringWriter; 16 | import java.io.Writer; 17 | import java.util.*; 18 | 19 | /** 20 | * @author Carl Cocchiaro 21 | * 22 | * ExtentReports HTML Reporter Class 23 | * 24 | */ 25 | public class ExtentTestNGIReporterListener implements IReporter { 26 | private String bitmapDir = Global_VARS.REPORT_PATH; 27 | private String seleniumRev = "3.7.1", docTitle = "SELENIUM FRAMEWORK DESIGN IN DATA-DRIVEN TESTING"; 28 | private ExtentReports extent; 29 | 30 | /** 31 | * generateReport method 32 | * 33 | * @param xmlSuites 34 | * @param suites 35 | * @param outputDirectory 36 | */ 37 | @Override 38 | public void generateReport(List xmlSuites, 39 | List suites, 40 | String outputDirectory) { 41 | 42 | for (ISuite suite : suites) { 43 | init(suite); 44 | Map results = suite.getResults(); 45 | 46 | for ( ISuiteResult result : results.values() ) { 47 | try { 48 | processTestResults(result); 49 | 50 | } catch (Exception e) { 51 | e.printStackTrace(); 52 | } 53 | } 54 | } 55 | 56 | extent.flush(); 57 | } 58 | 59 | /** 60 | * init method to customize report 61 | * 62 | * @param suite 63 | */ 64 | private void init(ISuite suite) { 65 | File directory = new File(Global_VARS.REPORT_PATH); 66 | 67 | if ( !directory.exists() ) { 68 | directory.mkdirs(); 69 | } 70 | 71 | ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(Global_VARS.REPORT_PATH + suite.getName() + ".html"); 72 | 73 | // report attributes 74 | htmlReporter.config().setDocumentTitle(docTitle); 75 | htmlReporter.config().setReportName(suite.getName().replace("_", " ")); 76 | htmlReporter.config().setChartVisibilityOnOpen(false); 77 | htmlReporter.config().setTheme(Theme.STANDARD); 78 | htmlReporter.config().setEncoding("UTF-8"); 79 | htmlReporter.config().setProtocol(Protocol.HTTPS); 80 | htmlReporter.config().setTimeStampFormat("MMM-dd-yyyy HH:mm:ss a"); 81 | htmlReporter.loadXMLConfig(new File(Global_VARS.REPORT_CONFIG_FILE)); 82 | 83 | extent = new ExtentReports(); 84 | 85 | // report system info 86 | extent.setSystemInfo("Browser", Global_VARS.DEF_BROWSER); 87 | extent.setSystemInfo("Environment", Global_VARS.DEF_ENVIRONMENT); 88 | extent.setSystemInfo("Platform", Global_VARS.DEF_PLATFORM); 89 | extent.setSystemInfo("OS Version", System.getProperty("os.version")); 90 | extent.setSystemInfo("Java Version", System.getProperty("java.version")); 91 | extent.setSystemInfo("Selenium Version", seleniumRev); 92 | 93 | extent.attachReporter(htmlReporter); 94 | extent.setReportUsesManualConfiguration(true); 95 | } 96 | 97 | /** 98 | * processTestResults method to create report 99 | * 100 | * @param r 101 | * @throws Exception 102 | */ 103 | private void processTestResults(ISuiteResult r) throws Exception { 104 | ExtentTest test = null; 105 | Status status = null; 106 | String message = null; 107 | 108 | // gather results 109 | Set passed = r.getTestContext().getPassedTests().getAllResults(); 110 | Set failed = r.getTestContext().getFailedTests().getAllResults(); 111 | Set skipped = r.getTestContext().getSkippedTests().getAllResults(); 112 | Set configs = r.getTestContext().getFailedConfigurations().getAllResults(); 113 | Set tests = new HashSet(); 114 | 115 | tests.addAll(passed); 116 | tests.addAll(skipped); 117 | tests.addAll(failed); 118 | 119 | // process results 120 | if ( tests.size() > 0 ) { 121 | // sort results by the Date field 122 | List resultList = new LinkedList(tests); 123 | 124 | class ResultComparator implements Comparator { 125 | public int compare(ITestResult r1, ITestResult r2) { 126 | return getTime(r1.getStartMillis()).compareTo(getTime(r2.getStartMillis())); 127 | } 128 | } 129 | 130 | Collections.sort(resultList , new ResultComparator ()); 131 | 132 | for ( ITestResult result : resultList ) { 133 | if ( getTestParams(result).isEmpty() ) { 134 | test = extent.createTest(result.getMethod().getMethodName()); 135 | } 136 | 137 | else { 138 | if ( getTestParams(result).split(",")[0].contains(result.getMethod().getMethodName()) ) { 139 | test = extent.createTest(getTestParams(result).split(",")[0], getTestParams(result).split(",")[1]); 140 | } 141 | 142 | else { 143 | test = extent.createTest(result.getMethod().getMethodName(), getTestParams(result).split(",")[1]); 144 | } 145 | } 146 | 147 | test.getModel().setStartTime(getTime(result.getStartMillis())); 148 | test.getModel().setEndTime(getTime(result.getEndMillis())); 149 | 150 | for ( String group : result.getMethod().getGroups() ) { 151 | if ( !group.isEmpty() ) { 152 | test.assignCategory(group); 153 | } 154 | 155 | else { 156 | int size = result.getMethod().getTestClass().toString().split("\\.").length; 157 | String testName = result.getMethod().getRealClass().getName().toString().split("\\.")[size-1]; 158 | test.assignCategory(testName); 159 | } 160 | } 161 | 162 | // get status 163 | switch(result.getStatus() ) { 164 | case 1: 165 | status = Status.PASS; 166 | break; 167 | case 2: 168 | status = Status.FAIL; 169 | break; 170 | case 3: 171 | status = Status.SKIP; 172 | break; 173 | default: 174 | status = Status.INFO; 175 | break; 176 | } 177 | 178 | // set colors of status 179 | if ( status.equals(Status.PASS) ) { 180 | message = "" + status.toString().toUpperCase() + ""; 181 | } 182 | 183 | else if ( status.equals(Status.FAIL) ) { 184 | message = "" + status.toString().toUpperCase() + ""; 185 | } 186 | 187 | else if ( status.equals(Status.SKIP) ) { 188 | message = "" + status.toString().toUpperCase() + ""; 189 | } 190 | 191 | else { 192 | message = "" + status.toString().toUpperCase() + ""; 193 | } 194 | 195 | // log status in report 196 | test.log(status, message); 197 | 198 | if ( !getTestParams(result).isEmpty() ) { 199 | test.log(Status.INFO, "TEST DATA = [" + getTestParams(result) + "]"); 200 | } 201 | 202 | if ( result.getThrowable() != null ) { 203 | test.log(Status.INFO, "EXCEPTION = [" + result.getThrowable().getMessage() + "]"); 204 | 205 | if ( !getTestParams(result).isEmpty() ) { 206 | // must capture screenshot to include in report 207 | if ( result.getAttribute("testBitmap") != null) { 208 | test.log(Status.INFO, "SCREENSHOT", MediaEntityBuilder.createScreenCaptureFromPath(bitmapDir + result.getAttribute("testBitmap")).build()); 209 | } 210 | 211 | test.log(Status.INFO, "STACKTRACE" + getStrackTrace(result)); 212 | } 213 | } 214 | } 215 | } 216 | } 217 | 218 | /** 219 | * getTime method to retrieve current date/time 220 | * 221 | * @param millis 222 | * @return Date 223 | */ 224 | private Date getTime(long millis) { 225 | Calendar calendar = Calendar.getInstance(); 226 | calendar.setTimeInMillis(millis); 227 | 228 | return calendar.getTime(); 229 | } 230 | 231 | /** 232 | * getTestParams method to retieve test parameters 233 | * 234 | * @param tr 235 | * @return String 236 | * @throws Exception 237 | */ 238 | private String getTestParams(ITestResult tr) throws Exception { 239 | TestNG_ConsoleRunner runner = new TestNG_ConsoleRunner(); 240 | 241 | return runner.getTestParams(tr); 242 | } 243 | 244 | /** 245 | * getStrackTrace method to retrieve stack trace 246 | * 247 | * @param result 248 | * @return String 249 | */ 250 | private String getStrackTrace(ITestResult result) { 251 | Writer writer = new StringWriter(); 252 | PrintWriter printWriter = new PrintWriter(writer); 253 | result.getThrowable().printStackTrace(printWriter); 254 | 255 | return "
\n" + writer.toString().replace(System.lineSeparator(), "
\n"); 256 | } 257 | 258 | } -------------------------------------------------------------------------------- /Chapter18/Global_VARS.java: -------------------------------------------------------------------------------- 1 | package com.framework.ux.utils.chapter10; 2 | 3 | import java.io.File; 4 | 5 | /** 6 | * @author Carl Cocchiaro 7 | * 8 | * Global Variable Utility Class 9 | * 10 | */ 11 | public class Global_VARS { 12 | // browser defaults 13 | public static final String BROWSER = "chrome"; 14 | public static final String PLATFORM = "Windows 7"; 15 | public static final String ENVIRONMENT = "local"; 16 | public static String DEF_BROWSER = null; 17 | public static String DEF_PLATFORM = null; 18 | public static String DEF_ENVIRONMENT = null; 19 | 20 | // suite folder defaults 21 | public static String SUITE_NAME = null; 22 | public static final String TARGET_URL = "http://www.practiceselenium.com/"; 23 | public static String propFile = "src/main/java/com/framework/ux/utils/chapter10/selenium.properties"; 24 | public static final String SE_PROPS = new File(propFile).getAbsolutePath(); 25 | public static final String TEST_OUTPUT_PATH = "test-output/"; 26 | public static final String LOGFILE_PATH = TEST_OUTPUT_PATH + "Logs/"; 27 | public static final String REPORT_PATH = TEST_OUTPUT_PATH + "Reports/"; 28 | public static final String REPORT_CONFIG_FILE = "src/main/java/com/framework/ux/utils/chapter10/extent-config.xml"; 29 | 30 | // suite timeout defaults 31 | public static final int TIMEOUT_MINUTE = 60; 32 | public static final int TIMEOUT_ELEMENT = 10; 33 | } 34 | -------------------------------------------------------------------------------- /Chapter18/JSONDataProvider.java: -------------------------------------------------------------------------------- 1 | package com.framework.ux.utils.chapter10; 2 | 3 | import org.json.simple.JSONArray; 4 | import org.json.simple.JSONObject; 5 | import org.json.simple.parser.JSONParser; 6 | import org.testng.annotations.DataProvider; 7 | 8 | import java.io.FileReader; 9 | import java.lang.reflect.Method; 10 | import java.util.ArrayList; 11 | import java.util.Arrays; 12 | import java.util.List; 13 | 14 | /** 15 | * @author Carl Cocchiaro 16 | * 17 | * TestNG JSON DataProvider Utility Class 18 | * 19 | */ 20 | public class JSONDataProvider { 21 | public static String dataFile = ""; 22 | public static String testCaseName = "NA"; 23 | 24 | public JSONDataProvider() throws Exception { 25 | } 26 | 27 | /** 28 | * fetchData method to retrieve test data for specified method 29 | * 30 | * @param method 31 | * @return Object[][] 32 | * @throws Exception 33 | */ 34 | @DataProvider(name = "fetchData_JSON") 35 | public static Object[][] fetchData(Method method) throws Exception { 36 | Object rowID, description; 37 | Object[][] result; 38 | testCaseName = method.getName(); 39 | List testDataList = new ArrayList(); 40 | JSONArray testData = (JSONArray) extractData_JSON(dataFile).get(method.getName()); 41 | 42 | for ( int i = 0; i < testData.size(); i++ ) { 43 | testDataList.add((JSONObject) testData.get(i)); 44 | } 45 | 46 | // include Filter 47 | if ( System.getProperty("includePattern") != null ) { 48 | String include = System.getProperty("includePattern"); 49 | List newList = new ArrayList(); 50 | List tests = Arrays.asList(include.split(",", -1)); 51 | 52 | for ( String getTest : tests ) { 53 | for ( int i = 0; i < testDataList.size(); i++ ) { 54 | if ( testDataList.get(i).toString().contains(getTest) ) { 55 | newList.add(testDataList.get(i)); 56 | } 57 | } 58 | } 59 | 60 | // reassign testRows after filtering tests 61 | testDataList = newList; 62 | } 63 | 64 | // exclude Filter 65 | if ( System.getProperty("excludePattern") != null ) { 66 | String exclude =System.getProperty("excludePattern"); 67 | List tests = Arrays.asList(exclude.split(",", -1)); 68 | 69 | for ( String getTest : tests ) { 70 | // start at end of list and work backwards so index stays in sync 71 | for ( int i = testDataList.size() - 1 ; i >= 0; i-- ) { 72 | if ( testDataList.get(i).toString().contains(getTest) ) { 73 | testDataList.remove(testDataList.get(i)); 74 | } 75 | } 76 | } 77 | } 78 | 79 | // create object for dataprovider to return 80 | try { 81 | result = new Object[testDataList.size()][testDataList.get(0).size()]; 82 | 83 | for ( int i = 0; i < testDataList.size(); i++ ) { 84 | rowID = testDataList.get(i).get("rowID"); 85 | description = testDataList.get(i).get("description"); 86 | result[i] = new Object[] { rowID, description, testDataList.get(i) }; 87 | } 88 | } 89 | 90 | catch(IndexOutOfBoundsException ie) { 91 | result = new Object[0][0]; 92 | } 93 | 94 | return result; 95 | } 96 | 97 | /** 98 | * extractData_JSON method to get JSON data from file 99 | * 100 | * @param file 101 | * @return JSONObject 102 | * @throws Exception 103 | */ 104 | public static JSONObject extractData_JSON(String file) throws Exception { 105 | FileReader reader = new FileReader(file); 106 | JSONParser jsonParser = new JSONParser(); 107 | 108 | return (JSONObject) jsonParser.parse(reader); 109 | } 110 | 111 | } 112 | -------------------------------------------------------------------------------- /Chapter18/PassionTeaCo.json: -------------------------------------------------------------------------------- 1 | { 2 | "tc001_passionTeaCo": [ 3 | { 4 | "rowID": "tc001_passionTeaCo.01", 5 | "description": "Navigate Passion Tea Co 'Welcome' Page", 6 | "menu": "Welcome", 7 | "title": "Welcome" 8 | }, 9 | { 10 | "rowID": "tc001_passionTeaCo.02", 11 | "description": "Navigate Passion Tea Co 'Our Passion' Page", 12 | "menu": "Our Passion", 13 | "title": "Our Passion" 14 | }, 15 | { 16 | "rowID": "tc001_passionTeaCo.03", 17 | "description": "Navigate Passion Tea Co 'Menu' Page", 18 | "menu": "Menu", 19 | "title": "Menu" 20 | }, 21 | { 22 | "rowID": "tc001_passionTeaCo.04", 23 | "description": "Navigate Passion Tea Co 'Let's Talk Tea' Page", 24 | "menu": "Talk Tea", 25 | "title": "Let's Talk Tea" 26 | }, 27 | { 28 | "rowID": "tc001_passionTeaCo.05", 29 | "description": "Navigate Passion Tea Co 'Check Out' Page", 30 | "menu": "Check Out", 31 | "title": "Check Out" 32 | } 33 | ], 34 | 35 | "tc002_passionTeaCo": [ 36 | { 37 | "rowID": "tc002_passionTeaCo.01", 38 | "description": "Verify Image Source 'TEA CUP'", 39 | "img": "TEA_CUP", 40 | "src": "http://nebula.wsimg.com/7cbbd331e278a100b443a12aa4cce77b?AccessKeyId=7ECBEB9592E2269F1812&disposition=0&alloworigin=1" 41 | }, 42 | { 43 | "rowID": "tc002_passionTeaCo.02", 44 | "description": "Verify Image Source 'HERBAL TEA'", 45 | "img": "HERBAL_TEA", 46 | "src": "http://nebula.wsimg.com/d892360c0e73575efa3e5307c619db41?AccessKeyId=7ECBEB9592E2269F1812&disposition=0&alloworigin=1" 47 | }, 48 | { 49 | "rowID": "tc002_passionTeaCo.03", 50 | "description": "Verify Image Source 'LOOSE TEA'", 51 | "img": "LOOSE_TEA", 52 | "src": "http://nebula.wsimg.com/18f9b21e513a597e4b8d4c805321bbe3?AccessKeyId=7ECBEB9592E2269F1812&disposition=0&alloworigin=1" 53 | }, 54 | { 55 | "rowID": "tc002_passionTeaCo.04", 56 | "description": "Verify Image Source 'FLAVORED TEA'", 57 | "img": "FLAVORED_TEA", 58 | "src": "http://nebula.wsimg.com/d0554952ea0bea9e79bf01ab564bf666?AccessKeyId=7ECBEB9592E2269F1812&disposition=0&alloworigin=1" 59 | }, 60 | { 61 | "rowID": "tc002_passionTeaCo.05", 62 | "description": "Verify Image Source 'PASSION TEA CO'", 63 | "img": "PASSION_TEA_CO", 64 | "src": "http://nebula.wsimg.com/01e56eb76d18b60c5fb3dcf451c080a1?AccessKeyId=7ECBEB9592E2269F1812&disposition=0&alloworigin=1" 65 | }, 66 | { 67 | "rowID": "tc002_passionTeaCo.06", 68 | "description": "Verify Image Source 'LEAF'", 69 | "img": "LEAF", 70 | "src": "http://nebula.wsimg.com/ab7db4b80e0c0644f5f9226f2970739b?AccessKeyId=7ECBEB9592E2269F1812&disposition=0&alloworigin=1" 71 | }, 72 | { 73 | "rowID": "tc002_passionTeaCo.07", 74 | "description": "Verify Image Source 'ORGANIC'", 75 | "img": "ORGANIC", 76 | "src": "http://nebula.wsimg.com/cd390673d46bead889c368ae135a6ec2?AccessKeyId=7ECBEB9592E2269F1812&disposition=0&alloworigin=1" 77 | } 78 | ], 79 | 80 | "tc003_passionTeaCo": [ 81 | { 82 | "rowID": "tc003_passionTeaCo.01", 83 | "description": "Verify Span Text 'See our line of organic teas.'", 84 | "pattern": "See our line", 85 | "text": "See our line of organic teas." 86 | }, 87 | { 88 | "rowID": "tc003_passionTeaCo.02", 89 | "description": "Verify Span Text 'Tea of the month club'", 90 | "pattern": "month club", 91 | "text": "Tea of the month club" 92 | }, 93 | { 94 | "rowID": "tc003_passionTeaCo.03", 95 | "description": "Verify Span Text 'It's the gift that keeps on giving all year long.'", 96 | "pattern": "gift that keeps on giving", 97 | "text": "It's the gift that keeps on giving all year long." 98 | }, 99 | { 100 | "rowID": "tc003_passionTeaCo.04", 101 | "description": "Verify Span Text 'For more than 25 years...'", 102 | "pattern": "For more than 25 years, Passion Tea Company has revolutionized the tea industry", 103 | "text": "For more than 25 years, Passion Tea Company has revolutionized the tea industry by letting our customers create a blend that combines their favorite herbs and spices. We offer thousands of natural flavors from all over the world and want you to have the opportunity to create a tea, and call it yours! We proudly partner with seleniumframework.com to help them use our website for Continuous Test Automation practice exercises " 104 | }, 105 | { 106 | "rowID": "tc003_passionTeaCo.05", 107 | "description": "Verify Span Text 'Herbal Tea'", 108 | "pattern": "Herbal Tea", 109 | "text": "Herbal Tea" 110 | }, 111 | { 112 | "rowID": "tc003_passionTeaCo.06", 113 | "description": "Verify Span Text 'Loose Tea'", 114 | "pattern": "Loose Tea", 115 | "text": "Loose Tea" 116 | }, 117 | { 118 | "rowID": "tc003_passionTeaCo.07", 119 | "description": "Verify Span Text 'Flavored Tea'", 120 | "pattern": "Flavored Tea", 121 | "text": "Flavored Tea." 122 | } 123 | ], 124 | 125 | "tc004_passionTeaCo": [ 126 | { 127 | "rowID": "tc004_passionTeaCo.01", 128 | "description": "Verify Heading Text 'We're passionate about tea.'", 129 | "pattern": "passionate about tea", 130 | "text": "We're passionate about tea. " 131 | } 132 | ], 133 | 134 | "tc005_passionTeaCo": [ 135 | { 136 | "rowID": "tc005_passionTeaCo.01", 137 | "description": "Verify Paragraph Text 'Copyright...'", 138 | "pattern": "Copyright", 139 | "text": "Copyright Selenium Practice Website. All rights reserved." 140 | } 141 | ], 142 | 143 | "tc006_passionTeaCo": [ 144 | { 145 | "rowID": "tc006_passionTeaCo.01", 146 | "description": "Verify Menu Link Text 'MENU'", 147 | "element": "MENU", 148 | "title": "Menu" 149 | }, 150 | { 151 | "rowID": "tc006_passionTeaCo.02", 152 | "description": "Verify Menu Link Text 'MORE 1'", 153 | "element": "MORE_1", 154 | "title": "Menu" 155 | }, 156 | { 157 | "rowID": "tc006_passionTeaCo.03", 158 | "description": "Verify Menu Link Text 'MORE 2'", 159 | "element": "MORE_2", 160 | "title": "Menu" 161 | }, 162 | { 163 | "rowID": "tc006_passionTeaCo.04", 164 | "description": "Verify Menu Link Text 'HERBAL TEA'", 165 | "element": "HERBAL_TEA", 166 | "title": "Menu" 167 | }, 168 | { 169 | "rowID": "tc006_passionTeaCo.05", 170 | "description": "Verify Menu Link Text 'LOOSE TEA'", 171 | "element": "LOOSE_TEA", 172 | "title": "Menu" 173 | }, 174 | { 175 | "rowID": "tc006_passionTeaCo.06", 176 | "description": "Verify Menu Link Text 'FLAVORED TEA'", 177 | "element": "FLAVORED_TEA", 178 | "title": "Menu" 179 | }, 180 | { 181 | "rowID": "tc006_passionTeaCo.07", 182 | "description": "Verify Menu Link Text 'SEE COLLECTION 1'", 183 | "element": "SEE_COLLECTION1", 184 | "title": "Menu" 185 | }, 186 | { 187 | "rowID": "tc006_passionTeaCo.08", 188 | "description": "Verify Menu Link Text 'SEE COLLECTION 2'", 189 | "element": "SEE_COLLECTION2", 190 | "title": "Menu" 191 | }, 192 | { 193 | "rowID": "tc006_passionTeaCo.09", 194 | "description": "Verify Menu Link Text 'SEE COLLECTION 3'", 195 | "element": "SEE_COLLECTION3", 196 | "title": "Menu" 197 | } 198 | ] 199 | } 200 | 201 | -------------------------------------------------------------------------------- /Chapter18/PassionTeaCo.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 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Chapter18/PassionTeaCoBasePO.java: -------------------------------------------------------------------------------- 1 | package com.framework.ux.utils.chapter10; 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 static org.testng.Assert.assertEquals; 10 | 11 | /** 12 | * @author Carl Cocchiaro 13 | * 14 | * Passion Tea Company Base Page Object Class 15 | * 16 | */ 17 | public abstract class PassionTeaCoBasePO { 18 | // local variables 19 | protected String pageTitle = ""; 20 | 21 | // constructor 22 | public PassionTeaCoBasePO() throws Exception { 23 | PageFactory.initElements(CreateDriver.getInstance().getDriver(),this); 24 | } 25 | 26 | // elements 27 | @FindBy(css = "img[src*='01e56eb76d18b60c5fb3dcf451c080a1']") 28 | protected M passionTeaCoImg; 29 | 30 | @FindBy(css = "img[src*='ab7db4b80e0c0644f5f9226f2970739b']") 31 | protected M leafImg; 32 | 33 | @FindBy(css = "img[src*='cd390673d46bead889c368ae135a6ec2']") 34 | protected M organicImg; 35 | 36 | @FindBy(css = "a[href='welcome.html']") 37 | protected M welcome; 38 | 39 | @FindBy(css = "(//a[@href='menu.html'])[2]") 40 | protected M menu; 41 | 42 | @FindBy(css = "a[href='our-passion.html']") 43 | protected M ourPassion; 44 | 45 | @FindBy(css = "a[href='let-s-talk-tea.html']") 46 | protected M letsTalkTea; 47 | 48 | @FindBy(css = "a[href='check-out.html']") 49 | protected M checkOut; 50 | 51 | @FindBy(css = "//p[contains(text(),'Copyright')]") 52 | protected M copyright; 53 | 54 | // abstract methods 55 | 56 | protected abstract void setTitle(String pageTitle); 57 | protected abstract String getTitle(); 58 | 59 | // common methods 60 | 61 | /** 62 | * verifyTitle method to verify page title 63 | * 64 | * @param title 65 | * @throws AssertionError 66 | */ 67 | public void verifyTitle(String title) throws AssertionError { 68 | WebDriver driver = CreateDriver.getInstance().getDriver(); 69 | 70 | assertEquals(driver.getTitle(), title, "Verify Page Title"); 71 | } 72 | 73 | /** 74 | * navigate method to switch pages in app 75 | * 76 | * @param page 77 | * @throws Exception 78 | */ 79 | public void navigate(String page) throws Exception { 80 | WebDriver driver = CreateDriver.getInstance().getDriver(); 81 | BrowserUtils.waitForClickable(By.xpath("//a[contains(text(),'" + page + "')]"), Global_VARS.TIMEOUT_MINUTE); 82 | driver.findElement(By.xpath("//a[contains(text(),'" + page + "')]")).click(); 83 | 84 | // wait for page title 85 | BrowserUtils.waitFor(this.getTitle(), Global_VARS.TIMEOUT_ELEMENT); 86 | } 87 | 88 | /** 89 | * loadPage method to navigate to Target URL 90 | * 91 | * @param url 92 | * @param timeout 93 | * @throws Exception 94 | */ 95 | public void loadPage(String url, int timeout) throws Exception { 96 | WebDriver driver = CreateDriver.getInstance().getDriver(); 97 | driver.navigate().to(url); 98 | 99 | // wait for page URL 100 | BrowserUtils.waitForURL(Global_VARS.TARGET_URL, timeout); 101 | } 102 | 103 | /** 104 | * verifyText method to verify page text 105 | * 106 | * @param pattern 107 | * @param text 108 | * @throws AssertionError 109 | */ 110 | public void verifySpan(String pattern, String text) throws AssertionError { 111 | String getText = null; 112 | WebDriver driver = CreateDriver.getInstance().getDriver(); 113 | 114 | getText = driver.findElement(By.xpath("//span[contains(text(),'" + pattern + "')]")).getText(); 115 | assertEquals(getText, text, "Verify Span Text"); 116 | } 117 | 118 | /** 119 | * verifyHeading method to verify page headings 120 | * 121 | * @param pattern 122 | * @param text 123 | * @throws AssertionError 124 | */ 125 | public void verifyHeading(String pattern, String text) throws AssertionError { 126 | String getText = null; 127 | WebDriver driver = CreateDriver.getInstance().getDriver(); 128 | 129 | getText = driver.findElement(By.xpath("//h1[contains(text(),'" + pattern + "')]")).getText(); 130 | assertEquals(getText, text, "Verify Heading Text"); 131 | } 132 | 133 | /** 134 | * verifyParagraph method to verify paragraph text 135 | * 136 | * @param pattern 137 | * @param text 138 | * @throws AssertionError 139 | */ 140 | public void verifyParagraph(String pattern, String text) throws AssertionError { 141 | String getText = null; 142 | WebDriver driver = CreateDriver.getInstance().getDriver(); 143 | 144 | getText = driver.findElement(By.xpath("//p[contains(text(),'" + pattern + "')]")).getText(); 145 | assertEquals(getText, text, "Verify Paragraph Text"); 146 | } 147 | 148 | } 149 | -------------------------------------------------------------------------------- /Chapter18/PassionTeaCoTest.java: -------------------------------------------------------------------------------- 1 | package com.framework.ux.utils.chapter10; 2 | 3 | import com.framework.ux.utils.chapter10.PassionTeaCoWelcomePO.WELCOME_PAGE_IMG; 4 | import com.framework.ux.utils.chapter10.PassionTeaCoWelcomePO.MENU_LINKS; 5 | import org.json.simple.JSONObject; 6 | import org.openqa.selenium.WebDriver; 7 | import org.openqa.selenium.WebElement; 8 | import org.testng.ITestContext; 9 | import org.testng.ITestResult; 10 | import org.testng.annotations.*; 11 | import org.testng.annotations.Optional; 12 | 13 | /** 14 | * @author Carl Cocchiaro 15 | * 16 | * Passion Tea Co Test Class 17 | * 18 | */ 19 | public class PassionTeaCoTest { 20 | // local vars 21 | private PassionTeaCoWelcomePO welcome = null; 22 | private static final String DATA_FILE = "src/main/java/com/framework/ux/utils/chapter10/PassionTeaCo.json"; 23 | 24 | // constructor 25 | public PassionTeaCoTest() throws Exception { 26 | } 27 | 28 | // setup/teardown methods 29 | 30 | /** 31 | * suiteSetup method 32 | * 33 | * @param environment 34 | * @param context 35 | * @throws Exception 36 | */ 37 | @Parameters({"environment"}) 38 | @BeforeSuite(alwaysRun = true, enabled = true) 39 | protected void suiteSetup(@Optional(Global_VARS.ENVIRONMENT) String environment, 40 | ITestContext context) 41 | throws Exception { 42 | 43 | Global_VARS.DEF_ENVIRONMENT = System.getProperty("environment", environment); 44 | Global_VARS.SUITE_NAME = context.getSuite().getXmlSuite().getName(); 45 | } 46 | 47 | /** 48 | * suiteTeardown method 49 | * 50 | * @throws Exception 51 | */ 52 | @AfterSuite(alwaysRun = true, enabled = true) 53 | protected void suiteTeardown() throws Exception { 54 | } 55 | 56 | /** 57 | * testSetup method 58 | * 59 | * @param browser 60 | * @param platform 61 | * @param includePattern 62 | * @param excludePattern 63 | * @param ctxt 64 | * @throws Exception 65 | */ 66 | @Parameters({"browser", "platform", "includePattern", "excludePattern"}) 67 | @BeforeTest(alwaysRun = true, enabled = true) 68 | protected void testSetup(@Optional(Global_VARS.BROWSER) String browser, 69 | @Optional(Global_VARS.PLATFORM) String platform, 70 | @Optional String includePattern, 71 | @Optional String excludePattern, 72 | ITestContext ctxt) 73 | throws Exception { 74 | 75 | // data provider filters 76 | if ( includePattern != null ) { 77 | System.setProperty("includePattern", includePattern); 78 | } 79 | 80 | if ( excludePattern != null ) { 81 | System.setProperty("excludePattern", excludePattern); 82 | } 83 | 84 | // global variables 85 | Global_VARS.DEF_BROWSER = System.getProperty("browser", browser); 86 | Global_VARS.DEF_PLATFORM = System.getProperty("platform", platform); 87 | 88 | // create driver 89 | CreateDriver.getInstance().setDriver(Global_VARS.DEF_BROWSER, 90 | Global_VARS.DEF_PLATFORM, 91 | Global_VARS.DEF_ENVIRONMENT); 92 | } 93 | 94 | /** 95 | * testTeardown method 96 | * 97 | * @throws Exception 98 | */ 99 | @AfterTest(alwaysRun = true, enabled = true) 100 | protected void testTeardown() throws Exception { 101 | // close driver 102 | CreateDriver.getInstance().closeDriver(); 103 | } 104 | 105 | /** 106 | * testClassSetup method 107 | * 108 | * @param context 109 | * @throws Exception 110 | */ 111 | @BeforeClass(alwaysRun = true, enabled = true) 112 | protected void testClassSetup(ITestContext context) throws Exception { 113 | // instantiate page object classes 114 | welcome = new PassionTeaCoWelcomePO(); 115 | 116 | // set datafile for data provider 117 | JSONDataProvider.dataFile = DATA_FILE; 118 | 119 | // load page 120 | welcome.loadPage(Global_VARS.TARGET_URL, Global_VARS.TIMEOUT_MINUTE); 121 | } 122 | 123 | /** 124 | * testClassTeardown method 125 | * 126 | * @param context 127 | * @throws Exception 128 | */ 129 | @AfterClass(alwaysRun = true, enabled = true) 130 | protected void testClassTeardown(ITestContext context) throws Exception { 131 | } 132 | 133 | /** 134 | * testMethodSetup method 135 | * 136 | * @param result 137 | * @throws Exception 138 | */ 139 | @BeforeMethod(alwaysRun = true, enabled = true) 140 | protected void testMethodSetup(ITestResult result) throws Exception { 141 | } 142 | 143 | /** 144 | * testMethodTeardown method 145 | * 146 | * @param result 147 | * @throws Exception 148 | */ 149 | @AfterMethod(alwaysRun = true, enabled = true) 150 | protected void testMethodTeardown(ITestResult result) throws Exception { 151 | WebDriver driver = CreateDriver.getInstance().getDriver(); 152 | 153 | if ( !driver.getCurrentUrl().contains("welcome.html") ) { 154 | welcome.setTitle("Welcome"); 155 | welcome.navigate("Welcome"); 156 | } 157 | } 158 | 159 | // test methods 160 | 161 | /** 162 | * tc001_passionTeaCo method to test page navigation 163 | * 164 | * @param rowID 165 | * @param description 166 | * @param testData 167 | * @throws Exception 168 | */ 169 | @Test(groups={"PASSION_TEA"}, dataProvider="fetchData_JSON", dataProviderClass=JSONDataProvider.class, enabled=true) 170 | public void tc001_passionTeaCo(String rowID, 171 | String description, 172 | JSONObject testData) throws Exception { 173 | 174 | // set the page title on-the-fly 175 | welcome.setTitle(testData.get("title").toString()); 176 | 177 | // navigate to the new page 178 | welcome.navigate(testData.get("menu").toString()); 179 | 180 | // retrieve and verify the page title 181 | welcome.verifyTitle(testData.get("title").toString()); 182 | } 183 | 184 | /** 185 | * tc002_passionTeaCo method to test image source 186 | * 187 | * @param rowID 188 | * @param description 189 | * @param testData 190 | * @throws Exception 191 | */ 192 | @Test(groups={"PASSION_TEA"}, dataProvider="fetchData_JSON", dataProviderClass=JSONDataProvider.class, enabled=true) 193 | public void tc002_passionTeaCo(String rowID, 194 | String description, 195 | JSONObject testData) throws Exception { 196 | 197 | // verify image source 198 | welcome.verifyImgSrc(WELCOME_PAGE_IMG.valueOf(testData.get("img").toString()), 199 | testData.get("src").toString()); 200 | } 201 | 202 | /** 203 | * tc003_passionTeaCo method to test page span text 204 | * 205 | * @param rowID 206 | * @param description 207 | * @param testData 208 | * @throws Exception 209 | */ 210 | @Test(groups={"PASSION_TEA"}, dataProvider="fetchData_JSON", dataProviderClass=JSONDataProvider.class, enabled=true) 211 | public void tc003_passionTeaCo(String rowID, 212 | String description, 213 | JSONObject testData) throws Exception { 214 | 215 | // verify text labels 216 | welcome.verifySpan(testData.get("pattern").toString(), 217 | testData.get("text").toString()); 218 | } 219 | 220 | /** 221 | * tc004_passionTeaCo method to test page heading text 222 | * 223 | * @param rowID 224 | * @param description 225 | * @param testData 226 | * @throws Exception 227 | */ 228 | @Test(groups={"PASSION_TEA"}, dataProvider="fetchData_JSON", dataProviderClass=JSONDataProvider.class, enabled=true) 229 | public void tc004_passionTeaCo(String rowID, 230 | String description, 231 | JSONObject testData) throws Exception { 232 | 233 | // verify headings 234 | welcome.verifyHeading(testData.get("pattern").toString(), 235 | testData.get("text").toString()); 236 | } 237 | 238 | /** 239 | * tc005_passionTeaCo method to test page paragraph text 240 | * 241 | * @param rowID 242 | * @param description 243 | * @param testData 244 | * @throws Exception 245 | */ 246 | @Test(groups={"PASSION_TEA"}, dataProvider="fetchData_JSON", dataProviderClass=JSONDataProvider.class, enabled=true) 247 | public void tc005_passionTeaCo(String rowID, 248 | String description, 249 | JSONObject testData) throws Exception { 250 | 251 | // verify paragraphs 252 | welcome.verifyParagraph(testData.get("pattern").toString(), 253 | testData.get("text").toString()); 254 | } 255 | 256 | /** 257 | * tc006_passionTeaCo method to test navigating all "Menu" links 258 | * 259 | * @param rowID 260 | * @param description 261 | * @param testData 262 | * @throws Exception 263 | */ 264 | @Test(groups={"PASSION_TEA"}, dataProvider="fetchData_JSON", dataProviderClass=JSONDataProvider.class, enabled=true) 265 | public void tc006_passionTeaCo(String rowID, 266 | String description, 267 | JSONObject testData) throws Exception { 268 | 269 | // verify menu links 270 | welcome.navigateMenuLink(MENU_LINKS.valueOf(testData.get("element").toString()), 271 | testData.get("title").toString()); 272 | } 273 | 274 | } -------------------------------------------------------------------------------- /Chapter18/PassionTeaCoWelcomePO.java: -------------------------------------------------------------------------------- 1 | package com.framework.ux.utils.chapter10; 2 | 3 | import org.openqa.selenium.*; 4 | import org.openqa.selenium.support.FindBy; 5 | 6 | import static org.testng.Assert.assertEquals; 7 | 8 | /** 9 | * @author Carl Cocchiaro 10 | * 11 | * Passion Tea Company Welcome Sub-class Page Object Class 12 | * 13 | */ 14 | public class PassionTeaCoWelcomePO extends PassionTeaCoBasePO { 15 | // local variables 16 | private static final String WELCOME_TITLE = "Welcome"; 17 | private static final String MENU_TITLE = "Menu"; 18 | protected static enum WELCOME_PAGE_IMG { PASSION_TEA_CO, LEAF, ORGANIC, TEA_CUP, HERBAL_TEA, LOOSE_TEA, FLAVORED_TEA }; 19 | protected static enum MENU_LINKS { MENU, MORE_1, MORE_2, HERBAL_TEA, LOOSE_TEA, FLAVORED_TEA, SEE_COLLECTION1, SEE_COLLECTION2, SEE_COLLECTION3 }; 20 | 21 | // constructor 22 | public PassionTeaCoWelcomePO() throws Exception { 23 | super(); 24 | 25 | setTitle(WELCOME_TITLE); 26 | } 27 | 28 | // elements 29 | @FindBy(css = "img[src*='7cbbd331e278a100b443a12aa4cce77b']") 30 | protected M teaCupImg; 31 | 32 | @FindBy(xpath = "//h1[contains(text(),'We're passionate about tea')]") 33 | protected M caption; 34 | 35 | @FindBy(xpath = "//span[contains(text(),'For more than 25 years')]") 36 | protected M paragraph; 37 | 38 | @FindBy(css = "a[href='http://www.seleniumframework.com']") 39 | protected M seleniumFramework; 40 | 41 | @FindBy(xpath = "//span[.='Herbal Tea']") 42 | protected M herbalTea; 43 | 44 | @FindBy(xpath = "//span[.='Loose Tea']") 45 | protected M looseTea; 46 | 47 | @FindBy(xpath = "//span[.='Flavored Tea']") 48 | protected M flavoredTea; 49 | 50 | @FindBy(css = "img[src*='d892360c0e73575efa3e5307c619db41']") 51 | protected M herbalTeaImg; 52 | 53 | @FindBy(css = "img[src*='18f9b21e513a597e4b8d4c805321bbe3']") 54 | protected M looseTeaImg; 55 | 56 | @FindBy(css = "img[src*='d0554952ea0bea9e79bf01ab564bf666']") 57 | protected M flavoredTeaImg; 58 | 59 | @FindBy(xpath = "(//span[contains(@class,'button-content')])[1]") 60 | protected M flavoredTeaCollect; 61 | 62 | @FindBy(xpath = "(//span[contains(@class,'button-content')])[2]") 63 | protected M herbalTeaCollect; 64 | 65 | @FindBy(xpath = "(//span[contains(@class,'button-content')])[3]") 66 | protected M looseTeaCollect; 67 | 68 | // abstract methods 69 | 70 | /** 71 | * setTitle method to set page title 72 | * 73 | * @param pageTitle 74 | */ 75 | @Override 76 | protected void setTitle(String pageTitle) { 77 | this.pageTitle = pageTitle; 78 | } 79 | 80 | /** 81 | * getTitle method to get page title 82 | * 83 | * @return String 84 | */ 85 | @Override 86 | public String getTitle() { 87 | return this.pageTitle; 88 | } 89 | 90 | // common methods 91 | 92 | /** 93 | * verifyImgSrc method to verify page image source 94 | * 95 | * @param img 96 | * @param src 97 | * @throws AssertionError 98 | */ 99 | public void verifyImgSrc(WELCOME_PAGE_IMG img, String src) throws AssertionError { 100 | String getText = null; 101 | 102 | switch(img) { 103 | case PASSION_TEA_CO: 104 | getText = passionTeaCoImg.getAttribute("src"); 105 | break; 106 | case LEAF: 107 | getText = leafImg.getAttribute("src"); 108 | break; 109 | case ORGANIC: 110 | getText = organicImg.getAttribute("src"); 111 | break; 112 | case TEA_CUP: 113 | getText = teaCupImg.getAttribute("src"); 114 | break; 115 | case HERBAL_TEA: 116 | getText = herbalTeaImg.getAttribute("src"); 117 | break; 118 | case LOOSE_TEA: 119 | getText = looseTeaImg.getAttribute("src"); 120 | break; 121 | case FLAVORED_TEA: 122 | getText = flavoredTeaImg.getAttribute("src"); 123 | break; 124 | } 125 | 126 | assertEquals(getText, src, "Verify Image Source"); 127 | } 128 | 129 | /** 130 | * navigateMenuLink method to navigate page menu links 131 | * 132 | * @param link 133 | * @param title 134 | * @throws AssertionError 135 | */ 136 | public void navigateMenuLink(MENU_LINKS link, String title) throws Exception { 137 | String index = null; 138 | WebDriver driver = CreateDriver.getInstance().getDriver(); 139 | 140 | switch(link) { 141 | case HERBAL_TEA: 142 | index = "1"; 143 | break; 144 | case MENU: 145 | index = "2"; 146 | break; 147 | case SEE_COLLECTION3: 148 | index = "3"; 149 | break; 150 | case MORE_2: 151 | index = "4"; 152 | break; 153 | case MORE_1: 154 | index = "5"; 155 | break; 156 | case LOOSE_TEA: 157 | index = "6"; 158 | break; 159 | case SEE_COLLECTION1: 160 | index = "7"; 161 | break; 162 | case SEE_COLLECTION2: 163 | index = "8"; 164 | break; 165 | case FLAVORED_TEA: 166 | index = "9"; 167 | break; 168 | } 169 | 170 | // Firefox occasionally fails to execute WebDriver API click 171 | String query = "(//a[@href='menu.html'])" + "[" + index + "]"; 172 | 173 | try { 174 | driver.findElement(By.xpath(query)).click(); 175 | BrowserUtils.waitFor(MENU_TITLE, Global_VARS.TIMEOUT_ELEMENT); 176 | } 177 | 178 | // make 2nd attempt with JavaScript API click 179 | catch(TimeoutException e) { 180 | BrowserUtils.click(By.xpath(query)); 181 | BrowserUtils.waitFor(MENU_TITLE, Global_VARS.TIMEOUT_ELEMENT); 182 | } 183 | 184 | assertEquals(MENU_TITLE, title, "Navigate Menu Link"); 185 | } 186 | 187 | } 188 | -------------------------------------------------------------------------------- /Chapter18/TestNG_ConsoleRunner.java: -------------------------------------------------------------------------------- 1 | package com.framework.ux.utils.chapter10; 2 | 3 | import org.testng.ITestContext; 4 | import org.testng.ITestResult; 5 | import org.testng.TestListenerAdapter; 6 | 7 | import java.io.*; 8 | import java.text.DateFormat; 9 | import java.text.SimpleDateFormat; 10 | import java.util.Date; 11 | 12 | /** 13 | * @author Carl Cocchiaro 14 | * 15 | * TestNG Listener Utility Class 16 | * 17 | */ 18 | public class TestNG_ConsoleRunner extends TestListenerAdapter { 19 | private static String logFile = null; 20 | 21 | /** 22 | * onStart method 23 | * 24 | * @param testContext 25 | */ 26 | @Override 27 | public void onStart(ITestContext testContext) { 28 | super.onStart(testContext); 29 | } 30 | 31 | /** 32 | * onFinish method 33 | * 34 | * @param testContext 35 | */ 36 | @Override 37 | public void onFinish(ITestContext testContext) { 38 | log("\nTotal Passed = " + getPassedTests().size() + ", Total Failed = " + getFailedTests().size() + ", Total Skipped = " + getSkippedTests().size() + "\n"); 39 | 40 | super.onFinish(testContext); 41 | } 42 | 43 | /** 44 | * onTestStart method 45 | * 46 | * @param tr 47 | */ 48 | @Override 49 | public void onTestStart(ITestResult tr) { 50 | if ( logFile == null ) { 51 | logFile = Global_VARS.LOGFILE_PATH + Global_VARS.SUITE_NAME + "-" + new SimpleDateFormat("MM.dd.yy.HH.mm.ss").format(new Date()) + ".log"; 52 | } 53 | 54 | log("\n---------------------------------- Test '" + tr.getName() + getTestDescription(tr) + "' ----------------------------------\n"); 55 | log(tr.getStartMillis(),"START-> " + tr.getName() + "\n"); 56 | log(" ***Test Parameters = " + getTestParams(tr) + "\n"); 57 | 58 | super.onTestStart(tr); 59 | } 60 | 61 | /** 62 | * onTestSuccess method 63 | * 64 | * @param tr 65 | */ 66 | @Override 67 | public void onTestSuccess(ITestResult tr) { 68 | log(" ***Result = PASSED\n"); 69 | log(tr.getEndMillis(),"END -> " + tr.getName()); 70 | log("\n---\n"); 71 | 72 | super.onTestSuccess(tr); 73 | } 74 | 75 | /** 76 | * onTestFailure method 77 | * 78 | * @param tr 79 | */ 80 | @Override 81 | public void onTestFailure(ITestResult tr) { 82 | if ( !getTestMessage(tr).equals("") ) { 83 | log(getTestMessage(tr) + "\n"); 84 | } 85 | 86 | log(" ***Result = FAILED\n"); 87 | log(tr.getEndMillis(),"END -> " + tr.getInstanceName() + "." + tr.getName()); 88 | log("\n---\n"); 89 | 90 | super.onTestFailure(tr); 91 | } 92 | 93 | /** 94 | * onTestSkipped method 95 | * 96 | * @param tr 97 | */ 98 | @Override 99 | public void onTestSkipped(ITestResult tr) { 100 | if ( !getTestMessage(tr).equals("") ) { 101 | log(getTestMessage(tr) + "\n"); 102 | } 103 | 104 | log(" ***Result = SKIPPED\n"); 105 | log(tr.getEndMillis(),"END -> " + tr.getInstanceName() + "." + tr.getName()); 106 | log("\n---\n"); 107 | 108 | super.onTestSkipped(tr); 109 | } 110 | 111 | /** 112 | * onConfigurationSuccess method 113 | * 114 | * @param itr 115 | */ 116 | @Override 117 | public void onConfigurationSuccess(ITestResult itr) { 118 | super.onConfigurationSuccess(itr); 119 | } 120 | 121 | /** 122 | * onConfigurationFailure method 123 | * 124 | * @param tr 125 | */ 126 | @Override 127 | public void onConfigurationFailure(ITestResult tr) { 128 | if ( !getTestMessage(tr).equals("") ) { 129 | log(getTestMessage(tr) + "\n"); 130 | } 131 | 132 | log(" ***Result = CONFIGURATION FAILED\n"); 133 | log(tr.getEndMillis(),"END CONFIG -> " + tr.getInstanceName() + "." + tr.getName()); 134 | log("\n---\n"); 135 | 136 | super.onConfigurationFailure(tr); 137 | } 138 | 139 | /** 140 | * onConfigurationSkip method 141 | * 142 | * @param tr 143 | */ 144 | @Override 145 | public void onConfigurationSkip(ITestResult tr) { 146 | log(getTestMessage(tr)); 147 | log(" ***Result = CONFIGURATION SKIPPED\n"); 148 | log(tr.getEndMillis(),"END CONFIG -> " + tr.getInstanceName() + "." + tr.getName()); 149 | log("\n---\n"); 150 | 151 | super.onConfigurationSkip(tr); 152 | } 153 | 154 | /** 155 | * log method 156 | * 157 | * @param dateMillis 158 | * @param line 159 | */ 160 | public void log(long dateMillis,String line) { 161 | System.out.format("%s: %s%n",String.valueOf(new Date(dateMillis)),line); 162 | 163 | if ( logFile != null ) { 164 | writeTestngLog(logFile, line); 165 | } 166 | } 167 | 168 | /** 169 | * log method 170 | * 171 | * @param line 172 | */ 173 | public void log(String line) { 174 | System.out.format("%s%n", line); 175 | 176 | if ( logFile != null ) { 177 | writeTestngLog(logFile, line); 178 | } 179 | } 180 | 181 | /** 182 | * getTestMessage method 183 | * 184 | * @param tr 185 | * @return String 186 | */ 187 | public String getTestMessage(ITestResult tr) { 188 | Boolean found = false; 189 | 190 | if ( tr != null && tr.getThrowable() != null ) { 191 | found = true; 192 | } 193 | 194 | if ( found == true ) { 195 | return tr.getThrowable().getMessage() == null ? "" : tr.getThrowable().getMessage(); 196 | } 197 | 198 | else { 199 | return ""; 200 | } 201 | } 202 | 203 | /** 204 | * getTestParams method 205 | * 206 | * @param tr 207 | * @return String 208 | */ 209 | public String getTestParams(ITestResult tr) { 210 | int iLength = tr.getParameters().length; 211 | String message = ""; 212 | 213 | try { 214 | if ( tr.getParameters().length > 0 ) { 215 | message = tr.getParameters()[0].toString(); 216 | 217 | for ( int iCount = 0; iCount < iLength; iCount++ ) { 218 | if ( iCount == 0 ) { 219 | message = tr.getParameters()[0].toString(); 220 | } 221 | else { 222 | message = message + ", " + tr.getParameters()[iCount].toString(); 223 | } 224 | } 225 | } 226 | } 227 | 228 | catch(Exception e) { 229 | // do nothing... 230 | } 231 | 232 | return message; 233 | } 234 | 235 | /** 236 | * getTestDescription method 237 | * 238 | * @param tr 239 | * @return String 240 | */ 241 | public String getTestDescription(ITestResult tr) { 242 | String message = ""; 243 | 244 | try { 245 | if ( tr.getParameters().length > 0 ) { 246 | message = ": " + tr.getParameters()[1].toString(); 247 | } 248 | } 249 | 250 | catch(Exception e) { 251 | // do nothing... 252 | } 253 | 254 | return message; 255 | } 256 | 257 | /** 258 | * writeTestngLog method 259 | * 260 | * @param logFile 261 | * @param line 262 | */ 263 | public void writeTestngLog(String logFile,String line) { 264 | DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 265 | Date date = new Date(); 266 | File directory = new File(Global_VARS.LOGFILE_PATH); 267 | File file = new File(logFile); 268 | 269 | try { 270 | if ( !directory.exists() ) { 271 | directory.mkdirs(); 272 | } 273 | 274 | else if ( !file.exists() ) { 275 | file.createNewFile(); 276 | } 277 | 278 | BufferedWriter writer = new BufferedWriter(new FileWriter(logFile, true)); 279 | 280 | if ( line.contains("START") || line.contains("END") ) { 281 | writer.append("[" + dateFormat.format(date) + "] " + line); 282 | 283 | } 284 | 285 | else { 286 | writer.append(line); 287 | } 288 | 289 | writer.newLine(); 290 | writer.close(); 291 | } 292 | 293 | catch(IOException e) { 294 | // do nothing... 295 | } 296 | } 297 | 298 | } 299 | 300 | -------------------------------------------------------------------------------- /Chapter18/extent-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | standard 7 | 8 | 9 | 10 | UTF-8 11 | 12 | 13 | 14 | https 15 | 16 | 17 | 18 | 19 | 20 | 21 | 23 | 24 | 25 | 26 | 27 | bottom 28 | 29 | 30 | 31 | 32 | 33 | 34 | MM-dd-yyyy 35 | 36 | 37 | 38 | HH:mm:ss 39 | 40 | 41 | 42 | 47 | 48 | 49 | 50 | 51 | i:nth-child(1) { color: #1e90ff; } 67 | .category-content .category-status-counts:nth-child(3) { background-color: #1e90ff; } 68 | .yellow.darken-2 { background-color: #1e90ff !important; } 69 | ]]> 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /Chapter18/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | org.seleniumhq.selenium 4 | selenium-java 5 | 3.7.1 6 | selenium-java 7 | 8 | Selenium automates browsers. That's it! What you do with that power is entirely up to you. 9 | 10 | http://www.seleniumhq.org/ 11 | 12 | 13 | The Apache Software License, Version 2.0 14 | http://www.apache.org/licenses/LICENSE-2.0.txt 15 | repo 16 | 17 | 18 | 19 | scm:git:git@github.com:SeleniumHQ/selenium.git 20 | scm:git:git@github.com:SeleniumHQ/selenium.git 21 | https://github.com/SeleniumHQ/selenium/ 22 | 23 | 24 | 25 | org.seleniumhq.selenium 26 | selenium-api 27 | 3.7.1 28 | 29 | 30 | 31 | org.seleniumhq.selenium 32 | selenium-chrome-driver 33 | 3.7.1 34 | 35 | 36 | 37 | org.seleniumhq.selenium 38 | selenium-edge-driver 39 | 3.7.1 40 | 41 | 42 | 43 | org.seleniumhq.selenium 44 | selenium-firefox-driver 45 | 3.7.1 46 | 47 | 48 | 49 | org.seleniumhq.selenium 50 | selenium-ie-driver 51 | 3.7.1 52 | 53 | 54 | 55 | org.seleniumhq.selenium 56 | selenium-opera-driver 57 | 3.7.1 58 | 59 | 60 | 61 | org.seleniumhq.selenium 62 | selenium-remote-driver 63 | 3.7.1 64 | 65 | 66 | 67 | org.seleniumhq.selenium 68 | selenium-safari-driver 69 | 3.7.1 70 | 71 | 72 | 73 | org.seleniumhq.selenium 74 | selenium-support 75 | 3.7.1 76 | 77 | 78 | 79 | net.bytebuddy 80 | byte-buddy 81 | 1.7.5 82 | 83 | 84 | 85 | org.apache.commons 86 | commons-exec 87 | 1.3 88 | 89 | 90 | 91 | commons-codec 92 | commons-codec 93 | 1.10 94 | 95 | 96 | 97 | commons-logging 98 | commons-logging 99 | 1.2 100 | 101 | 102 | 103 | com.google.code.gson 104 | gson 105 | 2.8.2 106 | 107 | 108 | 109 | com.google.guava 110 | guava 111 | 23.0 112 | 113 | 114 | 115 | org.apache.httpcomponents 116 | httpclient 117 | 4.5.3 118 | 119 | 120 | 121 | org.apache.httpcomponents 122 | httpcore 123 | 4.4.6 124 | 125 | 126 | 127 | net.java.dev.jna 128 | jna 129 | 4.1.0 130 | 131 | 132 | 133 | net.java.dev.jna 134 | jna-platform 135 | 4.1.0 136 | 137 | 138 | 139 | org.testng 140 | testng 141 | 6.11 142 | test 143 | 144 | 145 | io.appium 146 | java-client 147 | 5.0.4 148 | 149 | 150 | 151 | javax.mail 152 | mail 153 | 1.4.7 154 | 155 | 156 | 157 | commons-io 158 | commons-io 159 | 2.5 160 | 161 | 162 | 163 | com.googlecode.json-simple 164 | json-simple 165 | 1.1.1 166 | 167 | 168 | 169 | com.aventstack 170 | extentreports 171 | 3.1.0 172 | provided 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | -------------------------------------------------------------------------------- /Chapter18/selenium.properties: -------------------------------------------------------------------------------- 1 | # Selenium Properties File 2 | 3 | selenium.revision=3.7.1 4 | geckodriver.revision=0.19.1 5 | chromedriver.revision=2.33 6 | iedriver.revision=11.0 7 | 8 | firefox.revision=57.0 9 | chrome.revision=62.0 10 | ie.revision=11.0 11 | 12 | gecko.driver.windows.path=drivers/geckodriver.exe 13 | chrome.driver.windows.path=drivers/chromedriver.exe 14 | ie.driver.windows.path=drivers/IEDriverServer.exe 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Learn-Selenium 5 | The Selenium WebDriver 3.x is an open source API to test both browser and mobile applications. With a solid foundation, you can easily perform end-to-end testing on web and mobile browsers. 6 | 7 | You’ll begin by getting introduced to the Selenium page object design patterns in software development. You’ll architect your own framework with a scalable driver class, Java utility classes, and support for third-party tools and plugins. You'll design and build a Selenium grid from scratch to enable the framework to scale and support different browsers, mobile devices, and platforms. You’ll strategize and handle rich web UI using the advanced WebDriver API and learn techniques to handle real-time challenges in WebDriver. You’ll perform different types of testing, such as cross-browser testing, load testing, and mobile testing. Finally, you will also be introduced to data-driven testing using TestNG to create your own automation framework. 8 | 9 | By the end of this Learning Path, you’ll be able to design your own automation testing framework and perform data-driven testing with Selenium WebDriver. 10 | 11 | This Learning Path includes content from the following Packt products: 12 | Selenium WebDriver 3 Practical Guide - Second Edition by Unmesh Gundecha 13 | Selenium Framework Design in Data-Driven Testing by Carl Cocchiaro 14 | 15 | #What you'll learn 16 | 17 | Use different mobile and desktop browser platforms with Selenium 3 18 | Use the Actions API for performing various keyboard and mouse actions 19 | Design the Selenium Driver Class for local, remote, and third-party grid support 20 | Build page object classes with the Selenium Page Object Model 21 | Develop data-driven test classes using the TestNG framework 22 | Encapsulate data using the JSON protocol 23 | Build a Selenium Grid for RemoteWebDriver testing 24 | Build and use utility classes in synchronization, file I/O, reporting and test listener classes 25 | 26 | #Software Requirements 27 | 28 | Java JDK 1.8 29 | IntelliJ IDEA 2017.3+ 30 | Selenium WebDriver 3.7.1+ JAR 31 | Selenium Stand-alone Server 3.7.1+ JAR 32 | Appium Java Client 5.0.4+ JAR 33 | Appium Server 1.7.1 JAR for iOS or Linux 34 | TestNG 6.11 JAR 35 | ExtentReports 3.1.0 JAR 36 | Browsers: Google Chrome 62.0, Mozilla Firefox 57.0, Microsoft Internet Explorer 11.0 37 | Drivers: chromedriver.exe 2.33, geckodriver.exe 0.19.1, IEDriverServer.exe 3.7.1+ 38 | Apple Xcode and iPhone Simulators for iOS 39 | Google Android SDK and Samsung Galaxy emulators for Linux 40 | VMware virtual machines 41 | 42 | 43 | ### Download a free PDF 44 | 45 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
46 |

https://packt.link/free-ebook/9781838983048

--------------------------------------------------------------------------------