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

ReferenceError: 'exports' is undefined in the context of Typescript Jest

I'm currently delving into unit testing with jest and encountered an error that looks like this: > npm run unit > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="771f181012374659475947">[email protected]</ ...

Angular service is able to return an Observable after using the .then method

I am currently facing an issue with retrieving the authentication status in a service method. Everything seems to be working fine except for the return statement. I am struggling with the usage of .then inside .map and I am unable to figure out how to retu ...

Dynamically generating an Angular component and populating it with data

I am currently working with Angular 7/8 and I have some code that adds a new component dynamically. In the parent component, my .ts file includes the following: PARENT COMPONENT Within the .ts file: @ViewChild(InjectDirective) injectComp: InjectDirect ...

Angular 15 experiences trouble with child components sending strings to parent components

I am facing a challenge with my child component (filters.component.ts) as I attempt to emit a string to the parent component. Previously, I successfully achieved this with another component, but Angular seems to be hesitant when implementing an *ngFor loop ...

The function for batch insertion only functions with Postgresql and SQL Server databases

I am a beginner in JavaScript and I am currently working on creating a new restaurant. I have come across a code snippet that inserts a relation into a join-table: await newRestaurant.$relatedQuery('tags', trx).relate(tagIds); Is it not possible ...

Error message pops up in WebStorm when attempting to access the map object in Angular

Within one of the services in my Angular application, I have utilized the map() function to retrieve data from the GitHub API. getUser(username: string) { // Regular Expression used for String Manipulation return this.http.get('https://api.github.com ...

Converting a React Typescript project to Javascript ES5: A step-by-step guide

I have a react typescript project and I need to convert the source code (NOT THE BUILD) to ES3 or ES5 JavaScript. This is because I want to use this code as a component in another React app. Can you suggest which preset and plugins I should use for this t ...

What techniques can be employed to dynamically modify Typescript's AST and run it while utilizing ts-node?

Below is my approach in executing a TypeScript file: npx ts-node ./tinker.ts In the file, I am reading and analyzing the Abstract Syntax Tree (AST) of another file named sample.ts, which contains the following line: console.log(123) The goal is to modify ...

Troubleshooting: Angular Application Fails to Launch Following Git Repository Cloning

After successfully completing the Angular Tour of Heroes tutorial with some tweaks, I attempted to push it to a GIT repo in VSTS and clone it onto another machine. However, upon cloning it to the new machine, I am encountering issues trying to get it up an ...

"Encountering an issue while installing Express with the Node Package Manager

While trying to install express using the node package manager on my Windows 7 x64 machine, I encountered the following error. I am running cmd as an administrator. Can anyone provide assistance? Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Mic ...

NextJS: Build error - TypeScript package not detected

I'm facing an issue while setting up my NextJS application in TypeScript on my hosting server. On my local machine, everything works fine when I run next build. However, on the server, I'm encountering this error: > next build It seems that T ...

Transforming a JSON file that has been previously converted to an Observable into a TypeScript map within an Angular application

There is a json data file named dummy, with the following structure: [ {"key":"KEY1", "value":["alpha","beta","gamma"]}, {"key":"KEY2", "value":["A","B","C"]}, {"key":"KEY3", "value":["One","Foo","Bar"]} ] The goal is to convert this json f ...

What is the most efficient method to locate the precise location of a particular file within a node dependency?

Currently, I'm facing an issue accessing a file located within a node dependency. In the past, I solved this by directly referencing the file using require('../../node_modules/foo/bar.png'). However, with npm v3, this hardcoded path is no lo ...

Angular2 Service Failing to Return Expected Value

It's frustrating that my services are not functioning properly. Despite spending the last two days scouring Stack Overflow for solutions, I haven't been able to find a solution that matches my specific issue. Here is a snippet of my Service.ts c ...

What is the reason behind the Typescript compiler not converting .ts files to .js files automatically?

Displayed below are the folders on the left showcasing my Typescript file in /src (blue) compiled into Javascript in /dist (purple) using tsc. https://i.stack.imgur.com/7XNkU.png In the source file on the left, there is a reference to a .ts module file t ...

Interfaces and Accessor Methods

Here is my code snippet: interface ICar { brand():string; brand(brand:string):void; } class Car implements ICar { private _brand: string; get brand():string { return this._brand; } set brand(brand:string) { this. ...

After utilizing the d3-scale function to declare an object, my developer visual suddenly ceases to function

Upon completing a section of a Power BI tutorial, the developer encountered a visual that displayed nothing but a blank page (despite running correctly). Unable to pinpoint the issue, debugging was initiated. The following test code snippet for debugging w ...

Is it possible to merge these two scripts into a single one within Vue?

As a newcomer to VUE, I am faced with the task of modifying some existing code. Here is my dilemma: Within a single component, there are two script tags present. One of them contains an import for useStore from the vuex library. The other script tag incl ...

Encountered a problem during npm installation while trying to add the libxml

Attempting to install the libxml package using the command: npm i libxml Encountering an unfamiliar error message as follows: % npm i libxml npm ERR! code 1 npm ER ...

Angular encountered an error when trying to access the property "fruits" of an undefined object

When working with Angular, I encountered an issue where I received the error message "cannot read property 'fruits' of undefined." Within my class definition, I have included the following: export class MyClass implements OnInit { fruits: any[] ...