From 755af301530421ddfba8b0abb3d9e22ffbbcc3fc Mon Sep 17 00:00:00 2001 From: Karan Shah <64479353+karanshah-browserstack@users.noreply.github.com> Date: Tue, 12 Jul 2022 12:30:17 +0530 Subject: [PATCH] Update Username Access Key from env vars --- pom.xml | 208 +++++++++--------- .../com/browserstack/app/JavaLocalSample.java | 90 ++++---- .../browserstack/app/JavaParallelSample.java | 142 ++++++------ .../java/com/browserstack/app/JavaSample.java | 93 ++++---- 4 files changed, 272 insertions(+), 261 deletions(-) diff --git a/pom.xml b/pom.xml index d0c5b34..8a87fca 100644 --- a/pom.xml +++ b/pom.xml @@ -1,113 +1,113 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.browserstack.app - java-browserstack - 1.0-SNAPSHOT + com.browserstack.app + java-browserstack + 1.0-SNAPSHOT - java-browserstack - - http://www.browserstack.com + java-browserstack + + http://www.browserstack.com - - UTF-8 - 1.7 - 1.7 - + + UTF-8 + 1.8 + 1.8 + - - - junit - junit - 4.11 - test - - - commons-io - commons-io - 1.3.2 - - - org.seleniumhq.selenium - selenium-java - 4.1.0 - - - com.browserstack - browserstack-local-java - 1.0.3 - - - com.googlecode.json-simple - json-simple - 1.1.1 - - + + + junit + junit + 4.11 + test + + + commons-io + commons-io + 1.3.2 + + + org.seleniumhq.selenium + selenium-java + 4.1.0 + + + com.browserstack + browserstack-local-java + 1.0.3 + + + com.googlecode.json-simple + json-simple + 1.1.1 + + - - - - - - maven-clean-plugin - 3.1.0 - - - - maven-resources-plugin - 3.0.2 - - - maven-compiler-plugin - 3.8.0 - - - maven-surefire-plugin - 2.22.1 - - - maven-jar-plugin - 3.0.2 - - - maven-install-plugin - 2.5.2 - - - maven-deploy-plugin - 2.8.2 - - - - maven-site-plugin - 3.7.1 - - - maven-project-info-reports-plugin - 3.0.0 - - - - - - - single - - - - org.apache.maven.plugins - maven-surefire-plugin - - - com/browserstack/app/JavaSample.java - - - - - - - + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + + + single + + + + org.apache.maven.plugins + maven-surefire-plugin + + + com/browserstack/app/JavaSample.java + + + + + + + diff --git a/src/test/java/com/browserstack/app/JavaLocalSample.java b/src/test/java/com/browserstack/app/JavaLocalSample.java index f3f1dc2..369cb4e 100644 --- a/src/test/java/com/browserstack/app/JavaLocalSample.java +++ b/src/test/java/com/browserstack/app/JavaLocalSample.java @@ -1,11 +1,13 @@ package com.browserstack.app; //Sample test in Java to run Automate session. + import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.JavascriptExecutor; + import java.net.URL; import java.time.Duration; import java.util.HashMap; @@ -15,55 +17,57 @@ import com.browserstack.local.Local; public class JavaLocalSample { - public static final String AUTOMATE_USERNAME = "BROWSERSTACK_USERNAME"; - public static final String AUTOMATE_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY"; -public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub"; -public static void main(String[] args) throws Exception { - DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setCapability("browserName", "Chrome"); - capabilities.setCapability("browserVersion", "latest"); - HashMap browserstackOptions = new HashMap(); - browserstackOptions.put("os", "OS X"); - browserstackOptions.put("osVersion", "Sierra"); - browserstackOptions.put("local", "true"); - browserstackOptions.put("seleniumVersion", "4.0.0"); - capabilities.setCapability("bstack:options", browserstackOptions); - capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name - capabilities.setCapability("buildName", "BStack Local Build Number 1"); // CI/CD job or build name - + public static final String AUTOMATE_USERNAME = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : "BROWSERSTACK_USERNAME"; + public static final String AUTOMATE_ACCESS_KEY = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : "BROWSERSTACK_ACCESS_KEY"; + public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub"; + + public static void main(String[] args) throws Exception { + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability("browserName", "Chrome"); + capabilities.setCapability("browserVersion", "latest"); + HashMap browserstackOptions = new HashMap(); + browserstackOptions.put("os", "OS X"); + browserstackOptions.put("osVersion", "Sierra"); + browserstackOptions.put("local", "true"); + browserstackOptions.put("seleniumVersion", "4.0.0"); + capabilities.setCapability("bstack:options", browserstackOptions); + capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name + capabilities.setCapability("buildName", "BStack Local Build Number 1"); // CI/CD job or build name + //Creates an instance of Local -Local bsLocal = new Local(); + Local bsLocal = new Local(); // You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY". -HashMap bsLocalArgs = new HashMap(); -bsLocalArgs.put("key", AUTOMATE_ACCESS_KEY); + HashMap bsLocalArgs = new HashMap(); + bsLocalArgs.put("key", AUTOMATE_ACCESS_KEY); // Starts the Local instance with the required arguments -bsLocal.start(bsLocalArgs); + bsLocal.start(bsLocalArgs); // Check if BrowserStack local instance is running -System.out.println(bsLocal.isRunning()); + System.out.println(bsLocal.isRunning()); - final WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities); - try { - driver.get("http://bs-local.com:45691/check"); - final WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); - // getting name of the product - String bodyText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body"))).getText(); - - if (bodyText.equals("Up and running")) { - markTestStatus("passed", "Local Test is successful and up and running", driver); - } - } catch (Exception e) { - markTestStatus("failed", "Could'nt connect the local", driver); - } - //Stop the Local instance - bsLocal.stop(); - driver.quit(); - } -// This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack -public static void markTestStatus(String status, String reason, WebDriver driver) { - final JavascriptExecutor jse = (JavascriptExecutor) driver; - jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}"); -} + final WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities); + try { + driver.get("http://bs-local.com:45691/check"); + final WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); + // getting name of the product + String bodyText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body"))).getText(); + + if (bodyText.equals("Up and running")) { + markTestStatus("passed", "Local Test is successful and up and running", driver); + } + } catch (Exception e) { + markTestStatus("failed", "Could'nt connect the local", driver); + } + //Stop the Local instance + bsLocal.stop(); + driver.quit(); + } + + // This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack + public static void markTestStatus(String status, String reason, WebDriver driver) { + final JavascriptExecutor jse = (JavascriptExecutor) driver; + jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"" + status + "\", \"reason\": \"" + reason + "\"}}"); + } } diff --git a/src/test/java/com/browserstack/app/JavaParallelSample.java b/src/test/java/com/browserstack/app/JavaParallelSample.java index 5c13332..24e222b 100644 --- a/src/test/java/com/browserstack/app/JavaParallelSample.java +++ b/src/test/java/com/browserstack/app/JavaParallelSample.java @@ -1,9 +1,11 @@ package com.browserstack.app; -import java.util.*; + +import java.util.*; import java.net.MalformedURLException; import java.net.URL; import java.time.Duration; import java.util.Hashtable; + import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; @@ -14,84 +16,84 @@ import org.openqa.selenium.support.ui.WebDriverWait; class ParallelTest implements Runnable { - public static final String USERNAME = "BROWSERSTACK_USERNAME"; - public static final String AUTOMATE_KEY = "BROWSERSTACK_ACCESS_KEY"; - public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub"; - Hashtable capsHashtable; - String sessionName; + public static final String USERNAME = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : "BROWSERSTACK_USERNAME"; + public static final String AUTOMATE_KEY = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : "BROWSERSTACK_ACCESS_KEY"; + public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub"; + Hashtable capsHashtable; + String sessionName; - ParallelTest(Hashtable cap, String sessionString){ - capsHashtable = cap; - sessionName = sessionString; - } + ParallelTest(Hashtable cap, String sessionString) { + capsHashtable = cap; + sessionName = sessionString; + } - public void run() { - DesiredCapabilities caps = new DesiredCapabilities(); - caps.setCapability("bstack:options", capsHashtable); - caps.setCapability("sessionName", sessionName); // test name - caps.setCapability("buildName", "BStack-[Java] Sample buildName"); // CI/CD job or build name - WebDriver driver; - try { - driver = new RemoteWebDriver(new URL(URL), caps); - final JavascriptExecutor jse = (JavascriptExecutor) driver; - try { - // Searching for 'BrowserStack' on google.com - driver.get("https://bstackdemo.com/"); - WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); - wait.until(ExpectedConditions.titleIs("StackDemo")); - // Getting name of the product - String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText(); - //checking whether the Add to Cart button is clickable - WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\'1\']/div[4]"))); - // clicking the 'Add to cart' button - cart_btn.click(); - // checking if the Cart pane is visible - wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content"))); - // getting the product's name added in the cart - final String product_in_cart = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'__next\']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).getText(); - // checking if the product added to cart is available in the cart - if (product_name.equals(product_in_cart)) { - jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"Product has been added to the cart!\"}}"); - } - } catch (Exception e) { - jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"failed\", \"reason\": \"Some elements failed to load..\"}}"); - } - driver.quit(); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - } + public void run() { + DesiredCapabilities caps = new DesiredCapabilities(); + caps.setCapability("bstack:options", capsHashtable); + caps.setCapability("sessionName", sessionName); // test name + caps.setCapability("buildName", "BStack-[Java] Sample buildName"); // CI/CD job or build name + WebDriver driver; + try { + driver = new RemoteWebDriver(new URL(URL), caps); + final JavascriptExecutor jse = (JavascriptExecutor) driver; + try { + // Searching for 'BrowserStack' on google.com + driver.get("https://bstackdemo.com/"); + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); + wait.until(ExpectedConditions.titleIs("StackDemo")); + // Getting name of the product + String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText(); + //checking whether the Add to Cart button is clickable + WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\'1\']/div[4]"))); + // clicking the 'Add to cart' button + cart_btn.click(); + // checking if the Cart pane is visible + wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content"))); + // getting the product's name added in the cart + final String product_in_cart = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'__next\']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).getText(); + // checking if the product added to cart is available in the cart + if (product_name.equals(product_in_cart)) { + jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"Product has been added to the cart!\"}}"); + } + } catch (Exception e) { + jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"failed\", \"reason\": \"Some elements failed to load..\"}}"); + } + driver.quit(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } } public class JavaParallelSample { - public static void main(String[] args) throws Exception { - List> caps = new ArrayList>(); + public static void main(String[] args) throws Exception { + List> caps = new ArrayList>(); - //device 1 - Hashtable cap1 = new Hashtable(); - cap1.put("deviceName", "iPhone 12 Pro"); - cap1.put("realMobile", "true"); - caps.add(cap1); + //device 1 + Hashtable cap1 = new Hashtable(); + cap1.put("deviceName", "iPhone 12 Pro"); + cap1.put("realMobile", "true"); + caps.add(cap1); - //device 2 - Hashtable cap2 = new Hashtable(); - cap2.put("deviceName", "Samsung Galaxy S20"); - cap2.put("realMobile", "true"); - caps.add(cap2); + //device 2 + Hashtable cap2 = new Hashtable(); + cap2.put("deviceName", "Samsung Galaxy S20"); + cap2.put("realMobile", "true"); + caps.add(cap2); - //device 3 - Hashtable cap3 = new Hashtable(); - cap3.put("os", "OS X"); - caps.add(cap3); + //device 3 + Hashtable cap3 = new Hashtable(); + cap3.put("os", "OS X"); + caps.add(cap3); - //device 4 - Hashtable cap4 = new Hashtable(); - cap4.put("os", "windows"); - caps.add(cap4); + //device 4 + Hashtable cap4 = new Hashtable(); + cap4.put("os", "windows"); + caps.add(cap4); - for (Hashtable cap : caps) { - Thread thread = new Thread(new ParallelTest(cap, "session name")); - thread.start(); + for (Hashtable cap : caps) { + Thread thread = new Thread(new ParallelTest(cap, "session name")); + thread.start(); + } } - } } diff --git a/src/test/java/com/browserstack/app/JavaSample.java b/src/test/java/com/browserstack/app/JavaSample.java index a9d42fd..d4edaf9 100644 --- a/src/test/java/com/browserstack/app/JavaSample.java +++ b/src/test/java/com/browserstack/app/JavaSample.java @@ -1,61 +1,66 @@ package com.browserstack.app; //Sample test in Java to run Automate session. + import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.JavascriptExecutor; + import java.net.URL; import java.time.Duration; import java.util.HashMap; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; + public class JavaSample { - public static final String AUTOMATE_USERNAME = "BROWSERSTACK_USERNAME"; - public static final String AUTOMATE_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY"; -public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub"; -public static void main(String[] args) throws Exception { - DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setCapability("browserName", "Chrome"); - capabilities.setCapability("browserVersion", "latest"); - HashMap browserstackOptions = new HashMap(); - browserstackOptions.put("os", "OS X"); - browserstackOptions.put("osVersion", "Sierra"); - browserstackOptions.put("local", "false"); - browserstackOptions.put("seleniumVersion", "4.0.0"); - capabilities.setCapability("bstack:options", browserstackOptions); - capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name - capabilities.setCapability("buildName", "BStack Build Number 1"); // CI/CD job or build name - final WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities); - try { - driver.get("https://bstackdemo.com/"); - final WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); - wait.until(ExpectedConditions.titleIs("StackDemo")); - // getting name of the product - String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText(); - // waiting for the Add to cart button to be clickable - WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\'1\']/div[4]"))); - // clicking the 'Add to cart' button - cart_btn.click(); - // checking if the Cart pane is visible - wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content"))); - // getting the product's name added in the cart - final String product_in_cart = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'__next\']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).getText(); - // verifying whether the product added to cart is available in the cart - if (product_name.equals(product_in_cart)) { - markTestStatus("passed", "Product has been successfully added to the cart!", driver); - } - } catch (Exception e) { - markTestStatus("failed", "Some elements failed to load", driver); - } - driver.quit(); - } -// This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack -public static void markTestStatus(String status, String reason, WebDriver driver) { - final JavascriptExecutor jse = (JavascriptExecutor) driver; - jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}"); -} + public static final String AUTOMATE_USERNAME = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : "BROWSERSTACK_USERNAME"; + public static final String AUTOMATE_ACCESS_KEY = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : "BROWSERSTACK_ACCESS_KEY"; + public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub"; + + public static void main(String[] args) throws Exception { + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability("browserName", "Chrome"); + capabilities.setCapability("browserVersion", "latest"); + HashMap browserstackOptions = new HashMap(); + browserstackOptions.put("os", "OS X"); + browserstackOptions.put("osVersion", "Sierra"); + browserstackOptions.put("local", "false"); + browserstackOptions.put("seleniumVersion", "4.0.0"); + capabilities.setCapability("bstack:options", browserstackOptions); + capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name + capabilities.setCapability("buildName", "BStack Build Number 1"); // CI/CD job or build name + final WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities); + try { + driver.get("https://bstackdemo.com/"); + final WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); + wait.until(ExpectedConditions.titleIs("StackDemo")); + // getting name of the product + String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText(); + // waiting for the Add to cart button to be clickable + WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\'1\']/div[4]"))); + // clicking the 'Add to cart' button + cart_btn.click(); + // checking if the Cart pane is visible + wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content"))); + // getting the product's name added in the cart + final String product_in_cart = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'__next\']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).getText(); + // verifying whether the product added to cart is available in the cart + if (product_name.equals(product_in_cart)) { + markTestStatus("passed", "Product has been successfully added to the cart!", driver); + } + } catch (Exception e) { + markTestStatus("failed", "Some elements failed to load", driver); + } + driver.quit(); + } + + // This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack + public static void markTestStatus(String status, String reason, WebDriver driver) { + final JavascriptExecutor jse = (JavascriptExecutor) driver; + jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"" + status + "\", \"reason\": \"" + reason + "\"}}"); + } }