From cf16c596e190b2eb970f0be9e711aa050e097731 Mon Sep 17 00:00:00 2001 From: Rahul Singh Date: Mon, 22 Dec 2025 21:05:03 +0530 Subject: [PATCH 1/2] add diverse tests for TRA --- browserstack.yml | 2 + jest.config.js | 5 +- package.json | 4 +- src/moduleA/test.js | 98 +++++++++++++++++++++++++++++++++++++ src/moduleB/test.js | 114 ++++++++++++++++++++++++++++++++++++++++++++ src/moduleC/test.js | 98 +++++++++++++++++++++++++++++++++++++ 6 files changed, 316 insertions(+), 5 deletions(-) create mode 100644 src/moduleA/test.js create mode 100644 src/moduleB/test.js create mode 100644 src/moduleC/test.js diff --git a/browserstack.yml b/browserstack.yml index 4ed57c2..ec6498d 100644 --- a/browserstack.yml +++ b/browserstack.yml @@ -23,6 +23,8 @@ buildIdentifier: '#${BUILD_NUMBER}' # Supports strings along with either/both ${ # Set `source` in the syntax `:sample-: source: jest-js:sample-main:v1.0 +CUSTOM_TAG_1: bstack_sample + # ======================================= # Platforms (Browsers / Devices to test) # ======================================= diff --git a/jest.config.js b/jest.config.js index 476b808..d8f5992 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,9 +1,8 @@ module.exports = { coverageProvider: "v8", - maxConcurrency: 5, maxWorkers: 5, roots: ["src"], - testMatch: ["**/*.test.js"], + testMatch: ["**/test.js"], testPathIgnorePatterns: ["/node_modules/"], - testTimeout: 60 * 1000, + testTimeout: 30 * 1000, }; diff --git a/package.json b/package.json index 4624b76..42f0c0a 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,8 @@ "selenium-webdriver": "4.1.2" }, "scripts": { - "sample-test": "browserstack-node-sdk jest src/sample.test.js", + "sample-test": "browserstack-node-sdk jest 'src/module[A-Z]/test.js'", "sample-local-test": "browserstack-node-sdk jest src/sample-local.test.js", "postinstall": "npm update browserstack-node-sdk" } -} +} \ No newline at end of file diff --git a/src/moduleA/test.js b/src/moduleA/test.js new file mode 100644 index 0000000..a182711 --- /dev/null +++ b/src/moduleA/test.js @@ -0,0 +1,98 @@ +const { Builder, By, Key, until, Capabilities } = require("selenium-webdriver"); +const assert = require("assert"); + +describe("BStack demo test Module A", () => { + let driver; + + beforeAll(async () => { + driver = new Builder() + .usingServer(`http://localhost:4444/wd/hub`) + .withCapabilities(Capabilities.chrome()) + .build(); + + await driver.get("https://bstackdemo.com"); + await driver.wait(until.titleMatches(/StackDemo/i), 10000); + }); + + afterAll(async () => { + await driver.quit(); + }) + + test( + "flaky test - add products to cart", + async () => { + let elementLocator = Math.random() < 0.5 ? '//*[@id="1"]/p' : '//*[@id="random"]/p'; + + // locating product on webpage and getting name of the product + await driver.wait(until.elementLocated(By.xpath(elementLocator))); + let productText = await driver + .findElement(By.xpath('//*[@id="1"]/p')) + .getText(); + // clicking the 'Add to cart' button + await driver.findElement(By.xpath('//*[@id="1"]/div[4]')).click(); + // waiting until the Cart pane has been displayed on the webpage + await driver.wait(until.elementLocated(By.className("float-cart__content"))); + await driver.findElement(By.className("float-cart__content")); + // locating product in cart and getting name of the product in cart + let productCartText = await driver + .findElement( + By.xpath( + '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]' + ) + ) + .getText(); + // checking whether product has been added to cart by comparing product name + expect(productText).toBe(productCartText); + }, + 10000 + ); + + test("always failing test - missing element 1", async () => { + await driver.wait(until.elementLocated(By.xpath('//*[@id="non-existent-1"]/p'))); + }, 2000); + + test("always failing test - same stacktrace 1", async () => { + await driver.wait(until.elementLocated(By.xpath('//*[@id="common-error"]/p'))); + }, 2000); + + test("always failing test - same stacktrace 2", async () => { + await driver.wait(until.elementLocated(By.xpath('//*[@id="common-error"]/p'))); + }, 2000); + + test("always passing test - example F", async () => { + assert(true); + }, 10000); + + test("always passing test - example G", async () => { + assert(true); + }, 10000); + + test("always passing test - example H", async () => { + assert(true); + }, 10000); + + test("always passing test - example I", async () => { + assert(true); + }, 10000); + + test("always passing test - example A", async () => { + assert(true); + }, 10000); + + test("always passing test - verify page title", async () => { + await driver.get("https://bstackdemo.com"); + await driver.wait(until.titleMatches(/StackDemo/i), 10000); + }, 10000); + + test("Test with framework-level retry - 2 retries configured", async () => { + jest.retryTimes(2); + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + assert(randomOutcome); + }); + + test("Another Test with framework-level retry - 2 retries configured", async () => { + jest.retryTimes(2); + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + assert(randomOutcome); + }); +}); diff --git a/src/moduleB/test.js b/src/moduleB/test.js new file mode 100644 index 0000000..73d382e --- /dev/null +++ b/src/moduleB/test.js @@ -0,0 +1,114 @@ +const { Builder, By, Key, until, Capabilities } = require("selenium-webdriver"); +const assert = require("assert"); + +describe("BStack demo test Module B", () => { + let driver; + + beforeAll(async () => { + driver = new Builder() + .usingServer(`http://localhost:4444/wd/hub`) + .withCapabilities(Capabilities.chrome()) + .build(); + + await driver.get("https://bstackdemo.com"); + await driver.wait(until.titleMatches(/StackDemo/i), 10000); + }); + + afterAll(async () => { + await driver.quit(); + }) + + test( + "flaky test - add products to cart", + async () => { + let elementLocator = Math.random() < 0.5 ? '//*[@id="1"]/p' : '//*[@id="random"]/p'; + + // locating product on webpage and getting name of the product + await driver.wait(until.elementLocated(By.xpath(elementLocator))); + let productText = await driver + .findElement(By.xpath('//*[@id="1"]/p')) + .getText(); + // clicking the 'Add to cart' button + await driver.findElement(By.xpath('//*[@id="1"]/div[4]')).click(); + // waiting until the Cart pane has been displayed on the webpage + await driver.wait(until.elementLocated(By.className("float-cart__content"))); + await driver.findElement(By.className("float-cart__content")); + // locating product in cart and getting name of the product in cart + let productCartText = await driver + .findElement( + By.xpath( + '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]' + ) + ) + .getText(); + // checking whether product has been added to cart by comparing product name + expect(productText).toBe(productCartText); + }, + 10000 + ); + + test("always failing test - same stacktrace 1", async () => { + await driver.wait(until.elementLocated(By.xpath('//*[@id="common-error"]/p'))); + }, 2000); + + test("always failing test - same stacktrace 2", async () => { + await driver.wait(until.elementLocated(By.xpath('//*[@id="common-error"]/p'))); + }, 2000); + + test("always passing test - example F", async () => { + assert(true); + }, 10000); + + test("always passing test - example G", async () => { + assert(true); + }, 10000); + + test("always passing test - example H", async () => { + assert(true); + }, 10000); + + test("always passing test - example I", async () => { + assert(true); + }, 10000); + + test("always passing test - example A", async () => { + assert(true); + }, 10000); + + test("always passing test - example B", async () => { + assert(true); + }, 10000); + + test("always passing test - example C", async () => { + assert(true); + }, 10000); + + test("always passing test - example D", async () => { + assert(true); + }, 10000); + + test("always passing test - example E", async () => { + assert(true); + }, 10000); + + test("always passing test - verify page title", async () => { + await driver.get("https://bstackdemo.com"); + await driver.wait(until.titleMatches(/StackDemo/i), 10000); + }, 10000); + + test("Test with framework-level retry - 2 retries configured", async () => { + jest.retryTimes(2); + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + if (!randomOutcome) { + throw new Error("Test failed, retrying..."); + } + }); + + test("Another Test with framework-level retry - 2 retries configured", async () => { + jest.retryTimes(2); + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + if (!randomOutcome) { + throw new Error("Test failed, retrying..."); + } + }); +}); diff --git a/src/moduleC/test.js b/src/moduleC/test.js new file mode 100644 index 0000000..40e25d8 --- /dev/null +++ b/src/moduleC/test.js @@ -0,0 +1,98 @@ +const { Builder, By, Key, until, Capabilities } = require("selenium-webdriver"); +const assert = require("assert"); + +describe("BStack demo test Module C", () => { + let driver; + + beforeAll(async () => { + driver = new Builder() + .usingServer(`http://localhost:4444/wd/hub`) + .withCapabilities(Capabilities.chrome()) + .build(); + + await driver.get("https://bstackdemo.com"); + await driver.wait(until.titleMatches(/StackDemo/i), 10000); + }); + + afterAll(async () => { + await driver.quit(); + }) + + test( + "flaky test - add products to cart", + async () => { + let elementLocator = Math.random() < 0.5 ? '//*[@id="1"]/p' : '//*[@id="random"]/p'; + + // locating product on webpage and getting name of the product + await driver.wait(until.elementLocated(By.xpath(elementLocator))); + let productText = await driver + .findElement(By.xpath('//*[@id="1"]/p')) + .getText(); + // clicking the 'Add to cart' button + await driver.findElement(By.xpath('//*[@id="1"]/div[4]')).click(); + // waiting until the Cart pane has been displayed on the webpage + await driver.wait(until.elementLocated(By.className("float-cart__content"))); + await driver.findElement(By.className("float-cart__content")); + // locating product in cart and getting name of the product in cart + let productCartText = await driver + .findElement( + By.xpath( + '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]' + ) + ) + .getText(); + // checking whether product has been added to cart by comparing product name + expect(productText).toBe(productCartText); + }, + 10000 + ); + + test("always failing test - missing element 1", async () => { + await driver.wait(until.elementLocated(By.xpath('//*[@id="non-existent-1"]/p'))); + }, 2000); + + test("always failing test - same stacktrace 1", async () => { + await driver.wait(until.elementLocated(By.xpath('//*[@id="common-error"]/p'))); + }, 2000); + + test("always failing test - same stacktrace 2", async () => { + await driver.wait(until.elementLocated(By.xpath('//*[@id="common-error"]/p'))); + }, 2000); + + test("always passing test - example F", async () => { + assert(true); + }, 10000); + + test("always passing test - example G", async () => { + assert(true); + }, 10000); + + test("always passing test - example H", async () => { + assert(true); + }, 10000); + + test("always passing test - example I", async () => { + assert(true); + }, 10000); + + test("always passing test - verify page title", async () => { + await driver.get("https://bstackdemo.com"); + await driver.wait(until.titleMatches(/StackDemo/i), 10000); + }, 10000); + + test("Test with framework-level retry - 2 retries configured", async () => { + jest.retryTimes(2); + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + if (!randomOutcome) { + throw new Error("Test failed, retrying..."); + } + }); + + test("Another Test with framework-level retry - 2 retries configured", async () => { + jest.retryTimes(2); + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + if (!randomOutcome) { + throw new Error("Test failed, retrying..."); + } + }); +}); From eb2a0c4f672a0961992836cb0e4e628bd54312d5 Mon Sep 17 00:00:00 2001 From: Rahul Singh Date: Fri, 16 Jan 2026 18:15:29 +0530 Subject: [PATCH 2/2] refactor for retry tests --- jest.config.js | 2 +- src/moduleA/test-retry.js | 31 +++++++++++++++++++++++++++++++ src/moduleA/test.js | 12 ------------ src/moduleB/test-retry.js | 35 +++++++++++++++++++++++++++++++++++ src/moduleB/test.js | 16 ---------------- src/moduleC/test-retry.js | 35 +++++++++++++++++++++++++++++++++++ src/moduleC/test.js | 2 -- 7 files changed, 102 insertions(+), 31 deletions(-) create mode 100644 src/moduleA/test-retry.js create mode 100644 src/moduleB/test-retry.js create mode 100644 src/moduleC/test-retry.js diff --git a/jest.config.js b/jest.config.js index d8f5992..95341cb 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,7 +2,7 @@ module.exports = { coverageProvider: "v8", maxWorkers: 5, roots: ["src"], - testMatch: ["**/test.js"], + testMatch: ["**/test*.js"], testPathIgnorePatterns: ["/node_modules/"], testTimeout: 30 * 1000, }; diff --git a/src/moduleA/test-retry.js b/src/moduleA/test-retry.js new file mode 100644 index 0000000..ba4e73c --- /dev/null +++ b/src/moduleA/test-retry.js @@ -0,0 +1,31 @@ +const { Builder, By, Key, until, Capabilities } = require("selenium-webdriver"); +const assert = require("assert"); + +describe("BStack demo test Module A", () => { + let driver; + jest.retryTimes(2, {retryImmediately: true}); + + beforeAll(async () => { + driver = new Builder() + .usingServer(`http://localhost:4444/wd/hub`) + .withCapabilities(Capabilities.chrome()) + .build(); + + await driver.get("https://bstackdemo.com"); + await driver.wait(until.titleMatches(/StackDemo/i), 10000); + }); + + afterAll(async () => { + await driver.quit(); + }) + + test("Test with framework-level retry - 2 retries configured", async () => { + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + assert(randomOutcome); + }); + + test("Another Test with framework-level retry - 2 retries configured", async () => { + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + assert(randomOutcome); + }); +}); diff --git a/src/moduleA/test.js b/src/moduleA/test.js index a182711..60a89ca 100644 --- a/src/moduleA/test.js +++ b/src/moduleA/test.js @@ -83,16 +83,4 @@ describe("BStack demo test Module A", () => { await driver.get("https://bstackdemo.com"); await driver.wait(until.titleMatches(/StackDemo/i), 10000); }, 10000); - - test("Test with framework-level retry - 2 retries configured", async () => { - jest.retryTimes(2); - const randomOutcome = Math.random() > 0.7; // 30% chance of passing - assert(randomOutcome); - }); - - test("Another Test with framework-level retry - 2 retries configured", async () => { - jest.retryTimes(2); - const randomOutcome = Math.random() > 0.7; // 30% chance of passing - assert(randomOutcome); - }); }); diff --git a/src/moduleB/test-retry.js b/src/moduleB/test-retry.js new file mode 100644 index 0000000..feab626 --- /dev/null +++ b/src/moduleB/test-retry.js @@ -0,0 +1,35 @@ +const { Builder, By, Key, until, Capabilities } = require("selenium-webdriver"); +const assert = require("assert"); + +describe("BStack demo test Module B", () => { + let driver; + jest.retryTimes(2, {retryImmediately: true}); + + beforeAll(async () => { + driver = new Builder() + .usingServer(`http://localhost:4444/wd/hub`) + .withCapabilities(Capabilities.chrome()) + .build(); + + await driver.get("https://bstackdemo.com"); + await driver.wait(until.titleMatches(/StackDemo/i), 10000); + }); + + afterAll(async () => { + await driver.quit(); + }) + + test("Test with framework-level retry - 2 retries configured", async () => { + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + if (!randomOutcome) { + throw new Error("Test failed, retrying..."); + } + }); + + test("Another Test with framework-level retry - 2 retries configured", async () => { + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + if (!randomOutcome) { + throw new Error("Test failed, retrying..."); + } + }); +}); diff --git a/src/moduleB/test.js b/src/moduleB/test.js index 73d382e..77cdde8 100644 --- a/src/moduleB/test.js +++ b/src/moduleB/test.js @@ -95,20 +95,4 @@ describe("BStack demo test Module B", () => { await driver.get("https://bstackdemo.com"); await driver.wait(until.titleMatches(/StackDemo/i), 10000); }, 10000); - - test("Test with framework-level retry - 2 retries configured", async () => { - jest.retryTimes(2); - const randomOutcome = Math.random() > 0.7; // 30% chance of passing - if (!randomOutcome) { - throw new Error("Test failed, retrying..."); - } - }); - - test("Another Test with framework-level retry - 2 retries configured", async () => { - jest.retryTimes(2); - const randomOutcome = Math.random() > 0.7; // 30% chance of passing - if (!randomOutcome) { - throw new Error("Test failed, retrying..."); - } - }); }); diff --git a/src/moduleC/test-retry.js b/src/moduleC/test-retry.js new file mode 100644 index 0000000..db45f81 --- /dev/null +++ b/src/moduleC/test-retry.js @@ -0,0 +1,35 @@ +const { Builder, By, Key, until, Capabilities } = require("selenium-webdriver"); +const assert = require("assert"); + +describe("BStack demo test Module C", () => { + let driver; + jest.retryTimes(2, {retryImmediately: true}); + + beforeAll(async () => { + driver = new Builder() + .usingServer(`http://localhost:4444/wd/hub`) + .withCapabilities(Capabilities.chrome()) + .build(); + + await driver.get("https://bstackdemo.com"); + await driver.wait(until.titleMatches(/StackDemo/i), 10000); + }); + + afterAll(async () => { + await driver.quit(); + }) + + test("Test with framework-level retry - 2 retries configured", async () => { + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + if (!randomOutcome) { + throw new Error("Test failed, retrying..."); + } + }); + + test("Another Test with framework-level retry - 2 retries configured", async () => { + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + if (!randomOutcome) { + throw new Error("Test failed, retrying..."); + } + }); +}); diff --git a/src/moduleC/test.js b/src/moduleC/test.js index 40e25d8..c258710 100644 --- a/src/moduleC/test.js +++ b/src/moduleC/test.js @@ -81,7 +81,6 @@ describe("BStack demo test Module C", () => { }, 10000); test("Test with framework-level retry - 2 retries configured", async () => { - jest.retryTimes(2); const randomOutcome = Math.random() > 0.7; // 30% chance of passing if (!randomOutcome) { throw new Error("Test failed, retrying..."); @@ -89,7 +88,6 @@ describe("BStack demo test Module C", () => { }); test("Another Test with framework-level retry - 2 retries configured", async () => { - jest.retryTimes(2); const randomOutcome = Math.random() > 0.7; // 30% chance of passing if (!randomOutcome) { throw new Error("Test failed, retrying...");