我们在使用casper.js进行功能测试时遇到了一些问题.
我们两次请求相同的资源,首先使用GET,然后使用POST方法.现在,当等待第二个资源(POST)时,它与第一个资源匹配,并直接转到"then"函数.
我们希望能够在"test"函数中检查HTTP方法,这样我们就可以正确地识别资源.现在我们使用状态代码(res.status),但这并没有完全解决我们的问题,我们真的需要http方法.
// create new email
this.click(xPath('//div[@id="tab-content"]//a[@class="button create"]'));
// GET
this.waitForResource('/some/resource',
function then() {
this.test.assertExists(xPath('//form[@id="email_edit_form"]'), 'Email edit form is there');
this.fill('form#email_edit_form', {
'email_entity[email]': 'test.bruce@im.com',
'email_entity[isMain]': 1
}, true);
// POST
this.waitForResource(
function test(res) {
return res.url.search('/some/resource') !== -1 && res.status === 201;
},
function then() {
this.test.assert(true, 'Email creation worked.');
},
function timeout() {
this.test.fail('Email creation did not work.');
}
);
},
function timeout() {
this.test.fail('Email adress creation form has not been loaded');
});
Run Code Online (Sandbox Code Playgroud)
或者也许有更好的方法来测试这种情况?虽然这是一个功能测试,但我们需要在一次测试中保留所有这些步骤.