您如何以编程方式与赛普拉斯的 Angular/NGRX 交互? cypress 文档似乎仅指 React:https://www.cypress.io/blog/2018/11/14/testing-redux-store/
// expose store when run in Cypress
if (window.Cypress) {
window.store = store
}
cy
.window()
.its('store')
.invoke('dispatch', { type: 'ADD_TODO', text: 'Test dispatch' })
// check if the app has updated its UI
这就是 React 方法;那么 Angular 呢?
在 Angular 中几乎是一样的。在您的
AppComponent
或任何您拥有商店的地方,您可以执行以下操作:
// Expose the store
@Component({...})
export class AppComponent {
constructor(private store: Store<AppState>){
if(window.Cypress){
window.store = this.store;
}
}
}
然后您可以创建自己的 Cypress 实用程序:
function dispatchAction(action: Action): Cypress.Chainable<any> {
return cy.window().then(w => {
const store = w.store;
store.dispatch(action);
});
}
最后你可以在赛普拉斯测试中使用它:
dispatchAction(new MyAction()).then(() => {
// Assert the side effect of your action
// ...
// cy.get('.name').should('exist');
});
您可以像这样访问状态:
import { firstValueFrom } from 'rxjs';
cy.window().its('store').then((store: any) => {
firstValueFrom(store.source).then((state) => {
cy.log('got state', state);
});
});