DevOps 实践

我想大多数的团队都面临这样的问题:

  1. 发布周期长
  2. 开发和测试时间短
  3. 开发和测试是两个独立的团队
  4. 不稳定的交付质量
  5. 低收益难维护的UI自动化测试脚本
  6. 不合理的测试权重分配

解决方法:

引入 DevOps 和分层自动化

  • 组件化产品
    • 产品开发引入模块化,数据驱动会使得产品更加容易实施 Unit,Server,UI 自动化测试
  • 优化工程师
    • 开发和测试在未来将没有界限,他们都是开发者,都会产品的质量和客户负责
  • 分层自动化
    • 更合理的测试权重分配,更底层的测试收益越高
  • 引入工具
    • 实施DevOps引入必要的工具,Bitbucket, Jenkins, Sonar, Pipelines, Docker, test framework …

Git 命令备忘

有些git命令总是记不住,在我这台 Ubuntu 使用 web 版 OneNote 不方便,那就把他们记到 Blog 里吧,需要的时候翻看一下。

git remote

git remote -v                 # 查看当前位置的远程代码库
git remote remove origin # 取消远程仓库
git remote add orgin git@github.com:shenxianpeng/nightwatch.git # 关联新的仓库

git log

# 得到某一时段提交日志
git log --after='2017-12-04' --before='2017-12-08' --author=xshen --pretty=oneline --abbrev-commit

git tag

git tag -a v1.6.700 -m 'Release v1.6.700'

# 给前面的提交补上 tag
git log --pretty=oneline
git tag -a v1.6.700 -m 'Release v1.6.700' e454ad98862

git push tag
git push origin --tag

设置 npm install 代理

npm config set proxy=http://10.17.201.60:8080       # 设置代理
npm config set proxy null # 取消代理

设置 cnpm

npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install [name]
cnpm sync connect
cnpm info connect

Git remove and add remote repository

如果是通过 https 方式来 pull 和 push 代码,每次都要输入烦人的账号和密码
可以通过切成成 ssh 方式:

# 取消远程仓库
git remote remove origin

# 关联远程仓库
git remote add origin git@github.com:shenxianpeng/blog.git

Nightwatch 使用 VS code 进行调试

除了通过增加

console.log('===========')

来调试 Nightwatch 代码,如何通过配置 VS code 来 Debug Nightwatch 代码?

Ctrl+Shift+D 打开 Debug 界面,配置如下:

{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"type": "node",
"request": "launch",
"name": "npm test",
"program": "${workspaceRoot}/node_modules/nightwatch/bin/runner.js",
"args": [
"tests/DQA/DQA-221/login.js"
]
}
]
}

Nightwatch 持续集成问题

在持续集成执行自动化测试用例时候会遇到那些问题呢

  1. 运行时间过长
  2. 因为某些错误程序卡住
  3. 异常处理

针对以上三种情况,通过下面的三种方式进行解决

运行时间过长, E2E 测试脚本中难免需要时间等待,例如

this.pause(1000);
// 尽可能将说有的 pause 换成 wait,例如:
this.element('@columns').to.be.visible.before(2000);
// 或
this.waitForElementVisible('@columns', 5000);

因为某些错误程序卡住, 在 TestCase 中进行验证时,例如

this.assert.equal(result.value.length, 1);
// 如果只想标注失败,继续执行后面的代码,则需将 assert 换成 verify
this.veriry.equal(result.value.length, 1);

// 在 waitForElementVisible 中加 abortOnFailure 参数,当设置为 false,在 wait 超时时,就会标志为 false 继续继续执行
this.waitForElementVisible('@columns', 5000, false);

//还可以通过在 nightwatch.conf.js 设置全局变量
abortOnAssertionFailure: false

异常处理

当程序执行运行一次时,程序运行正常,一旦遇到异常时,下次执行就回出错。
例如:比如邀请账号登录系统的操作。管理员添加一个新用户,然后用这个新用户登录,之后管理员删除这个账户。但如果删除这个账号失败时,下次执行这个程序再邀请这个账号时就会提示这个账号存在的,可能这个时候这个程序就执行不下去了。这个时候就需要考虑这些异常情况处理,保证程序能够良好的执行下去。

Nightwatch 打开多个窗口

如果想打开两个窗口并控制那个窗口怎么办?

var url = process.env.BASE_URL, newWindow;

client.execute(function (url, newWindow) {
window.open(url, newWindow, 'height=768,width=1024');
}, [url, newWindow]);

client.window_handles(function(result) {
this.verify.equal(result.value.length, 2, 'There should be 2 windows open');
newWindow = result.value[1];
this.switchWindow(newWindow);
})

Ubuntu 上使用 VPN

如何在 Ubuntu 上连接 Cisco AnyConnect VPN

打开Terminal,执行:

sudo /sbin/modprobe tun

安装OpenConnect,执行:

sudo apt-get install openconnect

连接VPN,执行:

sudo openconnect yourvpn.example.com

将提示你输入用户名和密码,输入争取后,VPN连接成功。

原文 请点击

Ubuntu 上安装 VS Code

在 Ubuntu 下面安装 Visual Studio Code

sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make
sudo apt-get update
sudo apt-get install ubuntu-make
umake web visual-studio-code

亲测,好用。

Nightwatch wait For Text

在使用 Nightwatch 做自动化测试的时候,会遇到这样一种情况:
创建一个 query, 等待这个query的状态从 Wait 变成 Running 最后到 Available 时再执行操作。
Nightwatch 并没有提供这样的方法,可以通过下面的方式解决。

'Wait for text': function waitForText(client) {
const query = client.page.query();
query.navigate();
for (let i = 0; i <= 10; i++) {
client.getText('status', function (result) {
if (result.value.indexOf('Available') == 0) {
this.break;
} else {
client.pause(1000);
i++;
}
});
}
// TODO something
}

Nightwatch 元素判断

Nightwatch 元素常用验证方法

验证元素的值信息

andesFormSection
.assert.containsText('@errorMessage', 'The email address is invalid.')

验证元素是否可用

andesFormSection
.assert.attributeEquals('@continueBtn', 'disabled', 'true');

等待元素可用

andesFormSection
.expect.element('@signInBtn').to.be.visible.before(5000);

或者

andesFormSection
waitForElementVisible('signInBtn', 5000);

等待元素呈现

andesFormSection
.expect.element('@signInBtn').to.be.present.before(5000);

或者

andesFormSection
waitForElementPresent('signInBtn', 5000);