From 6fa6e5d1d2d76dd83317b1c455ec660129cdfcad Mon Sep 17 00:00:00 2001 From: Kamalpreet Kaur Date: Wed, 14 Dec 2022 22:53:24 +0530 Subject: [PATCH 1/4] add comment run workflow --- .github/workflows/comment-run.yml | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/comment-run.yml diff --git a/.github/workflows/comment-run.yml b/.github/workflows/comment-run.yml new file mode 100644 index 0000000..fa28912 --- /dev/null +++ b/.github/workflows/comment-run.yml @@ -0,0 +1,46 @@ +# 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 }} + strategy: + 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: Set up Chrome + uses: browser-actions/setup-chrome@latest + with: + chrome-version: beta + - run: chrome --version + - 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 From 6c7190f64f1b7202832643b26dc1bac3afcad3ad Mon Sep 17 00:00:00 2001 From: Kamalpreet Kaur Date: Fri, 16 Dec 2022 12:19:31 +0530 Subject: [PATCH 2/4] delete: remove github workflow --- .github/workflows/comment-run.yml | 46 ------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 .github/workflows/comment-run.yml diff --git a/.github/workflows/comment-run.yml b/.github/workflows/comment-run.yml deleted file mode 100644 index fa28912..0000000 --- a/.github/workflows/comment-run.yml +++ /dev/null @@ -1,46 +0,0 @@ -# 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 }} - strategy: - 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: Set up Chrome - uses: browser-actions/setup-chrome@latest - with: - chrome-version: beta - - run: chrome --version - - 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 From 466f54d33bcdb0250ee4518a3c03c1b2897cc587 Mon Sep 17 00:00:00 2001 From: Kamalpreet Kaur <38219887+kamal-kaur04@users.noreply.github.com> Date: Thu, 22 Dec 2022 21:32:17 +0530 Subject: [PATCH 3/4] Dump sdk to master (#15) * adding source to yml as we are using chromeDriver now * create workflow for maven testing on testNG sample repo * add browserstack creds * add branch name * fix get env var from secrets * fix run multiple commands in one go * set env vars when building with maven * remove build step and add env var at root level * add CODEOWNERS for yml files * add codeowners and change profile name * add local WebDriver profile and webdriver manager * change profile names and run with bstack as per env var * update: added comments to describe chromedriver setup * run tests on PR and comment creation (#12) * run tests on PR and comment creation * fix pr comment * add conditions for tests trigger * add conditions for tests trigger * fix: conditional expression * fix: conditional expression format * fix: corrected conditions placement * add: java version 11 and ubuntu latest in matrices * add: print PR comment * fix: trigger issue comment event on sdk branch * update: add event action for issue comment event * remove: commenting condition * add conditional run for job (#13) * add conditional run for job * create another job for comment run * trigger tests on PR comment * create separate jobs for test run * run jobs consecutively * set max parallels in strategy * continue other tests when one fails --- .github/CODEOWNERS | 1 + .github/pull_request_template.md | 5 + .github/workflows/comment-run.yml | 43 +++ .github/workflows/maven.yml | 43 +++ README.md | 10 +- build.gradle | 14 +- pom.xml | 251 ++++++++++-------- .../java/com/browserstack/SeleniumTest.java | 3 + 8 files changed, 253 insertions(+), 117 deletions(-) create mode 100644 .github/CODEOWNERS create mode 100644 .github/pull_request_template.md create mode 100644 .github/workflows/comment-run.yml create mode 100644 .github/workflows/maven.yml diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..8f0fab7 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +.github/workflows/*.yml @browserstack/asi-devs diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..bc80a5f --- /dev/null +++ b/.github/pull_request_template.md @@ -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 diff --git a/.github/workflows/comment-run.yml b/.github/workflows/comment-run.yml new file mode 100644 index 0000000..adc6417 --- /dev/null +++ b/.github/workflows/comment-run.yml @@ -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 diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..1870150 --- /dev/null +++ b/.github/workflows/maven.yml @@ -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 diff --git a/README.md b/README.md index 5a7e4fe..a421784 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/build.gradle b/build.gradle index ac3875c..e9f95f4 100644 --- a/build.gradle +++ b/build.gradle @@ -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 diff --git a/pom.xml b/pom.xml index fcef07c..83c40e8 100644 --- a/pom.xml +++ b/pom.xml @@ -1,121 +1,150 @@ - - 4.0.0 + + 4.0.0 - com.browserstack - testng-browserstack - 1.0-SNAPSHOT - jar + com.browserstack + testng-browserstack + 1.0-SNAPSHOT + jar - testng-browserstack - https://www.github.com/browserstack/testng-browserstack + testng-browserstack + https://www.github.com/browserstack/testng-browserstack - - UTF-8 - 1.8 - 1.8 - 7.4.0 - 2.19.1 - 4.1.4 - 1.1.1 - config/sample-test.testng.xml - + + UTF-8 + 1.8 + 1.8 + 7.4.0 + 2.19.1 + 4.1.4 + 1.1.1 + config/sample-test.testng.xml + - - - org.testng - testng - ${testng.version} - - - org.seleniumhq.selenium - selenium-java - ${selenium.version} - - - com.browserstack - browserstack-java-sdk - LATEST - compile - - + + + org.testng + testng + ${testng.version} + + + commons-io + commons-io + 2.11.0 + + + io.github.bonigarcia + webdrivermanager + 5.3.0 + + + org.seleniumhq.selenium + selenium-java + ${selenium.version} + + + com.browserstack + browserstack-java-sdk + LATEST + compile + + - + + + + maven-dependency-plugin + + + getClasspathFilenames + + properties + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + + ${config.file} + + + -javaagent:${com.browserstack:browserstack-java-sdk:jar} + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + + sample-local-test + - - maven-dependency-plugin - - - getClasspathFilenames - - properties - - - - - - org.apache.maven.plugins - maven-surefire-plugin - ${surefire.version} - - - ${config.file} - - - -javaagent:${com.browserstack:browserstack-java-sdk:jar} - - - - - org.apache.maven.plugins - maven-compiler-plugin - - ${maven.compiler.source} - ${maven.compiler.target} - - + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + + config/sample-local-test.testng.xml + + -javaagent:${com.browserstack:browserstack-java-sdk:jar} + + - + + - - - sample-local-test - - - - org.apache.maven.plugins - maven-surefire-plugin - - - config/sample-local-test.testng.xml - - - -javaagent:${com.browserstack:browserstack-java-sdk:jar} - - - - - - + + skip-browserstack-test + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + + config/sample-test.testng.xml + + -javaagent:${com.browserstack:browserstack-java-sdk:jar} + + false + + + + + + - - sample-test - - - - org.apache.maven.plugins - maven-surefire-plugin - - - config/sample-test.testng.xml - - - -javaagent:${com.browserstack:browserstack-java-sdk:jar} - - - - - - - + + sample-test + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.version} + + + config/sample-test.testng.xml + + -javaagent:${com.browserstack:browserstack-java-sdk:jar} + + + + + + diff --git a/src/test/java/com/browserstack/SeleniumTest.java b/src/test/java/com/browserstack/SeleniumTest.java index 0d8d717..b871e35 100644 --- a/src/test/java/com/browserstack/SeleniumTest.java +++ b/src/test/java/com/browserstack/SeleniumTest.java @@ -5,6 +5,7 @@ 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; @@ -12,6 +13,8 @@ public class SeleniumTest { @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); From df4f25155d3d4ba8bd179b9638da2287939d85a4 Mon Sep 17 00:00:00 2001 From: Kamalpreet Kaur Date: Mon, 23 Jan 2023 16:21:30 +0530 Subject: [PATCH 4/4] update: modify driver type to threadlocal --- src/test/java/com/browserstack/BStackDemoTest.java | 12 ++++++------ src/test/java/com/browserstack/LocalTest.java | 4 ++-- src/test/java/com/browserstack/SeleniumTest.java | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/browserstack/BStackDemoTest.java b/src/test/java/com/browserstack/BStackDemoTest.java index 8ce65cf..322599a 100644 --- a/src/test/java/com/browserstack/BStackDemoTest.java +++ b/src/test/java/com/browserstack/BStackDemoTest.java @@ -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); } } diff --git a/src/test/java/com/browserstack/LocalTest.java b/src/test/java/com/browserstack/LocalTest.java index bfd160c..3acf75b 100644 --- a/src/test/java/com/browserstack/LocalTest.java +++ b/src/test/java/com/browserstack/LocalTest.java @@ -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")); } } diff --git a/src/test/java/com/browserstack/SeleniumTest.java b/src/test/java/com/browserstack/SeleniumTest.java index b871e35..6ac6a28 100644 --- a/src/test/java/com/browserstack/SeleniumTest.java +++ b/src/test/java/com/browserstack/SeleniumTest.java @@ -8,7 +8,7 @@ import io.github.bonigarcia.wdm.WebDriverManager; public class SeleniumTest { - public WebDriver driver; + public static ThreadLocal driver = new ThreadLocal<>(); @BeforeMethod(alwaysRun = true) @SuppressWarnings("unchecked") @@ -17,11 +17,11 @@ public void setUp() throws Exception { 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(); } }