簡體   English   中英

從 Python 運行 testcafe 測試

[英]run testcafe tests from Python

我的問題摘要:目標:我有一個 javascript package,它使用 testcafe 在 python.org 上執行簡單搜索。 我正在嘗試使用 js2py 在 python 測試框架中使用這個 package 但是當我嘗試'require(“pyCafe”)時它給了我錯誤。

預期:能夠在 python 中毫無問題地使用 js package。 我的目標是使用 Python 中的 testcafe 自動化腳本,不確定是否可行。

錯誤:解析文件 /private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/fast-glob/out/readers/sync.js:在 Deps.parseDeps (/private/var/) 出現意外的令牌 (15:10)在 getDeps (/private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/module-deps/ js:447:44) 在 /private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/module-deps/index.js:430:38 在 ConcatStream。 (/private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/concat-stream/index.js:37:43) 在 ConcatStream.emit (events.js:327:22) 在 finishMaybe (/private/var /folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/readable-stream/lib/_stream_writable.js:630:14)在endWritable(/private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlannodes/-可讀模塊/ /lib/_stream_writable.js:638:3) 在 ConcatStream.Writable.end (/private/var/folders/1n/tr0826l14yv89b5ypl4sbsc46qb010/T/tmpgnnrdlan/node_modules/readable-stream/lib/_stream_writable.js:594:41) 在Object.onceWrapper (events.js:421:28)

到目前為止我已經嘗試過:

  1. 我在 javascript/testCafe 中創建了一個 UI 自動化框架並在內部發布

  2. 創建整個 javascript/testcafe 框架的 npm package 'pyCafe' 並將其推送到內部工件。 通過運行 'npm install pyCafe' 進行測試,它安裝得很好,但在 python 中使用 'require('pyCafe')' 會出現上述錯誤。

  3. 還嘗試從 js 代碼本身調用上述 package ,這也會產生錯誤。 {"code":"E1","isTestCafeError":true,"callsite":{"filename":"/Users/xxx/work/newjstest/node_modules/pyCafe/features/search.js","lineNum":23 ,"callsiteFrameIdx":5,"stackFrames":[{},{},{},{},{},{},{},{},{},{},{},{},{} ,{},{},{},{},{},{},{}],"isV8Frames":true},"errStack":"錯誤:未找到廣告展示位置。\n at w ( https:/ /media.ethicalads.io/media/client/v1.4.0/ethicalads.min.js:2:12068 )\n 在https://media.ethicalads.io/media/client/v1.4.0/ethicalads.min。 js:2:13187","pageDestUrl":"https://www.python.org/","id":"KgxFSGD"}下面的代碼,因為我無法上傳到 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. 測試代碼
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...')
    }
});

所以這是我的問題:

  1. 我們可以直接從 python 調用 js 庫嗎?如何。
  2. 是否可以使用 testcafe 從 python 調用瀏覽器? 我猜不是,但無論如何都要檢查。
  3. 如何使用上面的示例在 js 中正確使用 js package。

您可以通過命令行界面使用任何語言執行 TestCafe 測試。 如我所見,您已經在使用它。 代碼片段:

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

您正確運行了 TestCafe 測試。 您對Java Script error tracking功能感到困惑。 默認情況下,TestCafe 會在測試的 web 頁面上處理 JavaScript 錯誤,如果發生則無法通過測試。 https://www.python.org/出現 JavaScript 錯誤。 在此處輸入圖像描述

TestCafe 處理它。 要禁用此行為,您可以指定“--skip-js-error”選項。 對於您的用例,它需要更改代碼如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM