【问题标题】:How to correctly waitFor() a saveScreenShot() end of execution如何正确 waitFor() a saveScreenShot() 结束执行
【发布时间】:2013-10-17 21:51:20
【问题描述】:

这是我完整的第一个工作测试:

var expect = require('chai').expect;
var assert = require('assert');
var webdriverjs = require('webdriverjs');
var client = {};
var webdriverOptions = {
    desiredCapabilities: {
       browserName: 'phantomjs'
    },
    logLevel: 'verbose'
};

describe('Test mysite', function(){
    before(function() {
        client = webdriverjs.remote( webdriverOptions );
        client.init();
    });
    var selector = "#mybodybody";
    it('should see the correct title', function(done) {
       client.url('http://localhost/mysite/')
             .getTitle( function(err, title){
                expect(err).to.be.null;
                assert.strictEqual(title, 'My title page' );
             })
             .waitFor( selector, 2000, function(){ 
                client.saveScreenshot( "./ExtractScreen.png" );
             })
             .waitFor( selector, 7000, function(){ })
             .call(done);
    });
    after(function(done) {
       client.end(done);
    });
});

好的,它做的不多,但是在工作了很多小时以正确设置环境之后,它通过了。现在,我让它工作的唯一方法是使用 waitFor() 方法并调整延迟。它有效,但我仍然不明白如何确定等待将 png 文件保存在磁盘上。由于我将处理测试订单,我最终会在安全保存文件之前从测试脚本中挂断。

现在,我该如何改进此屏幕保存顺序并避免丢失我的屏幕截图?

【问题讨论】:

    标签: webdriver phantomjs webdriver-io


    【解决方案1】:

    由于您使用的是 webdriverjs 的 chaning 功能,因此在 waitFor 函数中使用回调是错误的。

    函数saveScreenshot 也被链接。所以正确的方法如下:

    it('should see the correct title', function(done) {
       client.url('http://localhost/mysite/')
             .getTitle( function(err, title){
                expect(err).to.be.null;
                assert.strictEqual(title, 'My title page' );
             })
             .waitFor( selector, 2000) 
             .saveScreenshot( "./ExtractScreen.png" )
             .call(done);
    });
    

    【讨论】:

    • 问题发布已经 2 年了。我接受这个答案,直到我测试它,我现在远离这种用法。
    猜你喜欢
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2012-01-20
    相关资源
    最近更新 更多