Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but object given

I am trying to assert a drop down value under tab li. Here is my code for that:

await searchPage.enterSearch("/html/body/nw-search/nw-text-search/div/div/div/div/div[2]/input","Honda");
await  element.all(by.css("body > nw-search > nw-text-search > div > div > div > div > div.autocomplete-wrapper > ul > li:nth-child(1) > ul li")).then(async function(items) {
              expect(items.length).equals(5);
              await  expect(items[0].getText()).contain("honda: Civic");
            });

However, while running the above code I am getting below error. Even though I am passing a string. I tried both single quotes and double quotes. :

AssertionError: object tested must be an array, a map, an object, a set, a string, or a weakset, but object given

like image 351
Zeus Avatar asked Jul 02 '18 18:07

Zeus


2 Answers

Items is not an Array, it is ElementArrayFinder. Use count method instead of length.

like image 101
Oleksii Avatar answered Nov 13 '22 10:11

Oleksii


This happens when you are returning a set of web elements instead of an array per se. So, you need to loop through the set of web elements and save them individually within a regular array, then make your assertion over this.

Below an example step by step, using Cypress. But it's applicable or can be easily adapted in any other JS framework.

Let's say you have a method, which return the set of element (rows) belonging to a table.

Cypress.Commands.add('getTableData', () => {
  cy.get('[data-test="table-body"]').find('tr').as('table')
  return cy.get('@table')
}) 

Now, in your test you must validate if any value (in this case CODE0000000080) is within your set of elements, but not directly, but after saving them in a regular array.

it('Verify the value is in the table', () => {
    var element = 'CODE0000000080'
    var members = []
    cy.getTableData().each(el => {
       members.push(el.text());
    }).then(() => {
       expect(members).includes(element)
    })
})
like image 1
Vict01 Avatar answered Nov 13 '22 10:11

Vict01