2

我可以通过Chrome点击登录按钮。我正在使用量角器并将其自动化,因此我们可以使用phantomjs进行e2e测试。我无法使phantomjs点击登录按钮

的HTML是:

<div class="form-input"> 
    <input type="submit" id="login-button" class="btn" value="Sign in"> 

我曾尝试:

element(by.css('[class="form-input"]')).click(); 
element(by.css('[type="submit"]')).click(); 
element.all(by.id('login-button')).first().click(); 
element(by.id('login-button')).click(); 
element(by.css('[class="btn"]')).click(); 
element(by.css('[value="Sign in"]')).click(); 
element.all(by.className('form-input')).last().click(); 

这是我的建议,e2econf.js

var fs = require('fs'); 

//retrieving current date 
var today = new Date(); 
var dd = today.getDate(); 
var mm = today.getMonth()+1; //January is 0! 
var yyyy = today.getFullYear(); 

//retrieving current time 
var time = new Date(); 
var hour = time.getHours() + ":"; 
var minutes = time.getMinutes() + ":"; 

if(dd<10) { 
    dd='0'+dd 
} 
if(mm<10) { 
    mm='0'+mm 
} 

today = mm+'/'+dd+'/'+yyyy; 
time = hour + minutes; 

var results = '\nThe following Protractor tests have failed on '+ today + ' @' + time + '\n\n'; 

exports.config = { 
    capabilities: { 
    browserName: 'chrome', 
    version: '', 
    platform: 'ANY' 
    }, 

    seleniumServerJar: 'node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar', 

    specs: [ 
      'test/specs/manage/*.js', 
      'test/specs/manage/clone/*.js', 
      'test/specs/regression/*.js', 
      'test/specs/create/create-basicAd.js', 
      'test/specs/create/create-pagepost.js', 
      'test/specs/create/create-targeting.js' 
      ], 

    onPrepare: function() { 
    require('jasmine-reporters'); 
    jasmine.getEnv().addReporter(new jasmine.ConsoleReporter(console.log)); 
    jasmine.getEnv().addReporter(new function() { 
     this.reportSpecResults = function(spec) { 
     if (!spec.results().passed()) { 
      results += spec.suite.description + ' ' + spec.description; 
      if(spec.suite.description.substring(0, 4) == 'ADS-') { 
      results += 'censored' + spec.suite.description; 
      results += 'censored'; 
      } 
      results += '\n'; 
     } 
     }; 
    }); 
    }, 

    jasmineNodeOpts: { 
    showColors: true, 
    defaultTimeoutInterval: 1000000, 
    isVerbose: true 
    }, 

    allScriptsTimeout: 1000000, 

    onCleanUp: function() { 
    fs.writeFileSync('/tmp/results.txt', results); 
    } 
} 

这是我的人缘配置文件。 // Karma configuration

module.exports = function(config) { 
    'use strict'; 

    config.set({ 
    // base path, that will be used to resolve files and exclude 
    basePath: '', 

    // testing framework to use (jasmine/mocha/qunit/...) 
    frameworks: ['jasmine'], 

    // list of files/patterns to load in the browser 
    files: [ 
     'app/bower_components/jquery/dist/jquery.js', 
     'app/bower_components/lodash/dist/lodash.js', 
     'app/bower_components/angular/angular.js', 
     'app/bower_components/angular-mocks/angular-mocks.js', 
     'app/bower_components/angular-resource/angular-resource.js', 
     'app/bower_components/angular-cookies/angular-cookies.js', 
     'app/bower_components/angular-sanitize/angular-sanitize.js', 
     'app/bower_components/angular-route/angular-route.js', 
     'app/bower_components/angular-lodash/angular-lodash.js', 
     'app/bower_components/angular-bootstrap/ui-bootstrap.js', 
     'app/bower_components/ui-utils/ui-utils.js', 
     'app/bower_components/angulartics/src/angulartics.js', 
     'app/bower_components/angulartics/src/angulartics-ga.js', 
     'app/bower_components/ng-csv/src/ng-csv/ng-csv.js', 
     'app/bower_components/angulartics/src/angulartics-ga.js', 
     'app/bower_components/moment/moment.js', 
     'app/bower_components/ng-bs-daterangepicker/src/ng-bs-daterangepicker.js', 
     'app/bower_components/select2/select2.js', 
     'app/bower_components/angular-ui-select/dist/select.js', 
     'app/bower_components/angular-ui-select2/src/select2.js', 
     'app/bower_components/bn-tokens-angular/dist/bn-tokens-angular.js', 
     'app/bower_components/bn-catm-angular/dist/bn-catm-angular.js', 
     'app/bower_components/bn-topnav/dist/js/bn-topnav.js', 
     'app/bower_components/angular-ui-router/release/angular-ui-router.js', 
     'app/bower_components/bn-expression-builder/dist/bn-expression-builder.js', 
     'app/bower_components/bn-appbar/dist/js/bn-appbar.js', 
     'app/bower_components/zeroclipboard/dist/ZeroClipboard.js', 
     'app/bower_components/ng-clip/dest/ng-clip.min.js', 
     'app/bower_components/bn.dayparting/dist/bn.dayparting.js', 
     'app/lib/ng-grid/build/ng-grid.js', 
     'app/lib/ns-popover/nsPopover.js', 
     'app/lib/ps-input-time.js', 
     'app/lib/angular-ui-scrollfix/scrollfix.js', 
     'app/scripts/services/facebookAPI/providers/fbcommon.js', // FML 
     'app/scripts/services/facebookAPI/providers/facebook.js', // FMLx2 
     'app/scripts/*.js', 
//  'app/mock/api/api.mock.js', 
//  'app/mock/api/*.mock.js', 
     'app/scripts/**/*.js', 
     'app/templates/**/*.html', 
     'test/unit/**/*.js' 
    ], 

    // generate js files from html templates to expose them during testing. 
    preprocessors: { 
     'app/templates/**/*.html': 'html2js' 
    }, 

// list of files/patterns to exclude 
exclude: [], 

// web server port 
port: 9010, 

// level of logging 
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG 
logLevel: config.LOG_INFO, 


// enable/disable watching file and executing tests whenever any file changes 
//autoWatch: true, 


// Start these browsers, currently available: 
// - Chrome 
// - ChromeCanary 
// - Firefox 
// - Opera 
// - Safari (only Mac) 
// - PhantomJS 
// - IE (only Windows) 
//browsers: ['Chrome'], 

// Timeout for capturing a browser (in ms). 
captureTimeout: 60 * 1e3, 

// to avoid DISCONNECTED messages 
browserDisconnectTimeout : 10000, // default 2000 
browserDisconnectTolerance : 1, // default 0 
browserNoActivityTimeout : 60 * 1e3, //default 10000 


// Continuous Integration mode 
// if true, it capture browsers, run tests and exit 
singleRun: false 

}); };

这是我的package.json:

{ 
    "name": "client", 
    "version": "0.0.0", 
    "dependencies": {}, 
    "devDependencies": { 
    "grunt": "^0.4.5", 
    "grunt-angular-templates": "^0.5.4", 
    "grunt-autoprefixer": "~0.4.0", 
    "grunt-concurrent": "~0.4.1", 
    "grunt-contrib-clean": "~0.5.0", 
    "grunt-contrib-coffee": "~0.7.0", 
    "grunt-contrib-compass": "~0.6.0", 
    "grunt-contrib-concat": "~0.3.0", 
    "grunt-contrib-connect": "~0.5.0", 
    "grunt-contrib-copy": "~0.4.1", 
    "grunt-contrib-cssmin": "~0.7.0", 
    "grunt-contrib-htmlmin": "~0.1.3", 
    "grunt-contrib-imagemin": "~0.3.0", 
    "grunt-contrib-jshint": "~0.7.1", 
    "grunt-contrib-uglify": "~0.2.0", 
    "grunt-contrib-watch": "~0.5.2", 
    "grunt-google-cdn": "~0.2.0", 
    "grunt-jscs": "^0.8.1", 
    "grunt-karma": "~0.6.2", 
    "grunt-newer": "~0.5.4", 
    "grunt-ng-constant": "^1.1.0", 
    "grunt-ngmin": "~0.0.2", 
    "grunt-protractor-runner": "~0.2.3", 
    "grunt-rev": "~0.1.0", 
    "grunt-svgmin": "~0.2.0", 
    "grunt-usemin": "~2.0.0", 
    "grunt-wiredep": "~2.0.0", 
    "jasmine-reporters": "~0.4.1", 
    "jshint-stylish": "~0.1.3", 
    "karma": "~0.10.9", 
    "karma-chrome-launcher": "~0.1.2", 
    "karma-coffee-preprocessor": "~0.1.2", 
    "karma-firefox-launcher": "~0.1.3", 
    "karma-html2js-preprocessor": "~0.1.0", 
    "karma-jasmine": "~0.1.5", 
    "karma-ng-html2js-preprocessor": "~0.1.0", 
    "karma-ng-scenario": "~0.1.0", 
    "karma-phantomjs-launcher": "~0.1.1", 
    "karma-requirejs": "~0.2.1", 
    "karma-script-launcher": "~0.1.0", 
    "load-grunt-tasks": "~0.2.0", 
    "phantomjs": "~1.9.7-1", 
    "protractor": "~2.0.0", 
    "requirejs": "~2.1.10", 
    "time-grunt": "~0.2.1" 
    }, 
    "engines": { 
    "node": ">=0.10.0" 
    }, 
    "scripts": { 
    "start": "grunt serve", 
    "test": "grunt test" 
    } 
} 
+0

尝试'元素(通过。 id('login-button'))。trigger('click');' – 2015-03-30 18:40:31

+0

你尝试添加一些暂停或等待身体呈现? – Marcus 2015-03-30 18:48:22

+0

@马库斯FYI,这是我也建议作为一个选项。 – alecxe 2015-03-30 18:51:10

回答

2

PhantomJS是与protractor端对端测试最坏的选择。

即使protractor核心开发recommend against it

我们建议不要使用PhantomJS用量角器测试。有 许多报告PhantomJS崩溃的问题和行为 不同于真正的浏览器。

如果你仍然想PhantomJS解决这个问题,这里的事情的清单尝试:

  • 触发点击通过executeScript()

    browser.executeScript("arguments[0].click();", button.getWebElement()); 
    
  • 等待元素在点击前变为可点击

    var EC = protractor.ExpectedConditions; 
    browser.wait(EC.elementToBeClickable(button), 5000); 
    
  • 最大化浏览器窗口在启动:

    browser.manage().window().maximize(); 
    

其中button是你找到了,如一个元素:

var button = element(by.id("login-button")); 
+0

我知道这是案件/问题。我不得不使用它,因为我是实习生,这不是我需要改变的环境。 – tosh 2015-03-30 18:45:09

+0

@tosh ok,更新了答案。 – alecxe 2015-03-30 18:47:40

+0

没有解决方案。我在5002ms错误后等待超时。我有一个expect(元素(by.id('login-button'))。isPresent())。toBe(true);'所以我知道它在那里。 – tosh 2015-03-30 19:33:54