run testcafe tests from Python

Viewed 65

Summary of my issue: Goal: I have a javascript package that uses testcafe to perform simple search on python.org. I am trying to use this package in python test framework using js2py but it's giving me error when I try 'require("pyCafe").

Expected: able to use the js package in python without issue. My goal is to use testcafe automation scripts from Python, not sure if it's even possible.

Error: Parsing file /private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/fast-glob/out/readers/sync.js: Unexpected token (15:10) at Deps.parseDeps (/private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/module-deps/index.js:519:15) at getDeps (/private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/module-deps/index.js:447:44) at /private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/module-deps/index.js:430:38 at ConcatStream. (/private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/concat-stream/index.js:37:43) at ConcatStream.emit (events.js:327:22) at finishMaybe (/private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/readable-stream/lib/_stream_writable.js:630:14) at endWritable (/private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/readable-stream/lib/_stream_writable.js:638:3) at ConcatStream.Writable.end (/private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/readable-stream/lib/_stream_writable.js:594:41) at DuplexWrapper.onend (/private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/readable-stream/lib/_stream_readable.js:577:10) at Object.onceWrapper (events.js:421:28) {

What I have tried so far:

  1. I have created a UI automation framework in javascript/testCafe and published it internally

  2. created npm package 'pyCafe' of entire javascript/testcafe framework and pushed it to internal artifactory. tested this by running 'npm install pyCafe' which installs just fine but using 'require('pyCafe')' in python gives the above error.

  3. Also tried calling the above package from js code itself which also gives error. {"code":"E1","isTestCafeError":true,"callsite":{"filename":"/Users/xxx/work/newjstest/node_modules/pyCafe/features/search.js","lineNum":23,"callsiteFrameIdx":5,"stackFrames":[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}],"isV8Frames":true},"errStack":"Error: No ad placements found.\n at w (https://media.ethicalads.io/media/client/v1.4.0/ethicalads.min.js:2:12068)\n at https://media.ethicalads.io/media/client/v1.4.0/ethicalads.min.js:2:13187","pageDestUrl":"https://www.python.org/","id":"KgxFSGD"} Code below as I couldn't upload to git.

  4. Pycafe/features/serach.js

var  { Selector, t, ClientFunction } =  require('testcafe');

class SearchPageModel {
    constructor () {
        this.TIMEOUT = 500
        this.search = Selector('#id-search-field')
        this.go = Selector('#submit')
        this.noResult = Selector('p').withExactText('No results found.')
    }
}

class SearchPage {
    constructor() {
        this.model = new SearchPageModel()
    }

    async search(item='webdriver') {
        await t
            .typeText(this.model.search, item)
            .click(this.model.go)
            .expect(this.model.noResult.exists).ok()

    }
}

module.exports = new SearchPage()
  1. index.js
#!/usr/bin/env node

module.exports = {
Search: require('./features/search.js'),
}

  1. package.json

{
  "name": "pyCafe",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "cd tests; testcafe chrome test.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.21.1",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babelify": "^10.0.0",
    "browserify": "^17.0.0",
    "browserify-shim": "^3.8.15",
    "js-yaml": "^3.14.1",
    "minimist": "^1.2.5",
    "testcafe": "^1.18.0",
    "winston": "^3.8.1"
  }
}
  1. test code
var {  t } = require('testcafe');
var {search} = require('../features/search.js')

fixture `Test`
    .page("https://www.python.org/")

test
    .meta({
        'testcase': 'C221913',
    })
    ('Test https://www.python.org/', async t => {
    try {
        console.log('search python.org')
        await search.search()
    } catch (e) {
        console.log('Testcase C221913 failed...')
    }
});

So here are my questions:

  1. Can we call js library from python directly and how.
  2. Is it possible to just invoke browser from python using testcafe? I guess not but checking anyway.
  3. how to use the js package in js correctly using the above example.
1 Answers

You can execute TestCafe tests using any language with the command-line interface. As I see, you are already using it. Code fragment:

"scripts": {
    "test": "cd tests; testcafe chrome test.js"
  },

You correctly run TestCafe tests. You were confused by the Java Script error tracking feature. By default, TestCafe handles JavaScript errors on the tested web page and fails test if it has occurred. https://www.python.org/ has a JavaScript error. enter image description here

TestCafe handles it. To disable this behavior, you can specify the '--skip-js-error' option. For your use case, it needs to change the code as follows:

"scripts": {
    "test": "cd tests; testcafe chrome test.js -e"
  },
Related