Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sauce demo #37

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: java
jdk:
- oraclejdk8
- oraclejdk9
before_install:
- export | grep SAUCE_
script: mvn clean install
Expand Down
60 changes: 0 additions & 60 deletions src/test/java/com/yourcompany/Pages/GuineaPigPage.java

This file was deleted.

44 changes: 44 additions & 0 deletions src/test/java/com/yourcompany/Tests/AddItemsToCartTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.yourcompany.Tests;

import org.testng.Assert;
import org.testng.annotations.Test;
import org.openqa.selenium.By;

import java.lang.reflect.Method;
import java.net.MalformedURLException;

public class AddItemsToCartTest extends TestBase {


@Test(dataProvider = "hardCodedBrowsers")
public void addOneItem(String browser, String version, String os, Method method) throws MalformedURLException {
this.createDriver(browser, version, os, method.getName());

getDriver().get("http://www.saucedemo.com/inventory.html");
getDriver().findElement(By.className("btn_primary")).click();

Assert.assertEquals(getDriver().findElement(By.className("shopping_cart_badge")).getText(), "1");

getDriver().get("http://www.saucedemo.com/cart.html");
long expected = getDriver().findElements(By.className("inventory_item_name")).size();

Assert.assertEquals(expected, 1);

}

@Test(dataProvider = "hardCodedBrowsers")
public void addTwoItems(String browser, String version, String os, Method method) throws MalformedURLException {
this.createDriver(browser, version, os, method.getName());

getDriver().get("http://www.saucedemo.com/inventory.html");
getDriver().findElement(By.className("btn_primary")).click();
getDriver().findElement(By.className("btn_primary")).click();

Assert.assertEquals(getDriver().findElement(By.className("shopping_cart_badge")).getText(), "2");

getDriver().get("http://www.saucedemo.com/cart.html");
long expected = getDriver().findElements(By.className("inventory_item_name")).size();

Assert.assertEquals(expected, 2);
}
}
42 changes: 0 additions & 42 deletions src/test/java/com/yourcompany/Tests/FollowLinkTest.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/test/java/com/yourcompany/Tests/InvalidLoginTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.yourcompany.Tests;

import org.testng.Assert;
import org.testng.annotations.Test;
import org.openqa.selenium.By;

import java.lang.reflect.Method;
import java.net.MalformedURLException;

public class InvalidLoginTest extends TestBase {


@Test(dataProvider = "hardCodedBrowsers")
public void blankCredentials(String browser, String version, String os, Method method) throws MalformedURLException {
this.createDriver(browser, version, os, method.getName());

getDriver().get("http://www.saucedemo.com");

getDriver().findElement(By.id("user-name")).sendKeys("");
getDriver().findElement(By.id("password")).sendKeys("");
getDriver().findElement(By.cssSelector(".btn_action")).click();

Assert.assertTrue(getDriver().findElement(By.cssSelector(".error-button")).isDisplayed());
}
}
29 changes: 29 additions & 0 deletions src/test/java/com/yourcompany/Tests/RemoveItemFromCartTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.yourcompany.Tests;

import org.testng.Assert;
import org.testng.annotations.Test;
import org.openqa.selenium.By;

import java.lang.reflect.Method;
import java.net.MalformedURLException;


public class RemoveItemFromCartTest extends TestBase {


@Test(dataProvider = "hardCodedBrowsers")
public void removeOne(String browser, String version, String os, Method method) throws MalformedURLException {
this.createDriver(browser, version, os, method.getName());

getDriver().get("http://www.saucedemo.com/inventory.html");
getDriver().findElement(By.className("btn_primary")).click();
getDriver().findElement(By.className("btn_primary")).click();
getDriver().findElement(By.className("btn_secondary")).click();

Assert.assertEquals(getDriver().findElement(By.className("shopping_cart_badge")).getText(), "1");

getDriver().get("http://www.saucedemo.com/cart.html");
long expected = getDriver().findElements(By.className("inventory_item_name")).size();
Assert.assertEquals(expected, 1);
}
}
27 changes: 10 additions & 17 deletions src/test/java/com/yourcompany/Tests/TestBase.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.yourcompany.Tests;

import com.saucelabs.common.SauceOnDemandAuthentication;
import com.saucelabs.common.SauceOnDemandSessionIdProvider;
import com.saucelabs.testng.SauceOnDemandAuthenticationProvider;
import com.saucelabs.testng.SauceOnDemandTestListener;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
Expand All @@ -12,7 +8,6 @@
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;

import java.lang.reflect.Method;
import java.net.MalformedURLException;
Expand All @@ -35,7 +30,7 @@ public class TestBase {
/**
* ThreadLocal variable which contains the {@link WebDriver} instance which is used to perform browser interactions with.
*/
private ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
private ThreadLocal<WebDriver> driver = new ThreadLocal<WebDriver>();

/**
* ThreadLocal variable which contains the Sauce Job Id.
Expand All @@ -51,10 +46,8 @@ public class TestBase {
@DataProvider(name = "hardCodedBrowsers", parallel = true)
public static Object[][] sauceBrowserDataProvider(Method testMethod) {
return new Object[][]{
new Object[]{"MicrosoftEdge", "17.17134", "Windows 10"},
new Object[]{"firefox", "latest", "Windows 10"},
new Object[]{"internet explorer", "11", "Windows 8.1"},
new Object[]{"safari", "12.0", "macOS 10.13"},
new Object[]{"safari", "12.1", "macOS 10.13"},
new Object[]{"chrome", "70", "macOS 10.13"},
new Object[]{"firefox", "latest-1", "Windows 7"},
};
Expand All @@ -63,8 +56,8 @@ public static Object[][] sauceBrowserDataProvider(Method testMethod) {
/**
* @return the {@link WebDriver} for the current thread
*/
public WebDriver getWebDriver() {
return webDriver.get();
public WebDriver getDriver() {
return driver.get();
}

/**
Expand All @@ -88,7 +81,7 @@ public String getSessionId() {
* @throws MalformedURLException if an error occurs parsing the url
*/
protected void createDriver(String browser, String version, String os, String methodName)
throws MalformedURLException, UnexpectedException {
throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();

// set desired capabilities to launch appropriate browser on Sauce
Expand All @@ -102,12 +95,12 @@ protected void createDriver(String browser, String version, String os, String me
}

// Launch remote browser and set it as the current thread
webDriver.set(new RemoteWebDriver(
driver.set(new RemoteWebDriver(
new URL("https://" + username + ":" + accesskey + "@ondemand.saucelabs.com/wd/hub"),
capabilities));

// set current sessionId
String id = ((RemoteWebDriver) getWebDriver()).getSessionId().toString();
String id = ((RemoteWebDriver) getDriver()).getSessionId().toString();
sessionId.set(id);
}

Expand All @@ -118,11 +111,11 @@ protected void createDriver(String browser, String version, String os, String me
*/
@AfterMethod
public void tearDown(ITestResult result) throws Exception {
((JavascriptExecutor) webDriver.get()).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));
webDriver.get().quit();
((JavascriptExecutor) driver.get()).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));
driver.get().quit();
}

protected void annotate(String text) {
((JavascriptExecutor) webDriver.get()).executeScript("sauce:context=" + text);
((JavascriptExecutor) driver.get()).executeScript("sauce:context=" + text);
}
}
43 changes: 0 additions & 43 deletions src/test/java/com/yourcompany/Tests/TextInputTest.java

This file was deleted.

24 changes: 24 additions & 0 deletions src/test/java/com/yourcompany/Tests/ValidLoginTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.yourcompany.Tests;

import org.testng.Assert;
import org.testng.annotations.Test;
import org.openqa.selenium.By;

import java.lang.reflect.Method;
import java.net.MalformedURLException;

public class ValidLoginTest extends TestBase {

@Test(dataProvider = "hardCodedBrowsers")
public void standardUser(String browser, String version, String os, Method method) throws MalformedURLException {
this.createDriver(browser, version, os, method.getName());

getDriver().get("http://www.saucedemo.com");

getDriver().findElement(By.id("user-name")).sendKeys("standard_user");
getDriver().findElement(By.id("password")).sendKeys("secret_sauce");
getDriver().findElement(By.cssSelector(".btn_action")).click();

Assert.assertTrue(getDriver().getCurrentUrl().contains("inventory.html"));
}
}
8 changes: 3 additions & 5 deletions src/test/java/com/yourcompany/Tests/W3CTestNG.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.yourcompany.Tests;

import com.yourcompany.Pages.GuineaPigPage;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeOptions;
Expand Down Expand Up @@ -74,9 +72,9 @@ public void teardown(ITestResult result){
public void simpleCase(MutableCapabilities options, String browserVersion, String platformName) throws MalformedURLException {
this.createDriverOptions(options, browserVersion, platformName);

GuineaPigPage page = GuineaPigPage.visitPage(driver);
String title = page.driver.getTitle();
this.driver.get("http://www.saucedemo.com");
String title = this.driver.getTitle();

Assert.assertTrue(title.contains("Sauce"));
Assert.assertTrue(title.contains("Swag Labs"));
}
}