使用赛普拉斯存储元素数组

时间:2020-04-13 21:12:24

标签: arrays cypress

我想存储使用cypress从页面获取的字符串数组。

到目前为止,我有:

def read_last_line(file_path):
    with open(file_path, 'r') as file:
        size_file = os.path.getsize(file_path)
        return_file_empty = " "
        last_line = (list(file)[-1])
        print(last_line)
        if size_file == 0:
            return return_file_empty
        else:
            return last_line

如您所见,我必须为每个项目分别提出请求。但是我想以某种方式存储整个数组,然后发出一个请求。

我已经读过this page on variables and aliases,但感到自己离实现自己想要的目标越来越近。

如何存储使用赛普拉斯命令生成的项目数组?

2 个答案:

答案 0 :(得分:1)

您可以使用.then()代替.each()来检索所有分类为“产品名称”的元素。该参数是一个可迭代的集合,可以将其转换为数组。

参考Array.from

cy.get(".product-name").then(($els) => {

  const texts = Array.from($els, el => el.innerText);

  cy.request("POST", "http://localhost:3000/sale-items", {
    texts
  });
  cy.wait(1000);
});

答案 1 :(得分:0)

cypress提供.each($ elem,index,$ list)方法来迭代每个 数组的元素。

cy.get(".product-name").each(($els, index, $list) => {
   // $list itself is collection of element
   // $elem is each element of array
   // index is postion of element in array
});