To get started:
- Start by downloading Chromedriver and adding it to PATH
- Clone this repository and navigate to
MochaTypescriptTest-101/
- Run
npm install
- Execute
mocha test\SeleniumDemo.test.js
While this may not be exactly what you are seeking, it includes tests that can help you begin with selenium and typescript. Study their configuration for a smooth start. For more information, refer to this link which provides detailed tests like the ones listed below:
before – sets up chrome driver
before(function () {
// setting up chrome driver
driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
// maximizing chrome browser
driver.manage().window().maximize();
});
afterEach – captures screenshot on test failure, gathers logs etc
afterEach(function () {
let testCaseName: string = this.currentTest.title;
let testCaseStatus: string = this.currentTest.state;
if (testCaseStatus === 'failed') {
console.log(`Test: ${testCaseName}, Status: Failed!`);
// taking screenshot when test fails
driver.takeScreenshot().then((data) => {
let screenshotPath = `TestResults/Screenshots/${testCaseName}.png`;
console.log(`Saving Screenshot as: ${screenshotPath}`);
fs.writeFileSync(screenshotPath, data, 'base64');
});
} else if (testCaseStatus === 'passed') {
console.log(`Test: ${testCaseName}, Status: Passed!`);
} else {
console.log(`Test: ${testCaseName}, Status: Unknown!`);
}
});
after – closes the browser
after(function () {
driver.quit();
});
it – executes test operation and verifies result. e.g. open bing.com and search for a text
it('should search for nilay shah at bing.com', function () {
let Url: string = `http://www.bing.com`;
return driver.get(Url).then(function () {
console.log(`Page "${Url}" opened`);
}).then(() => {
return driver.getCurrentUrl().then((currentUrl) => {
currentUrl.should.include(
`www.bing.com`,
`Expected url: www.bing.com, Actual url: ${currentUrl}`);
}).then(() => {
let searchBox = driver.findElement(webdriver.By.name('q'));
searchBox.sendKeys('nilay shah');
return searchBox.getAttribute('value').then((value) => {
value.should.equal('nilay shah');
});
});
});
});