Tips for verifying the truth of one expectation from a set of multiple expected conditions

I am facing a situation in which I need to search for a text string that may appear in any of the fields within the returned results. This text could potentially be found in the title, summary, or description of multiple results. I am looking to create a test that can check all three fields and if at least one is true, then the test should pass.

Is there a way to set up multiple expect conditions with an OR condition?

Answer №1

To tackle this issue, you can utilize the protractor.promise.all() method:

var title = element(by.id("title")),
    summary = element(by.id("summary")),
    description = element(by.id("description"));

protractor.promise.all([
    title.isPresent(),
    summary.isPresent(),
    description.isPresent()
]).then(function (arrExists) {
    expect(arrExists.reduce(function(a,b) { return a || b; })).toBe(true);
});

In this scenario, the test will pass if at least one of the 3 fields is present.


If your concern revolves around waiting for an element to show up, you can opt for using the

protractor.ExpectedConditions.or()
technique:

var title = element(by.id("title")),
    summary = element(by.id("summary")),
    description = element(by.id("description"));

browser.wait(EC.or(
    EC.presenceOf(title), 
    EC.presenceOf(summary),
    EC.presenceOf(description)), 5000);

Answer №2

When working with Java, we can utilize the OR operator in the following manner:

      String expected="cool"; //this is my expected value

      String actual1="cool"; //get title from driver
      String actual2="xyz"; //get summary from driver
      String actual3="abc"; //get required text from driver

    Assert.assertTrue((expected.equals(actual1)) | (expected.equals(actual2)) | (expected.equals(actual3)));

If you need to search for a specific word within a sentence of a title or summary, you can use the following approach:

      String actual1="its very cool"; //get title from driver
      String actual2="xyz"; //get summary from driver
      String actual3="abcd"; //get required text from driver

    //checking for the presence of "cool"      
    Assert.assertTrue((actual1.matches(".*cool.*")) | (actual2.matches(".*cool.*")) | (actual3.matches(".*cool.*")));

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

NextJS's conversion of HTML to image is malfunctioning, resulting in the download of the identical reference

Having encountered difficulties with the html-to-image library while working on a Next.js project, I used the following code to convert images: import Image from "next/image"; import {toPng} from 'html-to-image' const divReferenceA = u ...

Maintaining a security token across multiple requests

A prototype application is being developed with the following features: An HTML website integrated with knockoutjs Communication with Web API services using jQuery/Ajax The goal is to restrict access to services only to authorized users. Security measur ...

Import Document from Project - Automation Framework Visual Studio 2010 C# using Selenium WebDriver

Is it possible to upload a file to a webpage using Selenium WebDriver in VisualStudio10 without specifying the file's path on my computer? Instead, can I add the file to the project and access it through code without having to hardcode the file path? ...

Is there a way to trigger another API call when clicking a button in Vue.js?

I recently started using vue js and I am facing a challenge with this component. I am attempting to trigger another API call when one of the list options is clicked. <template> <div> <h1 class="text-center text-4xl font-bold">Our ...

Failed to retrieve the item stored in the local storage

I am encountering an issue where I am unable to retrieve an item from local storage and display it. In store.js, I am trying to get the shippingAddress from local storage but it is not showing up in the form. Although I am able to set the shippingAddress i ...

Exploring the possibilities of node-webkit: node-odbc encounters a setback

Currently, I'm in the process of developing a desktop application utilizing node-webkit. The main functionality of the app involves querying an Oracle database. To establish the connection with the database, I have integrated node-odbc. To ensure tha ...

I'm struggling to concentrate and unable to type in the email field

Today while working on a website, I encountered something strange. In the footer section of the site, there is a quick contact form. However, I noticed that in Firefox, I am unable to focus on the email field. Surprisingly, this issue does not occur in Chr ...

React app (storybook) experiencing loading issues with @font-face

I am struggling to load custom fonts from a local directory. Can someone provide assistance? Below is the code I am currently using: @font-face { font-family: 'My Custom Font'; src: url('./fonts/MyCustomFont.eot'); src: url(&apo ...

Automating mouse scrolling with Selenium IDE: A step-by-step guide

My goal is to automate the scrolling of a webpage using Selenium IDE to capture certain events, such as reaching the end of a friend list on Facebook. Here's what I have accomplished so far: I can successfully log in to Facebook with my cu ...

Send the user to a specified destination

Currently, I am working on creating a form that allows users to input a location and have the page redirect to that location after submission. However, I am facing difficulty in determining how to set the value for action="" when I do not know what the loc ...

Send the user back to the previous page once authentication is complete

I am integrating Google authentication through Passport in my web application and I am facing an issue with redirecting the user back to the original page they requested after a successful sign-in. It seems like the use of location.reload() might be causin ...

Executing a function to erase the stored value in local storage during an Angular unit test

Looking to verify whether the localStorage gets cleared when I execute my function. Component ngOnInit() { // Logging out when reaching login screen for login purposes this.authService.logout(); } authService logout() { // Removing logged i ...

A guide to eliminating TextRow and inserting a string into JSON using NodeJs

To remove TextRow and add the string true to JSON in NodeJs, I have included the following code: NodeJs Code: function groupBy(objectArray, property) { return objectArray.reduce(function (acc, obj) { let key = obj[property] if (!acc[key]) { ...

Is your Jquery validation malfunctioning?

I am currently facing a challenge with required validation on an asp.net page. In order to validate the inputs, I have implemented multiple controls which can be hidden or displayed based on certain conditions. These controls include checkboxlists, dropdow ...

Determine the total number of nested objects within a JSON structure using Javascript

Consider the JSON structure below: { "id": "foo", "list": [ { "id": "A", "list": [ { "id": "B", "list": [ { "id": "C", "list": [ { ...

What could be causing the slowdown in my NodeJS IRC-bot?

Decided to challenge myself today by creating an IRC-bot using JavaScript with NodeJS. So far, everything is functioning correctly, but it seems like there may be a bottleneck somewhere. For instance, when I input a command multiple times, the initial res ...

Converting and downloading CSV to XLSX directly from the front end using TypeScript and React

After successfully converting a JSON response to CSV format for download using the function below, I am now looking to achieve the same functionality but with xlsx files on the front end. The current function works well for CSV files and handles Japanese ...

Unable to assign a value to the HTMLInputElement's property: The input field can only be set to a filename or an empty string programmatically

When attempting to upload an image, I encountered the error message listed in the question title: This is my template <input type="file" formControlName="avatar" accept=".jpg, .jpeg .svg" #fileInput (change)="uploa ...

Issue with Vue.js: The input value text is not being updated

In my current setup, I am dealing with an input component that is linked to a variable called 'searchText' in the parent component. This variable stores the text value of the search input. The {{searchText}} in the template updates accurately bas ...

Techniques for slowing down the propagation of events with jQuery

Is there a way to show a note after a user submits a form but before they leave the page? Here is an example of what I'm currently using: $('form').submit(function(event) { $('.note').show(); setTimeout(function() { ...