-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselenium-node.js
More file actions
54 lines (53 loc) · 2.61 KB
/
selenium-node.js
File metadata and controls
54 lines (53 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const webdriver = require('selenium-webdriver');
const { By } = require('selenium-webdriver');
const assert = require('assert');
// Input capabilities
var capabilities = {
'bstack:options' : {
"os" : "OS X",
"osVersion" : "Sierra",
"buildName" : "Final-Snippet-Test",
"sessionName" : "Selenium-4 Nodejs snippet test",
"local" : "false",
"seleniumVersion" : "4.0.0",
"userName" : "YOUR_USER_NAME",
"accessKey" : "YOUR_ACCESS_KEY",
},
"browserName" : "Chrome",
"browserVersion" : "latest",
}
async function runTestWithCaps () {
const username = ""
const accesskey = ""
let driver = new webdriver.Builder().usingServer(`https://hub-cloud.browserstack.com/wd/hub`).withCapabilities(capabilities).build();
try{
await driver.get("https://bstackdemo.com/");
await driver.wait(webdriver.until.titleMatches(/StackDemo/i), 10000);
// locating product on webpage
const productOnScreen = await driver.wait(webdriver.until.elementLocated(By.xpath('//*[@id="1"]/p')), 10000)
// getting name of the product when the product is visible
const productText = await driver.wait(webdriver.until.elementIsVisible(productOnScreen, 10000)).getText();
// clicking the 'Add to cart' button
await driver.wait(webdriver.until.elementIsVisible(driver.findElement(By.xpath('//*[@id="1"]/div[4]'), 10000))).click();
// waiting until the Cart pane has been displayed on the webpage
await driver.wait(webdriver.until.elementIsVisible(driver.findElement(By.className('float-cart__content'), 10000)));
// locating product in cart
const productInCart = await driver.wait(webdriver.until.elementLocated(By.xpath('//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]')), 10000);
// getting name of the product in cart if the product is visible on web page
const productCartText = await driver.wait(webdriver.until.elementIsVisible(productInCart, 10000)).getText();
// checking whether product has been added to cart by comparing product name
assert(productText === productCartText);
//marking the test as Passed if product has been added to the cart
await driver.executeScript(
'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Product has been successfully added to the cart!"}}'
);
} catch(e) {
//marking the test as Failed if product has not been added to the cart
console.log("Error:", e.message)
await driver.executeScript(
'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Some elements failed to load."}}'
);
}
await driver.quit();
}
runTestWithCaps();