From 440e11ebc9e807cc1abc3044d8a1692900b7e2f1 Mon Sep 17 00:00:00 2001 From: eyaly Date: Mon, 10 Jul 2023 10:59:48 +0100 Subject: [PATCH] Amend the DesktopTests (#153) --- best-practice/pom.xml | 17 +++ .../test/java/com/saucedemo/Constants.java | 13 ++ .../java/com/saucedemo/tests/BaseTest.java | 142 ++++++++++++++++++ .../com/saucedemo/tests/DesktopTests.java | 61 +------- pom.xml | 2 +- 5 files changed, 174 insertions(+), 61 deletions(-) create mode 100644 best-practice/src/test/java/com/saucedemo/Constants.java create mode 100644 best-practice/src/test/java/com/saucedemo/tests/BaseTest.java diff --git a/best-practice/pom.xml b/best-practice/pom.xml index 169417a4..de5e46ad 100644 --- a/best-practice/pom.xml +++ b/best-practice/pom.xml @@ -11,6 +11,10 @@ best-practice + + 4.10.0 + + com.saucelabs @@ -18,6 +22,19 @@ ${sauce_junit4.version} test + + + org.seleniumhq.selenium + selenium-api + ${selenium.version} + + + + org.seleniumhq.selenium + selenium-java + ${selenium.version} + + diff --git a/best-practice/src/test/java/com/saucedemo/Constants.java b/best-practice/src/test/java/com/saucedemo/Constants.java new file mode 100644 index 00000000..d89febae --- /dev/null +++ b/best-practice/src/test/java/com/saucedemo/Constants.java @@ -0,0 +1,13 @@ +package com.saucedemo; + +public class Constants { + public static final String region = System.getProperty("region", "eu"); + + public static final String SAUCE_EU_URL = "https://ondemand.eu-central-1.saucelabs.com:443/wd/hub"; + public static final String SAUCE_US_URL = "https://ondemand.us-west-1.saucelabs.com:443/wd/hub"; + + public static final String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME"); + public static final String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY"); + +} + diff --git a/best-practice/src/test/java/com/saucedemo/tests/BaseTest.java b/best-practice/src/test/java/com/saucedemo/tests/BaseTest.java new file mode 100644 index 00000000..4ae05f4d --- /dev/null +++ b/best-practice/src/test/java/com/saucedemo/tests/BaseTest.java @@ -0,0 +1,142 @@ +package com.saucedemo.tests; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.rules.TestName; +import org.junit.rules.TestRule; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.junit.runners.Parameterized; +import org.openqa.selenium.MutableCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; + +import java.net.MalformedURLException; +import java.net.URL; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.Collection; + +import static com.saucedemo.Constants.*; + +public class BaseTest { + protected RemoteWebDriver driver; + + protected String WEB_URL = "https://www.saucedemo.com/"; + + @Parameterized.Parameter + public String platform; + @Parameterized.Parameter(1) + public String browserDeviceName; + @Parameterized.Parameter(2) + public String browserPlatformVersion; + @Parameterized.Parameter(3) + public String platformName; + + @Parameterized.Parameters() + public static Collection crossPlatformData() { + return Arrays.asList(new Object[][] { + { "desktop", "safari", "latest", "macOS 11.00" }, + { "desktop", "chrome", "latest-1", "macOS 13" }, + { "desktop", "firefox", "latest", "Windows 11" }, + { "desktop", "chrome", "latest", "Windows 10" } + }); + } + @Rule + public TestName name = new TestName(); + + @Before + public void setup() throws MalformedURLException { + + System.out.println("BeforeMethod hook"); + URL url; + + switch (region) { + case "us": + url = new URL(SAUCE_US_URL); + break; + case "eu": + default: + url = new URL(SAUCE_EU_URL); + break; + } + + boolean isBuildCap = false; + MutableCapabilities caps = new MutableCapabilities(); + MutableCapabilities sauceOptions = new MutableCapabilities(); + + switch (platform) { + case "desktop": + caps.setCapability("browserName", browserDeviceName); + caps.setCapability("browserVersion", browserPlatformVersion); + caps.setCapability("platformName", platformName); + break; + case "android": + caps.setCapability("platformName", "android"); + caps.setCapability("appium:automationName", "UiAutomator2"); + caps.setCapability("browserName", "chrome"); + caps.setCapability("appium:deviceName", browserDeviceName); + caps.setCapability("appium:platformVersion", browserPlatformVersion); + break; + case "ios": + caps.setCapability("platformName", "iOS"); + caps.setCapability("appium:automationName", "XCuiTest"); + caps.setCapability("browserName", "safari"); + caps.setCapability("appium:deviceName", browserDeviceName); + caps.setCapability("appium:platformVersion", browserPlatformVersion); + break; + default: + throw new IllegalStateException("Unexpected value: " + platform); + } + + sauceOptions.setCapability("username", System.getenv("SAUCE_USERNAME")); + sauceOptions.setCapability("accessKey", System.getenv("SAUCE_ACCESS_KEY")); + sauceOptions.setCapability("name", name.getMethodName()); + + if (!isBuildCap) { //handle build cap + LocalDateTime dateTime = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy-HH"); + String buildLocal = "sauceDemo-" +dateTime.format(formatter); + String buildVal = System.getenv("BUILD_TAG"); + sauceOptions.setCapability("build", buildVal == null ? buildLocal : buildVal); + } + + caps.setCapability("sauce:options", sauceOptions); + + try { + driver = new RemoteWebDriver(url, caps); + + } catch (Exception e) { + System.out.println("*** Problem to create the driver " + e.getMessage()); + throw new RuntimeException(e); + } + } + + + @Rule + public TestRule watcher = new TestWatcher() { + + @Override + protected void succeeded(Description description) { + if(driver != null) + { + System.out.println("Test Passed!"); + driver.executeScript("sauce:job-result=passed"); + driver.quit(); + } + } + + @Override + public void failed(Throwable e, Description description) { + if(driver != null) + { + System.out.println("Test Failed!"); + driver.executeScript("sauce:job-result=failed"); + driver.executeScript("sauce:context=" +e.getMessage()); + driver.quit(); + } + } + + }; + +} diff --git a/best-practice/src/test/java/com/saucedemo/tests/DesktopTests.java b/best-practice/src/test/java/com/saucedemo/tests/DesktopTests.java index abb4a4a5..caa80e02 100644 --- a/best-practice/src/test/java/com/saucedemo/tests/DesktopTests.java +++ b/best-practice/src/test/java/com/saucedemo/tests/DesktopTests.java @@ -2,76 +2,17 @@ import com.saucedemo.pages.LoginPage; import com.saucedemo.pages.ProductsPage; -import com.saucelabs.saucebindings.Browser; -import com.saucelabs.saucebindings.SaucePlatform; -import com.saucelabs.saucebindings.junit4.SauceBaseTest; -import com.saucelabs.saucebindings.options.SauceOptions; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.openqa.selenium.TimeoutException; -import java.util.Arrays; -import java.util.Collection; - import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** Desktop Tests. */ @RunWith(Parameterized.class) -public class DesktopTests extends SauceBaseTest { - /* - * Configure our data driven parameters - * */ - @Parameterized.Parameter - public Browser browserName; - @Parameterized.Parameter(1) - public String browserVersion; - @Parameterized.Parameter(2) - public SaucePlatform platform; - - @Parameterized.Parameters() - public static Collection crossBrowserData() { - return Arrays.asList(new Object[][] { - { Browser.CHROME, "latest", SaucePlatform.WINDOWS_10 }, - { Browser.CHROME, "latest-1", SaucePlatform.WINDOWS_10 }, - { Browser.SAFARI, "latest", SaucePlatform.MAC_MOJAVE }, - { Browser.CHROME, "latest", SaucePlatform.MAC_MOJAVE } - /* - // Duplication below for demo purposes of massive parallelization - {Browser.CHROME, "latest", SaucePlatform.WINDOWS_10}, - {Browser.CHROME, "latest-1", SaucePlatform.WINDOWS_10}, - {Browser.SAFARI, "latest", SaucePlatform.MAC_MOJAVE}, - {Browser.CHROME, "latest", SaucePlatform.MAC_MOJAVE}, - {Browser.CHROME, "latest", SaucePlatform.WINDOWS_10}, - {Browser.CHROME, "latest-1", SaucePlatform.WINDOWS_10}, - {Browser.SAFARI, "latest", SaucePlatform.MAC_MOJAVE}, - {Browser.CHROME, "latest", SaucePlatform.MAC_MOJAVE}, - {Browser.CHROME, "latest", SaucePlatform.WINDOWS_10}, - {Browser.CHROME, "latest-1", SaucePlatform.WINDOWS_10}, - {Browser.SAFARI, "latest", SaucePlatform.MAC_MOJAVE}, - {Browser.CHROME, "latest", SaucePlatform.MAC_MOJAVE}, - {Browser.CHROME, "latest", SaucePlatform.WINDOWS_10}, - {Browser.CHROME, "latest-1", SaucePlatform.WINDOWS_10}, - {Browser.SAFARI, "latest", SaucePlatform.MAC_MOJAVE}, - {Browser.CHROME, "latest", SaucePlatform.MAC_MOJAVE}, - {Browser.CHROME, "latest", SaucePlatform.WINDOWS_10}, - {Browser.CHROME, "latest-1", SaucePlatform.WINDOWS_10}, - {Browser.SAFARI, "latest", SaucePlatform.MAC_MOJAVE}, - {Browser.CHROME, "latest", SaucePlatform.MAC_MOJAVE}, - */ - }); - } - - @Override - public SauceOptions createSauceOptions() { - SauceOptions sauceOptions = new SauceOptions(); - sauceOptions.setBrowserName(browserName); - sauceOptions.setBrowserVersion(browserVersion); - sauceOptions.setPlatformName(platform); - - return sauceOptions; - } +public class DesktopTests extends BaseTest { @Test() public void loginWorks() { diff --git a/pom.xml b/pom.xml index e760bec8..3c2b1905 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ 1.0-SNAPSHOT - 4.7.0 + 4.10.0 8.3.0 1.2.0 1.0.0