Skip to content

Commit c6c314d

Browse files
authored
Merge pull request #2 from Vishalan/inception-first-commit
sample demo first commit
2 parents c853148 + 4ba1264 commit c6c314d

File tree

8 files changed

+345
-2
lines changed

8 files changed

+345
-2
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.png
2+
target/
3+
local.log
4+
.idea
5+
*.iml
6+
.gradle
7+
build/
8+
.DS_Store
9+
gradle
10+
gradlew
11+
gradlew.bat
12+
logs
13+
browserstack.err

README.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,81 @@
1-
# ai-authoring-automate-demo-browserstack
2-
AI Authoring Automate Demo Repository for Browserstack
1+
# AI Authoring Agent-Automate Demo for Web (BrowserStack)
2+
3+
[TestNG](http://testng.org) Integration with BrowserStack.
4+
5+
![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)
6+
7+
## What Does This Repo Do?
8+
9+
This repository showcases the use of **BrowserStack's AI Authoring Agent** for web automation using TestNG. The key feature is the ability to write test steps in **Natural Language (Plain English)**, moving away from explicit Selenium locators and actions.
10+
11+
It contains a demo script:
12+
* **`BstackAIAuthoring.java`**: Runs complex test objectives (like selecting a product in a specific price range or navigating to a quote page) using simple English commands.
13+
14+
---
15+
16+
## How It Works
17+
18+
1. **Enablement:** The feature is enabled by declaring the capability `aiAuthoring: true` in your `browserstack.yml` configuration file.
19+
2. **The Command:** Inside the test script (`BstackAIAuthoring.java`), the test uses a standard Selenium `JavascriptExecutor` to pass the Natural Language instruction to the BrowserStack environment:
20+
```java
21+
// Example Objective: Find Google Pixel 3 phone on the website and add 5 of them to the cart
22+
jse.executeScript("browserstack_executor: {\"action\": \"ai\", \"arguments\": [\" Click on the Google button and Add 5 Pixel 3 phones to cart\"]}");
23+
```
24+
3. **Execution:** The BrowserStack AI Agent intercepts this command, interprets the natural language, and executes the equivalent low-level Selenium actions on the target web page.
25+
26+
---
27+
28+
## Running the Tests
29+
30+
### Using Maven
31+
32+
#### Run sample build
33+
34+
- Clone the repository
35+
- Replace `YOUR_USERNAME` and `YOUR_ACCESS_KEY` with your BrowserStack access credentials in `browserstack.yml`.
36+
- Declare capability **`aiAuthoring: true`** in `browserstack.yml` file (if not already present).
37+
- Install dependencies `mvn compile`
38+
- To run the test suite having cross-platform with parallelization, run **`mvn test -P sample-test`**.
39+
- To run local tests, run **`mvn test -P sample-local-test`**.
40+
41+
Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github).
42+
43+
#### Integrate your test suite
44+
45+
* Add maven dependency of `browserstack-java-sdk` in your `pom.xml` file:
46+
```xml
47+
<dependency>
48+
<groupId>com.browserstack</groupId>
49+
<artifactId>browserstack-java-sdk</artifactId>
50+
<version>LATEST</version>
51+
<scope>compile</scope>
52+
</dependency>
53+
```
54+
* Modify your build plugin to run tests by adding `argLine -javaagent:${com.browserstack:browserstack-java-sdk:jar}` in the Surefire plugin configuration.
55+
* Install dependencies `mvn compile`.
56+
57+
### Using Gradle
58+
59+
#### Prerequisites
60+
61+
- If using Gradle, Java v9+ is required.
62+
63+
#### Run sample build
64+
65+
- Clone the repository
66+
- Install dependencies `gradle build`
67+
- To run the test suite having cross-platform with parallelization, run **`gradle sampleTest`**.
68+
- To run local tests, run **`gradle sampleLocalTest`**.
69+
70+
#### Integrate your test suite
71+
72+
* Add `compileOnly 'com.browserstack:browserstack-java-sdk:latest.release'` in dependencies in your `gradle.build`.
73+
* Fetch Artifact Information and add `jvmArgs` property in tasks *SampleTest* and *SampleLocalTest*.
74+
* Install dependencies `gradle build`.
75+
76+
---
77+
78+
## Notes
79+
80+
* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate).
81+
* For detailed documentation on general Selenium and Java setup with BrowserStack Automate, please refer to the [official documentation](https://www.browserstack.com/docs/automate/selenium?fw-lang=java).

browserstack.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
userName: YOUR_USERNAME
2+
accessKey: YOUR_ACCESS_KEY
3+
framework: true
4+
platforms:
5+
- os: Windows
6+
osVersion: 10
7+
browserName: Chrome
8+
browserVersion: 120.0
9+
browserstackLocal: true
10+
buildName: browserstack-AI agent authoring for Automate(Latest)
11+
projectName: BrowserStack Sample
12+
aiAuthoring: true # setting this capability to true will help in running browserstack NL Authoring commands
13+
14+
15+
16+

build.gradle

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
repositories { mavenCentral() }
6+
7+
dependencies {
8+
implementation 'org.testng:testng:7.4.0'
9+
implementation 'commons-io:commons-io:1.3.2'
10+
implementation 'org.seleniumhq.selenium:selenium-java:4.1.4'
11+
implementation 'com.browserstack:browserstack-local-java:1.0.6'
12+
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
13+
compileOnly 'com.browserstack:browserstack-java-sdk:latest.release'
14+
}
15+
16+
group = 'com.browserstack'
17+
version = '1.0-SNAPSHOT'
18+
description = 'testng-browserstack'
19+
sourceCompatibility = '1.8'
20+
21+
def browserstackSDKArtifact = configurations.compileClasspath.resolvedConfiguration.resolvedArtifacts.find { it.name == 'browserstack-java-sdk' }
22+
23+
tasks.withType(JavaCompile) {
24+
options.encoding = 'UTF-8'
25+
}
26+
27+
tasks.withType(Test) {
28+
systemProperties = System.properties
29+
}
30+
31+
task sampleTest(type: Test) {
32+
useTestNG() {
33+
dependsOn cleanTest
34+
useDefaultListeners = true
35+
suites "config/sample-test.testng.xml"
36+
jvmArgs "-javaagent:${browserstackSDKArtifact.file}"
37+
}
38+
}
39+
40+
task sampleLocalTest(type: Test) {
41+
useTestNG() {
42+
dependsOn cleanTest
43+
useDefaultListeners = true
44+
suites "config/sample-local-test.testng.xml"
45+
jvmArgs "-javaagent:${browserstackSDKArtifact.file}"
46+
}
47+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Local">
4+
<test name="LocalTest">
5+
<classes>
6+
<class name="com.browserstack.BstackAIAuthoring"/>
7+
</classes>
8+
</test>
9+
</suite>
10+

config/sample-test.testng.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite name="Cross-Platform">
4+
<test name="BStackDemo">
5+
<classes>
6+
<class name="com.browserstack.BstackAIAuthoring" />
7+
</classes>
8+
</test>
9+
</suite>

pom.xml

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.browserstack</groupId>
6+
<artifactId>testng-browserstack</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>testng-browserstack</name>
11+
<url>https://www.github.com/browserstack/testng-browserstack</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
<testng.version>7.4.0</testng.version>
18+
<surefire.version>2.19.1</surefire.version>
19+
<selenium.version>4.1.4</selenium.version>
20+
<json-simple.version>1.1.1</json-simple.version>
21+
<config.file>config/sample-test.testng.xml</config.file>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.testng</groupId>
27+
<artifactId>testng</artifactId>
28+
<version>${testng.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.seleniumhq.selenium</groupId>
32+
<artifactId>selenium-java</artifactId>
33+
<version>${selenium.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.browserstack</groupId>
37+
<artifactId>browserstack-java-sdk</artifactId>
38+
<version>LATEST</version>
39+
<scope>compile</scope>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<artifactId>maven-dependency-plugin</artifactId>
47+
<executions>
48+
<execution>
49+
<id>getClasspathFilenames</id>
50+
<goals>
51+
<goal>properties</goal>
52+
</goals>
53+
</execution>
54+
</executions>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-surefire-plugin</artifactId>
59+
<version>${surefire.version}</version>
60+
<configuration>
61+
<suiteXmlFiles>
62+
<suiteXmlFile>${config.file}</suiteXmlFile>
63+
</suiteXmlFiles>
64+
<argLine>
65+
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
66+
</argLine>
67+
</configuration>
68+
</plugin>
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-compiler-plugin</artifactId>
72+
<configuration>
73+
<source>${maven.compiler.source}</source>
74+
<target>${maven.compiler.target}</target>
75+
</configuration>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
80+
<profiles>
81+
<profile>
82+
<id>sample-local-test</id>
83+
<build>
84+
<plugins>
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<artifactId>maven-surefire-plugin</artifactId>
88+
<configuration>
89+
<suiteXmlFiles>
90+
<suiteXmlFile>config/sample-local-test.testng.xml</suiteXmlFile>
91+
</suiteXmlFiles>
92+
<argLine>
93+
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
94+
</argLine>
95+
</configuration>
96+
</plugin>
97+
</plugins>
98+
</build>
99+
</profile>
100+
101+
<profile>
102+
<id>sample-test</id>
103+
<build>
104+
<plugins>
105+
<plugin>
106+
<groupId>org.apache.maven.plugins</groupId>
107+
<artifactId>maven-surefire-plugin</artifactId>
108+
<configuration>
109+
<suiteXmlFiles>
110+
<suiteXmlFile>config/sample-test.testng.xml</suiteXmlFile>
111+
</suiteXmlFiles>
112+
<argLine>
113+
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
114+
</argLine>
115+
</configuration>
116+
</plugin>
117+
</plugins>
118+
</build>
119+
</profile>
120+
</profiles>
121+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.browserstack;
2+
3+
import org.openqa.selenium.JavascriptExecutor;
4+
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.chrome.ChromeDriver;
6+
import org.testng.Assert;
7+
import org.testng.annotations.AfterClass;
8+
import org.testng.annotations.BeforeClass;
9+
import org.testng.annotations.Test;
10+
11+
public class BstackAIAuthoring {
12+
13+
WebDriver driver;
14+
15+
@BeforeClass
16+
public void setUp() {
17+
// Make sure you have chromedriver in your PATH or set the path here
18+
// System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
19+
driver = new ChromeDriver();
20+
}
21+
22+
@Test
23+
public void openGoogle() {
24+
25+
JavascriptExecutor jse = (JavascriptExecutor)driver;
26+
27+
//Natural language authoring in Automate using Browserstack AI Agent
28+
// NL AI Objective 1: Navigate to tupperware and add any product to cart in the price range of 20 to 50$
29+
driver.get("https://www.tupperware.com/collections/fresh-picks");
30+
jse.executeScript("browserstack_executor: {\"action\": \"ai\", \"arguments\": [\"Select any product between the price range of 20 and 50$ and add to cart \"]}");
31+
32+
// NL AI Objective 2: Get to the quote page from the homepage
33+
driver.get("https://www.travelinsured.com/");
34+
jse.executeScript("browserstack_executor: {\"action\": \"ai\", \"arguments\": [\"Click on the get quote button\"]}");
35+
36+
// NL AI Objective 3: Find Google Pixel 3 phone on the website and add 5 of them to the cart
37+
driver.get("https://www.bstackdemo.com/");
38+
jse.executeScript("browserstack_executor: {\"action\": \"ai\", \"arguments\": [\" Click on the Google button and Add 5 Pixel 3 phones to cart\"]}");
39+
40+
}
41+
42+
@AfterClass
43+
public void tearDown() {
44+
if (driver != null) {
45+
driver.quit();
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)