安装步骤
- cd your workspace path
- npm init (确保创建cypress工程时已经有node module,这样cypress会安装在你的当前目录下)
- npm install cypress --save-dev (Node.js 10 or 12 and above)
install时遇到的问题
安装失败,unsafe-perm in lifecycle true 可以从_log中看到具体信息
解决方法
在网上查了众多资料后发现是node version太老
- npm 先查看版本
node -v
npm -v
- 更新npm
npm install -g npm
- 更新node版本
npm cache clean -f //清缓存
npm install -g n //安装n模块
sudo n stable //升级node.js到最新稳定版
source /etc/profile //让更改生效
node -v //此时可看到升级后的版本
npm install cypress --save-dev //再install,便会成功安装
安装cypress-social-logins插件解决redirect问题
npm install --save-dev cypress-social-logins // package.json会出现social login的依赖包
- import the plugin ./plugins/index.js
const { GoogleSocialLogin } = require('./plugin'); //GoogleSocialLogin没有处理otp,所以对其进行了改写,重新实现, plugin中是具体登陆的操作
module.exports = (on, config) => {
on('task', {
GoogleSocialLogin,
});
};
// 调用
return cy.task('GoogleSocialLogin', socialLoginOptions).then(({ cookies }) => {
cookies.filter(cookie => cookie.domain === domain)
.forEach((cookie) => {
cy.setCookie(
cookie.name,
cookie.value,
{
domain: cookie.domain, //要redirect to的链接,需要set的 cookieName也是所有包含这个domain的 link对应的cookie name
expiry: cookie.expires,
httpOnly: cookie.httpOnly,
path: cookie.path,
secure: cookie.secure
});
});
Cypress.Cookies.defaults({
preserve: cookieNames //不知是不是和黑人运动有关,cypress官方将 whitelist rename为preserve
});
});
- 有Google auth认证,需要输入time base otp的话可以借助speakeasy
npm install --save speakeasy //倒入speakeasy
const speakeasy = require("speakeasy");
function generateOTP(secret) {
const otp = speakeasy.totp({
secret,
encoding: 'base32' //我的是base32的
})
return otp
}
module.exports = { generateOTP }
secret怎么获取呢,可以通过google登陆到google账户下的security tab https://myaccount.google.com/u/1/security
-- 2-Step Verification 的设置里出现二维码时选择底下不能扫描二维码的链接,这时会生成一串密钥,这个密钥就是我们要的 secret.