属性“ cookies”在类型“ undefined”上不存在

时间:2020-06-17 19:34:52

标签: typescript cypress

我尝试使用以下插件实现Google的Cypress登录测试:https://github.com/lirantal/cypress-social-logins/(我使用TypeScript),代码为:

it('Login through Google', () => {
const username = Cypress.env('googleSocialLoginUsername')
const password = Cypress.env('googleSocialLoginPassword')
const loginUrl = Cypress.env('loginUrl')
const cookieName = Cypress.env('cookieName')
const socialLoginOptions = {
  username,
  password,
  loginUrl,
  headless: false,
  isPopup: true,
  logs: false,
  loginSelector: 'a[href="/auth/auth0/google-oauth2"]',
  postLoginSelector: '.account-panel'
}

return cy.task('GoogleSocialLogin', socialLoginOptions).then(({cookies}) => {
  cy.clearCookies()

  const cookie = cookies.filter((cookie: { name: any; }) => cookie.name === cookieName).pop()
  if (cookie) {
    cy.setCookie(cookie.name, cookie.value, {
      domain: cookie.domain,
      expiry: cookie.expires,
      httpOnly: cookie.httpOnly,
      path: cookie.path,
      secure: cookie.secure
    })

    Cypress.Cookies.defaults({
      whitelist: cookieName
    })
  }
})
});

我遇到以下错误:

类型为'undefined'的属性'cookies'不存在。ts(2339)

有什么办法解决吗?

1 个答案:

答案 0 :(得分:0)

解决方案

看代码:

Cypress.on('window:before:load', (win) => {
  // because this is called before any scripts
  // have loaded - the ga function is undefined
  // so we need to create it.
  (<any>win).gapi = cy.stub().as('gapi')
  win.gapi = {
    load(){
      return null;
    },
    auth2: {
      authorize({}, callback){
        callback( {id_token: 'example'} );
      },
    }
}})

const login = () => {
  cy.getCookies().should('be.empty');
  cy.visit('/');
  cy.title()
    .should('equal', 'Solution Catalog and Costing Tool')
    .pause();

  cy.fixture('user').as('loginApiResponse');
  cy.fixture('projects').as('initialProjects');
  cy.fixture('account').as('getAccount');

  cy.server();

  cy.route({
    method: 'POST',
    url: '**/api/*/login',
    status: 200,
    response: '@loginApiResponse',
    delay: 500,
  }).as('getUser');

  cy.route({
    method: 'GET',
    url: '**/api/*/account',
    status: 200,
    response: '@getAccount',
    delay: 500,
  }).as('getAccount');

  cy.route({
    method: 'GET',
    url: '**/api/*/estimates?limit=10&offset=0',
    status: 200,
    response: '@initialProjects',
    delay: 500
  });

  cy.get('[data-cy=GoogleButton]')
    .click()
    .should('be.disabled');

  cy.checkReduxState('login', 'loginInProgress');

  cy.wait('@getUser');

  cy.url().should('include', '/estimates');

  cy.fixture('user').then(user => {
    cy.checkReduxState('login', 'user', user);
  });
};

Cypress.Commands.add('login', () => login());
Cypress.Commands.add('checkReduxState', (store, name, data) => checkReduxState(store, name, data));
Cypress.Commands.add('waitForResource', (name, options = {}) => waitForResource(name, options));
Cypress.Commands.overwrite('visit', (originalFn, url, options) =>
  overwriteFetch(originalFn, url, options),
);

这里的文档:https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/stubbing-spying__google-analytics 或多或少是同一个问题。