Using the expect statement within a Protractor if-else block

My script involves an if-else condition to compare expected and actual values. If they do not match, it should go to the else block and print "StepFailed". However, it always executes the if block and the output is "step passed" even when expected does not equal actual. Here is my code:


var expected = ['Select training program using Index', 'Selenium','A','UFT/QTP','Loadrunner'];
var els = element.all(by.xpath("//select[@id='dropdown1']/option"));
   for (var i = 0; i < expected.length; ++i) {
     if(expect(els.get(i).getText()).to.eventually.equals(expected[i])){
        console.log('' +'Steppassed'+ '');
     }else{
        console.log('' +'Stepfailed'+ '');
     }
});

The dropdown values are: 'Select training program using Index', 'Selenium', 'Appium','UFT/QTP','Loadrunner'. I have mistakenly used 'A' instead of "Appium' which means the step should fail, but that's not what's happening.

Please provide suggestions on how to resolve this issue. Thank you.

Answer №1

When verifying a conditional value, it is unnecessary to assert it.

An alternative approach could be:

var expectedValues = ['Select training program using Index', 'Selenium', 
'A','UFT/QTP','Loadrunner'];

var elements = element.all(by.xpath("//select[@id='dropdown1']/option"));

for (var index = 0; index < expectedValues.length; ++index) {
   elements.get(index).getText().then((text) => {
       if(text === expectedValues[index] ) {
           console.log('' +'Test Passed'+ '');
         } else {
            console.log('' +'Test Failed'+ '');
           }
         });
       }

To improve promise handling and readability, consider utilizing async/await syntax!

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

What is the best way to pass a conditional true or false value to React boolean props using TypeScript?

I am currently utilizing the Material UI library in combination with React and Typescript. Whenever I attempt to pass a conditional boolean as the "button" prop of the component, I encounter a typescript error stating: Type 'boolean' is not assi ...

Utilizing npm serve for Location Proxy

Is it feasible to implement a location proxy while serving a react application using npm install -g serve; serve -s build? For instance, redirecting any requests from https://example.com/api/* to https://example.com:8000/api/* ...

What is the best approach to incorporate Column Reordering in react-data-grid?

Within my REACT application, I have incorporated the npm package react-data-grid. They offer a sample showcasing Column Reordering on their website. I wish to replicate this functionality in my own code. Upon reviewing their source code, I am puzzled abou ...

Where should Babel and Webpack be placed in your package.json file - under devDependencies or Dependencies?

I'm a beginner when it comes to npm and I have doubts on what should be included in dependencies as opposed to devDependencies. I understand that testing libraries belong in dev, but what about tools like babel and webpack? Should they also be categor ...

The functionality of connect-flash in Express JS is compromised when used in conjunction with express-mysql-session

I am facing a unique issue in my project. I have identified the source of the problem but I am struggling to find a solution. My project utilizes various modules such as cookie-parser, express-mysql-session, express-session, connect-flash, passport and m ...

Implementing Typescript for React Navigation: Configuring navigationOptions effectively

Within a React Native app utilizing React Navigation, I am working on a screen component where I aim to set the title based on given parameters using the navigationOptions property like so: static navigationOptions = ({navigation}) => ({ title: nav ...

What are the steps to resolve warnings in an imported json file?

I am working on a Vue project where I have imported a JSON file into my TypeScript script using import jsonData from '@/assets/data1.json'; Although the data is accessible and functions correctly, I am encountering numerous warnings during the b ...

What is the comparable alternative to promise<void> in observables?

I've been working with Angular using TypeScript and I'm attempting to return a promise from an observable. What is the correct way to accomplish this? So far, I have tried the following: of(EMPTY).toPromise() // error: Promise<Observable<n ...

Exploring the process of incorporating types for a Vue plugin

I am currently trying to integrate a self-made plugin for Vue with TypeScript. However, when I try to use the method from my vue prototype, I encounter an issue where my method $auth is not recognized on type 'myComponent'. I have also included ...

Passing the entire command dynamically as an argument/parameter to a script value in NPM or Yarn

Package.json { custom: "", "build:xapp": "webpack ....", "test:xapp": "karma ....", "build:yapp": "webpack ....", "test:yapp": "karma ...." ... n number of apps } If I need to pass the entire command for ...

Encountering an 'EPERM: operation not permitted' issue while trying to compile a Nuxt application

After executing npm run dev, encountering one of the following intermittent errors: EPERM: operation not permitted, mkdir 'D:\projects\my_project\.nuxt\components' EPERM: operation not permitted, lstat 'D:\projects ...

What is the most effective way to code and define a MatSelect's MatSelectTrigger using programming techniques?

How can I programmatically set the MatSelectTrigger template for a MatSelect instance using the provided reference? The documentation mentions a settable customTrigger property, but information on the MatSelectTrigger class or how to create one dynamically ...

Saving an array object to a file in JSON formatting

In the midst of my work on an Angular project, I have successfully compiled data into an array and am able to download it as a CSV file using an Angular package. However, I have not been able to locate a suitable package that allows me to download the sa ...

Ways to resolve the issue of the experimental syntax 'jsx' not being enabled

I encountered an issue while trying to implement react-fancybox and received an error. To resolve the error, I executed the following commands: npm install --save-dev @babel/preset-react, npm install --save-dev @babel/plugin-syntax-jsx, and npm install -- ...

Creating TypeScript utility scripts within an npm package: A Step-by-Step Guide

Details I'm currently working on a project using TypeScript and React. As part of my development process, I want to automate certain tasks like creating new components by generating folders and files automatically. To achieve this, I plan to create u ...

Setting up environment variables for node.js using NPM task: a comprehensive guide

I have a specific npm task that runs concurrently, involving node-inspector and node-supervisor. { "start": "concurrently --kill-others \"node-inspector\" \"set NODE_PATH=.&&supervisor -n error -- ./bin/www --debug\"", "prestar ...

Utilize ngx-filter-pipe to Streamline Filtering of Multiple Values

Need assistance with filtering an array using ngx-filter-pipe. I have managed to filter based on a single value condition, but I am unsure how to filter based on multiple values in an array. Any guidance would be appreciated. Angular <input type="text ...

Implementing Server-Side API Response Caching in React-Query and Next JS

My server-side rendering setup with react-query is working smoothly. I am aware that react-query stores a cache on the client side to serve data if the query key is fresh and available. Here is the code snippet depicting this setup - // pages/_app.tsx imp ...

Optimal design strategy for a website using NPM and Webpack

Currently diving into the world of webpack and pondering a theoretical scenario. Contemplating creating a module specific to each webpage in a site that will in turn require a module for universal site-wide code. Considering utilizing webpack's splitC ...

Updating Angular 13 with charts.js and ng2-charts may expose unfixed vulnerabilities

I am facing an issue while updating chart.js and ng2-charts in Angular 13. After running npm i [email protected] and npm i [email protected], I encountered vulnerabilities that couldn't be resolved with npm audit fix. Do I need to update any ...