Encountered a 'obj.indexOf is not a function' TypeError while trying to write a selenium test script

I am in the process of creating an automated test script using Selenium Webdriver and Cucumber for my company's website. To get started, I utilized Matt-B's example as a foundation. After obtaining the necessary files, I made adjustments to the google.feature file:

  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"

I have now begun modifying google-steps.js as follows:

'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');
  });
};

The initial steps are functioning correctly by opening the website, maximizing it, and clicking the button. I am currently working on validating that I have landed on the correct page based on a section of the URL. If anyone has suggestions on fixing the "TypeError: obj.indexOf is not a function" or providing an alternative solution, I would greatly appreciate it. Apologies if this is a straightforward fix, as this is my first time working on something like this.

The complete error message displayed in the console is:

 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)

Answer №1

Encountering the same issue with

expect(object).to.contain('string')
in Chai prompted me to investigate further. While delving into it, I discovered an alternative approach using JavaScript regular expressions coupled with the match() function instead of 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$/);

If you need to incorporate variables within the regex pattern, check out another insightful answer on Stack Overflow:

Answer №2

The indexOf function is a property of Array and not an object

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Spinning cubemap texture in Three.js

I've created this cubetexture in Three.js. Now, I want to rotate the cubeTexture itself by 180 degrees, not the camera. Is there a way to achieve this? Specifically, I aim to rotate the x axis of the cubeTexture to display the opposite side. It would ...

Using Python with Selenium continues to consistently load web pages

Attempting to access a webpage with a basic Python/Selenium script # encoding=utf8 from selenium import webdriver from selenium.webdriver.common.keys import Keys import datetime as dt import codecs import os myDriver=webdriver.Chrome() myDriver.get("http ...

Having trouble receiving a JSON array after making an Ajax request

I have read through previous posts on this issue, but I still can't seem to figure it out. Hopefully, someone here can help me solve this problem. My challenge lies in retrieving an array, which I have encoded in json, from a PHP script and passing i ...

Select an element within a repeater using Protractor following an $http request

As I embark on protractor testing for my sails.js / AngularJS application, I encounter an issue with the ng-repeat element in the HTML code: <div ng-repeat="book in books"> book.name </div> During my test, a button click triggers a $http PO ...

It's incredibly frustrating when the Facebook like button is nowhere to be found

Currently, I am in the process of developing a website for a local bakery and have decided to incorporate a Facebook like button on the homepage. After visiting developers.facebook, I proceeded to copy and paste the code provided. It appears that there use ...

The regular expression provided is not valid: nothing can be repeated - error message in Safari

I am encountering an issue with my code that works perfectly on other browsers, but fails to function on iPhones and the Safari browser. The debugging process reveals an error message stating "Invalid regular expression: nothing to repeat" in reference to ...

There is no matching overload for this call in React Native

I am working on organizing the styles for elements in order to enhance readability. Here is the code I have written: let styles={ search:{ container:{ position:"absolute", top:0, }, } } After defining the s ...

Attempting to modify the key values within an object, however, it is mistakenly doubling the values instead

I'm encountering an issue when trying to update key values within an object. It seems to be adding duplicate values or combining all values together instead of assigning each name a specific language slug. I can't figure out where I'm going ...

Deployment to Amazon Amplify encounters failure when using Next JS

I've been encountering continuous failures while trying to deploy an SSG app on Next JS. The build consistently fails, and I'm met with an error message. Despite following the deployment documentation for SSG sites on Amazon diligently, the error ...

Uploading pictures to Imgur without the need for an API key

I've been attempting to utilize the imgur API feature that allows you to upload an image by sending a GET request to http://api.imgur.com/2/upload with a URL in the form data. However, I have been unsuccessful in making it work as intended, as it simp ...

Using the fetch method to consume a RapidAPI JSON response

Currently, I am in the process of learning React Native and have initiated a project for that purpose. In this project, I have integrated RapidAPI (link: ) to fetch necessary data. Upon hitting the API, I receive a 200OK status, but unfortunately, I am fa ...

Adjust the width of the flex columns?

In one of my columns, I have a list that I'm splitting using CSS to wrap the data in the next column after a specific height. However, in the demo, you'll notice there's a lot of white space between the first and second columns. I'm uns ...

Verify if spacebar is pressed and then use jQuery to add a hashtag with multi-language support

I am attempting to use jQuery to add a hashtag (#) after the user types and presses space. I have created a demonstration on CodePen. In this demo, when you type something like (how are you), the JavaScript code will change it to (#how #are #you). To ach ...

Having trouble setting the select value with JavaScript in the Selenium web driver

I am working on a web page that includes a cascaded dropdown feature. The data in the second dropdown appears to be generated via ajax based on the selection made in the first dropdown. Here is the code for the first select option: <select class="form- ...

Randomize the array values and assign them to div elements using jQuery or JS

Currently, I am in the process of constructing a unique CSS masonry layout that accommodates varying heights and widths. There are 5 different sizes available which will be assigned randomly to each <li>. Additionally, every subsequent <li> ele ...

I am retrieving data from a service and passing it to a component using Angular and receiving '[object Object]'

Searching for assistance with the problem below regarding my model class. I've attempted various approaches using the .pipe.map() and importing {map} from rxjs/operators, but still encountering the error message [object Object] export class AppProfile ...

Feature that increments the count in Google Sheets

Is there a way to create a count-up feature in Google Sheets? I want a timer to start counting up as soon as the user enters a date in cell A2, displaying the elapsed time in cell B2. All the scripts I come across seem to only offer countdown features. An ...

Removing a particular item from an Observable of arrays containing any type

My application has an Observable that contains an array of places: places: Observable<Array<any>>; In the template, I am using the async pipe to iterate over the array: <tr *ngFor="let place of places | async"> ... </tr> After ...

Consistent information displayed on Google Maps infowindow

I am facing an issue where this code is always returning the last value on click. How can I fix this error? Any help would be greatly appreciated. js: <?php foreach($pets as $pet):?> var markerData = { lat: <?php echo $pet['pet_lat']?& ...

Is there a JavaScript function available to remove comments from previously commented HTML code?

<div id="div1">bar</div> JQuery function addComment(element){ element.wrap(function() { return '<!--' + this.outerHTML + '"-->'; }); } addComment($('#div1')); Looking for assistance in unc ...