|
1 | 1 | package com.browserstack.app; |
2 | 2 |
|
3 | 3 | //Sample test in Java to run Automate session. |
| 4 | + |
4 | 5 | import org.openqa.selenium.By; |
5 | 6 | import org.openqa.selenium.WebDriver; |
6 | 7 | import org.openqa.selenium.remote.DesiredCapabilities; |
7 | 8 | import org.openqa.selenium.remote.RemoteWebDriver; |
8 | 9 | import org.openqa.selenium.JavascriptExecutor; |
| 10 | + |
9 | 11 | import java.net.URL; |
10 | 12 | import java.time.Duration; |
11 | 13 | import java.util.HashMap; |
|
15 | 17 | import com.browserstack.local.Local; |
16 | 18 |
|
17 | 19 | public class JavaLocalSample { |
18 | | - public static final String AUTOMATE_USERNAME = "BROWSERSTACK_USERNAME"; |
19 | | - public static final String AUTOMATE_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY"; |
20 | | -public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub"; |
21 | | -public static void main(String[] args) throws Exception { |
22 | | - DesiredCapabilities capabilities = new DesiredCapabilities(); |
23 | | - capabilities.setCapability("browserName", "Chrome"); |
24 | | - capabilities.setCapability("browserVersion", "latest"); |
25 | | - HashMap<String, Object> browserstackOptions = new HashMap<String, Object>(); |
26 | | - browserstackOptions.put("os", "OS X"); |
27 | | - browserstackOptions.put("osVersion", "Sierra"); |
28 | | - browserstackOptions.put("local", "true"); |
29 | | - browserstackOptions.put("seleniumVersion", "4.0.0"); |
30 | | - capabilities.setCapability("bstack:options", browserstackOptions); |
31 | | - capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name |
32 | | - capabilities.setCapability("buildName", "BStack Local Build Number 1"); // CI/CD job or build name |
33 | | - |
| 20 | + public static final String AUTOMATE_USERNAME = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : "BROWSERSTACK_USERNAME"; |
| 21 | + public static final String AUTOMATE_ACCESS_KEY = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : "BROWSERSTACK_ACCESS_KEY"; |
| 22 | + public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub"; |
| 23 | + |
| 24 | + public static void main(String[] args) throws Exception { |
| 25 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 26 | + capabilities.setCapability("browserName", "Chrome"); |
| 27 | + capabilities.setCapability("browserVersion", "latest"); |
| 28 | + HashMap<String, Object> browserstackOptions = new HashMap<String, Object>(); |
| 29 | + browserstackOptions.put("os", "OS X"); |
| 30 | + browserstackOptions.put("osVersion", "Sierra"); |
| 31 | + browserstackOptions.put("local", "true"); |
| 32 | + browserstackOptions.put("seleniumVersion", "4.0.0"); |
| 33 | + capabilities.setCapability("bstack:options", browserstackOptions); |
| 34 | + capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name |
| 35 | + capabilities.setCapability("buildName", "BStack Local Build Number 1"); // CI/CD job or build name |
| 36 | + |
34 | 37 | //Creates an instance of Local |
35 | | -Local bsLocal = new Local(); |
| 38 | + Local bsLocal = new Local(); |
36 | 39 |
|
37 | 40 | // You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY". |
38 | | -HashMap<String, String> bsLocalArgs = new HashMap<String, String>(); |
39 | | -bsLocalArgs.put("key", AUTOMATE_ACCESS_KEY); |
| 41 | + HashMap<String, String> bsLocalArgs = new HashMap<String, String>(); |
| 42 | + bsLocalArgs.put("key", AUTOMATE_ACCESS_KEY); |
40 | 43 |
|
41 | 44 | // Starts the Local instance with the required arguments |
42 | | -bsLocal.start(bsLocalArgs); |
| 45 | + bsLocal.start(bsLocalArgs); |
43 | 46 |
|
44 | 47 | // Check if BrowserStack local instance is running |
45 | | -System.out.println(bsLocal.isRunning()); |
| 48 | + System.out.println(bsLocal.isRunning()); |
46 | 49 |
|
47 | | - final WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities); |
48 | | - try { |
49 | | - driver.get("http://bs-local.com:45691/check"); |
50 | | - final WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); |
51 | | - // getting name of the product |
52 | | - String bodyText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body"))).getText(); |
53 | | - |
54 | | - if (bodyText.equals("Up and running")) { |
55 | | - markTestStatus("passed", "Local Test is successful and up and running", driver); |
56 | | - } |
57 | | - } catch (Exception e) { |
58 | | - markTestStatus("failed", "Could'nt connect the local", driver); |
59 | | - } |
60 | | - //Stop the Local instance |
61 | | - bsLocal.stop(); |
62 | | - driver.quit(); |
63 | | - } |
64 | | -// This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack |
65 | | -public static void markTestStatus(String status, String reason, WebDriver driver) { |
66 | | - final JavascriptExecutor jse = (JavascriptExecutor) driver; |
67 | | - jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}"); |
68 | | -} |
| 50 | + final WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities); |
| 51 | + try { |
| 52 | + driver.get("http://bs-local.com:45691/check"); |
| 53 | + final WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); |
| 54 | + // getting name of the product |
| 55 | + String bodyText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body"))).getText(); |
| 56 | + |
| 57 | + if (bodyText.equals("Up and running")) { |
| 58 | + markTestStatus("passed", "Local Test is successful and up and running", driver); |
| 59 | + } |
| 60 | + } catch (Exception e) { |
| 61 | + markTestStatus("failed", "Could'nt connect the local", driver); |
| 62 | + } |
| 63 | + //Stop the Local instance |
| 64 | + bsLocal.stop(); |
| 65 | + driver.quit(); |
| 66 | + } |
| 67 | + |
| 68 | + // This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack |
| 69 | + public static void markTestStatus(String status, String reason, WebDriver driver) { |
| 70 | + final JavascriptExecutor jse = (JavascriptExecutor) driver; |
| 71 | + jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"" + status + "\", \"reason\": \"" + reason + "\"}}"); |
| 72 | + } |
69 | 73 | } |
0 commit comments