I am trying to figure out if it is possible to run a TestCafe test from within the context of an Express.js app. My idea was to create a form, accept input (like a UPC code), and from clicking submit on the form it would cause a TestCafe test to spawn in headless mode (somewhere) and load that upc into this page https://www.ebay.com/sh/research . From there, TestCafe would obtain the results from the page and stick them in my database.
I placed the test in a file named tests.js and put it like this routes/tests.js
// routes/test.js
const eBayScrapingHub = (upc) => {
test('Scraper 1', async t => {
await t
.wait(3000)
.typeText(Selector('input[name="productId"]'), upc)
.click(Selector('div#mainContent button[type="submit"].search-button.btn.btn--medium.btn--primary'))
.wait(5000)
console.log('finished!')
});
}
module.exports = eBayScrapingHub;
The routes file itself, named ebay.js
// routes/ebay.js
const { eBayScrapingHub } = require('./tests');
const { Selector } = require('testcafe');
// Process Ebay Form
router.post('/', ensureAuthenticated, (req, res)=>{
var query = req.body.input;
console.log('This is your query: ', query);
fixture `Ebay Seller Hub Scraper`
.page `https://www.ebay.com/sh/research`;
eBayScrapingHub(query);
}
});
The form
// views/ebay/add.handlebars
<div class="card card-body">
<h3>Add UPC</h3>
<p>Input a UPC Code to search eBay</p>
<form action="/ebay" method="post">
<div class="form-group">
<label for="input">Input</label>
<input type="text" class="form-control" name="input" required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
Unfortunately, all sorts of errors appear the moment I tried doing this.
I tried importing the selector with import { Selector } from 'testcafe' and got this error:
import { Selector } from 'testcafe';
^
SyntaxError: Unexpected token {
So I changed it to const { Selector } = require('testcafe');
to make the error go away.
Now it won't work due to not recognizing "fixture":
ReferenceError: fixture is not defined
Can anyone let me know if this is possible or how to do this right? Thank you!
Do not mix TestCafe fixture/test definitions with your custom code. Keep it separately.
Take a look at the Programming Interface article that describes how to run TestCafe tests from your custom Node.js code.
For others whom this may be useful for in the future, I have solved this in the following way:
In express, the /routes file:
// /routes/testcafe.js
const express = require('express');
const router = express.Router();
const createTestCafe = require('testcafe');
var testcafe = null;
router.get('/testcafe', (req, res) => {
createTestCafe('localhost', 1337, 1338)
.then(tc => {
testcafe = tc;
const runner = testcafe.createRunner();
console.log('About to start tests/myTest.js!');
return runner
.src(['/root/user/express_server/routes/tests/myTest.js'])
.browsers(['firefox:headless'])
.run()
})
.then(failedCount => {
console.log('Tests failed: ' + failedCount);
testcafe.close()
});
});
module.exports = router;
After placing this above code in the file, it completely fired the test successfully by just sending a get request to my server at address like this:
http://express_server_ip:port/testcafe
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.