Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何使用Typescript在量角器自动化中在数组中存储CSS值_Angular_Typescript_Protractor - Fatal编程技术网

Angular 如何使用Typescript在量角器自动化中在数组中存储CSS值

Angular 如何使用Typescript在量角器自动化中在数组中存储CSS值,angular,typescript,protractor,Angular,Typescript,Protractor,我正在用Typescript写量角器自动化脚本。 我有一个作为ElementArrayFinder返回的webelements数组。 我需要将每个元素的“字体权重”存储在单独的数组/映射中并返回它。 我的脚本显示,即使在Push语句之后,数组的长度也是0。有人能帮我做到这一点吗 public getFontWeightOfElements () : promise.Promise<string>[] { let array1 : promise.Promise<stri

我正在用Typescript写量角器自动化脚本。 我有一个作为ElementArrayFinder返回的webelements数组。 我需要将每个元素的“字体权重”存储在单独的数组/映射中并返回它。 我的脚本显示,即使在Push语句之后,数组的长度也是0。有人能帮我做到这一点吗

public getFontWeightOfElements () : promise.Promise<string>[]
{
    let array1 : promise.Promise<string>[] = [];
    //taskList is array of WebElements
    this.taskList.each((element) =>
    {
        element.getCssValue('font-weight')
          .then((value) =>
          {
            array1.push(value);
          })
    })
    console.log(array1.length); // getting 0 each time
    return array1;
publicGetFontWeightOfElements():promise.promise[]
{
让我们安排1:承诺。承诺[]=[];
//taskList是WebElements的数组
此.taskList.each((元素)=>
{
元素。getCssValue('font-weight')
。然后((值)=>
{
阵列1.推送(值);
})
})
console.log(array1.length);//每次得到0
返回阵列1;

问题在于,在异步操作(承诺)得到解决之前,您正在返回数组

getFontWeightOfElements(): Promise<string[]> {
  return this.taskList.map(element => element.getCssValue('font-weight'));
}

非常感谢Aluan。它对我起了作用。再次感谢!对return语句-getFontWeightOfElements()进行了细微的更改:Promise{return this.taskList.map(element=>element.getCssValue('font-weight');}Promise。这一切都不是必需的。
getFontWeightOfElements().then(fontWieghts => {
  fontWieghts.forEach(fontWieght => {
    console.log(fontWeight);
  });
});