Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions browserstack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ buildIdentifier: '#${BUILD_NUMBER}' # Supports strings along with either/both ${
# Set `source` in the syntax `<fw-name>:sample-<branch-name>:<version-number>
source: jest-js:sample-main:v1.0

CUSTOM_TAG_1: bstack_sample

# =======================================
# Platforms (Browsers / Devices to test)
# =======================================
Expand Down
5 changes: 2 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
31 changes: 31 additions & 0 deletions src/moduleA/test-retry.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
86 changes: 86 additions & 0 deletions src/moduleA/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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);
});
35 changes: 35 additions & 0 deletions src/moduleB/test-retry.js
Original file line number Diff line number Diff line change
@@ -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...");
}
});
});
98 changes: 98 additions & 0 deletions src/moduleB/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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);
});
35 changes: 35 additions & 0 deletions src/moduleC/test-retry.js
Original file line number Diff line number Diff line change
@@ -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...");
}
});
});
Loading