From b5c700884b890b192a9dbe7f909ba566f31b29cb Mon Sep 17 00:00:00 2001 From: Rahul Singh Date: Sun, 5 Oct 2025 21:36:54 +0530 Subject: [PATCH] add specs to diversify tests --- conf/base.conf.js | 2 +- tests/moduleA/specs/test.js | 68 +++++++++++++++++++++++++++++ tests/moduleB/specs/test.js | 85 +++++++++++++++++++++++++++++++++++++ tests/moduleC/specs/test.js | 63 +++++++++++++++++++++++++++ 4 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 tests/moduleA/specs/test.js create mode 100644 tests/moduleB/specs/test.js create mode 100644 tests/moduleC/specs/test.js diff --git a/conf/base.conf.js b/conf/base.conf.js index b6f6ffbc9..32ee30ded 100644 --- a/conf/base.conf.js +++ b/conf/base.conf.js @@ -3,7 +3,7 @@ exports.config = { key: process.env.BROWSERSTACK_ACCESS_KEY || 'BROWSERSTACK_ACCESS_KEY', updateJob: false, - specs: ['./tests/specs/test.js'], + specs: ['./tests/m*/**'], exclude: [], logLevel: 'warn', diff --git a/tests/moduleA/specs/test.js b/tests/moduleA/specs/test.js new file mode 100644 index 000000000..f169d9084 --- /dev/null +++ b/tests/moduleA/specs/test.js @@ -0,0 +1,68 @@ +describe("BStackDemo Tests Module A", () => { + before(async () => { + await browser.url("https://bstackdemo.com/"); + await browser.waitUntil( + async () => (await browser.getTitle()).match(/StackDemo/i), + 5000, + "Title didn't match with BrowserStack" + ); + }); + + it("flaky test - random product selection", async () => { + const randomProductIndex = Math.random() > 0.5 ? "1" : "2000"; + const productOnScreen = await $(`//*[@id="${randomProductIndex}"]/p`); + const productOnScreenText = await productOnScreen.getText(); + + const addToCart = await $(`//*[@id="${randomProductIndex}"]/div[4]`); + await addToCart.click(); + + const productInCart = await $('//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]'); + await browser.waitUntil(async () => ( + await productInCart.getText()).match(productOnScreenText), + { timeout: 5000 } + ); + }); + + it("always failing test - missing element 1", async () => { + const nonExistentElement = await $('//*[@id="non-existent-1"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("always failing test - missing element 2", async () => { + const nonExistentElement = await $('//*[@id="non-existent-2"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("always failing test - same stacktrace 1", async () => { + const nonExistentElement = await $('//*[@id="common-error"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("always failing test - same stacktrace 2", async () => { + const nonExistentElement = await $('//*[@id="common-error"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("always failing test - same stacktrace 3", async () => { + const nonExistentElement = await $('//*[@id="common-error"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("passing test - verify page title", async () => { + const title = await browser.getTitle(); + expect(title).to.match(/StackDemo/i); // Use 'match' instead of 'toMatch' + }); + + it("Test with framework-level retry - 2 retries configured", function () { + this.retries(2); // Framework-level retry + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + if (!randomOutcome) { + throw new Error("Test failed, retrying..."); + } + }); + + it("always passing test - example A", async () => { + const url = await browser.getUrl(); + expect(url).to.include("bstackdemo"); + }); +}); \ No newline at end of file diff --git a/tests/moduleB/specs/test.js b/tests/moduleB/specs/test.js new file mode 100644 index 000000000..18e53d95b --- /dev/null +++ b/tests/moduleB/specs/test.js @@ -0,0 +1,85 @@ +describe("BStackDemo Tests Module B", () => { + before(async () => { + await browser.url("https://bstackdemo.com/"); + await browser.waitUntil( + async () => (await browser.getTitle()).match(/StackDemo/i), + 5000, + "Title didn't match with BrowserStack" + ); + }); + + it("Flaky test - random product selection", async () => { + const randomProductIndex = Math.random() > 0.7 ? "1" : "2000"; + const productOnScreen = await $(`//*[@id="${randomProductIndex}"]/p`); + const productOnScreenText = await productOnScreen.getText(); + + const addToCart = await $(`//*[@id="${randomProductIndex}"]/div[4]`); + await addToCart.click(); + + const productInCart = await $('//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]'); + await browser.waitUntil(async () => ( + await productInCart.getText()).match(productOnScreenText), + { timeout: 5000 } + ); + }); + + it("Failing test - clicking on a non-existent element", async () => { + const nonExistentElement = await $('//*[@id="non-existent-1"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("Failing test - same stacktrace", async () => { + const nonExistentElement = await $('//*[@id="common-error"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("Failing test - same stacktrace 2", async () => { + const nonExistentElement = await $('//*[@id="common-error"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("Failing test - same stacktrace 3", async () => { + const nonExistentElement = await $('//*[@id="common-error"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("Passing test - verify page title", async () => { + const title = await browser.getTitle(); + expect(title).to.match(/StackDemo/i); // Use 'match' instead of 'toMatch' + }); + + it("Always passing test", async () => { + const result = 6 + 3; + expect(result).to.equal(9); + }); + + it("Always passing test - example B", async () => { + const result = 1000 * 2; + expect(result).to.equal(2000); + }); + + it("Always passing test - example C", async () => { + const result = 1000 * 2; + expect(result).to.equal(2000); + }); + + it("Always passing test - example D", async () => { + const str1 = "BrowserStack is better than LambdaTest"; + const str2 = str1.substring(3, 10); + expect(str2).to.equal("wserSta"); + }); + + it("Always passing test - example E", async () => { + const str1 = "BrowserStack is better than LambdaTest"; + const str2 = str1.substring(3, 11); + expect(str2).to.equal("wserStac"); + }); + + it("Test with framework-level retry - 2 retries configured", function () { + this.retries(2); // Framework-level retry + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + if (!randomOutcome) { + throw new Error("Test failed, retrying..."); + } + }); +}); \ No newline at end of file diff --git a/tests/moduleC/specs/test.js b/tests/moduleC/specs/test.js new file mode 100644 index 000000000..6471c867c --- /dev/null +++ b/tests/moduleC/specs/test.js @@ -0,0 +1,63 @@ +describe("BStackDemo Tests Module C", () => { + before(async () => { + await browser.url("https://bstackdemo.com/"); + await browser.waitUntil( + async () => (await browser.getTitle()).match(/StackDemo/i), + 5000, + "Title didn't match with BrowserStack" + ); + }); + + it("flaky test - random product selection", async () => { + const randomProductIndex = Math.random() > 0.5 ? "1" : "2000"; + const productOnScreen = await $(`//*[@id="${randomProductIndex}"]/p`); + const productOnScreenText = await productOnScreen.getText(); + + const addToCart = await $(`//*[@id="${randomProductIndex}"]/div[4]`); + await addToCart.click(); + + const productInCart = await $('//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]'); + await browser.waitUntil(async () => ( + await productInCart.getText()).match(productOnScreenText), + { timeout: 5000 } + ); + }); + + it("always failing test - missing element 1", async () => { + const nonExistentElement = await $('//*[@id="non-existent-1"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("always failing test - missing element 2", async () => { + const nonExistentElement = await $('//*[@id="non-existent-2"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("always failing test - same stacktrace 1", async () => { + const nonExistentElement = await $('//*[@id="common-error"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("always failing test - same stacktrace 2", async () => { + const nonExistentElement = await $('//*[@id="common-error"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("always failing test - same stacktrace 3", async () => { + const nonExistentElement = await $('//*[@id="common-error"]/p'); + await nonExistentElement.click(); // This will throw an error + }); + + it("passing test - verify page title", async () => { + const title = await browser.getTitle(); + expect(title).to.match(/StackDemo/i); // Use 'match' instead of 'toMatch' + }); + + it("Test with framework-level retry - 2 retries configured", function () { + this.retries(2); // Framework-level retry + const randomOutcome = Math.random() > 0.7; // 30% chance of passing + if (!randomOutcome) { + throw new Error("Test failed, retrying..."); + } + }); +}); \ No newline at end of file