编写selenium测试脚本时出现“TypeError:obj.indexOf不是函数”错误

时间:2016-09-19 08:46:32

标签: javascript selenium cucumber

我目前正在为我公司的网站使用Selenium Webdriver和Cucumber制作自动化测试脚本。为此,我使用Matt-B's example作为开始。自从获取文件后,我将google.feature的内容修改为:

  Feature: Changing Pages
    As an internet user
    In order to move through a website
    I want to be able to click a button and go to another page

    Scenario: Digital3rd Testing page
      Given I am on the Digital3rd website
      When I click the "Testing" button
      Then I should go to "../testing"

现在开始将google-steps.js修改为:

'use strict';

var expect = require('chai').expect;

module.exports = function() {
  this.World = require('../support/world.js').World;

  this.Given(/^I am on the Digital(\d+)rd website$/, function (arg1, callback) { //declaring function that relates to Cucumber Script
        this.driver.get('http://www.Digital3rd.org'); //open intended website
    callback(null, 'set-up');
  });

  this.When(/^I click the "([^"]*)" button$/, function (arg1, callback) { //declaring function that relates to Cucumber Script  
      this.onload = //when the page loads...
         this.driver.manage().window().maximize(); //maximise the screen...
         this.driver.findElement({id: 'menu-item-24'}).click(); //click the "Testing" button.
    callback(null, 'test');
  });

  this.Then(/^I should go to "([^"]*)"$/, function (promise) {
        var wbaddrss = this.driver.getCurrentUrl();
        return expect(wbaddrss).to.contain('/testing');
  });
};

通过打开网站,最大化它并单击按钮,前两个步骤可以正常工作。我现在正在尝试使用URL的一部分来验证我在正确的页面上。任何帮助修复“TypeError:obj.indexOf不是一个函数”错误或给我一个替代将不胜感激。很抱歉,如果这是一个简单的答案,但这是我第一次做这样的事情。

我在控制台中获得的完整错误代码是:

 TypeError: obj.indexOf is not a function
     at Assertion.include (C:\Users\User\Test program\node_modules\chai\lib\chai\core\assertions.js:228:45)
     at Assertion.assert (C:\Users\User\Test program\node_modules\chai\lib\chai\utils\addChainableMethod.js:84:49)
     at World.<anonymous> (C:\Users\User\Test program\features\step_definitions\google-steps.js:32:30)
     at nextTickCallbackWith0Args (node.js:420:9)
     at process._tickCallback (node.js:349:13)

2 个答案:

答案 0 :(得分:0)

I just ran into the same issue with expect(object).to.contain('string') in Chai and would like to step through it to find out what's going wrong. Meanwhile, you can judiciously use JS regular expressions with the match() function rather than contain():

expect(object1).to.match(/String to contain/);
expect(object2).to.match(/^Exact string to match$/);

expect(object3).to.match(/^This string might contain other things/);
expect(object3).to.match(/before it ends here$/);

To use variables in the regex, see this other SO answer: https://stackoverflow.com/a/17886301/949911/

答案 1 :(得分:-1)

indexOf是Array属性而不是对象