期望在pagesource-Protractor中缺少文本

时间:2017-03-15 10:39:41

标签: javascript jasmine webdriver protractor

我需要获取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:\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'],

3 个答案:

答案 0 :(得分:0)

请根据您的要求尝试这种正则表达方式。

示例:

 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");
  });

答案 1 :(得分:0)

一对建议。

  1. 您无需解决承诺。 Jasmine期待为您服务
  2. expect(browser.getPageSource()).not.toContain("hello world")

    是的,你正在做的是正确的 - 剩下的 - 我的意思是Jasmine断言和使用not是正确的,它应该按预期工作

答案 2 :(得分:0)

如果您使用基于 Angular 的应用,则应该执行 strange

由于https://github.com/angular/protractor/blob/master/docs/timeouts.md#how-to-disable-waiting-for-angular,您应该禁用对Angular本身的等待

示例:

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);
    });
});