【发布时间】:2021-05-07 11:08:38
【问题描述】:
场景:我网站的访问者单击链接并导航到显示一些文本的页面。
问题:我正在尝试测试用户单击链接时是否显示组件。
当我运行调试时,我看到显示的组件,但是,测试失败。任何想法我在测试中缺少什么?
谢谢
组件
首页组件
import React from 'react';
import { Link } from 'react-router-dom';
import styles from './home-page.scss';
const HomePage = () => {
return (
<div
data-qa="home-page"
className={styles.container}
>
<h2 data-qa="home-page-title">Home page</h2>
<div data-qa="content-page-link">
<Link to="/content">Content Page</Link>
</div>
</div>
);
};
export default HomePage;
内容页面组件
import React from 'react';
import { Link } from 'react-router-dom';
import Content from '../content';
import styles from './content-page.scss';
const ContentPage = () => {
return (
<div
data-qa="content-page"
className={styles.container}
>
<h2 data-qa="content-page-title">Content Page</h2>
<Content />
<div data-qa="home-page-link">
<Link to="/">Home Page</Link>
</div>
</div>
);
};
export default ContentPage;
内容组件
import React from 'react';
import styles from './content.scss';
const Content = () => {
return (
<div
data-qa="content"
className={styles.container}
>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
);
};
export default Content;
测试
describe('content page', () => {
it("should navigate to content page", async () => {
await browser.url('http://localhost:9000');
const contentLink = await browser.react$('Link', {
props: {
to: '/content'
}
});
await contentLink.click();
// await browser.debug();
expect(await (await browser.react$('Content')).isDisplayed()).toBe(true);
});
});
INFO webdriver: RESULT { message: 'React element with selector "Content" wasn't found' }
【问题讨论】:
-
如果您在测试结束时摆脱了预期中的额外等待会发生什么?
-
它抛出一个错误,所有 Webdriver IO 命令都返回一个 Promise。 webdriver.io/docs/sync-vs-async/#async-mode
-
到底为什么需要双
await? -
因为我们在异步模式下运行。 webdriver.io/docs/sync-vs-async/#async-mode
标签: reactjs webdriver-io