What is the method for comparing a value in TypeScript that does not match a given value?

I am new to scripting languages and encountered an issue while using enums with if-else statements in TypeScript. To work around this problem, I have decided to use switch-case instead of if-else conditions.

Despite trying !== and !===, they do not seem to be effective. The first index of the enum does not work unless I specify the if condition as not equal to the compared value.

public static async getElementByLocatorType(locatorType: customLocators, locatorValue: string, expectedText?: string) {
        switch(locatorType) {
            case customLocators.CSS:
                return element(by.css(locatorValue);
            case customLocators.XPATH:
                return element(await by.xpath(locatorValue));
            case customLocators.CSSTEXT:
                return element(by.cssContainingText(locatorValue, expectedText));
            case customLocators.LINKTEXT:
                return element(by.linkText(locatorValue));
            case customLocators.BUTTONTEXT:
                return element(by.buttonText(locatorValue));
            case customLocators.PARTIALBUTTONTEXT:
                return element(by.partialButtonText(locatorValue));
            case customLocators.PARTIALLINKTEXT:
                return element(by.partialLinkText(locatorValue));
            default:
                logger.info('Cannot find any locator listed above');
        }
    }

export enum customLocators {
    'CSS', 'XPATH', 'CSSTEXT', 'LINKTEXT', 'BUTTONTEXT', 'PARTIALBUTTONTEXT', 'PARTIALLINKTEXT'
}

I am facing an issue where no element is found when using (by.css), while other elements work perfectly fine. This occurs specifically with the locatorType === customLocators.CSS, which is the first index of the enum.

Answer №1

In the code snippet above, I have implemented a switch case statement as an alternative solution, which has proven to work effectively.

public static async getElementByLocatorType(locatorType: customLocators, 
    locatorValue: string, expectedText?: string) {
            switch (locatorType) {
                case customLocators.CSS:
                   return element(by.css(locatorValue));
                    break;
                case customLocators.XPATH:
                    return element(by.xpath(locatorValue));
                    break;
                case customLocators.LINKTEXT:
                    return element(by.linkText(locatorValue));
                    break;
                case customLocators.BUTTONTEXT:
                    return element(by.buttonText(locatorValue));
                    break;
                case customLocators.CSSTEXT:
                    return element(by.cssContainingText(locatorValue, expectedText));
                    break;
                case customLocators.PARTIALLINKTEXT:
                    return element(by.partialLinkText(locatorValue));
                    break;
                case customLocators.PARTIALBUTTONTEXT:
                    return element(by.partialButtonText(locatorValue));
                    break;
                default:
                    logger.info('browser : ' + locatorType + ' is invalid, Please check the selector name..');
            }
        }

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

Retrieve dashboard configurations in AngularJS by making an $http request prior to initiating the dashboard controller

I am currently immersing myself in Angular and tackling a complex dashboard framework all built with Angular. Prior to loading the controllers, I need to fetch various dashboard settings from the server using $HTTP. These settings play a crucial role in de ...

The proper method for redirecting the view after a successful AJAX request in a MVC application

Explanation of the Issue: I have added a search function to the header section of my MVC website. It includes an input text box and a 'Search' button. The Problem at Hand: Currently, I have incorporated an AJAX function in the shared master la ...

What is the best way to transfer an object between views in Django?

Background/Issue In my web application, I have a job that involves running a python script to log into LinkedIn. This script launches headless chromium, navigates to the LinkedIn login page, and logs in with the proper credentials. However, sometimes Link ...

Unable to verify POST $http request using $httpBackend

Currently, I am in the process of writing unit tests to cover the http requests of my app. Using Jasmine and Karma, I have been following a tutorial on how to use $httpBackend from https://docs.angularjs.org/api/ngMock/service/. However, I encountered an e ...

Issue: Expressjs is throwing a TypeError due to an attempt to read the 'id' property of undefined

I am currently working on a registration function in expressjs, but I keep encountering the following error message: TypeError: Cannot read properties of undefined (reading 'id') This is my user model: Users.ts interface UserAttributes { id: ...

Looking for the MIME file type requirement for Excel or spreadsheets?

Values in the 2nd column indicate the file MIME type. https://i.sstatic.net/qPcQD.png I am looking to create a condition that will be true for all of the above file MIME types. Specifically, I need a condition for all Excel files. This is what I attempte ...

No location matched any routes

I'm currently working on a notes application, and I've encountered an error when trying to edit the notes. The error message says "No routes matched location id ...". Any idea what could be causing this issue? The approach I'm taking is to ...

When using Sequelize, you may notice that extra spaces are automatically added at the end of the DataTypes.CHAR data type

Here is an example of how I structure my Store.ts file: import {DataTypes, Model, ModelAttributes} from "sequelize"; export default class Store extends Model { declare id: number declare name: string declare phone: string } export const S ...

Utilizing vuetifyjs: Selectively incorporating necessary icons into the build

I am currently working on a vuetifyjs-app using the default "Material Design Icons". For the production build, I am only utilizing 2 icons from this font (which are being used by the vuetify-component chips). Following recommendations, I have included the ...

"Trouble with Angular's http.get method failing to retrieve data from MySQL through Node

I am struggling to retrieve data from MySQL using Angular and Node.js. Despite trying, I am unable to make it work. When I check Postman using the link http://localhost:8080/locations, I can see the data. { "status": "200", "items": [ { "cit ...

Implementing personalized callback methods for AJAX requests in Prototype

After refactoring my code to use proper objects, I am facing an issue with getting Prototype's AJAX.Request to work correctly. The code snippet below is functioning within the context of YUI's DataTable: SearchTable.prototype.setTableColumns = f ...

In React using Flow Type, the syntax [...mySet] doesn't function as expected, while Array.from(mySet) successfully converts the

Why is it that with React and Flow Type, the line of code displaying [{}] when using JSON.stringify([...mySet]) but shows the correct values with JSON.stringify(Array.from(mySet))? The issue seems perplexing as this behavior cannot b ...

Monitor Changes with Grunt: Be Prepared for Some Waiting!

My Grunt watch task is experiencing significant delays between detecting a file change and starting to work. Output similar to the following is frequently seen: >> File "src/static/app/brandManager/addChannel.html" changed. Running "html2js:main" ...

Exploring the functionality of routes with the identifier :id

Is there a way to test routes with :id parameters included in them? Let's consider an example where we have a route like this: Router.get('/:id/profile I came across a scenario in someone else's code where they passed a string of numbers ...

Technique for transferring information between properties of a class instance within an Express server's architecture

I am in the process of developing a monitoring server for a library using Express. My goal is to create different routers and routes, while also being able to access functions and variables from the monitor-server class. Currently, I have passed the ' ...

Click event recursion occurs due to the use of $.post

I have a collection of product rows available for customers to select from. Each row is designated with the class "product-line". Upon clicking on a row, I aim to visually indicate its selection status by toggling the "product-checked" class, and subsequen ...

FInding the inner value of a Vuetify chip

I have a Vue application that utilizes Vuetify chips to display information. I'm trying to log the value inside a specific chip when it is clicked, but I keep getting an undefined error when trying to access the array where the information comes from. ...

Protractor and Gulp: Selenium WebDriver terminated with status code 1

I've recently encountered an issue while using Bangular, a fantastic MEAN stack Yeoman generator, to create a project. Everything was running smoothly until I performed a series of npm updates on the project. Now, when attempting to run the gulp-protr ...

Is there a way to stop the execution of myFunc after it has been performed 5 times using clearInterval?

This code is designed to notify online shoppers that they need to choose a color from a dropdown menu before adding an item to their shopping cart. If no color is selected, the text will blink to alert them. How can I modify this code so that the blinking ...

Unexpected behavior observed with callback function when inserting a query in Node.js

Having a minor issue with using the POST method and adding an INSERT. The functionality works correctly as shown below, but I am looking to implement a callback after the data has been inserted. Currently, the database is updated successfully, but I am una ...