javascript - TestCafe firefox fails when page load takes too long
问题描述
I'm pretty new to UI testing and just started exploring testCafe
I'm writing a simple test in testCafe that will go to google, search testCafe, click on the submit button , then click on the first search result that appears and verify if it is the testCafe page:
import {Selector} from 'testcafe';
import {ClientFunction} from 'testcafe';
const getURL = ClientFunction(()=> window.location.href);
fixture `Getting Started`
.page `https://www.google.com/`;
test('Google Search test', async t =>{
await t
.typeText('input[name="q"]','testcafe')
.click('input[type="submit"]')
.click('div.r > a')
.expect(getURL()).eql("https://devexpress.github.io/testcafe/documentation/getting-started/")
})
This test works perfectly with Chrome and Safari, however Firefox is taking too long to load the google home page, and it is somehow not able to find the input[type="submit"]
button. Thus the test fails. It seems to me that the test starts executing even before the pageLoad finishes.
Here is the error that comes in Firefox:
1) The element that matches the specified selector is not visible.
Browser: Firefox 75.0 / macOS 10.14
7 | .page `https://www.google.com/`;
8 |
9 |test('Google Search test', async t =>{
10 | await t
11 | .typeText('input[name="q"]','testcafe')
> 12 | .click('input[type="submit"]')
13 | .click('div.r > a')
14 | .expect(getURL()).eql("https://devexpress.github.io/testcafe/documentation/getting-started/")
15 |
16 |})
at <anonymous> (/Users/goplap/TestCafeTest/tests/sampleTest.js:12:6)
at <anonymous> (/Users/goplap/TestCafeTest/tests/sampleTest.js:9:1)
at <anonymous> (/Users/goplap/TestCafeTest/node_modules/testcafe/src/api/wrap-test-function.js:17:28)
at TestRun._executeTestFn (/Users/goplap/TestCafeTest/node_modules/testcafe/src/test-run/index.js:295:19)
at TestRun.start (/Users/goplap/TestCafeTest/node_modules/testcafe/src/test-run/index.js:345:24)
1/1 failed (18s)
Is there a better way to write this test?
解决方案
See code below:
import {Selector} from 'testcafe';
import {ClientFunction} from 'testcafe';
const getURL = ClientFunction(()=> window.location.href);
fixture `Getting Started`
.page `https://www.google.com/`;
test('Google Search test', async t =>{
const inputBox = Selector('input[name="q"]');
const anchorLinks = Selector(".l");
const gettingStartedText = "Getting Started";
await t
.typeText(inputBox,'testcafe')
.pressKey('enter')
.click(anchorLinks.withText(gettingStartedText))
.expect(getURL()).eql("https://devexpress.github.io/testcafe/documentation/getting-started/")
});
It is good practice to create variables for your selectors, that makes it easy to read. Also instead of clicking on the submit button, All did was hit the enter key. You can read up more here
You can also pass in a command line argument to slow down the test a little bit. You can read up more here as well
推荐阅读
- tensorflow - 'NumpyArrayIterator' 中的输入数据应该有 4 级。你传递了一个形状为数组的数组', (120000, 0)
- flutter - 在颤动中渲染时间后将子小部件的大小获取到父小部件
- java - 如何在 thymeleaf 页面中显示来自 BindingResult.reject("message") 的消息
- matlab - 在 simscape 多体中建模 segway 机器人
- spring-boot - Spring Boot - 本地化到达自定义(DWR)AbstractController 的请求,本地化子线程,LocaleResolver 与 LocaleChangeInterceptor
- python - 如何将二维 numpy 数组中的所有数字转换为字符串?
- spring-boot - Spring Boot Maven 插件 > 2.4.x 构建镜像在 GitLab 注册表上发布
- sql-server - 无法使用特殊主体 'sa' - ant sql 任务
- html - 调整浏览器大小时,我的 div 容器溢出
- java - java.sql.SQLSyntaxErrorException: ORA-01797: 此运算符后面必须跟 ANY 或 ALL