首页 > 解决方案 > 如何在 Cypress.io 中使用 Firebase Auth 的 Google 登录选项

问题描述

在使用 Cypress 测试我的 Angular 6 应用程序时,我需要使用 Google Sign-In 的帮助。它无法使用登录弹出窗口,因此我尝试遵循赛普拉斯的建议“始终cy.request()通过其 API 与 3rd 方服务器通信”。这来自https://docs.cypress.io/guides/references/best-practices.html#Visiting-external-sites然后将我们指向这个示例:https ://github.com/cypress-io/cypress-example -recipes/blob/master/examples/logging-in__single-sign-on/cypress/integration/logging-in-single-sign-on-spec.js - 有关如何执行此操作的更多信息,请参阅演示文稿中的第 23 分钟赛普拉斯作者:https ://www.youtube.com/watch?v=5XQOK0v_YRE

我正在使用视频解决方案并尝试根据https://firebase.google.com/docs/auth/web/google-signin#advanced-authenticate-with-firebase-in-nodejs为 Firebase Auth 修改它,但我'我陷入了如何获得正确的问题id_token,到目前为止,我的 commands.js 文件中有这段代码:

Cypress.Commands.add('login', () => {
  var id_token = ___?____;
  cy.request({
    method: 'POST',
    url: 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=[API_KEY]',
    body: {
      "requestUri": "http://localhost:3500",
      "postBody": `id_token=${id_token}&providerId=google.com`,
      "returnSecureToken": true,
      "returnIdpCredential": true
    }
  })
})

我相信我需要使用不同的 API 来启动登录并请求用户凭据,其中将包括 id_token,因此我尝试了https://firebase.google.com/docs/auth/web/google-signin#advanced- authenticate-with-firebase-in-nodejs但还不够熟练,无法将外部 js 文件 ( https://apis.google.com/js/platform.js ) 拉入 Node (这在 js 文件中可能是不可能的它在 html 中的方式)。使用这个包:https ://github.com/google/google-auth-library-nodejs可能是我的下一个尝试。有没有人可以从这里取走?

Is it possible to use Cypress e2e testing with a firebase auth project?- 但他们正在使用用户登录并通过。

标签: node.jsfirebaseintegration-testinggoogle-authenticationcypress

解决方案


推荐阅读