vue-cli3 +电子+ chromedriver + selenium-webdriver找不到元素

时间:2019-05-09 10:54:53

标签: selenium-webdriver webpack electron vue-cli-3

我使用vue-cli项目(安装了electronic + seleniumWebDriver)与ChromeDriver一起运行测试。

我继续运行chromedriver(安装在vue-cli项目中),然后运行测试文件“ node ff.js”。

Electron应用程序在屏幕上运行,但未发生任何反应。然后等到该进程退出,结果变为错误“ UnhandledPromiseRejectionWarning:WebDriverError:未知错误:DevToolsActivePort文件不存在”。

我在网上搜索了一下,发现其他人认为它可以正常工作的解决方案。它只是在chrome选项中添加了“ --disable-dev-shm-usage”。但这仍然是错误的。

ff.js

const webdriver = require('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome');

const driver = new webdriver.Builder()
  // The "9515" is the port opened by chrome driver.
  .usingServer('http://localhost:9515')
  .withCapabilities({
    chromeOptions: {
      args: ['--headless', '--disable-gpu', '--no-sandbox', '--disable-extensions', '--disable-dev-shm-usage', 'start-minimized'],
      // Here is the path to your Electron binary.
      binary: './dist_electron/mac/myapp.app/Contents/MacOS/myapp'
    }
  })
  .setChromeOptions(new chrome.Options().addArguments('--remote-debugging-port=7070'))
  .forBrowser('electron')
  .build()

driver.quit()

package.json

"devDependencies": {
    "@types/chai": "^4.1.0",
    "@types/mocha": "^5.2.4",
    "@vue/cli-plugin-babel": "^3.5.0",
    "@vue/cli-plugin-e2e-nightwatch": "^3.6.0",
    "@vue/cli-plugin-eslint": "^3.5.0",
    "@vue/cli-plugin-typescript": "^3.5.0",
    "@vue/cli-plugin-unit-mocha": "^3.5.0",
    "@vue/cli-service": "^3.5.0",
    "@vue/eslint-config-standard": "^4.0.0",
    "@vue/eslint-config-typescript": "^4.0.0",
    "@vue/test-utils": "1.0.0-beta.29",
    "babel-eslint": "^10.0.1",
    "babel-plugin-istanbul": "^5.1.3",
    "bootstrap": "^4.3.1",
    "chai": "^4.1.2",
    "chromedriver": "^73.0.0",
    "copy-webpack-plugin": "^5.0.2",
    "electron": "^4.0.0",
    "electron-rebuild": "^1.8.4",
    "eslint": "^5.8.0",
    "eslint-plugin-vue": "^5.0.0",
    "jquery": "^3.4.0",
    "mocha": "^6.1.4",
    "mochawesome": "^3.1.2",
    "node-sass": "^4.9.0",
    "nyc": "^14.0.0",
    "popper.js": "^1.15.0",
    "sass-loader": "^7.1.0",
    "selenium-webdriver": "^4.0.0-alpha.1",
    "ts-node": "^8.1.0",
    "ts-protoc-gen": "^0.9.0",
    "typescript": "^3.2.1",
    "vue-cli-plugin-electron-builder": "^1.2.0",
    "vue-template-compiler": "^2.5.21",
    "vuex-module-decorators": "^0.9.8",
    "webdriverio": "^5.8.3"
  }

ps。我曾尝试使用ChromeDriver@74.x.x,但仍会导致相同的错误。

实际上,我尝试使用Selenium RobotFramework仍然发现相同的错误。我认为我的测试写作或某些设置有问题。 (我的测试工具不好。)

仅供参考

mytest.robot

*** Settings ***
Library           Selenium2Library
Variables         vars.py

*** Test Cases ***
Foohaha
    Create Webdriver    Remote    desired_capabilities=${binary_location}    command_executor=http://localhost:9515
    Log To Console  ${item.get_attribute('innerHTML')}
    [Teardown]    Close All Browsers

P.s。之所以没有Spectron是我只使用硒的测试仪。

1 个答案:

答案 0 :(得分:0)

在尝试了几乎推荐的测试库之后。我结束了testcafe。将首选应用程序路径设置为我的分布式应用程序的二进制文件,然后将页面设置为以html文件形式在测试文件中打开。主路径是默认值。

这就是我的设置方式。

.testcafe-electron-rc

{
  "mainWindowUrl": "app://./index.html",
  "electronPath": "/Users/xxx/Documents/ProjectFolder/dist_electron/mac/my-lovely-app.app/Contents/MacOS/my-lovely-app",
  "openDevTools": "true"
}

package.json

"scripts": {
    ...
    "cafe": "testcafe \"electron:/Users/xxx/Documents/ProjectFolder/.testcafe-electron-rc\" \"/Users/xxx/Documents/ProjectFolder/tests/e2e/specs/testcafe.js\""
  },
"devDependencies": {
    ...
    "selenium-webdriver": "^4.0.0-alpha.1",
    "testcafe": "^1.1.4",
    "testcafe-browser-provider-electron": "0.0.8"
  }

testcafe.js

import { Selector } from 'testcafe';

fixture(`Index page`)
  .page('/Users/xxx/Documents/ProjectFolder/dist_electron/bundled/index.html');


test('Body > Paragraph contains "Hello World!"', async testController => {

  const paragraphSelector = await new Selector('#nav > a.router-link-exact-active.router-link-active');
  const txt = await new Selector('#app > div.auth-page > div > div > div > form > fieldset:nth-child(1) > input');

  await testController.click(paragraphSelector)
            .typeText(txt, 'Peter Parker');
            // .expect(paragraphSelector.innerText).eql("Login");//.eql('Login');
});

希望这会对某人有所帮助。