当有2个`catch`

时间:2018-07-08 23:38:50

标签: javascript

    elementX.isPresent()
        .then(() => {

              elementX.all(by.cssContainingText('option', text)).click()

                  .catch(error => {
                        throw {message: "Unable to select text in dropdown box"}
                    })

        })
        .catch (error => {
                throw {message : "element is not present"}

        })

我想抛出2条不同的错误消息1.如果缺少元素2.下拉菜单中缺少给定的文本。

如果缺少元素,则正确引发错误。但是,如果存在element,并且给定的文本丢失,则仍然会抛出“ element not present”。应该如何更改代码以引发其他错误消息?

我尝试了

  elementX.isPresent()
 .then(
    _ => elementX.all(by.cssContainingText('option', text)).click()
          .then(() => {
                     console.log("hello")
          })
         .catch(_ => { throw {message: "Unable to select text in dropdown box"} }),
    _ => { throw {message : "element is not present"} }
  )

如此处建议的那样,该方法不起作用-仅返回最后一个捕获消息

1 个答案:

答案 0 :(得分:2)

最简单的例子是:

elementX.isPresent()
  .then(
    _ => elementX.all(by.cssContainingText('option', text)).click()
           .catch(_ => { throw {message: "Unable to select text in dropdown box"} }),
    _ => { throw {message : "element is not present"} }

更新

您可以在控制台中测试的示例:

elementIsPresent.then(
  _ => Promise.reject().then(_ => {}).catch(_ => { throw { e: '1' } }),
  _ => { throw { e: '2' } }
)

在执行上述代码之前,请设置以下内容:

elementIsPresent = Promise.resolve()

执行以上代码段的结果将是拒绝{ e: '1' }。然后,您可以设置以下内容:

elementIsPresent = Promise.reject()

如果您设置了此代码段,则应用了该代码段,结果将被{ e: '2' }拒绝。