Coding with JS (Mocha)
Resources:
https://npmdoc.github.io/node-npmdoc-wd/build/apidoc.html
https://www.npmjs.com/package/wd/v/0.0.34
https://www.npmjs.com/package/wd/v/1.12.1
JS- Mocha project’s package.json default dependencies:
"@babel/runtime": "^7.10.2",
"babel": "^6.23.0",
"esm": "^3.2.25",
"mocha": "^7.2.0",
"wd": "^1.12.1",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"adbkit": "^2.11.0",
"axios": "^0.19.2",
Above are the libraries added to your JS-Mocha project.
//Click element
let element = await driver.elementById("io.selendroid.testapp:id/visibleButtonTest");
await element.click();
//Wait for 2 seconds
return driver.sleep(2000)
//Clear and Sendkeys to element
let element = await driver.elementById("io.selendroid.testapp:id/my_text_field");
await element.clear();
await element.sendKeys("example@mail.com");
//Swipe action with start XY and end XY coordinates
await (new wd.TouchAction(driver))
.press({x: 706, y: 1478})
.moveTo({x: 655, y: 709})
.release()
.perform()
//Scroll Down on Web
//Header
const {Key} = require('selenium-webdriver');
//Step Code
await driver.findElement(By.id('Password_ID')).sendKeys(Key.PAGE_DOWN);
//Wait 20 seconds until element is located on the screen
//Header
const {until} = require('selenium-webdriver');
//Step Code
const myElement = By.css("#form > div");
await driver.wait(until.elementLocated(myElement), 20000)
//Click Element If Exists
let element = await driver.elementByIdIfExists("io.selendroid.testapp:id/visibleButtonTest");
if(element !== null) {
await element.click();
}
//Get attribute of element and compare the text
let text = await driver.elementById("io.selendroid.testapp:id/visibleButtonTest").getAttribute("text")
if(text === "Login") {
await driver.elementById("io.selendroid.testapp:id/visibleButtonTest").click()
}
//Custom Logging
driver.log("my custom log)
//Dynamically Set Value for specified key on a TestCase and use it's value inside another TestCase
driver.setValue("myKey","myValue")
driver.getValue("myKey")
return driver.sleep(3000)
.elementByIdIfExists("Allow")
.then((el) => {
if (el) {
return el.click();
}
})
.elementByIdIfExists("Only While Using the App")
.then((el) => {
if (el) {
return el.click();
}
})
.elementByIdIfExists("Allow")
.then((el) => {
if (el) {
return el.click();
}
})
.elementByIdIfExists("Allow While Using App")
.then((el) => {
if (el) {
return el.click();
}
})
.elementByIdIfExists("Not Now")
.then((el) => {
if (el) {
return el.click();
}
})
.sleep(2000)
.elementByIdIfExists("Allow")
.then((el) => {
if (el) {
return el.click();
}
})
.elementByIdIfExists("Cancel")
.then((el) => {
if (el) {
return el.click();
}
})
.elementByIdIfExists("Not Now")
.then((el) => {
if (el) {
return el.click();
}
})
driver.execute('mobile: shell', [{command: 'input text 123456'}]);
return new Promise((resolve, reject) => {
return driver.context(“NATIVE_APP”)
.setGeoLocation(41.31, 19.81, 110)
.catch((error) => {
driver.log(error); resolve(null);
});
})