简体   繁体   English

试图从带有 cypress 的对象数组中获取 id

[英]Trying to get an id from an array of objects with cypress

I have to find a id from an array of objects with id, emails, birthday, etc我必须从具有 id、电子邮件、生日等的对象数组中找到一个 id
but i end up getting in the way但我最终妨碍了我

I thought of getting the index returned from the array, that has the right email/user (ig: c@c.com), and then accessing and returning the id, or something like that我想从数组返回索引,该索引具有正确的电子邮件/用户(ig:c@c.com),然后访问并返回 id,或类似的东西

How can i do it properly?我怎样才能正确地做到这一点?

Cypress.Commands.add('getId', (user, passwd) => {
    // user = c@c.com
    let arr = []
    cy.getToken(user, passwd).then(token => {
        cy.request({
            method: 'GET',
            url:'/users',
            headers:{ Authorization: `Bearer ${token}` }
        }).its('body.data.data').then((list) => Cypress._.map(list, 'email')).should('contain', 'c@c.com')
            .then(res => arr.push(res))
    }).then(() => {
       index = cy.log(arr.indexOf("c@c.com"))
       return index
    })//.its('body.id').then(id => {
        //return id
    //})
})

but this index return -1, and if i do cy.log(arr) returns the proper array, so i can't test if i can access the body.id by it但是这个索引返回-1,如果我这样做cy.log(arr)返回正确的数组,所以我无法测试我是否可以通过它访问 body.id

在此处输入图像描述

在此处输入图像描述

My getToken code:我的 getToken 代码:

    Cypress.Commands.add('getToken', (user, passwd) => {
    cy.request({
        method: 'POST',
        url: '/auth/login',
        body: {  
            email: user,
            password: passwd
        }
    }).its('body.data.token').should('not.be.empty') 
    .then(token => {
       return token
    })
} )

cy.log() yields null. cy.log()产生 null。 Try just returning the value instead of the one assigned.尝试只返回值而不是分配的值。

.then(() => {
  return arr.indexOf("c@c.com")
});
...

You have your results array nested within another array.您的结果数组嵌套在另一个数组中。

See screen grab shows [Array(8)] and item 0 is ['Leon', ...请参阅屏幕抓取显示[Array(8)]和项目 0 是['Leon', ...

So either:所以要么:

Cypress.Commands.add("getId", (user, passwd) => {

  let arr = []
  cy.getToken(user, passwd)
    .then((token) => {
      cy.request({
        method: "GET",
        url: "/users",
        headers: { Authorization: `Bearer ${token}` },
      })
      .its("body.data.data")
      .then((list) => Cypress._.map(list, "email"))
      .should("contain", "c@c.com")
      .then((res) => arr.push(res));
    })
    .then(() => {
      index = arr[0].indexOf("c@c.com")
      cy.log(index)
      return index;
    }); 
})

or:或者:

Cypress.Commands.add("getId", (user, passwd) => {

  cy.getToken(user, passwd)
    .then((token) => {
      cy.request({
        method: "GET",
        url: "/users",
        headers: { Authorization: `Bearer ${token}` },
      })
      .its("body.data.data")
      .then((list) => Cypress._.map(list, "email"))
      .should("contain", "c@c.com")
    })
    .then(arr => {
      index = arr.indexOf("c@c.com")
      cy.log(index)
      return index;
    }); 
})

Also .then((list) => Cypress._.map(list, "email")) is dubious, the "email" is supposed to be a function. .then((list) => Cypress._.map(list, "email"))也是可疑的,“email”应该是 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM