在量角器中按值选择选项失败

时间:2016-03-15 06:07:58

标签: javascript protractor options

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

Adding a Category

Adding a Subcategory

我正在做的是首先添加类别。我必须刷新页面,因为,只有刷新页面时,添加的类别才会列在子类别选项卡中显示的类别下拉列表中。  因此,刷新页面后,我单击子类别选项卡并尝试填充子类别中的字段。但是,从选项中选择类别时,我的脚本失败了。

以下是我试过的两种方式。

方法1:

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

方法2:

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

两种方法都会产生一个名为

的错误
  

失败:找不到使用定位器的元素......

  • 编辑 -

这是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>

当我使用Metod 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

当我使用Metod 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

1 个答案:

答案 0 :(得分: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');