diff --git a/.travis.yml b/.travis.yml index 032a2da..66d9c5f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: java jdk: -- oraclejdk8 +- oraclejdk9 before_install: - export | grep SAUCE_ script: mvn clean test diff --git a/src/test/java/com/yourcompany/Pages/GuineaPigPage.java b/src/test/java/com/yourcompany/Pages/GuineaPigPage.java deleted file mode 100644 index bf59de1..0000000 --- a/src/test/java/com/yourcompany/Pages/GuineaPigPage.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.yourcompany.Pages; - -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.support.FindBy; -import org.openqa.selenium.support.PageFactory; -import org.openqa.selenium.support.ui.WebDriverWait; -import org.openqa.selenium.support.ui.ExpectedConditions; - -public class GuineaPigPage { - - @FindBy(linkText = "i am a link") - private WebElement theActiveLink; - - @FindBy(id = "your_comments") - private WebElement yourCommentsSpan; - - @FindBy(id = "comments") - private WebElement commentsTextAreaInput; - - @FindBy(id = "submit") - private WebElement submitButton; - - public WebDriver driver; - public static String url = "https://saucelabs-sample-test-frameworks.github.io/training-test-page"; - - public static GuineaPigPage visitPage(WebDriver driver) { - GuineaPigPage page = new GuineaPigPage(driver); - page.visitPage(); - return page; - } - - public GuineaPigPage(WebDriver driver) { - this.driver = driver; - PageFactory.initElements(driver, this); - } - - public void visitPage() { - this.driver.get(url); - } - - public void followLink() { - this.theActiveLink.click(); - } - - public void submitComment(String text) { - this.commentsTextAreaInput.sendKeys(text); - this.submitButton.click(); - - // Race condition for time to populate yourCommentsSpan - WebDriverWait wait = new WebDriverWait(this.driver, 15); - wait.until(ExpectedConditions.textToBePresentInElement(yourCommentsSpan, text)); - } - - public String getSubmittedCommentText() { - return this.yourCommentsSpan.getText(); - } - - public boolean isOnPage() { - String title = "I am a page title - Sauce Labs"; - return this.driver.getTitle() == title; - } - -} diff --git a/src/test/java/com/yourcompany/Tests/AddItemsToCartTest.java b/src/test/java/com/yourcompany/Tests/AddItemsToCartTest.java new file mode 100644 index 0000000..aeb4b8d --- /dev/null +++ b/src/test/java/com/yourcompany/Tests/AddItemsToCartTest.java @@ -0,0 +1,41 @@ +package com.yourcompany.Tests; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; + +public class AddItemsToCartTest extends TestBase { + + public AddItemsToCartTest(String os, + String version, String browser, String deviceName, String deviceOrientation) { + super(os, version, browser, deviceName, deviceOrientation); + } + + @Test + public void addOneItem(){ + driver.get("http://www.saucedemo.com/inventory.html"); + driver.findElement(By.className("btn_primary")).click(); + + Assert.assertEquals(driver.findElement(By.className("shopping_cart_badge")).getText(), "1"); + + driver.get("http://www.saucedemo.com/cart.html"); + long expected = driver.findElements(By.className("inventory_item_name")).size(); + + Assert.assertEquals(expected, 1); + + } + + @Test + public void addTwoItems(){ + driver.get("http://www.saucedemo.com/inventory.html"); + driver.findElement(By.className("btn_primary")).click(); + driver.findElement(By.className("btn_primary")).click(); + + Assert.assertEquals(driver.findElement(By.className("shopping_cart_badge")).getText(), "2"); + + driver.get("http://www.saucedemo.com/cart.html"); + long expected = driver.findElements(By.className("inventory_item_name")).size(); + + Assert.assertEquals(expected, 2); + } +} diff --git a/src/test/java/com/yourcompany/Tests/FollowLinkTest.java b/src/test/java/com/yourcompany/Tests/FollowLinkTest.java deleted file mode 100644 index 6dc0b56..0000000 --- a/src/test/java/com/yourcompany/Tests/FollowLinkTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.yourcompany.Tests; - -import com.yourcompany.Pages.*; -import org.junit.Test; -import org.openqa.selenium.InvalidElementStateException; - -import static org.junit.Assert.*; - -public class FollowLinkTest extends TestBase { - - public FollowLinkTest(String os, - String version, String browser, String deviceName, String deviceOrientation) { - super(os, version, browser, deviceName, deviceOrientation); - } - - /** - * Runs a simple test verifying link can be followed. - * @throws InvalidElementStateException - */ - @Test - public void verifyLinkTest() throws InvalidElementStateException { - GuineaPigPage page = GuineaPigPage.visitPage(driver); - - page.followLink(); - - assertFalse(page.isOnPage()); - } -} \ No newline at end of file diff --git a/src/test/java/com/yourcompany/Tests/InvalidLoginTest.java b/src/test/java/com/yourcompany/Tests/InvalidLoginTest.java new file mode 100644 index 0000000..5aafdee --- /dev/null +++ b/src/test/java/com/yourcompany/Tests/InvalidLoginTest.java @@ -0,0 +1,24 @@ +package com.yourcompany.Tests; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; + +public class InvalidLoginTest extends TestBase { + + public InvalidLoginTest(String os, + String version, String browser, String deviceName, String deviceOrientation) { + super(os, version, browser, deviceName, deviceOrientation); + } + + @Test + public void blankCredentials(){ + driver.get("http://www.saucedemo.com"); + + driver.findElement(By.id("user-name")).sendKeys(""); + driver.findElement(By.id("password")).sendKeys(""); + driver.findElement(By.cssSelector(".btn_action")).click(); + + Assert.assertTrue(driver.findElement(By.cssSelector(".error-button")).isDisplayed()); + } +} diff --git a/src/test/java/com/yourcompany/Tests/RemoveItemFromCartTest.java b/src/test/java/com/yourcompany/Tests/RemoveItemFromCartTest.java new file mode 100644 index 0000000..6f6084f --- /dev/null +++ b/src/test/java/com/yourcompany/Tests/RemoveItemFromCartTest.java @@ -0,0 +1,28 @@ +package com.yourcompany.Tests; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; + +public class RemoveItemFromCartTest extends TestBase { + + public RemoveItemFromCartTest(String os, + String version, String browser, String deviceName, String deviceOrientation) { + super(os, version, browser, deviceName, deviceOrientation); + } + + @Test + public void removeOne() { + + driver.get("http://www.saucedemo.com/inventory.html"); + driver.findElement(By.className("btn_primary")).click(); + driver.findElement(By.className("btn_primary")).click(); + driver.findElement(By.className("btn_secondary")).click(); + + Assert.assertEquals(driver.findElement(By.className("shopping_cart_badge")).getText(), "1"); + + driver.get("http://www.saucedemo.com/cart.html"); + long expected = driver.findElements(By.className("inventory_item_name")).size(); + Assert.assertEquals(expected, 1); + } +} diff --git a/src/test/java/com/yourcompany/Tests/TestBase.java b/src/test/java/com/yourcompany/Tests/TestBase.java index 2979a92..f74a7e3 100644 --- a/src/test/java/com/yourcompany/Tests/TestBase.java +++ b/src/test/java/com/yourcompany/Tests/TestBase.java @@ -13,6 +13,7 @@ import com.saucelabs.junit.ConcurrentParameterized; import com.saucelabs.junit.SauceOnDemandTestWatcher; +import java.net.MalformedURLException; import java.net.URL; import java.util.LinkedList; @@ -91,11 +92,11 @@ public TestBase(String os, String version, String browser, String deviceName, St public static LinkedList browsersStrings() { LinkedList browsers = new LinkedList(); - browsers.add(new String[]{"Windows 10", "14.14393", "MicrosoftEdge", null, null}); - browsers.add(new String[]{"Windows 10", "49.0", "firefox", null, null}); - browsers.add(new String[]{"Windows 7", "11.0", "internet explorer", null, null}); - browsers.add(new String[]{"OS X 10.11", "10.0", "safari", null, null}); - browsers.add(new String[]{"OS X 10.10", "54.0", "chrome", null, null}); + browsers.add(new String[]{"Windows 10", "latest", "firefox", null, null}); + browsers.add(new String[]{"MacOS 10.14", "12", "safari", null, null}); + browsers.add(new String[]{"MacOS 10.13", "latest", "chrome", null, null}); + browsers.add(new String[]{"Windows 10", "latest-1", "firefox", null, null}); + browsers.add(new String[]{"Windows 10", "11.0", "internet explorer", null, null}); return browsers; } @@ -104,10 +105,10 @@ public static LinkedList browsersStrings() { * {@link #version} and {@link #os} instance variables, and which is configured to run against ondemand.saucelabs.com, using * the username and access key populated by the {@link #authentication} instance. * - * @throws Exception if an error occurs during the creation of the {@link RemoteWebDriver} instance. + * @throws MalformedURLException if an error occurs during the creation of the {@link RemoteWebDriver} instance. */ @Before - public void setUp() throws Exception { + public void setUp() throws MalformedURLException { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, browser); @@ -121,8 +122,8 @@ public void setUp() throws Exception { //Getting the build name. //Using the Jenkins ENV var. You can use your own. If it is not set test will run without a build id. - if (buildTag != null) { - capabilities.setCapability("build", buildTag); + if (buildTag == null) { + capabilities.setCapability("build", "joshs-build"); } this.driver = new RemoteWebDriver( new URL("https://" + username+ ":" + accesskey + seleniumURI +"/wd/hub"), diff --git a/src/test/java/com/yourcompany/Tests/TextInputTest.java b/src/test/java/com/yourcompany/Tests/TextInputTest.java deleted file mode 100644 index 7b96eef..0000000 --- a/src/test/java/com/yourcompany/Tests/TextInputTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.yourcompany.Tests; - -import com.yourcompany.Pages.*; -import org.junit.Test; -import org.openqa.selenium.InvalidElementStateException; -import static org.hamcrest.CoreMatchers.containsString; - -import java.util.UUID; - -import static org.junit.Assert.*; - -public class TextInputTest extends TestBase { - - public TextInputTest(String os, - String version, String browser, String deviceName, String deviceOrientation) { - super(os, version, browser, deviceName, deviceOrientation); - } - - /** - * Runs a simple test verifying if the comment input is functional. - * @throws InvalidElementStateException - */ - @Test - public void verifyCommentInputTest() throws InvalidElementStateException { - String commentInputText = UUID.randomUUID().toString(); - - GuineaPigPage page = GuineaPigPage.visitPage(driver); - page.visitPage(); - page.submitComment(commentInputText); - - assertThat(page.getSubmittedCommentText(), containsString(commentInputText)); - } -} \ No newline at end of file diff --git a/src/test/java/com/yourcompany/Tests/ValidLoginTest.java b/src/test/java/com/yourcompany/Tests/ValidLoginTest.java new file mode 100644 index 0000000..7bb42d6 --- /dev/null +++ b/src/test/java/com/yourcompany/Tests/ValidLoginTest.java @@ -0,0 +1,24 @@ +package com.yourcompany.Tests; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; + +public class ValidLoginTest extends TestBase { + + public ValidLoginTest(String os, + String version, String browser, String deviceName, String deviceOrientation) { + super(os, version, browser, deviceName, deviceOrientation); + } + + @Test + public void standardUser(){ + driver.get("http://www.saucedemo.com"); + + driver.findElement(By.id("user-name")).sendKeys("standard_user"); + driver.findElement(By.id("password")).sendKeys("secret_sauce"); + driver.findElement(By.cssSelector(".btn_action")).click(); + + Assert.assertTrue(driver.getCurrentUrl().contains("inventory.html")); + } +}