使用reduce和Promises在Nightmare js中循环播放

时间:2016-06-26 20:34:32

标签: javascript es6-promise nightmare

我尝试加载1个网址,然后从选择菜单中选择所有项目,下载多个银行对帐单。

我按照指南here循环使用reduce。

部分代码:

nightmare:actions .evaluate() fn on the page +3ms

reduce it: option=1:accepted:1 index=0
reduce it: option=3:accepted:3 index=1
reduce it: option=4:accepted:4 index=2

nightmare:actions .wait() for select[name="acctRef"] element +3ms
nightmare:actions .select() select[name="acctRef"] +4ms
nightmare:actions .click() on input[type="submit"] +2ms
nightmare:actions .wait() for td a img element +4ms
nightmare:actions .click() on td a +7s

after download. info => {
"filename": "ImageRequestor.pdf",
"mimetype": "application/pdf",
"receivedBytes": 172446,
"totalBytes": 172446,
"path": "/Users/cdock/Desktop/bank-statements/tmp/bankPDF_0",
"state": "completed"
}

nightmare:actions .wait() for select[name="acctRef"] element +2s
nightmare:actions .select() select[name="acctRef"] +1ms
nightmare:actions .click() on input[type="submit"] +1ms
nightmare:actions .wait() for td a img element +7s
nightmare:actions .click() on td a +2ms

after download. info => {
"filename": "ImageRequestor.pdf",
"mimetype": "application/pdf",
"receivedBytes": 172446,
"totalBytes": 172446,
"path": "/Users/cdock/Desktop/bank-statements/tmp/bankPDF_0",
"state": "completed"
}

nightmare:actions .wait() for select[name="acctRef"] element +1s
nightmare:actions .select() select[name="acctRef"] +1ms
nightmare:actions .click() on input[type="submit"] +1ms
nightmare:actions .wait() for td a img element +6s
nightmare:actions .click() on td a +2ms

after download. info => {
"filename": "ImageRequestor.pdf",
"mimetype": "application/pdf",
"receivedBytes": 172446,
"totalBytes": 172446,
"path": "/Users/cdock/Desktop/bank-statements/tmp/bankPDF_0",
"state": "completed"
}

运行后,tmp文件夹中有一个文件,它对应于第一个选择选项。

此外,文件名为bankPDF_0。

所以选项和索引不是在嵌套的恶梦部分内迭代,尽管它们在reduce回调中...

以下是一些控制台输出:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()


        let k = Draw(frame: CGRect(
            origin: CGPoint(x: 50, y: 50),
            size: CGSize(width: 100, height: 100)))

        k.draw(CGRect(
            origin: CGPoint(x: 50, y: 50),
            size: CGSize(width: 100, height: 100)));
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
class Draw: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        let h = rect.height
        let w = rect.width
        var color:UIColor = UIColor.yellow()

        var drect = CGRect(x: (w * 0.25),y: (h * 0.25),width: (w * 0.5),height: (h * 0.5))
        var bpath:UIBezierPath = UIBezierPath(rect: drect)

        color.set()
        bpath.stroke()

        print("it ran")

        NSLog("drawRect has updated the view")

    }

}

使用reduce / promises循环我做错了什么?

1 个答案:

答案 0 :(得分:0)

Answered here and here

There is a bug in nightmare-inline-download library which, when using .download() multiple times, results in multiple event handlers, forcing the downloaded file to always be handled by the first handler attached.