如何运行有序测试

时间:2015-04-08 13:44:57

标签: angularjs protractor

我想

  • 填写名称和密码输入字段
  • 点击登录按钮
  • 等待页面加载
  • 点击按钮

我怎样才能做到这一点?我意识到承诺会涉及到。这就是我的尝试:

conf.js

var env = require('./environment.js');

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',

  specs: ['test/e2e/app/test-spec.js'],

  baseUrl: env.baseUrl,

  onPrepare: function() {
    console.log("base url is: " + env.baseUrl);
    browser.driver.get(env.baseUrl + '#/login');

    element(by.model('my.username')).sendKeys('username');
    element(by.model('my.password')).sendKeys('password');

    console.log("clicking login button...");
    element(by.css('button.md-primary.md-button.md-default-theme')).click();

    // Login takes some time, so wait until it's done.
    // For the test app's login, we know it's done when it redirects to
    // /#/home.html.
    browser.driver.wait(function() {
      return browser.driver.getCurrentUrl().then(function(url) {
        console.log("read current url as" + url);
        console.log("returning...");

        return  /\/#\/home\//.test(url);
      });
    }, 10000);
  }
};

然后进行测试。

测试spec.js

describe('Click button on home page', function() {
  it('should click the button', function() {
    console.log("looking for button on home page")
    element(by.css('button.md-fab.md-accent.md-primary.md-button.md-default-theme')).click();
  });
});

供参考

environment.js

// Common configuration files with defaults plus overrides from environment vars
var webServerDefaultPort = 9002;

module.exports = {
 // The address of a running selenium server.
 seleniumAddress:
   (process.env.SELENIUM_URL || 'http://localhost:4444/wd/hub'),

 // Capabilities to be passed to the webdriver instance.
 capabilities: {
   'browserName':
       (process.env.TEST_BROWSER_NAME || 'chrome'),
   'version':
       (process.env.TEST_BROWSER_VERSION || 'ANY')
 },

 // Default http port to host the web server
 webServerDefaultPort: webServerDefaultPort,

 // A base URL for your application under test.
 baseUrl:
   'http://' + (process.env.HTTP_HOST || 'localhost') +
         ':' + (process.env.HTTP_PORT || webServerDefaultPort)

};

输出

  

http://localhost:4444/wd/hub使用selenium服务器   [launcher]运行WebDriver的1个实例
  基本网址是:http://localhost:9002
  点击登录按钮...
  寻找主页上的按钮

我从未见过"read current url as"告诉我承诺没有执行而我的测试执行得太早。

所以我得到的错误是有道理的,因为测试太早了:

  

NoSuchElementError:找不到使用定位器找到的元素:By.cssSelector(“button.md-> fab.md-accent.md-primary.md-button.md-default-theme”)

2 个答案:

答案 0 :(得分:1)

wait()来电置于beforeEach()

describe('Click button on home page', function() {

  beforeEach(function () {
    browser.wait(function() {
      return browser.getCurrentUrl().then(function(url) {
        console.log("read current url as" + url);
        console.log("returning...");

        return  /\/#\/home\//.test(url);
      });
    }, 10000);
  });

  it('should click the button', function() {
    console.log("looking for button on home page")
    element(by.css('button.md-fab.md-accent.md-primary.md-button.md-default-theme')).click();
  });
});

或者,您可以等待主页上的元素变为可见:

describe('Click button on home page', function() {
  var scope = {};

  beforeEach(function () {
    var EC = protractor.ExpectedConditions;
    scope.defaultTheme = element(by.css('button.md-fab.md-accent.md-primary.md-button.md-default-theme'));
    browser.wait(EC.visibilityOf(scope.defaultTheme), 10000);
  });

  it('should click the button', function() {
    console.log("looking for button on home page");
    scope.defaultTheme.click();
  });
});

答案 1 :(得分:1)

在摆弄后,我在以下设置中取得了最大的成功:

  • beforeAll()用于登录
    • sendkeys()无法在OnPrepare()内为我发挥作用。我将OnPrepare()缩短为简单的重定向到登录页面。
  • 我将config.js设置为使用firefox,因为我在让click()在chrome中运行时遇到了问题。
  • 使用Jasmine2(默认为jasmine1)
  • 使用一系列browser.wait来计算时间。您可以等待元素存在(仅在第2页上存在的元素)
    • browser.waitForAngular()执行类似的职责。我使用此功能的成功率较低
  • Protractor configuration file documentation

测试spec.js

    describe('Click button on home page', function() {
      beforeAll(function () {
        element(by.model('my.username')).sendKeys('username');
        element(by.model('my.password')).sendKeys('password');
        element(by.id('loginButton')).click();

        browser.wait(function() {
          return browser.driver.getCurrentUrl().then(function(url) {
            var result = url.indexOf("/#/home") > -1;//wait for url to change to this
            return result;
          });
        }, 10000);
      });

      it('should click the button', function() {
        console.log("executing test...");   
        browser.ignoreSynchronization = true;//Yea I had to use this here too...I dunno why this is required

        element(by.id('someButton')).click();
        browser.wait(function() {
           console.log('inside wait for vm.firstName');
           return browser.isElementPresent(by.model('vm.firstName'));
        }, 10000).then(function(){
          //rinse and repeat with more waits as pages change
        });
        expect(true).toBe(true);
      });
    });

conf.js

var env = require('./environment.js');

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',

  specs: ['test/e2e/app/test-spec.js'],

  baseUrl: env.baseUrl,

  capabilities: {
    browserName: 'firefox'
  },

  framework: 'jasmine2',

  onPrepare: function() {
    browser.driver.get(env.baseUrl + '#/login');
  }
};

environment.js

没有变化