我需要获取Pagesource&然后检查pagesource中是否不存在" Hello World"。
我正在尝试下面的方法。
var page_source=browser.getPageSource().then(function (text){
expect(text).not.toContain("hello world");
});
任何建议??
Error trace:
Stack:
Error: Failed expectation
at Object.<anonymous> (C:ProtractorTestcasestest.js:56:38)
at C:Protractornode_modulesjasminewd2index.js:110:25
at new ManagedPromise (C:Protractornode_modulesselenium-webdriverlibpromise.js:1067:7)
at ControlFlow.promise (C:Protractornode_modulesselenium-webdriverlibpromise.js:2396:12)
at schedulerExecute (C:Protractornode_modulesjasminewd2index.js:95:18)
at TaskQueue.execute_ (C:Protractornode_modulesselenium-webdriverlibpromise.js:2970:14)
at TaskQueue.executeNext_ (C:Protractornode_modulesselenium-webdriverlibpromise.js:2953:27)
at asyncRun (C:Protractornode_modulesselenium-webdriverlibpromise.js:2860:25)
at C:Protractornode_modulesselenium-webdriverlibpromise.js:676:7
1 spec, 1 failure
Finished in 85.814 seconds
[18:02:43] I/launcher - 0 instance(s) of WebDriver still running
[18:02:43] I/launcher - internet explorerANY #01 failed 1 test(s)
[18:02:43] I/launcher - overall: 1 failed spec(s)
[18:02:43] E/launcher - Process exited with error code 1
Specs file:
specs: ['./Testcases/test.js'],
尝试此正则表达方式以符合您的要求。
示例:
it("The 'toMatch' matcher is for regular expressions", function() {
var message = "foo bar baz";
expect(message).toMatch(/bar/);
expect(message).toMatch("bar");
expect(message).not.toMatch(/quux/);
});
fyi - tocontain() - 用于在数组中查找项目。
示例:
it("works for finding an item in an Array", function() {
var a = ["foo", "bar", "baz"];
expect(a).toContain("bar");
expect(a).not.toContain("quux");
});
几个建议。
- 您无需解决承诺。茉莉花期望为您
expect(browser.getPageSource()).not.toContain("hello world")
是的,您正在做的是正确的 - 其余的 - 我的意思是茉莉花主张和使用not
是正确的,它应该按预期工作
如果您基于 Angular 使用应用程序,请执行某件事
由于https://github.com/angular/protractor/blob/master/master/docs/timeouts.md#how-to-dis-disable-waiting-for-angular,您应该 disable等待等待角度本身。
示例:
describe('Google Site Verification', () => {
beforeEach(() => {
browser.waitForAngularEnabled(false);
browser.get(browser.baseUrl);
});
afterEach(() => {
browser.waitForAngularEnabled(true);
});
it('should be injected', () => {
const source = browser.getPageSource();
expect(source).toMatch(/google-site-verification/i);
});
});