Select 量角器中的值选项失败

Select option by value in Protractor fails

我正在尝试 select 按选项值的选项。在这种情况下,我有类别和子类别。这是我正在使用 Protractor 测试的应用程序。

我在这里做的是首先添加类别。而且我必须刷新页面,因为只有刷新页面,添加的类别才会列在子类别选项卡中显示的类别 drop-down 中。 因此,刷新页面后,我单击子类别选项卡并尝试填写子类别中的字段。但是,当 select 从选项中输入类别时,我的脚本失败了。

这是我试过的两种方法。

方法一:

// Select Category
element.all(by.model('sub_category.category_id')).each(function (eachElement, index)
{
    eachElement.click();
    browser.driver.sleep(500);
    element(by.css('.ng-not-empty > option:nth-child(10)')).click();
    browser.driver.sleep(500);
});

方法二:

var select = element(by.model('sub_category.category_id'));
select.$('[value="Test Supplier Category by Me"]').click();
browser.driver.sleep(500);

两种方法都会报错,

Failed: No element found using locator ...

这是HTML

<select required="required" class="form-control empty ng-pristine ng-empty ng-invalid ng-invalid-required ng-touched" name="category_id" ng-model="sub_category.category_id" ng-options="category.id as category.category for category in categories_index" ng-required="true"><option selected="selected" value="?"></option><option value="string:6" label="Staff Cost">Staff Cost</option><option value="string:7" label="Utility Bill Expenses">Utility Bill Expenses</option><option value="string:8" label="Administrative Expenses">Administrative Expenses</option><option value="string:9" label="Building Rent">Building Rent</option><option value="string:10" label="Professional Charges &amp; Fees">Professional Charges &amp; Fees</option><option value="string:11" label="Vehicle Expenses">Vehicle Expenses</option><option value="string:13" label="Training &amp; Workshop Cost">Training &amp; Workshop Cost</option><option value="string:14" label="Marketing &amp; Promotional Expenses">Marketing &amp; Promotional Expenses</option><option value="string:15" label="Printing &amp; Supplies">Printing &amp; Supplies</option><option value="string:16" label="Fixed Assets">Fixed Assets</option><option value="string:17" label="Finance Cost">Finance Cost</option><option value="string:18" label="Statutory Payments">Statutory Payments</option><option value="string:19" label="Overseas Travelling Expenses">Overseas Travelling Expenses</option><option value="string:20" label="Test Supplier Category By Me">Overseas Travelling Expenses</option></select>

当我使用方法 1 时,出现以下错误。

1) Protractor Demo App should have a title
  Message:
    Failed: No element found using locator: By.cssSelector(".ng-not-empty > opti
on:nth-child(10)")
  Stack:
    NoSuchElementError: No element found using locator: By.cssSelector(".ng-not-
empty > option:nth-child(10)")
        at new bot.Error (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protr
actor\node_modules\selenium-webdriver\lib\atoms\error.js:108:18)
        at C:\Users\Manuli\AppData\Roaming\npm\node_modules\protractor\lib\eleme
nt.js:674:15
        at goog.async.run.processWorkQueue (C:\Users\Manuli\AppData\Roaming\npm\
node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:12
4:15)
        at process._tickCallback (node.js:406:9)
    Error
        at [object Object].ElementArrayFinder.applyAction_ (C:\Users\Manuli\AppD
ata\Roaming\npm\node_modules\protractor\lib\element.js:380:21)
        at [object Object].ElementArrayFinder.(anonymous function) [as click] (C
:\Users\Manuli\AppData\Roaming\npm\node_modules\protractor\lib\element.js:78:17)

        at [object Object].ElementFinder.(anonymous function) [as click] (C:\Use
rs\Manuli\AppData\Roaming\npm\node_modules\protractor\lib\element.js:709:7)
        at D:\My Work\Protractor\Financial\supplierCategories.js:75:62
        at C:\Users\Manuli\AppData\Roaming\npm\node_modules\protractor\lib\eleme
nt.js:499:23
        at Array.map (native)
        at C:\Users\Manuli\AppData\Roaming\npm\node_modules\protractor\lib\eleme
nt.js:498:20
    From: Task: Run it("should have a title") in control flow
        at Object.<anonymous> (C:\Users\Manuli\AppData\Roaming\npm\node_modules\
protractor\node_modules\jasminewd2\index.js:81:14)
        at attemptAsync (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protra
ctor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1916:24)
        at QueueRunner.run (C:\Users\Manuli\AppData\Roaming\npm\node_modules\pro
tractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1871:9)
        at QueueRunner.execute (C:\Users\Manuli\AppData\Roaming\npm\node_modules
\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1859:10)
        at Spec.Env.queueRunnerFactory (C:\Users\Manuli\AppData\Roaming\npm\node
_modules\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:697:35
)
        at Spec.execute (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protra
ctor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:359:10)
        at Object.fn (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protracto
r\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:2479:37)
        at attemptAsync (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protra
ctor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1916:24)
        at QueueRunner.run (C:\Users\Manuli\AppData\Roaming\npm\node_modules\pro
tractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1871:9)
        at QueueRunner.execute (C:\Users\Manuli\AppData\Roaming\npm\node_modules
\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1859:10)
    From asynchronous test:
    Error
        at Suite.<anonymous> (D:\My Work\Protractor\Financial\supplierCategories
.js:2:2)
        at addSpecsToSuite (C:\Users\Manuli\AppData\Roaming\npm\node_modules\pro
tractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:833:25)
        at Env.describe (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protra
ctor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:802:7)
        at jasmineInterface.describe (C:\Users\Manuli\AppData\Roaming\npm\node_m
odules\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:3375:18)

        at Object.<anonymous> (D:\My Work\Protractor\Financial\supplierCategorie
s.js:1:1)

1 spec, 1 failure
Finished in 28.377 seconds
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 failed 1 test(s)
[launcher] overall: 1 failed spec(s)
[launcher] Process exited with error code 1

当我使用方法 2 时,出现以下错误。

1) Protractor Demo App should have a title
  Message:
    Failed: No element found using locator: By.cssSelector("[value=\"Test Suppli
er Category by Manuli\"]")
  Stack:
    NoSuchElementError: No element found using locator: By.cssSelector("[value=\
"Test Supplier Category by Manuli\"]")
        at new bot.Error (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protr
actor\node_modules\selenium-webdriver\lib\atoms\error.js:108:18)
        at C:\Users\Manuli\AppData\Roaming\npm\node_modules\protractor\lib\eleme
nt.js:674:15
        at goog.async.run.processWorkQueue (C:\Users\Manuli\AppData\Roaming\npm\
node_modules\protractor\node_modules\selenium-webdriver\lib\goog\async\run.js:12
4:15)
        at process._tickCallback (node.js:406:9)
    Error
        at [object Object].ElementArrayFinder.applyAction_ (C:\Users\Manuli\AppD
ata\Roaming\npm\node_modules\protractor\lib\element.js:380:21)
        at [object Object].ElementArrayFinder.(anonymous function) [as click] (C
:\Users\Manuli\AppData\Roaming\npm\node_modules\protractor\lib\element.js:78:17)

        at [object Object].ElementFinder.(anonymous function) [as click] (C:\Use
rs\Manuli\AppData\Roaming\npm\node_modules\protractor\lib\element.js:709:7)
        at Object.<anonymous> (D:\My Work\Protractor\Financial\supplierCategorie
s.js:81:59)
        at C:\Users\Manuli\AppData\Roaming\npm\node_modules\protractor\node_modu
les\jasminewd2\index.js:96:23
        at new wrappedCtr (C:\Users\Manuli\AppData\Roaming\npm\node_modules\prot
ractor\node_modules\selenium-webdriver\lib\goog\base.js:2468:26)
        at controlFlowExecute (C:\Users\Manuli\AppData\Roaming\npm\node_modules\
protractor\node_modules\jasminewd2\index.js:82:18)
    From: Task: Run it("should have a title") in control flow
        at Object.<anonymous> (C:\Users\Manuli\AppData\Roaming\npm\node_modules\
protractor\node_modules\jasminewd2\index.js:81:14)
        at attemptAsync (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protra
ctor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1916:24)
        at QueueRunner.run (C:\Users\Manuli\AppData\Roaming\npm\node_modules\pro
tractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1871:9)
        at QueueRunner.execute (C:\Users\Manuli\AppData\Roaming\npm\node_modules
\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1859:10)
        at Spec.Env.queueRunnerFactory (C:\Users\Manuli\AppData\Roaming\npm\node
_modules\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:697:35
)
        at Spec.execute (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protra
ctor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:359:10)
        at Object.fn (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protracto
r\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:2479:37)
        at attemptAsync (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protra
ctor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1916:24)
        at QueueRunner.run (C:\Users\Manuli\AppData\Roaming\npm\node_modules\pro
tractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1871:9)
        at QueueRunner.execute (C:\Users\Manuli\AppData\Roaming\npm\node_modules
\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1859:10)
    From asynchronous test:
    Error
        at Suite.<anonymous> (D:\My Work\Protractor\Financial\supplierCategories
.js:2:2)
        at addSpecsToSuite (C:\Users\Manuli\AppData\Roaming\npm\node_modules\pro
tractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:833:25)
        at Env.describe (C:\Users\Manuli\AppData\Roaming\npm\node_modules\protra
ctor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:802:7)
        at jasmineInterface.describe (C:\Users\Manuli\AppData\Roaming\npm\node_m
odules\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:3375:18)

        at Object.<anonymous> (D:\My Work\Protractor\Financial\supplierCategorie
s.js:1:1)

1 spec, 1 failure
Finished in 27.754 seconds
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 failed 1 test(s)
[launcher] overall: 1 failed spec(s)
[launcher] Process exited with error code 1

首先,我在发布的 HTML 中没有看到 Test Supplier Category by Me 选项。

此外,您似乎正在检查 value 属性,但应该检查选项的文本。这可以通过 XPath 定位器完成:

element(by.xpath("//option[. = 'Utility Bill Expenses']")).click();

但是,我真的会考虑使用这里提出的 Select 包装器:

那么,代码就这么简单:

var SelectWrapper  = require('select-wrapper');
var mySelect = new SelectWrapper(by.model('sub_category.category_id'));

mySelect.selectByText('Utility Bill Expenses');