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/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 0d8d717..6ac6a28 100644 --- a/src/test/java/com/browserstack/SeleniumTest.java +++ b/src/test/java/com/browserstack/SeleniumTest.java @@ -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 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(); } }