common/globalUtil.js
'use strict'; //使用严格模式
//调用q模块,解决node异步加载和回调问题
var Q = require('q');
//调用fs模块,处理文件
var fs = require('fs');
//封装全局方法
var globalUtil = function(){
var self = this;
//封装元素等待
this.waitForVisilibity = function(component){
//将promise对象返回
return browser.sleep(1000).then(function(){
return browser.wait(function(){
//返回是否是存在的
return component.isPresent();
},2000);
}).then(function(){
return browser.wait(function(){
//返回是否是可见的
return component.isDisplayed();
},2000);
}).then(function(){
return browser.wait(function(){
//返回是否是可编辑的
return component.isEnabled();
},2000);
});
};
//封装获取时间戳函数
this.getTimeStamp = function(){
//实例化一个日期对象
const currentDate = new Date();
let timeStamp = currentDate.getHours() + "_" + currentDate.getMinutes() + "_" + currentDate.getSeconds();
return timeStamp;
};
//封装截屏方法
this.takeScreenshot = function(path,name){
//将promise返回
return browser.driver.takeScreenshot().then(function(png){
//实例化一个promise对象
const deferred = Q.defer();
//定义截屏路径
let screenshotPath = path + name + "png";
console.log("screenshotPath"+screenshotPath);
//创建文件流
const stream = fs.createWriteStream(screenshotPath);
//写入截图,进行回调判断
stream.write(new Buffer(png,'base64'),function(error){
if (error){
return console.log('Take screenshot error:'+ error); //将输出返回
}
console.log('Screenshot write to disk');
stream.end(); //关闭文件流
deferred.resolve(true); //期待promise是成功的,进行处理
});
return deferred.promise; //返回promise对象
});
};
};
//将全局方法作为接口导出
module.exports = globalUtil
QQ_mail_login/loginPage.js
'use strict';
var loginPage = {
login_frame: function(){
return element(by.id('login_frame')).getWebElement();
},
usernameLabel: function(){
return element(by.css('#u'));
},
passwordLabel: function(){
return browser.driver.findElement(by.css('#p'));
},
loginButton: function(){
return browser.driver.findElement(by.css('#login_button'));
}
};
module.exports = loginPage;
QQ_mail_login/loginUtil.js
'use strict';
var LoginPage = require('./loginPage.js');
var GlobalUtil = require('../common/globalUtil.js');
var loginUtil = function () {
// const globalutil = new GlobalUtil(),
// self = this;
const self = this;
this.init = function () {
browser.waitForAngularEnabled(false);
browser.driver.get(browser.baseUrl);
browser.driver.manage().window().maximize();
return browser.sleep(2000);
};
this.login = function (_username, _password) {
browser.switchTo().frame(LoginPage.login_frame());
return LoginPage.usernameLabel().sendKeys(_username).then(function () {
return browser.sleep(2000);
}).then(function () {
return LoginPage.passwordLabel().sendKeys(_password);
}).then(function () {
return browser.sleep(2000);
}).then(function () {
return LoginPage.loginButton().click();
}).then(function(){
browser.sleep(10000);
});
};
this.go = function () {
return self.init().then(function () {
return self.login(browser.params.login.username, browser.params.login.password);
});
};
};
module.exports = loginUtil;
testcase/login_case.js
'use strict';
var loginutil = require('../QQ_mail_login/loginUtil.js');
describe('测试QQ邮箱',function(){
const login = new loginutil();
it('bbbbb',function(){
login.go();
try{
expect(element(by.css('#composebtn')).getText()).toBe('写信');
}catch(error){
throw new TypeError('断言错误!');
}
// expect(element(by.css('#composebtn')).getText()).toBe('写信');
});
});
conf.js
exports.config = {
framework: 'jasmine2', //指定测试框架
directConnect: true, //绕过selenium-server直接在nodejs中运行,提高效率
SELENIUM_PROMISE_MANAGER: true, //支持then方法,async(异步)调用
specs: ['./testcase/login_case.js'], //指定脚本case
// exclude: [], //可排除脚本case,剩余的都run
baseUrl: 'https://mail.qq.com/', //要打开的url地址
params: { //相关参数
login: {
username: '1347618115',
password: 'JZQ19960924.'
}
},
afterLaunch: function(exitCode) {
var endTime = new Date().toUTCString()
console.log('AfterLaunch:' + exitCode + 'endTime:' + endTime);
},
capabilities: {
//可以在此处设置浏览器
// browserName: 'chrome',
browserName: 'firefox',
acceptInsecureCerts: true, //接受不安全的证书
}
}