2017-03-15 60 views

回答

3

这是很难回答你的问题,如果你不指定正是发生。但下面是我如何解决它,我希望它可以帮助。

我这是怎么了SPECTRON特拉维斯运行我的电子申请(仅限Linux):

.travis.yml

os: 
    - linux 

language: node_js 

node_js: 
    - "7.7" 

before_script: 
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export DISPLAY=:99.0; fi 
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh -e /etc/init.d/xvfb start; fi 
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sleep 3; fi 

script: 
    - node --version 
    - npm --version 
    - npm install 
    - npm run e2e 

cache: 
    directories: 
    - node_modules 

notifications: 
    email: 
    on_success: never 
    on_failure: change 

utils.js

const electron = require('electron'); 

const beforeEach = function (env = {}) { 
    this.app = new Application({ 
    path: electron, 
    args: ['.'], 
    }); 

    return this.app.start(); 
}; 

const afterEach = function() { 
    if (this.app && this.app.isRunning()) { 
    return this.app.stop(); 
    } 
    return undefined; 
}; 

.e2e.js个-files

describe('test case', function() { 
    beforeEach(testUtils.beforeEach); 
    afterEach(testUtils.afterEach); 

    it('should run', function() { 
    }); 
}); 

如何存储

如果你有你的测试运行SPECTRON测试失败的截图,但他们由于某种原因失败,那么它可以帮助您可以通过截图电子应用。

utils.js

const fs = require('fs'); 
const saveErrorShot = function (e) { 
    const filename = `errorShot-${this.test.parent.title}-${this.test.title}-${new Date().toISOString()}.png` 
    .replace(/\s/g, '_') 
    .replace(/:/g, ''); 

    this.app.browserWindow.capturePage().then(imageBuffer => { 
    fs.writeFile(filename, imageBuffer, error => { 
     if (error) throw error; 

     console.info(`Screenshot saved: ${process.cwd()}/${filename}`); 
    }); 
    }); 

    throw e; 
}; 

.e2e.js -files

describe('test case', function() { 
    it('should store a screenshot', function() { 

    return this.app.client 
     .then(() => this.app.client.getText('.non-existing-element') 
     .catch(testUtils.saveErrorShot.bind(this)); 
    }); 
}); 

为了让特拉维斯发送您的文物,一个Amazon S3存储

加入您的AWS S3证书到您的Travis env变量,read here for more information

以下添加到.travis.yml

after_failure: 
    - curl -sL https://raw.githubusercontent.com/travis-ci/artifacts/master/install | bash 
    - artifacts upload $(git ls-files -o | grep -Fv -e node_modules) 
    - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then artifacts upload $(ls /home/travis/.npm/_logs/); fi 

它所做的是,它

  1. 下载并安装在你的仓库travis-artifacts
  2. 上传所有未跟踪文件,但不包括所有节点模块。也许你会有一些你想排除的其他文件,然后只需追加-e unnecessary_folder -e unnecesarry_file
  3. /home/travis/.npm/_logs上传npm日志。如果您正在运行MacOS,这些文件将出现在另一个文件夹中。