Skip to content

Add threadlocal driver #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.github/workflows/*.yml @browserstack/asi-devs
5 changes: 5 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#### How to run tests?
- Before running tests please take master merge in the PR
- Comment RUN_TESTS to run maven tests

Note: Tests will also be triggered when PR is opened or reopened
43 changes: 43 additions & 0 deletions .github/workflows/comment-run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This job is to test different maven profiles in sdk branch again Pull Request raised
# This workflow targets Java with Maven execution

name: TestNG SDK Test workflow for Maven on comment RUN_TESTS

on:
issue_comment:
types: [ created, edited ]

jobs:
comment-run:
if: contains(github.event.comment.body, 'RUN_TESTS')
runs-on: ${{ matrix.os }}
continue-on-error: true
strategy:
max-parallel: 3
matrix:
java: [ '8', '11', '17' ]
os: [ 'macos-latest', 'windows-latest', 'ubuntu-latest' ]
name: TestNG Repo ${{ matrix.Java }} - ${{ matrix.os }} Sample
env:
BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}

steps:
- uses: actions/checkout@v3
- name: Set up Java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: ${{ matrix.java }}
- name: Run mvn test
run: |
mvn compile
mvn test
- name: Run mvn profile sample-local-test
run: |
mvn compile
mvn test -P sample-local-test
- name: Run mvn profile sample-test
run: |
mvn compile
mvn test -P sample-test
43 changes: 43 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This job is to test different maven profiles in sdk branch again Pull Request raised
# This workflow targets Java with Maven execution

name: TestNG SDK Test workflow for Maven

on:
pull_request:
branches: [ "master", "sdk" ]
types: [ opened, reopened ]

jobs:
maven-run:
runs-on: ${{ matrix.os }}
continue-on-error: true
strategy:
max-parallel: 3
matrix:
java: [ '8', '11', '17' ]
os: [ 'macos-latest', 'windows-latest', 'ubuntu-latest' ]
name: TestNG Repo ${{ matrix.Java }} - ${{ matrix.os }} Sample
env:
BROWSERSTACK_USERNAME: ${{ secrets.BROWSERSTACK_USERNAME }}
BROWSERSTACK_ACCESS_KEY: ${{ secrets.BROWSERSTACK_ACCESS_KEY }}

steps:
- uses: actions/checkout@v3
- name: Set up Java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: ${{ matrix.java }}
- name: Run mvn test
run: |
mvn compile
mvn test
- name: Run mvn profile sample-local-test
run: |
mvn compile
mvn test -P sample-local-test
- name: Run mvn profile sample-test
run: |
mvn compile
mvn test -P sample-test
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -11,8 +11,9 @@
- Clone the repository
- Replace YOUR_USERNAME and YOUR_ACCESS_KEY with your BrowserStack access credentials in browserstack.yml.
- Install dependencies `mvn compile`
- To run the test suite having cross-platform with parallelization, run `mvn test -P sample-test`
- To run local tests, run `mvn test -P sample-local-test`
- To run the test suite with local webdriver, run `mvn test -P skip-browserstack-test`
- To run the test suite having cross-platform with parallelization on browserstack, run `mvn test -P sample-test`
- To run local (privately hosted websites) tests on browserstack, run `mvn test -P sample-local-test`

Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)

@@ -68,8 +69,9 @@ This repository uses the BrowserStack SDK to run tests on BrowserStack. Follow t

- Clone the repository
- Install dependencies `gradle build`
- To run the test suite having cross-platform with parallelization, run `gradle sampleTest`
- To run local tests, run `gradle sampleLocalTest`
- To run the test suite with local webdriver, run `gradle skipBrowserstackTest`
- To run the test suite having cross-platform with parallelization on browserstack, run `gradle sampleTest`
- To run local (privately hosted websites) tests on browserstack, run `gradle sampleLocalTest`

Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)

14 changes: 12 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@ repositories { mavenCentral() }

dependencies {
implementation 'org.testng:testng:7.4.0'
implementation 'commons-io:commons-io:1.3.2'
implementation 'commons-io:commons-io:2.11.0'
implementation 'org.seleniumhq.selenium:selenium-java:4.1.4'
implementation 'com.browserstack:browserstack-local-java:1.0.6'
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
implementation 'io.github.bonigarcia:webdrivermanager:5.3.0'
compileOnly 'com.browserstack:browserstack-java-sdk:latest.release'
}

@@ -24,6 +24,16 @@ tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

task skipBrowserstackTest(type: Test) {
useTestNG() {
environment = [ 'BROWSERSTACK_AUTOMATION': 'false' ]
dependsOn cleanTest
useDefaultListeners = true
suites "config/sample-test.testng.xml"
jvmArgs "-javaagent:${browserstackSDKArtifact.file}"
}
}

task sampleTest(type: Test) {
useTestNG() {
dependsOn cleanTest
251 changes: 140 additions & 111 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,121 +1,150 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
<modelVersion>4.0.0</modelVersion>
<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.browserstack</groupId>
<artifactId>testng-browserstack</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<groupId>com.browserstack</groupId>
<artifactId>testng-browserstack</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>testng-browserstack</name>
<url>https://www.github.com/browserstack/testng-browserstack</url>
<name>testng-browserstack</name>
<url>https://www.github.com/browserstack/testng-browserstack</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<testng.version>7.4.0</testng.version>
<surefire.version>2.19.1</surefire.version>
<selenium.version>4.1.4</selenium.version>
<json-simple.version>1.1.1</json-simple.version>
<config.file>config/sample-test.testng.xml</config.file>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<testng.version>7.4.0</testng.version>
<surefire.version>2.19.1</surefire.version>
<selenium.version>4.1.4</selenium.version>
<json-simple.version>1.1.1</json-simple.version>
<config.file>config/sample-test.testng.xml</config.file>
</properties>

<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>com.browserstack</groupId>
<artifactId>browserstack-java-sdk</artifactId>
<version>LATEST</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>com.browserstack</groupId>
<artifactId>browserstack-java-sdk</artifactId>
<version>LATEST</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${config.file}</suiteXmlFile>
</suiteXmlFiles>
<argLine>
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>sample-local-test</id>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${config.file}</suiteXmlFile>
</suiteXmlFiles>
<argLine>
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>config/sample-local-test.testng.xml</suiteXmlFile>
</suiteXmlFiles>
<argLine>-javaagent:${com.browserstack:browserstack-java-sdk:jar}</argLine>
</configuration>
</plugin>
</plugins>
</build>
</build>
</profile>

<profiles>
<profile>
<id>sample-local-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>config/sample-local-test.testng.xml</suiteXmlFile>
</suiteXmlFiles>
<argLine>
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>skip-browserstack-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration combine.self="override">
<suiteXmlFiles>
<suiteXmlFile>config/sample-test.testng.xml</suiteXmlFile>
</suiteXmlFiles>
<argLine>-javaagent:${com.browserstack:browserstack-java-sdk:jar}</argLine>
<environmentVariables>
<BROWSERSTACK_AUTOMATION>false</BROWSERSTACK_AUTOMATION>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>sample-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>config/sample-test.testng.xml</suiteXmlFile>
</suiteXmlFiles>
<argLine>
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<profile>
<id>sample-test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>config/sample-test.testng.xml</suiteXmlFile>
</suiteXmlFiles>
<argLine>-javaagent:${com.browserstack:browserstack-java-sdk:jar}</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
12 changes: 6 additions & 6 deletions src/test/java/com/browserstack/BStackDemoTest.java
Original file line number Diff line number Diff line change
@@ -9,21 +9,21 @@ public class BStackDemoTest extends SeleniumTest {
@Test
public void addProductToCart() throws Exception {
// navigate to bstackdemo
driver.get("https://www.bstackdemo.com");
driver.get().get("https://www.bstackdemo.com");

// Check the title
Assert.assertTrue(driver.getTitle().matches("StackDemo"));
Assert.assertTrue(driver.get().getTitle().matches("StackDemo"));

// Save the text of the product for later verify
String productOnScreenText = driver.findElement(By.xpath("//*[@id=\"1\"]/p")).getText();
String productOnScreenText = driver.get().findElement(By.xpath("//*[@id=\"1\"]/p")).getText();
// Click on add to cart button
driver.findElement(By.xpath("//*[@id=\"1\"]/div[4]")).click();
driver.get().findElement(By.xpath("//*[@id=\"1\"]/div[4]")).click();

// See if the cart is opened or not
Assert.assertTrue(driver.findElement(By.cssSelector(".float\\-cart__content")).isDisplayed());
Assert.assertTrue(driver.get().findElement(By.cssSelector(".float\\-cart__content")).isDisplayed());

// Check the product inside the cart is same as of the main page
String productOnCartText = driver.findElement(By.xpath("//*[@id=\"__next\"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]")).getText();
String productOnCartText = driver.get().findElement(By.xpath("//*[@id=\"__next\"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]")).getText();
Assert.assertEquals(productOnScreenText, productOnCartText);
}
}
4 changes: 2 additions & 2 deletions src/test/java/com/browserstack/LocalTest.java
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@ public class LocalTest extends SeleniumTest {

@Test
public void test() throws Exception {
driver.get("http://bs-local.com:45454/");
driver.get().get("http://bs-local.com:45454/");

Assert.assertTrue(driver.getTitle().contains("BrowserStack Local"));
Assert.assertTrue(driver.get().getTitle().contains("BrowserStack Local"));
}
}
9 changes: 6 additions & 3 deletions src/test/java/com/browserstack/SeleniumTest.java
Original file line number Diff line number Diff line change
@@ -5,20 +5,23 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import io.github.bonigarcia.wdm.WebDriverManager;

public class SeleniumTest {
public WebDriver driver;
public static ThreadLocal<WebDriver> driver = new ThreadLocal<>();

@BeforeMethod(alwaysRun = true)
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
/* WebDriverManager will set up chromedriver to run tests on locally existing chrome */
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
driver = new ChromeDriver(options);
driver.set(new ChromeDriver(options));
}

@AfterMethod(alwaysRun = true)
public void tearDown() throws Exception {
driver.quit();
driver.get().quit();
}
}