如果您使用的是材料形式,请在Angular中使用cypress进行选择

Att*_*s29 4 angular-material angular cypress

我有一个看起来像这样的表格:

<form class="col s12" materialize [formGroup]="newUserForm">
...
    <div class="row">
    <div class="input-field col m4 s12">
        <select formControlName="genderBound" materialize="material_select" id="gender" name="test">
            <option value="" disabled selected name="chooseGender">Choose gender</option>
            <option *ngFor="let gender of genders">{{gender}}</option>
        </select>
        <label for="gender">Gender</label>
    </div>
...
Run Code Online (Sandbox Code Playgroud)

当我尝试使用cypress选择下拉菜单时,它告诉我它不可见。当我遵循赛普拉斯提供的解释性URL时,它建议我{force: true}在单击内使用。这允许我的测试通过,但实际上似乎从未选择过这些项目。

我还遵循此处提供的解决方案,并在实际选项上实现了jQuery单击(请注意,我的select和option标记不是md-select和md-option标记)

cypress目录中的sample.spec.js:

...
it('signs up a new user', () =>{
    cy.get('button[id=new-account-button]').click();
    cy.get('input[id=affiliation]').type(affiliation);
    cy.get('input[id=password]').type(pass);
    cy.get('input[id=userName]').type(name);
    cy.get('input[id=email]').type(email);

    //Now the options
    cy.get('[name="chooseGender"]').click({force: true});
    cy.get('option').contains("Female").then(option =>{
      cy.wrap(option).contains("Female");
      option[0].click();
    });
...
Run Code Online (Sandbox Code Playgroud)

我想有两件事我不太了解:

  1. 为什么没有真正选择一个选项?
  2. 尽管如此,为什么测试仍通过?

我在下面提供了我的确切问题的回购协议:

git clone https://github.com/Atticus29/dataJitsu.git
cd dataJitsu
git checkout cypress-SO
Run Code Online (Sandbox Code Playgroud)

在/ src / app中制作一个api-keys.ts文件,并在其中填充以下文本

npm install
ng serve
Run Code Online (Sandbox Code Playgroud)

(在单独的终端选项卡中)

npm run e2e
Run Code Online (Sandbox Code Playgroud)

api-keys.ts:

export var masterFirebaseConfig = {
    apiKey: "AIzaSyCaYbzcG2lcWg9InMZdb10pL_3d1LBqE1A",
    authDomain: "dataJitsu.firebaseapp.com",
    databaseURL: "https://datajitsu.firebaseio.com",
    storageBucket: "",
    messagingSenderId: "495992924984"
  };

export var masterStripeConfig = {
  publicApiTestKey: "pk_test_NKyjLSwnMosdX0mIgQaRRHbS",
  secretApiTestKey: "sk_test_6YWZDNhzfMq3UWZwdvcaOwSa",
  publicApiKey: "",
  secretApiKey: ""
};
Run Code Online (Sandbox Code Playgroud)

Enr*_*ers 6

对于Angular 7+中的mat-select,您必须使用promise等待模态cdk-overlay中的选项可用。

这是一个可重用的辅助函数:

function selectMaterialDropDown(formControlName, selectOption) {
  cy.get(`[formcontrolname="${formControlName}"]`).click().then(() => {
    cy.get(`.cdk-overlay-container .mat-select-panel .mat-option-text`).should('contain', selectOption);
    cy.get(`.cdk-overlay-container .mat-select-panel .mat-option-text:contains("${selectOption}")`).first().click().then(() => {
      // After click, mat-select should contain the text of the selected option
      cy.get(`[formcontrolname="${formControlName}"]`).contains(selectOption);
    });
  });
}
Run Code Online (Sandbox Code Playgroud)

通话功能:

selectMaterialDropDown('myMatSelectControlName', 'OptionTextToSelect');


Ric*_*sen 5

我发现以下测试可与您的存储库配合使用(最新提交353633c),

describe('Test Gender Select', () => {

  before(function () {
    cy.visit('http://localhost:4200/')
  })

  it('signs up a new user', () => {
    cy.get('button').contains('Create New Account').click();

    cy.get('#gender').select('Female', {
      force: true
    });
    cy.get('#gender').contains('Female')
  });
});
Run Code Online (Sandbox Code Playgroud)

您可以从赛普拉斯的测试运行器中看到它确实选择了“ 女性”选项,因此我认为它涵盖了原始测试的目的。

如果我尝试使用click()像这样

cy.get('#gender').click({
  force: true
});
Run Code Online (Sandbox Code Playgroud)

柏树发出消息

CypressError:无法在元素上调用cy.click()。请改用cy.select()命令更改该值。

因此,遵循此说明并使用

cy.get('#gender').select('Female');
Run Code Online (Sandbox Code Playgroud)

给出以下有关可见性的错误消息,这对于select使用材质设计(角度材质和材质化)似乎是标准的。

CypressError:超时,重试:cy.select()失败,因为此元素不可见...解决此问题,或使用{force:true}禁用错误检查。

所以{ force: true }在上使用选项cy.select()可解决此问题。

我知道发生可见性问题是因为材料设计通过选项列表覆盖了父项,并且赛普拉斯基于父项使用了可见性标准(请参见此问题),尽管现在强制选项适用(对于Cypress v3.0.3)。