我遇到了一个问题,即 cypress 没有在我的应用程序的其他部分输入日期。 它确实在第一部分打字,但在第二部分没有打字,即使代码相同。
这是我的柏树测试:
it.only("Inputing expiry date", () => {
Cypress.on("uncaught:exception", (err, runnable) => {
return false;
});
cy.get("[data-cy=input-expiry]")
.type("20/01/2022", { force: true })
.click({ force: true })
.type("{enter}", { force: true })
.trigger("input");
});
这是我的反应组件:
<input {...props}
className={this.getInputClass()}
placeholder={this.props.placeholder}
onChange={(event)=>this.onChange(event)}
ref="myRef"
onKeyPress={(event) => this.handleKeyPress(event)}
onBlur={() => {
this.setState({blur:true},this.checkValid(this.state.value));
}}
readOnly
data-cy={this.props.cydatepicker}
/>
这就是我使用组件的方式:
<DatePicker
className="expiryDBAS"
defaultDate={
this.state.input.expiry
? moment(this.state.input.expiry)
: defaultYear
}
setDefaultDate={!!this.state.input.expiry}
value={
this.state.input.expiryDB
? this.buildDateValue(this.state.input.expiryDB)
: ""
}
selected={this.state.input.expiryDB}
onChange={(value) => this.updateInputDate("expiryDB", value)}
peekNextMonth
showMonthDropdown
showYearDropdown
dropdownMode="select"
placeholder="Expiry Date"
minDate={moment()}
validation={[required]}
cydatepicker="input-expiry"
/>;
更新:
我尝试使用此代码,但它仍然不允许我设置输入字段的值。
.then(elem => {
elem.val("20/01/2022")})
答案 0 :(得分:1)
我有这样的解决方案:
it.only("Selecting expiry date", () => {
cy.get("[data-cy=input-expiry]")
.then(elem => {
elem.val(expiryDate);
})
.trigger('change')
.type("{esc}", {
force: true,
});
});