【问题标题】:I cannot querySelect input of ion-searchbar in Jasmine tests我无法在 Jasmine 测试中查询选择离子搜索栏的输入
【发布时间】:2019-02-03 16:45:20
【问题描述】:

我正在运行一个简单的测试 - 在 Ionic 搜索栏中输入一个值。 我无法在我的 jasmine 测试中获得对搜索栏输入字段的引用。

我已经设置了一个带有单个测试页面的简单的香草 Ionic 4 应用程序。 以下测试均失败。

const el: HTMLElement = fixture.nativeElement;
const input = el.querySelector('input');
expect(input).not.toBeNull();  // FAILS 

const bar = el.querySelector('ion-searchbar');
expect(bar.innerHTML).not.toEqual(''); // FAILS
expect(bar.shadowRoot).not.toBeNull();  // FAILS

我不明白为什么? 我已经在 Ionic 列表上成功编写了其他测试来读取值。 我尝试放入计时器以给组件时间来呈现 html。 如果我在页面上放置一个简单的 HTML 输入字段,它就可以被选中。 我认为 shadow DOM 可能与此处相关,即使当我检查浏览器中的输入字段时它似乎不在 shadow DOM 中,并且标准 document.querySelector 工作正常。

HTML 页面

// test.page.html
<ion-header>
  <ion-toolbar>
    <ion-title>
      Test Page
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content padding>
  <ion-searchbar></ion-searchbar>
</ion-content>

组件类

// test.page.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-test',
  templateUrl: './test.page.html',
  styleUrls: ['./test.page.scss'],
})
export class TestPage {
  constructor() { }
}

测试规范

// test.page.spec.ts
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { TestPage } from './test.page';

describe('TestPage', () => {
  let component: TestPage;
  let fixture: ComponentFixture<TestPage>;

beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ TestPage ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA],
    })
    .compileComponents();

  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(TestPage);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should have a searchbar', () => {
    const el: HTMLElement = fixture.nativeElement;
    const bar = el.querySelector('ion-searchbar');
    fixture.detectChanges();
    expect(bar).not.toBeNull();             // PASSES
    // adding some checks to see what is going on 
    expect(bar.children.length).toBeGreaterThan(0);  // FAILS
    expect(bar.innerHTML).not.toEqual('');  // FAILS
    expect(bar.shadowRoot).not.toBeNull();  // FAILS
  });

  it('should have an input field', () => {
    const el: HTMLElement = fixture.nativeElement;
    const input = el.querySelector('input');
    expect(input).not.toBeNull();           // FAILS
  });

});

【问题讨论】:

    标签: ionic-framework jasmine ionic4


    【解决方案1】:

    这已经晚了一年多,但我认为值得为可能需要它的其他人回答。对于它的价值,我正在运行 @ionic-native/core 4.15 和 @angular/core 5.2.11 和 ionic-angular 3.9.2。

    我不知道为什么const input = el.querySelector('input'); 不适合你。以下代码适用于我:

    const toolbar = fixture.nativeElement.querySelector('ion-toolbar')
    const searchBar = toolbar.querySelector('ion-searchbar');
    const input = searchBar.querySelector('input');
    

    或者,您可以对输入进行更具体的查询:

    const input = searchBar.querySelector('div.searchbar-input-container > input.searchbar-input')
    

    我喜欢在测试中通过console.loging 来查看 HTMLElement 及其组件实例的结构。当我将此添加到我的测试中时:

    console.log(searchBar);
    

    捕获的测试浏览器中的控制台会输出:

    <ion-searchbar class="searchbar searchbar-md searchbar-left-aligned" ng-reflect-placeholder="Besmirch">
        <div class="searchbar-input-container">
            <button class="searchbar-md-cancel button button-md button-clear button-clear-md button-clear-md-dark" clear="" color="dark" ion-button="" mode="md" type="button" ng-reflect-color="dark" ng-reflect-mode="md" ng-reflect-clear=""><span class="button-inner"><ion-icon name="md-arrow-back" role="img" class="icon icon-md" aria-label="arrow back" ng-reflect-name="md-arrow-back"></ion-icon></span>
                <div class="button-effect"></div>
            </button>
            <div class="searchbar-search-icon"></div>
            <input class="searchbar-input" dir="auto" placeholder="Besmirch" type="search" autocomplete="off" autocorrect="off" spellcheck="false">
            <button class="searchbar-clear-icon button button-md button-clear button-clear-md" clear="" ion-button="" type="button" ng-reflect-mode="md" ng-reflect-clear=""><span class="button-inner"></span>
                <div class="button-effect"></div>
            </button>
        </div>
        <button class="searchbar-ios-cancel button button-ios button-clear button-clear-ios" clear="" ion-button="" mode="ios" type="button" ng-reflect-mode="ios" ng-reflect-clear="" tabindex="-1"><span class="button-inner">Cancel</span>
            <div class="button-effect"></div>
        </button>
    </ion-searchbar>
    

    在这个结构中,我们可以看到输入在.searchbar-input-container div中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-19
      • 2016-01-28
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多