asynchronous - Working with multiple chainer results in cypress
问题描述
Since Cypress can't easily use async/await, in order to use multiple chained values, right now I'm doing this:
const getScrubPosition = () => cy.getState().its('meta').its('scrubPosition');
const getScrollBar = () => cy.getByTestId('SCROLL_BAR').then(([bar]) => bar);
const getGutter = () => cy.getByTestId('GUTTER').then(([bar]) => bar);
// my workaround
const getScrubScrollGutter = () => getScrubPosition().then(initialPos =>
getScrollBar().then(([bar]) =>
getGutter().then(([gutter]) =>
({ initialPos, bar, gutter }))));
it('is ridiculous', () => {
getScrubPosition().then(initialPos => {
getScrollBarWidth().then(initialWidth => {
getScrollContainerWidth().then(containerWidth => {
// do something with initialPos, initialWidth, and containerWidth
});
});
});
it('>90 frames, with current series', () => {
clickSeries(1, 3); // x2x4, 94 frames, including pending
getScrubScrollGutter().then(({ initialPos, bar, gutter }) => {
expectPendingShown(true);
expectScrollBar();
// the bar shouldn't be wider than the gutter
expect(bar.offsetWidth).to.be.lessThan(gutter.offsetWidth);
// the right of the bar should be aligned with the right of the gutter
expect(rightEdgeOf(bar)).to.equal(rightEdgeOf(gutter));
debugger
// the bar shouldn't be to the left of the gutter
expect(bar.offsetLeft).to.be.greaterThan(gutter.offsetLeft);
});
There MUST be a better way to do this, right? The github issue mentioned above talks about things like
cy.get('something')
.then(x => doSomethingTo(x))
.then(y => console.log('please help'))
etc. But I'm having a hard time understanding it -- or, if I do understand it, it doesn't actually pass all the values along to the further contexts (i.e., I don't think you can use x
in the third line of what I just wrote.)
What about Promise.all
?
Yes, I tried that. Please only give a Promise.all
related answer if you have actually been able to use it in Cypress.
Thanks.
解决方案
推荐阅读
- python - 强制 pandas 将类型信息存储在输出文件中
- javascript - JavaScript 项目中的本地依赖项
- html - 如何跨行移动png
- mysql - 计数出现的总和
- xml - libxml2 对包含特殊字符(如“[”、“[”)的元素类型 anyURI 的 xsd 模式验证失败
- datepicker - Odoo 12:如何使用 datepicker 禁用过去的日期?
- javascript - React 中未处理的 Promise Rejection
- nginx - Ingress 和 nginx 配置问题
- elasticsearch - grafana 升级后 Cloudwatch 数据源无法正常工作
- sql-server - 如何在不使用反向函数的情况下反转 SQL Server 中的字符串?