如何在Protractor和CucumberJS中使用本地存储?

时间:2015-12-03 09:25:46

标签: javascript local-storage protractor bdd cucumberjs

如何在Protractor和CucumberJS中使用本地存储?

我想使用本地存储不仅使用Protractor,还使用Protractor和CucumberJS,但在执行我的功能时出错:

  Scenario: Authentication ADMIN success               # app/modules/user/tests/e2e/user.feature:7
    Given I am logged as "ADMIN"                       # app/modules/user/tests/e2e/user.feature:8
    Given I am on "#/" page                            # app/modules/user/tests/e2e/user.feature:9
    Given I check if "button-create-post" is "show"    # app/modules/user/tests/e2e/user.feature:10
    Given I check if "button-create-page" is "show"    # app/modules/user/tests/e2e/user.feature:11


  Scenario: Authentication USER success                # app/modules/user/tests/e2e/user.feature:13
    Given I am logged as "USER"                        # app/modules/user/tests/e2e/user.feature:14
      UnknownError: <unknown>: Failed to read the 'localStorage' property from 'Window': Storage is disabled inside 'data:' URLs.
        (Session info: chrome=47.0.2526.73)
        (Driver info: chromedriver=2.19.346063 (38b35413bd4a486d436a9749e090454bc9ff6708),platform=Mac OS X 10.11.0 x86_64)

测试/ E2E /步/ main.step.js

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

    this.World = require("../support/world.js").World;

    this.path = '#/';

    this.Given(/^I am logged as "?([^"]*)"?$/, function (role) {
        if (['ADMIN', 'EDITOR', 'USER'].indexOf(role) !== -1) {
            browser.executeScript("localStorage.setItem('user_role', '" + role + "');");
            return browser.refresh();
        } else {
            throw error;
        }
    });

    this.Given(/^I am on "?([^"]*)"? page$/, function (arg1) {
        browser.get(arg1);
    });

    this.Given(/^I wait "?([^"]*)"? seconds$/, function (arg1) {
        browser.sleep(arg1);
    });

    this.Given(/^I check if "?([^"]*)"? is "?([^"]*)"$/, function (field, state) {
        if (state === 'show') {
            this.expect(element(by.css('[data-el="' + field + '"]')).isPresent()).to.eventually.equal(true);
        } else if (state === 'hide') {
            this.expect(element(by.css('[data-el="' + field + '"]')).isPresent()).to.eventually.equal(false);
        }
    });

};

测试/ E2E /支撑/ world.js

var World, chai, chaiAsPromised;
chai = require('chai');
chaiAsPromised = require('chai-as-promised');

World = function World(callback) {
    chai.use(chaiAsPromised);
    this.expect = chai.expect;
    callback();
}

module.exports.World = World;

应用/模块/用户/测试/ E2E / user.feature

Feature: Login feature
  As a user
  I want Authentication my account

  Scenario: Authentication ADMIN success 
    Given I am logged as "ADMIN"
    Given I am on "#/" page
    Given I check if "button-create-post" is "show"
    Given I check if "button-create-page" is "show"

  Scenario: Authentication USER success 
    Given I am logged as "USER"
    Given I am on "#/" page
    Given I check if "button-create-post" is "show"
    Given I check if "button-create-page" is "hide"

protractor.conf.js

exports.config = {
  directConnect: true,

  seleniumServerJar: 'node_modules/selenium-server/lib/runner/selenium-server-standalone-2.48.2.jar',

  specs: [
      'app/modules/user/tests/e2e/*.feature'
  ],

  getPageTimeout: 30000,

  capabilities: {
    'browserName': 'chrome',
    version: '',
    platform: 'ANY'
  },

  onPrepare: function() {
      var width = 1024, height = 800;

      browser.get('#/');
      browser.driver.manage().window().setSize(width, height);
  },

  framework: 'cucumber',

  cucumberOpts: {
    require: [
        'tests/e2e/steps/main.step.js'
    ],
    format: 'pretty', // or summary
    keepAlive: false
  },

  onCleanUp: function() {}

};

的package.json

{
  "name": "myApp",
  "version": "1.0.0",
  "description": "myApp",
  "dependencies": {
  },
  "devDependencies": {
    "chai": "^3.3.0",
    "chai-as-promised": "^5.1.0",
    "jasmine-core": "~2.3.4",
    ...
    "protractor": "^2.5.1",
    "selenium-server": "^2.48.2",
    "selenium-standalone": "^4.7.0",
    "selenium-webdriver": "^2.48.0",
  }
}

0 个答案:

没有答案