Struggling with the compilation of this Typescript code

Encountering a compile error:

 error TS2339: Property 'waitForElementVisible' does not exist on type 'signinPage'

SigninPage code snippet:

export class signinPage{
constructor(){
     emailInput: {
        selector: 'input[type=email]';
      };
      passwordInput: {
        selector: 'input[name=password]';
      };
      signinButton: {
        selector: 'button[type=submit]';
      };
}

signin(email, password){
    return this.waitForElementVisible('@emailInput')
      .setValue('@emailInput', email)
      .setValue('@passwordInput', password)
      .waitForElementVisible('@signinButton')
      .click('@signinButton')
    }


}

The issue seems to be related to the usage of 'this'. The TypeScript documentation doesn't provide clarity (https://www.typescriptlang.org/docs/home.html)

Comparing with a similar JavaScript implementation:

const signinCommands = {  
  signin: function(email, password) {
    return this.waitForElementVisible('@emailInput')
      .setValue('@emailInput', email)
      .setValue('@passwordInput', password)
      .waitForElementVisible('@signinButton')
      .click('@signinButton');
  }
};

module.exports = {   
  commands: [signinCommands],
  elements: {
    emailInput: {
      selector: 'input[type=email]'
    },
    passwordInput: {
      selector: 'input[type=password]'
    },
    signinButton: {
      selector: 'input[type=button]'
    },
  }
};

An assertion in the TypeScript course that JavaScript files can seamlessly integrate is proven incorrect with this non-functional example. Misinformation abounds.

Answer №1

JavaScript library Nightwatch.js lacks type information, making it incompatible with TypeScript. To enable TypeScript to recognize functions like waitForElementVisible in Nightwatch, install typing information using the command:

npm install --save-dev @types/nightwatch

This eliminates the need for the workaround suggested by PlayMa256.

Answer №2

You must provide a clear definition of the element and invoke the waitForElementVisible function in TypeScript.

export class LoginPage {
    waitForElementVisible: (value?: string) => any;
  
    constructor() {
        emailInput: {
            selector: 'input[type=email]';
        };
      
        passwordInput: {
            selector: 'input[name=password]';
        };
      
        signinButton: {
            selector: 'button[type=submit]';
        };

        this.waitForElementVisible = () => {};
    }
  
    signin(email, password) {
        return this.waitForElementVisible('@emailInput')
          .setValue('@emailInput', email)
          .setValue('@passwordInput', password)
          .waitForElementVisible('@signinButton')
          .click('@signinButton');
    }
}

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

Utilizing React JS to Develop Cell Components for a Schedule, Ensuring Teams are Unique within the Same Row

I am currently facing a challenge involving a table that has a fixed number of timeslots (y axis) and pitches (x axis). In addition, I have a collection of match objects with the following structure: Object { id: 3, teamA: "pelt", teamB: "Ranelagh" } Eac ...

What is the best way to achieve line breaks in text that auto-wraps in JavaScript/PHP?

My resizable div contains text/sentences. As I adjust the size of the div, the text wraps itself to perfectly fit within the available space in the container div. Now, I am faced with the task of converting this dynamic text into an image using php and th ...

Tricks for preventing axios from caching in GET requests

I am utilizing axios in my React-Native application Firstly, I set up the headers function setupHeaders() { // After testing all three lines below, none of them worked axios.defaults.headers.common["Pragma"] = "no-cache"; axios.defaults.heade ...

How can Node.js handle an upgrade request effectively?

Dealing with a websocket 'upgrade' event from a Node.js http server presents an interesting challenge - The upgrade handler takes the form of function(req, socket, head) - Is there a method to respond to this upgrade request without access to a r ...

Inquiring about JavaScript's substring method in String.prototype

let vowels = "AEIOU"; let result = vowels.substring(0, 3); document.write(result); I'm puzzled as to why the output is AEI instead of AEIO. Is this due to the indexing starting from zero in programming languages? ...

Angular (TypeScript) time format in the AM and PM style

Need help formatting time in 12-hour AM PM format for a subscription form. The Date and Time are crucial for scheduling purposes. How can I achieve the desired 12-hour AM PM time display? private weekday = ['Sunday', 'Monday', &apos ...

implementing geolocation using jquery and javascript

I am currently learning JavaScript and experimenting with the geolocation function in my HTML and PHP code, following a tutorial like this one. Here is the code snippet I added to my custom.js file: $(document).ready(function() { 'use strict&apos ...

The Serverless Function appears to have encountered a critical error and has

Currently, I am in the process of deploying a backend Express app on Vercel. The server is primarily focused on handling a mailing API using Nodemailer. Below is my package.json: https://i.sstatic.net/uv3z7.png Here is my server.js file: import express ...

Challenges surrounding asynchronous functionality in React hooks

I've been facing some issues with this code and have resorted to debugging it using console.log(). However, the results I'm getting are not making any sense. Can someone help me identify what's wrong with this code? I noticed that my console ...

When working with Angular 12, the target environment lacks support for dynamic import() syntax. Therefore, utilizing external type 'module' within a script is not feasible

My current issue involves using dynamic import code to bring in a js library during runtime: export class AuthService { constructor() { import('https://apis.google.com/js/platform.js').then(result => { console.log(resul ...

Failure to trigger a follow-up function in webSQL transaction's success callback

Review the code below. I have called setQuestion() within the successCallBack of db.transaction but am encountering an error: Uncaught TypeError: this.setQuestions is not a function. Can you spot any issues in my code? game.module( "game.scenes.scene" ) ...

Guide on accessing and manipulating local files using an Angular application

For my Angular 7 web app project, I'm looking to allow users to complete surveys and submit their responses. Since I don't have a database set up yet, I thought of storing the results in a local json file for now. Unfortunately, I'm facing d ...

What is the best way to handle promises within the context of updating state in React

Recently, I've been working on a React code snippet focused on creating a text input field. onChangeDestination(url, index) { this.setState(prevState => { const rules = [...prevState.rules]; rules[index] = { ...rules[index], url}; ...

Is it just me or does Img never work, no matter what the "src" is?

I am implementing Google's React example of a "Complex Grid" (located here) to create a Card-like layout. This is how the code appears: return ( <div className={classes.root}> <Paper className={classes.paper} ...

Tips for preventing redirection in Vue 3 composition API with asynchronous requests

I have successfully configured a submission form to send data to my email and add it to a Google sheet using the method described in this GitHub link. The process worked seamlessly for both purposes. However, I am facing an issue where upon submitting the ...

Illustrative demonstration of AngularJS

One way to showcase data using AngularJS is by triggering a function with a button click. Here's an example: <!DOCTYPE html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"& ...

Transferring data between JavaScript and PHP

Is there a way to transfer data from JavaScript to PHP when a button is clicked? For instance, can the getdate() function's result be sent to PHP? This transferred value will then be utilized for database manipulation. ...

How to smoothly scroll with jQuery animation without relying on $.browser and preventing double firing

Many inquiries have arisen about the cross-browser functionality of $('html, body').animate();, yet I seem unable to find a solution for this particular issue: I am eager to eliminate $.browser from my code, but I do not want the scroll event to ...

Downloading a file through a JavaScript link in HTMLUnit: A step-by-step guide

Looking to download a file with HTMLUnit from a javascript link is proving to be quite challenging. The journey begins at this page. When clicking on the "Authenticate with Java Web Start (new method)" link, a .jnlp file is downloaded, initiating a Java pr ...

Unknown individual, yet revealed by the investigator

I'm attempting to develop a dynamic list in react.js generateList = () =>{ return this.state.user.map((u)=>{ console.log(u); return <li onClick={this.handleClick} id={u} name={u}>{u}</li>; }); } The hand ...