From 24a39e4714974cc36eacc390da246a1a060742cf Mon Sep 17 00:00:00 2001 From: Andrew Krug Date: Tue, 27 Oct 2015 08:23:59 -0400 Subject: [PATCH] 09 - multiple files 10 - retry test actions 12 - opt out ab tests 16 - screenshot on failure 22 - locator strategy 23 - dynamic pages 25 - tables 40 - disabled elements --- 07-use-a-page-object/java/example.java | 76 +++++++++++ .../java/multipleFileExample/Base.java | 59 +++++++++ .../multipleFileExample/GoogleSearch.java | 34 +++++ .../multipleFileExample/GoogleSearchTest.java | 40 ++++++ .../java/onePageExample.java | 123 ++++++++++++++++++ 10-retry-test-actions/java/example.java | 48 +++++++ 12-opt-out-of-ab-tests/java/example.java | 53 ++++++++ .../java/example.java | 51 ++++++++ 22-locator-strategy/java/example.java | 55 ++++++++ 23-dynamic-pages/java/example.java | 63 +++++++++ 25-tables/java/example.java | 85 ++++++++++++ 40-disabled-element/java/example.java | 36 +++++ 12 files changed, 723 insertions(+) create mode 100644 07-use-a-page-object/java/example.java create mode 100644 09-use-a-base-page-object/java/multipleFileExample/Base.java create mode 100644 09-use-a-base-page-object/java/multipleFileExample/GoogleSearch.java create mode 100644 09-use-a-base-page-object/java/multipleFileExample/GoogleSearchTest.java create mode 100644 09-use-a-base-page-object/java/onePageExample.java create mode 100644 10-retry-test-actions/java/example.java create mode 100644 12-opt-out-of-ab-tests/java/example.java create mode 100644 16-take-screenshot-on-failure/java/example.java create mode 100644 22-locator-strategy/java/example.java create mode 100644 23-dynamic-pages/java/example.java create mode 100644 25-tables/java/example.java create mode 100644 40-disabled-element/java/example.java diff --git a/07-use-a-page-object/java/example.java b/07-use-a-page-object/java/example.java new file mode 100644 index 00000000..c14a1743 --- /dev/null +++ b/07-use-a-page-object/java/example.java @@ -0,0 +1,76 @@ +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * Created by andrew on 8/22/15. + */ + +public class GoogleSearchTest { + + WebDriver driver; + + @Before + public void setUp() throws Exception { + driver = new FirefoxDriver(); + } + + @Test + public void workWithBasicAuthTest() { + GoogleSearch google = new GoogleSearch(driver); + google.searchFor("elemental selenium tips"); + boolean result = google.searchResultPresent("Recieve a Free, Weekly tip"); + assert (result == true); + } + + @After + public void tearDown() throws Exception { + driver.quit(); + } +} + +class GoogleSearch { + + private final String BASE_URL = "http://www.google.com"; + WebDriver driver; + + By searchBox = By.id("gbqfq"); + By searchBoxSubmit = By.id("gbqfb"); + By topSearchResult = By.cssSelector("#rso .g"); + + public GoogleSearch(WebDriver _driver) { + this.driver = _driver; + visit(); + assert (verifyPage() == true); + } + + public void visit() { + driver.get(this.BASE_URL); + } + + public void searchFor(String searchTerm) { + driver.findElement(searchBox).clear(); + driver.findElement(searchBox).sendKeys(searchTerm); + driver.findElement(searchBoxSubmit).click(); + } + + public boolean searchResultPresent(String searchResult) { + waitFor(topSearchResult); + return driver.findElement(topSearchResult).getText().contains(searchResult); + } + + public void waitFor(By locator) { + new WebDriverWait(driver, 5).until(ExpectedConditions.presenceOfElementLocated(locator)); + } + + public boolean verifyPage() { + return driver.getCurrentUrl().contains("Google"); + } +} diff --git a/09-use-a-base-page-object/java/multipleFileExample/Base.java b/09-use-a-base-page-object/java/multipleFileExample/Base.java new file mode 100644 index 00000000..d0167975 --- /dev/null +++ b/09-use-a-base-page-object/java/multipleFileExample/Base.java @@ -0,0 +1,59 @@ +package num9; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +public class Base { + + private String BASE_URL; + private final WebDriver driver; + + public Base(WebDriver _driver, String baseUrl) { + this.driver = _driver; + this.BASE_URL = baseUrl; + } + + public void visit() { + driver.get(BASE_URL); + } + + public void visit(String url) { + driver.get(BASE_URL + url); + } + + public WebElement find(By locator) { + return driver.findElement(locator); + } + + public void clear(By locator) { + find(locator).clear(); + } + + public void type(By locator, String input) { + find(locator).sendKeys(input); + } + + public void clickOn(By locator) { + find(locator).click(); + } + + public ExpectedCondition displayed(By locator) { + return ExpectedConditions.presenceOfElementLocated(locator); + } + + public String textOf(By locator) { + return find(locator).getText(); + } + + public String title() { + return driver.getCurrentUrl(); + } + + public WebDriverWait waitFor() { + return new WebDriverWait(driver, 5); + } +} diff --git a/09-use-a-base-page-object/java/multipleFileExample/GoogleSearch.java b/09-use-a-base-page-object/java/multipleFileExample/GoogleSearch.java new file mode 100644 index 00000000..27ccfc0a --- /dev/null +++ b/09-use-a-base-page-object/java/multipleFileExample/GoogleSearch.java @@ -0,0 +1,34 @@ +package num9; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; + +public class GoogleSearch extends Base { + + By searchBox = By.id("gbqfq"); + By searchBoxSubmit = By.id("gbqfb"); + By topSearchResult = By.cssSelector("#rso .g"); + + public GoogleSearch(WebDriver _driver) { + super(_driver, "http://www.google.com"); + visit(); + verifyPage(); + } + + + public void searchFor(String searchTerm) { + clear(searchBox); + type(searchBox, searchTerm); + clickOn(searchBoxSubmit); + } + + public boolean searchResultPresent(String searchResult) { + waitFor().until(displayed(topSearchResult)); + return textOf(topSearchResult).contains(searchResult); + } + + + public void verifyPage() { + assert (title().contains("Google")); + } +} diff --git a/09-use-a-base-page-object/java/multipleFileExample/GoogleSearchTest.java b/09-use-a-base-page-object/java/multipleFileExample/GoogleSearchTest.java new file mode 100644 index 00000000..37819b42 --- /dev/null +++ b/09-use-a-base-page-object/java/multipleFileExample/GoogleSearchTest.java @@ -0,0 +1,40 @@ +package num9; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * Created by andrew on 8/22/15. + */ + +public class GoogleSearchTest { + + WebDriver driver; + + @Before + public void setUp() throws Exception { + driver = new FirefoxDriver(); + } + + @Test + public void workWithBasicAuthTest() { + GoogleSearch google = new GoogleSearch(driver); + google.searchFor("elemental selenium tips"); + boolean result = google.searchResultPresent("Recieve a Free, Weekly tip"); + assert (result); + } + + @After + public void tearDown() throws Exception { + driver.quit(); + } +} + diff --git a/09-use-a-base-page-object/java/onePageExample.java b/09-use-a-base-page-object/java/onePageExample.java new file mode 100644 index 00000000..25a9bb3c --- /dev/null +++ b/09-use-a-base-page-object/java/onePageExample.java @@ -0,0 +1,123 @@ +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.util.NoSuchElementException; + +/** + * Created by andrew on 8/22/15. + */ + +public class GoogleSearchTest { + + WebDriver driver; + + @Before + public void setUp() throws Exception { + driver = new FirefoxDriver(); + } + + @Test + public void workWithBasicAuthTest() { + GoogleSearch google = new GoogleSearch(driver); + google.searchFor("elemental selenium tips"); + boolean result = google.searchResultPresent("Recieve a Free, Weekly tip"); + assert (result); + } + + @After + public void tearDown() throws Exception { + driver.quit(); + } +} + +public class GoogleSearch extends Base { + + WebDriver driver; + + By searchBox = By.id("gbqfq"); + By searchBoxSubmit = By.id("gbqfb"); + By topSearchResult = By.cssSelector("#rso .g"); + + public GoogleSearch(WebDriver _driver) { + super(_driver, "http://www.google.com"); + visit(); + verifyPage(); + } + + + public void searchFor(String searchTerm) { + clear(searchBox); + type(searchBox, searchTerm); + clickOn(searchBoxSubmit); + } + + public boolean searchResultPresent(String searchResult) { + waitFor().until(displayed(topSearchResult)); + return textOf(topSearchResult).contains(searchResult); + } + + + public void verifyPage() { + assert (title().contains("Google")); + } +} + +class Base { + + private String BASE_URL; + private final WebDriver driver; + + public Base(WebDriver _driver, String baseUrl) { + this.driver = _driver; + this.BASE_URL = baseUrl; + } + + public void visit() { + driver.get(BASE_URL); + } + + public void visit(String url) { + driver.get(BASE_URL + url); + } + + public WebElement find(By locator) { + return driver.findElement(locator); + } + + public void clear(By locator) { + find(locator).clear(); + } + + public void type(By locator, String input) { + find(locator).sendKeys(input); + } + + public void clickOn(By locator) { + find(locator).click(); + } + + public ExpectedCondition displayed(By locator) { + return ExpectedConditions.presenceOfElementLocated(locator); + } + + public String textOf(By locator) { + return find(locator).getText(); + } + + public String title() { + return driver.getCurrentUrl(); + } + + public WebDriverWait waitFor() { + return new WebDriverWait(driver, 5); + } +} \ No newline at end of file diff --git a/10-retry-test-actions/java/example.java b/10-retry-test-actions/java/example.java new file mode 100644 index 00000000..59f7322c --- /dev/null +++ b/10-retry-test-actions/java/example.java @@ -0,0 +1,48 @@ +package num10; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; + +/** + * Created by andrew on 8/22/15. + */ +public class example { + public class WorkWithBasicAuth { + WebDriver driver; + + @Before + public void setUp() throws Exception { + driver = new FirefoxDriver(); + } + + @Test + public void RetryTestActions() { + driver.get("http://the-internet.herokuapp.com/notification_message"); + assert retryIfNotificationMessageContains("please try again"); + } + + @After + public void tearDown() throws Exception { + driver.quit(); + } + + + public String getNotificationMessage() { + return driver.findElement(By.id("flash")).getText(); + } + + public boolean retryIfNotificationMessageContains(String message) { + for (int count = 0; count < 3; count++) { + if (getNotificationMessage().contains(message)) + driver.navigate().refresh(); + else + return true; + } + return false; + } + } +} diff --git a/12-opt-out-of-ab-tests/java/example.java b/12-opt-out-of-ab-tests/java/example.java new file mode 100644 index 00000000..84300275 --- /dev/null +++ b/12-opt-out-of-ab-tests/java/example.java @@ -0,0 +1,53 @@ +package num12; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.Cookie; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; + +/** + * Created by andrew on 8/22/15. + */ +public class example { + + WebDriver driver; + + @Before + public void setUp() throws Exception { + driver = new FirefoxDriver(); + } + + @After + public void tearDown() throws Exception { + driver.quit(); + } + + @Test + public void TestSwitchingCookie() { + driver.get("http://the-internet.herokuapp.com/abtest"); + String headingText = driver.findElement(By.tagName("h3")).getText(); + assert (headingText.equals("A/B Test Variation 1") || headingText.equals("A/B Test Control")); + driver.manage().addCookie(new Cookie("optimizelyOptOut", "true")); + driver.navigate().refresh(); + headingText = driver.findElement(By.cssSelector("h3")).getText(); + assert headingText.equals("No A/B Test"); + } + + @Test + public void TestCookie() { + driver.manage().addCookie(new Cookie("optimizelyOptOut", "true")); + driver.get("http://the-internet.herokuapp.com/abtest"); + assert driver.findElement(By.cssSelector("h3")).getText().equals("No A/B Test"); + } + + @Test + public void TestOptOutViaUrl() { + driver.get("http://the-internet.herokuapp.com/abtest?optimizely_opt_out=true"); + assert driver.findElement(By.cssSelector("h3")).getText().equals("No A/B Test"); + } + + +} diff --git a/16-take-screenshot-on-failure/java/example.java b/16-take-screenshot-on-failure/java/example.java new file mode 100644 index 00000000..9547354b --- /dev/null +++ b/16-take-screenshot-on-failure/java/example.java @@ -0,0 +1,51 @@ +package num16; + +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.OutputType; +import org.openqa.selenium.TakesScreenshot; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by andrew on 8/22/15. + */ +public class example { + WebDriver driver; + + @Before + public void setUp() throws Exception { + driver = new FirefoxDriver(); + + } + + @Test + public void screenShotOnErrorTest() { + driver.get("http://the-internet.herokuapp.com"); + try { + assert driver.findElement(By.cssSelector("h1")).getText().equals("Welcome to the Internet!"); + } + catch (Exception e){ + System.out.println(e); + File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); + try { + FileUtils.copyFile(scrFile, new File(new SimpleDateFormat("failshot__dd_MM_yyyy__HH_mm_ss").format(new Date()) + ".png")); + } catch (IOException e1) { + e1.printStackTrace(); + } + } + } + + @After + public void tearDown() throws Exception { + driver.quit(); + } +} diff --git a/22-locator-strategy/java/example.java b/22-locator-strategy/java/example.java new file mode 100644 index 00000000..fbbfab14 --- /dev/null +++ b/22-locator-strategy/java/example.java @@ -0,0 +1,55 @@ +package num22; + +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.OutputType; +import org.openqa.selenium.TakesScreenshot; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by andrew on 8/22/15. + */ +public class example { + WebDriver driver; + + @Before + public void setUp() throws Exception { + driver = new FirefoxDriver(); + + } + + @Test + public void locatorStategyCSS() { + driver.get("http://the-internet.herokuapp.com/download"); + String link = driver.findElement(By.cssSelector("a")).getAttribute("href"); + System.out.println(link); + } + + @Test + public void locatorStategyPreciseCSS() { + driver.get("http://the-internet.herokuapp.com/download"); + String link = driver.findElement(By.cssSelector("#content a")).getAttribute("href"); + System.out.println(link); + } + + @Test + public void locatorStategyExactCSS() { + driver.get("http://the-internet.herokuapp.com/download"); + String link = driver.findElement(By.cssSelector("#content .example a")).getAttribute("href"); + System.out.println(link); + } + + @After + public void tearDown() throws Exception { + driver.quit(); + } +} diff --git a/23-dynamic-pages/java/example.java b/23-dynamic-pages/java/example.java new file mode 100644 index 00000000..eacd5be2 --- /dev/null +++ b/23-dynamic-pages/java/example.java @@ -0,0 +1,63 @@ +package num23; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * Created by andrew on 8/22/15. + */ +public class example { + WebDriver driver; + + @Before + public void setUp() throws Exception { + driver = new FirefoxDriver(); + + } + + @Test + public void noSuchElementErrorTest() { + driver.get("http://the-internet.herokuapp.com/dynamic_loading/2"); + driver.findElement(By.cssSelector("#start button")).click(); + assert driver.findElement(By.cssSelector("#finish")).getText().equals("Hello World!"); + } + + @Test + public void webDriverWait8Test() { + driver.get("http://the-internet.herokuapp.com/dynamic_loading/2"); + driver.findElement(By.cssSelector("#start button")).click(); + new WebDriverWait(driver, 8).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#finish"))); + assert driver.findElement(By.cssSelector("#finish")).getText().equals("Hello World!"); + } + + @Test + public void webDriverWaitTimeOutErrorTest() { + driver.get("http://the-internet.herokuapp.com/dynamic_loading/2"); + driver.findElement(By.cssSelector("#start button")).click(); + new WebDriverWait(driver, 2).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#finish"))); + assert driver.findElement(By.cssSelector("#finish")).getText().equals("Hello World!"); + } + + @Test + public void cleanUpTest() { + driver.get("http://the-internet.herokuapp.com/dynamic_loading/2"); + driver.findElement(By.cssSelector("#start button")).click(); + waitFor(By.cssSelector("#finish")); + assert driver.findElement(By.cssSelector("#finish")).getText().equals("Hello World!"); + } + + public void waitFor(By locator) { + new WebDriverWait(driver, 8).until(ExpectedConditions.presenceOfElementLocated(locator)); + } + + @After + public void tearDown() throws Exception { + driver.quit(); + } +} diff --git a/25-tables/java/example.java b/25-tables/java/example.java new file mode 100644 index 00000000..9f38eae1 --- /dev/null +++ b/25-tables/java/example.java @@ -0,0 +1,85 @@ +package num25; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.firefox.FirefoxDriver; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +/** + * Created by andrew on 8/22/15. + */ +public class example { + WebDriver driver; + + @Before + public void setUp() throws Exception { + driver = new FirefoxDriver(); + + } + + @After + public void tearDown() throws Exception { + driver.quit(); + } + + @Test + public void test() + { + // sort dues ascending + driver.findElement(By.cssSelector("#table1 thead tr th:nth-of-type(4)")).click(); + + // get dues + List dues = driver.findElements(By.cssSelector("#table1 tbody tr td:nth-of-type(4)")); + List dueValues = new LinkedList(); + for(WebElement element : dues){ + dueValues.add(Double.parseDouble(element.getText().replace("$", ""))); + } + + // assert dues are ascending + for(int counter = 1; counter < dueValues.size(); counter++){ + assert(dueValues.get(counter - 1) <= dueValues.get(counter)); + } + + // sort dues descending + driver.findElement(By.cssSelector("#table1 thead tr th:nth-of-type(4)")).click(); + + // get dues + dues = driver.findElements(By.cssSelector("#table1 tbody tr td:nth-of-type(4)")); + dueValues = new LinkedList(); + for(WebElement element : dues){ + dueValues.add(Double.parseDouble(element.getText().replace("$", ""))); + } + + // assert dues are descending + for(int counter = 1; counter < dueValues.size(); counter++){ + assert(dueValues.get(counter - 1) >= dueValues.get(counter)); + } + + // sort email ascending + driver.findElement(By.cssSelector("#table1 thead tr th:nth-of-type(3)")).click(); + List emailsValues = new LinkedList(); + List emails = driver.findElements(By.cssSelector("#table1 tbody tr td:nth-of-type(3)")); + + for(int counter = 1; counter < emails.size(); counter++){ + assert (emailsValues.get(counter -1).compareToIgnoreCase(emailsValues.get(counter)) < 0); + } + } + + @Test + public void testDues() + { + driver.get("http://the-internet.herokuapp.com/tables"); + List dues = driver.findElements(By.cssSelector("#table1 tbody tr td:nth-of-type(4)")); + List dueValues = new LinkedList(); + for(WebElement element : dues){ + dueValues.add(Double.parseDouble(element.getText().replace("$", ""))); + } + } +} diff --git a/40-disabled-element/java/example.java b/40-disabled-element/java/example.java new file mode 100644 index 00000000..5c1de80a --- /dev/null +++ b/40-disabled-element/java/example.java @@ -0,0 +1,36 @@ +package num40; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.support.ui.Select; + +/** + * Created by andrew on 8/22/15. + */ +public class example { + WebDriver driver; + + @Before + public void setUp() throws Exception { + driver = new FirefoxDriver(); + + } + + @After + public void tearDown() throws Exception { + driver.quit(); + } + + @Test + public void test() { + driver.get("http://the-internet.herokuapp.com/dropdown"); + Select dropdown = new Select(driver.findElement(By.tagName("option"))); + WebElement itemsOfInterest = dropdown.getOptions().get(1); + assert (itemsOfInterest.isEnabled()); + } +}