当我看不到浏览器进行任何交互时,看到量角器黄瓜测试通过是正确的吗?

时间:2019-05-15 17:29:01

标签: javascript automation protractor cucumber

我是量角器黄瓜的新手。我已经在下面编写了功能。当我启动protractor protractor.conf.js时,浏览器将打开,然后立即关闭,然后显示我的测试已通过。这是正确的测试功能吗?我以为我需要查看登录过程的浏览器交互。

Scenario: Open the browser and login
    Given I am on the login page
    When I should be able to login with my credentials
    When I logout
    Then I should be able to see login page
     

场景:打开浏览器并登录

     

√鉴于我在登录页面上
      √什么时候我可以使用自己的凭据登录
      √注销时
      √然后我应该可以看到登录页面

     

登录页面

     

1个场景(1个通过)   4个步骤(已通过4个步骤)   0m00.005s

this.Given('I am on the login page', function() {

   browser.driver.get(browser.baseUrl);
});

this.When('I should be able to login with my credentials',  function() {
    let inputUsernameField = element(by.css(USERNAME_NAME));
    inputUsernameField.sendKeys(username);
    let inputPasswordField = element(by.css(PASSWORD_NAME));
    inputPasswordField.sendKeys(password);
    element(by.id(LOGIN_BUTTON_ID)).click();
});

this.When('I logout',  function() {

    element(by.className(HAMBERBURGER_MENU_ICON_CLASS)).click();
    element(by.className(LOGOUT_BUTTON_CLASS)).click();
});

this.Then('I should be able to see login page', {timeout:120*1000},function() {

    browser.driver.wait(protractor.ExpectedConditions.presenceOf($('#login_button')), 5000);
});

下面是protractor.conf.js

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  getPageTimeout: 600000,
  allScriptsTimeout: 500000,
  defaultTimeoutInterval: 30000,
  framework: 'custom',
  // path relative to the current config file
  frameworkPath: require.resolve('protractor-cucumber-framework'),
  multiCapabilities:
  [
  {
    'browserName': 'chrome'
  },
  {
    'browserName': 'firefox'
  }],

  // Spec patterns are relative to this directory.
  specs: [
    'features/*.feature'
  ],
  baseURL: 'http://localhost:8080/',
  ignoreSynchronization: true,
  cucumberOpts: {
    strict: true,
    require: [
              'hooks/reporter/js',
              'specs/*Spec.js'
            ],
    tags: false,
    profile: false,
    format: 'json:e2e/reports/cucumber-report.json',
    resultJsonOutputFile: 'e2e/reports/cucumber-report.json'
  },

  onPrepare: function() {

    var chai = require('chai');
    chai.use(require('chai-as-promised'));
    global.expect = chai.expect;
    global.baseURL = this.baseURL;
    browser.ignoreSynchronization = true;
    browser.driver.manage().window().maximize();
    browser.waitForAngular();           
    browser.driver.manage().timeouts().implicitlyWait(30000);  
  },

  onComplete: function() {

    const report = require('multiple-cucumber-html-reporter');

    report.generate({
        jsonDir: 'e2e/reports/',
        reportPath: 'e2e/reports/',

    });
  }
}

1 个答案:

答案 0 :(得分:0)

因此,事实证明,如果我没有在函数中传递任何内容作为回调,则需要使用async并等待。这是 JavaScript 语法,而不是 Typescript

this.Given('I am on the login page', function() {

   browser.driver.get(browser.baseUrl);
});

    this.Then('I should be able to see login page', {timeout:120*1000},function() {

    browser.driver.wait(protractor.ExpectedConditions.presenceOf($('#login_button')), 5000);
});

通过异步/等待更改为

this.Given('I am on the login page', async() => {

   await browser.driver.get(browser.baseUrl);
});

this.Then('I should be able to see login page', {timeout:120*1000}, async() => {

    await browser.driver.wait(protractor.ExpectedConditions.presenceOf($('#login_button')), 5000);
});