我正在学习 TestCafe 并尝试在网站上创建一个帐户,然后登录到 Gmail 以查找激活链接。当我尝试这样做时,我只是在进入输入密码的部分时收到浏览器不安全消息。如何让 Gmail 信任 TestCafe?
最佳答案
虽然您可能会成功这样做,但这不是一个好方法,因为:
更好的方法是使用像 Mailosaur 这样的服务,您可以在其中创建一个帐户并接收您以后可以通过 API 查询的电子邮件。您无需在 GUI 上进行整个 e2e 流程,而是在 Mailosaur 的 API 上请求一封电子邮件,如果存在这样的电子邮件,您将收到可以解析和检查各种内容的响应。
我过去做过这个,你可以在这里看到我的帖子:https://sqa.stackexchange.com/questions/40427/automating-verification-of-sent-email-sms-messages/45721#45721 这正是 Mailosaur 和 Testcafe(加上它需要 axios 作为一个包),所以它似乎是你要找的。
在此处添加相同的代码:
import config from '../config';
import { customAlphabet } from 'nanoid';
import axios from 'axios';
import Newsletter from '../Objects/newsletter';
async function request (reqObject) {
try {
return await axios(reqObject);
} catch (error) {
console.error(error);
}
}
function serverId () {
return process.env.MAILOSAUR_SERVER_ID;
}
function mailosaurFullEmail (id) {
return (id ? id : nanoid()) + '.' + serverId()
+ '@' + config.mailosaurDomain;
}
fixture `Newsletter`
.page(baseUrl);
test
('Sign Up For Newsletter', async t => {
const id = (customAlphabet('1234567890', 10))();
await t
.typeText(Newsletter.newsEmailInput, mailosaurFullEmail(id))
.click(Newsletter.consent)
.click(Newsletter.sendButton);
let res = await request({
method: 'POST',
url: config.mailosaurUrlEmail + serverId(),
headers: {
'Authorization': 'Basic '
+ Buffer.from(process.env.MAILOSAUR_API_KEY)
.toString('base64'),
'Content-Type': 'application/json'
},
data: {
sentTo: mailosaurFullEmail(id)
}
});
await t
.expect(res.status).eql(200);
});
它需要一些配置值:{
"mailosaurUrlEmail": "https://mailosaur.com/api/messages/await?server=",
"mailosaurDomain": "mailosaur.io"
}
这肯定要好得多,但它仍然有一些限制:如果您绝对必须通过 Gmail 进行操作,您最好还是查看他们的 API,它应该允许您搜索和查询电子邮件消息。
关于ssl - 在使用 TestCafe 时尝试登录 gmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64894932/