Skip to content

Commit f367590

Browse files
Merge pull request #8 from browserstack/update_env_vars
Update Username Access Key from env vars
2 parents 65ab463 + 755af30 commit f367590

4 files changed

Lines changed: 272 additions & 261 deletions

File tree

pom.xml

Lines changed: 104 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,113 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

33
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5-
<modelVersion>4.0.0</modelVersion>
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
66

7-
<groupId>com.browserstack.app</groupId>
8-
<artifactId>java-browserstack</artifactId>
9-
<version>1.0-SNAPSHOT</version>
7+
<groupId>com.browserstack.app</groupId>
8+
<artifactId>java-browserstack</artifactId>
9+
<version>1.0-SNAPSHOT</version>
1010

11-
<name>java-browserstack</name>
12-
<!-- FIXME change it to the project's website -->
13-
<url>http://www.browserstack.com</url>
11+
<name>java-browserstack</name>
12+
<!-- FIXME change it to the project's website -->
13+
<url>http://www.browserstack.com</url>
1414

15-
<properties>
16-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17-
<maven.compiler.source>1.7</maven.compiler.source>
18-
<maven.compiler.target>1.7</maven.compiler.target>
19-
</properties>
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
</properties>
2020

21-
<dependencies>
22-
<dependency>
23-
<groupId>junit</groupId>
24-
<artifactId>junit</artifactId>
25-
<version>4.11</version>
26-
<scope>test</scope>
27-
</dependency>
28-
<dependency>
29-
<groupId>commons-io</groupId>
30-
<artifactId>commons-io</artifactId>
31-
<version>1.3.2</version>
32-
</dependency>
33-
<dependency>
34-
<groupId>org.seleniumhq.selenium</groupId>
35-
<artifactId>selenium-java</artifactId>
36-
<version>4.1.0</version>
37-
</dependency>
38-
<dependency>
39-
<groupId>com.browserstack</groupId>
40-
<artifactId>browserstack-local-java</artifactId>
41-
<version>1.0.3</version>
42-
</dependency>
43-
<dependency>
44-
<groupId>com.googlecode.json-simple</groupId>
45-
<artifactId>json-simple</artifactId>
46-
<version>1.1.1</version>
47-
</dependency>
48-
</dependencies>
21+
<dependencies>
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
<version>4.11</version>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>commons-io</groupId>
30+
<artifactId>commons-io</artifactId>
31+
<version>1.3.2</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.seleniumhq.selenium</groupId>
35+
<artifactId>selenium-java</artifactId>
36+
<version>4.1.0</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.browserstack</groupId>
40+
<artifactId>browserstack-local-java</artifactId>
41+
<version>1.0.3</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>com.googlecode.json-simple</groupId>
45+
<artifactId>json-simple</artifactId>
46+
<version>1.1.1</version>
47+
</dependency>
48+
</dependencies>
4949

50-
<build>
51-
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
52-
<plugins>
53-
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
54-
<plugin>
55-
<artifactId>maven-clean-plugin</artifactId>
56-
<version>3.1.0</version>
57-
</plugin>
58-
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
59-
<plugin>
60-
<artifactId>maven-resources-plugin</artifactId>
61-
<version>3.0.2</version>
62-
</plugin>
63-
<plugin>
64-
<artifactId>maven-compiler-plugin</artifactId>
65-
<version>3.8.0</version>
66-
</plugin>
67-
<plugin>
68-
<artifactId>maven-surefire-plugin</artifactId>
69-
<version>2.22.1</version>
70-
</plugin>
71-
<plugin>
72-
<artifactId>maven-jar-plugin</artifactId>
73-
<version>3.0.2</version>
74-
</plugin>
75-
<plugin>
76-
<artifactId>maven-install-plugin</artifactId>
77-
<version>2.5.2</version>
78-
</plugin>
79-
<plugin>
80-
<artifactId>maven-deploy-plugin</artifactId>
81-
<version>2.8.2</version>
82-
</plugin>
83-
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
84-
<plugin>
85-
<artifactId>maven-site-plugin</artifactId>
86-
<version>3.7.1</version>
87-
</plugin>
88-
<plugin>
89-
<artifactId>maven-project-info-reports-plugin</artifactId>
90-
<version>3.0.0</version>
91-
</plugin>
92-
</plugins>
93-
</pluginManagement>
94-
</build>
95-
<profiles>
96-
<profile>
97-
<id>single</id>
98-
<build>
99-
<plugins>
100-
<plugin>
101-
<groupId>org.apache.maven.plugins</groupId>
102-
<artifactId>maven-surefire-plugin</artifactId>
103-
<configuration>
104-
<includes>
105-
<include>com/browserstack/app/JavaSample.java</include>
106-
</includes>
107-
</configuration>
108-
</plugin>
109-
</plugins>
110-
</build>
111-
</profile>
112-
</profiles>
50+
<build>
51+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
52+
<plugins>
53+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
54+
<plugin>
55+
<artifactId>maven-clean-plugin</artifactId>
56+
<version>3.1.0</version>
57+
</plugin>
58+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
59+
<plugin>
60+
<artifactId>maven-resources-plugin</artifactId>
61+
<version>3.0.2</version>
62+
</plugin>
63+
<plugin>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<version>3.8.0</version>
66+
</plugin>
67+
<plugin>
68+
<artifactId>maven-surefire-plugin</artifactId>
69+
<version>2.22.1</version>
70+
</plugin>
71+
<plugin>
72+
<artifactId>maven-jar-plugin</artifactId>
73+
<version>3.0.2</version>
74+
</plugin>
75+
<plugin>
76+
<artifactId>maven-install-plugin</artifactId>
77+
<version>2.5.2</version>
78+
</plugin>
79+
<plugin>
80+
<artifactId>maven-deploy-plugin</artifactId>
81+
<version>2.8.2</version>
82+
</plugin>
83+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
84+
<plugin>
85+
<artifactId>maven-site-plugin</artifactId>
86+
<version>3.7.1</version>
87+
</plugin>
88+
<plugin>
89+
<artifactId>maven-project-info-reports-plugin</artifactId>
90+
<version>3.0.0</version>
91+
</plugin>
92+
</plugins>
93+
</pluginManagement>
94+
</build>
95+
<profiles>
96+
<profile>
97+
<id>single</id>
98+
<build>
99+
<plugins>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-surefire-plugin</artifactId>
103+
<configuration>
104+
<includes>
105+
<include>com/browserstack/app/JavaSample.java</include>
106+
</includes>
107+
</configuration>
108+
</plugin>
109+
</plugins>
110+
</build>
111+
</profile>
112+
</profiles>
113113
</project>
Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.browserstack.app;
22

33
//Sample test in Java to run Automate session.
4+
45
import org.openqa.selenium.By;
56
import org.openqa.selenium.WebDriver;
67
import org.openqa.selenium.remote.DesiredCapabilities;
78
import org.openqa.selenium.remote.RemoteWebDriver;
89
import org.openqa.selenium.JavascriptExecutor;
10+
911
import java.net.URL;
1012
import java.time.Duration;
1113
import java.util.HashMap;
@@ -15,55 +17,57 @@
1517
import com.browserstack.local.Local;
1618

1719
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+
3437
//Creates an instance of Local
35-
Local bsLocal = new Local();
38+
Local bsLocal = new Local();
3639

3740
// 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);
4043

4144
// Starts the Local instance with the required arguments
42-
bsLocal.start(bsLocalArgs);
45+
bsLocal.start(bsLocalArgs);
4346

4447
// Check if BrowserStack local instance is running
45-
System.out.println(bsLocal.isRunning());
48+
System.out.println(bsLocal.isRunning());
4649

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+
}
6973
}

0 commit comments

Comments
 (0)