【问题标题】:Simple test but invalid locator :-(简单测试但定位器无效:-(
【发布时间】:2017-02-10 02:49:34
【问题描述】:

我有这个测试:

// import {by, element, browser} from "protractor";
describe('intro', () => {
  beforeEach(() => {
    browser.get('');
  });

it('should have multiple pages', () => {
    let buttonOnward = element(by.linkText('Continue'));
    expect(element.all(buttonOnward).count()).toBe(1);
  });
});

并得到这个结果。

1) intro should have multiple pages
  Message:
    Failed: Invalid locator
  Stack:
    TypeError: Invalid locator
        at Object.check [as checkedLocator] (C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\by.js:267:9)
        at WebDriver.findElements (C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver.js:919:18)
        at C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\built\element.js:161:44
        at ManagedPromise.invokeCallback_ (C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1379:14)
        at TaskQueue.execute_ (C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2913:14)
        at TaskQueue.executeNext_ (C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2896:21)
        at asyncRun (C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2775:27)
        at C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:639:7
        at process._tickCallback (internal/process/next_tick.js:103:7)
    From: Task: Run it("should have multiple pages") in control flow
        at Object.<anonymous> (C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:79:14)
        at C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:16:5
        at ManagedPromise.invokeCallback_ (C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1379:14)
        at TaskQueue.execute_ (C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2913:14)
        at TaskQueue.executeNext_ (C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2896:21)
        at asyncRun (C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2775:27)
    From asynchronous test:
    Error
        at Suite.describe (C:\xampp\htdocs\test\intro_spec.ts:11:3)
        at Object.<anonymous> (C:\xampp\htdocs\test\intro_spec.ts:2:1)
        at Module._compile (module.js:556:32)
        at Object.Module._extensions..js (module.js:565:10)
        at Module.load (module.js:473:32)
        at tryModuleLoad (module.js:432:12)

1 spec, 1 failure

我不知道为什么。它真的很简单。我下载了 Jasmine 的类型并检查了这个文件 C:\Users\Patrick\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\by.js

为它定义了一个函数:

文档说该功能也存在。

http://www.protractortest.org/#/api?view=ProtractorBy.prototype.buttonText

$ protractor --version
Version 4.0.9
$ npm -v
3.10.8
$ node -v
v6.7.0

提前感谢您的想法!

【问题讨论】:

    标签: javascript node.js selenium typescript protractor


    【解决方案1】:

    进一步扩展@Gunderson 的答案。主要问题是您使用的是ElementFinderelement() 调用的结果)而不是by 定位器。查看buttonOnward 是如何定义的:

    let buttonOnward = element(by.linkText('Continue'));
    

    现在,您正在使用 buttonOnward,它现在是 ElementFinder 来代替定位器:

    expect(element.all(buttonOnward).count()).toBe(1);
    

    这会导致“无效定位器”错误,这是可以理解的。


    您的意思是使用“by”定位器:

    expect(element.all(by.linkText('Continue')).count()).toBe(1);
    

    【讨论】:

      【解决方案2】:

      我认为您的错误与linkText 定位器完全无关,您的问题在于expect(element.all(buttonOnward).count()).toBe(1);,这是一个无效的定位器。如果你想计算按钮总数,你应该像这样声明你的定位器:

      let buttonOnward = element.all(by.linkText('Continue'));
      expect(buttonOnward.count()).toBe(1);
      

      【讨论】:

        【解决方案3】:

        就我而言,我使用的是browser.driver.findElement。这意味着我使用的是 Selenium API。但是,Selenium API 显然不支持 by.model 定位器。但是,Protractor API 确实包含对by.model locator 的支持,为了使用 Protractor API,我使用了element 函数:

        不起作用:

        //This would not work: 
        //error  E/launcher - Error: TypeError: Invalid locator
        browser.driver.findElement(by.model('login_form.email'))
        

        作品:

        //But this works; note it uses the `element` function 
        //instead of `browser.driver.findElement`
        element(by.model('login_form.email'))
        

        也有效:

        //And this also works; note it uses `browser.driver.findElement`
        //But it passes a different locator; not `by.model`, but `by.id`
        browser.driver.findElement(by.id('#login_form_email'))
        

        注意:

        量角器by.model 定位器最终将通过'ng-model' 前缀call a CSS querySelectorAll。 Protractor 添加by.model 定位器功能是有道理的,因为 Protractor 更侧重于 Angular。

        我假设 Selenium 本身不支持 by.model,因为“模型”定位器是 not listed among Selenium (Java) locators on this page

        • 身份证
        • 姓名
        • 类名
        • CSS
        • Xpath
        • 如何操作

        Nor in this Python Selenium 方法列表。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-06-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-06
          • 1970-01-01
          相关资源
          最近更新 更多