如何通过摩卡和柴匹配在网页上呈现的特定string

我正在使用mocha和chai包在我的Node JS应用程序中构build一个testing套件。 到目前为止,我通过检查页面成功呈现时返回的状态码(200)来validationtesting用例是否合格/不合格。

如何根据我在页面上呈现的内容来validation页面是否合格/不合格。 例如,如果页面显示“欢迎使用Express”,我想匹配“Welcome”或“Express”中的任何一个来validation它是否合格。

以下是我的代码检查状态代码的片段:

describe('Home Page', () => { it('This is the Home page', (done)=> { chai.request(server) .get('/home') .end((error, respond) => { expect(respond.statusCode).to.equal(200); done(); }); }); }); 

作为一个简单的解决scheme,您可以直接使用expect()语句中要匹配的内容/string,如下所示:

 describe('Home Page', () => { it('This is the Home page', (done)=> { chai.request(server) .get('/home') .end((error, respond) => { expect(/Welcome/, done); done(); }); }); });