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