Display the accurate prompt in the event of 2 `catch` statements

    elementX.isPresent()
        .then(() => {

              elementX.all(by.cssContainingText('option', text)).click()

                  .catch(error => {
                        throw {message: "Unable to select text in dropdown box"}
                    })

        })
        .catch (error => {
                throw {message : "element is not present"}

        })

I am facing an issue where I want to throw 2 different error messages, one if the element is missing and another if the given text is missing in the dropdown.

Currently, if the element is missing, the error is thrown correctly. However, if the element is present but the given text is missing, it still throws the "element is not present" error message. How can I modify the code to throw the correct error message?

I tried modifying the code as follows:

  elementX.isPresent()
 .then(
    _ => elementX.all(by.cssContainingText('option', text)).click()
          .then(() => {
                     console.log("hello")
          })
         .catch(_ => { throw {message: "Unable to select text in dropdown box"} }),
    _ => { throw {message : "element is not present"} }
  )

I followed a suggestion to change the code above, but it did not work as expected and only returned the last catch message.

Answer №1

To simplify the process for your specific scenario, you can use the following method:

elementX.isPresent()
  .then(
    _ => elementX.all(by.cssContainingText('option', text)).click()
           .catch(_ => { throw {message: "Unable to select text in dropdown box"} }),
    _ => { throw {message : "element is not present"} }

)

Update

Here's an example that you can run directly in the console:

elementIsPresent.then(
  _ => Promise.reject().then(_ => {}).catch(_ => { throw { e: '1' } }),
  _ => { throw { e: '2' } }
)

Before executing the code above, make sure to set the following:

elementIsPresent = Promise.resolve()

Upon running the snippet, it will result in a rejection with { e: '1' }. You can then update the variable as follows:

elementIsPresent = Promise.reject()

If you execute the snippet after making this change, it will lead to a rejection with { e: '2' }.

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

Ways to obtain the output of an If/Else statement

It seems like I might be missing something, but I am unsure of how to extract the result from an else-if statement. Take this code snippet that I've been working on for example: In this scenario, the output would read "It's warm!", and what I wa ...

Issue with Trix text editor not triggering the change event

Lately, I've been facing some difficulties in getting the tirx-change event to trigger when there are changes in the content of a trix-editor. Despite using React JS for the view, I haven't been able to identify the problem. Below is the code sni ...

NodeJs: Transforming strings into UUID v4 efficiently

Suppose I have the following string: const input = '83e54feeeb444c7484b3c7a81b5ba2fd'; I want to transform it into a UUID v4 format as shown below: 83e54fee-eb44-4c74-84b3-c7a81b5ba2fd My current approach involves using a custom function: funct ...

There is no index signature that accepts a parameter of type 'string' in the type '{ [key: string]: AbstractControl; }'

I'm currently tackling a challenge in my Angular project where I am creating a custom validator for a reactive form. However, I've encountered an error within the custom validators function that I am constructing. Below you will find the relevan ...

Check to see if modifying the HTML using jQuery results in any errors

Although it may sound straightforward, let me clarify. I am utilizing ajax calls to update the content of the body and I aim to trigger an alert if the updating process fails on the client side, specifically after receiving a response from ajax in case of ...

Scheduled tasks on Google Cloud Platform's App Engine are failing to run over the weekend

I am facing an issue with running a cron job in my node/express application using the node-cron library. The application is hosted on Google Cloud App Engine. My aim is to automate sending emails every day at 9 AM, but the cron seems to only work from Mon ...

Utilizing a filter within the ng-model directive

I have a question about using a filter with an h3 element. Here is the code snippet: {{ event.date | date:'dd-MM-yyyy' }} It's working perfectly fine and Angular is formatting the date as expected. However, when I try to use the same filte ...

The websocket connection established with apollo-server is somehow producing nonsensical output for the connection params

onConnect should receive the connectionParams supplied by the client and then validate that the token has not expired by checking the token property on the connectionParams object. On the client-side, I send these parameters in the following manner: const ...

Methods to retrieve an array's value list dynamically using the 'id' in Vuejs

**User.vue** <template> <div> <div v-for="list in lists" :key="list.id">{{ list.val }} {{ list.kk }}</div> </div> </template> <script> import { datalist } from "./datalist"; export default { name: "User ...

Creating a conditional property in TypeScript based on an existing type - a comprehensive guide

Imagine if I had the following: type Link = { text: string; link: string; } interface BigLink extends Link { some: number; something: string; else: string; } However, there's a variable that shares all these properties except for the fact ...

PHP script for batch string replacement

My function is triggered by a button click, where it takes two input IDs and forms a string as follows: var str = "headingText=" + $("#headingText").val() + "&centerText=" + $("#centerText").val(); $.ajax({ url: "indexpdf.php", data: str, ...

What is the most effective way to showcase a list of image URLs in HTML with Vue?

Currently, I am working with an array called thumbnails that holds the paths to various images. My goal is to use masonry in Vue to arrange these images in a grid format, but I'm encountering some difficulties achieving the desired outcome. This is m ...

Ways to resolve BuildJobExitNonZero issue on Digital Ocean

Hey everyone, this is my first time using Digital Ocean. I'm trying to deploy my app via Launch App, and my code is hosted on GitHub. However, when I try importing the code and building it, I'm encountering the following error that I don't u ...

Exploring the capabilities of dynamic pathname routing in Next.js

Consider this scenario: there is a path that reaches me as /example/123 and I must redirect it to /otherExample/123. My code utilizes next/router, extracting the URL from router.asPath. if(router.asPath == '/example/123') { Router.push(' ...

Angular2 does not load Js twice

I specified the path to my JS file in angular.cli. It loaded successfully during the initialization of the Angular app, but when navigating back to the component, it failed to load. Any suggestions on how to fix this issue would be greatly appreciated. Th ...

Unable to terminate TLS Connection

After establishing a connection using conn = TLS.connect, I am able to view this connection on the server side. However, when I attempt to terminate the connection by calling conn.end(), it does not actually end. The server continues to have an extra conne ...

Several treeviews for selecting data

I need some help with a specific assignment. The back end code is all set, but I'm struggling with the UI component. The task requires two tree views, one on the left and one on the right. The left tree view will contain states with multiple children, ...

What is the process of using AJAX, JavaScript, and HTML to submit data to a remote

I need help with sending data from an HTML page to a server. I have a JSON example below that illustrates what I want to achieve. On my HTML page, I have a question label and four options (A, B, C, D) as radio buttons. After clicking the send button, I ...

Ways to extract a single digit from the total sum of a date in the format of 15-06-2015

Is there a way to extract a single digit from the sum of a number in date format, such as 15-06-2015? My goal is to convert the date format 15-06-2015 into a single-digit value by adding up the numbers. For instance: In the case of 15-05-2015, the output ...

I am interested in rotating the yAxes scaleLabel in Angular using Chart.js

I have a vertical chart that I want to rotate horizontally. Unfortunately, there are no maxRotation and minRotation parameters for the yAxes, so I'm unsure of how to achieve this rotation. Although I found a similar question, I struggled to implement ...