我正在尝试启动一个UI自动化项目,该项目必须使用Selenium并使用typeScript。有人能分享一下如何做同样的事情吗?我在Java中使用了Selenium WebDriver。我已经安装了nodejs&我的机器上的npm。
您可以通过启动
- 下载Chromedriver并将其放入PATH
- 克隆此repo和
cd MochaTypescriptTest-101/
npm install
mocha testSeleniumDemo.test.js
这并不是你想要的,但它包含了一些测试,可以让你开始使用selenium和typescript。只要看看他们的配置,你就可以出发了。有关更多详细信息,您可以查看此链接,其中包含以下测试
before – initializes chrome driver
before(function () {
// initializing chrome driver
driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
// maximizing chrome browser
driver.manage().window().maximize();
});
afterEach – takes screenshot if test case fails, collects 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!`);
// capturing screenshot if 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 browser
after(function () {
driver.quit();
});
it – performs test operation and validates 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');
});
});
});
});