问题
I need to get the pagesource & then check whether the text "hello world" is absent in the pagesource.
I am trying the below method which is failing.
var page_source=browser.getPageSource().then(function (text){
expect(text).not.toContain("hello world");
});
Any suggestions ??
Error trace:
Stack:
Error: Failed expectation
at Object.<anonymous> (C:\Protractor\Testcases\test.js:56:38)
at C:\Protractor\node_modules\jasminewd2\index.js:110:25
at new ManagedPromise (C:\Protractor\node_modules\selenium-webdriver\lib\promise.js:1067:7)
at ControlFlow.promise (C:\Protractor\node_modules\selenium-webdriver\lib\promise.js:2396:12)
at schedulerExecute (C:\Protractor\node_modules\jasminewd2\index.js:95:18)
at TaskQueue.execute_ (C:\Protractor\node_modules\selenium-webdriver\lib\promise.js:2970:14)
at TaskQueue.executeNext_ (C:\Protractor\node_modules\selenium-webdriver\lib\promise.js:2953:27)
at asyncRun (C:\Protractor\node_modules\selenium-webdriver\lib\promise.js:2860:25)
at C:\Protractor\node_modules\selenium-webdriver\lib\promise.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'],
回答1:
Try this regular expression way for your requirement.
Examples:
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()- for finding an item in an Array.
Example:
it("works for finding an item in an Array", function() {
var a = ["foo", "bar", "baz"];
expect(a).toContain("bar");
expect(a).not.toContain("quux");
});
回答2:
Couple of Suggestions.
- You need not resolve the Promise. Jasmine expect does it for you
expect(browser.getPageSource()).not.toContain("hello world")
And yes, what you are doing is correct- The Rest of it - I mean the Jasmine assertion and use of not
is correct and it should work as expected
回答3:
If you use app based on Angular you shuold do something strange.
Due to https://github.com/angular/protractor/blob/master/docs/timeouts.md#how-to-disable-waiting-for-angular you should disable waiting for Angular itself.
Example:
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);
});
});
来源:https://stackoverflow.com/questions/42807340/expect-text-to-be-absent-in-pagesource-protractor