Experimenting with protractor to test UI-bootstrap-alert components

I am currently working on using protractor to verify the correct display of alerts. Below is an excerpt from one of my spec files.

The HTML code snippet looks like this:

<div class="alert ng-scope floater bottom left fadeZoomFadeDown alert-success" 
ng-class="[type ? 'alert-' + type : null]" 
style="display: block;"><!-- ngIf: dismissable -->
  <button type="button" class="close ng-scope" ng-if="dismissable" ng-click="$hide()">
  </button><!-- end ngIf: dismissable --> <strong ng-bind="title" class="ng-binding"></strong>&nbsp;
 <span ng-bind-html="content" class="ng-binding">Test Person added successfully. </span>
</div>

Included in the spec.js file for handling this alert:

it('should be able to add a person to the person list from the list person view', function() {
    element(by.linkText('Persons')).click();
    personsUrl = browser.getCurrentUrl();

    count = element.all(by.repeater('person in persons')).length;
    element(by.linkText('Add Person')).click();
    element(by.id('person.firstName')).sendKeys('Test Person');

    //Returning to the persons list view after adding a person.
    var alertElement = element(by.css('.alert-success'));
    element(by.buttonText('Add Person')).click().then(function(){
        expect(browser.getCurrentUrl()).toEqual(personsUrl);
        browser.wait(protractor.ExpectedConditions.visibilityOf(alertElement), 10000) //Waiting for 10 seconds until alert is visible
        .then(function(){ 
             expect(alertElement.isDisplayed()).toBe(true);
        });
    });
});

An error message is appearing in the terminal stating "Failed: No element found using locator: By.cssSelector(".alert-success")". However, upon manual inspection during browser pause, I can confirm that the alert contains multiple classes and "alert-success" is indeed among them. I am puzzled by what could be causing this discrepancy.

Any assistance would be greatly appreciated.

Answer №1

It seems like the alert may not show up immediately and won't always be present in the DOM. You'll need to wait until it becomes visible before performing any actions on it. Here's an example of how you can do that:

var successAlert = element(by.css('.alert-success'));
element(by.buttonText('Add Tenant')).click().then(function(){
    browser.wait(protractor.ExpectedConditions.visibilityOf(successAlert), 10000) //Wait for 10 seconds for the alert to become visible
    .then(function(){ 
         expect(successAlert.isDisplayed()).toBe(true);
         //Other actions to perform on the alert
    });
});

I hope this explanation helps.

Answer №2

When testing the <uib-alert> component, there is a challenge with using the dismiss-on-timeout attribute in Protractor. The timeout is detected as an outstanding action, causing Protractor to pause evaluation until the timeout elapses. Once the timeout expires, the <uib-alert> disappears.

Please note that this example pertains to Angular 1.5 and Protractor 3.3.0.

Assuming we have a DOM element:

<uib-alert id="successAlert" type="success" close="$ctrl.closeAlert('success')" dismiss-on-timeout="{{$ctrl.alertTimeout}}">Success - You are great, good job!</uib-alert>

The following browser.wait block will not function correctly (it will time out):

doSomethingToMakeTheAlertDisplay();

browser.wait(protractor.ExpectedConditions.visibilityOf(this.successAlert), 15000).then( function(result) {
  console.log("browser.wait is done waiting");
  return result;
})

However, if you temporarily disable synchronization and then re-enable it, the browser.wait will work as expected:

doSomethingToMakeTheAlertDisplay();

browser.ignoreSyncronization = true;

browser.wait(protractor.ExpectedConditions.visibilityOf(this.successAlert), 15000).then( function(result) {
  browser.ignoreSyncronization = false;
  console.log("browser.wait is done waiting");
  return result;
})

The root cause of this issue is discussed further here: https://github.com/angular/protractor/issues/169

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 switch the state of one element while causing other elements to respond

Here is the code snippet I am working with: function myFunction(x) { x.classList.toggle('change'); } body, hmtl { background- color: black; } .text { color: Black; } a { text-decoration: none; } .container { display: inline-block; ...

The margin set in Bootstrap isn't making any difference

I'm currently utilizing bootstrap 4 in my react project, and while the bootstrap components seem to be functional, I am facing an issue where setting margins does not work as expected. Here is a snippet of code from one of my pages: return ( < ...

How to showcase images and text from an array of objects using React

I'm currently working on a React lightbox that is supposed to display a loop of objects containing images and text. However, I'm facing an issue where the images are not showing up with the corresponding text label as intended. It seems like I a ...

What is the best way to connect my 'Projects' folder to app.js within a React application?

import "bootstrap/dist/css/bootstrap.min.css"; import Navbar from './components/Navbar'; import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import Home from './components/Home'; import Project ...

Sending multiple ajax requests with identical data

I have been trying to send multiple requests to the same URL. The goal is to ban all the users that are included in the 'users' array by sending individual POST requests for each user. However, I am facing an issue where I keep getting the same d ...

Can one enhance panel items in a Kendo panelbar widget by incorporating custom attributes?

I'm in search of a method to include and reference custom attributes, like an ID, for each item in the items array within my data source when using the PanelBar Widget with Kendo UI for JQuery. My components are being developed in React. Here is an ...

Learn how to effectively declare data as global within Angular2 or Typescript

I am facing an issue with fetching the id inside the Apiservice despite being able to get it in the console. Can anyone provide assistance on how to solve this problem? TS: deleteProduct(index,product) { var token = this.auth.getAccessTokenId(); ...

Error triggered by removeChild function

The form I have created has the following structure: <form id="donate_form" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name=" ...

Utilize JavaScript and jQuery to locate a particular character within a string and move it one position back in the sequence

Can you locate a particular character within a string and move it to the position before? For instance: Consider the following string: Kù Iù Mù The desired output is: ùK ùI ùM ...

What is the process for adding a JavaScript banner using JavaScript code?

I would like to insert a javascript ad banner using code. Here is the current code snippet: <script> document.write("<script src="http://adsite.com"></script>"); </script> Unfortunately, it doesn't seem to be functioning p ...

Displaying a base64 image in a new tab with JavaScript on iOS devices

Currently, I am receiving base64 data for an image and would like to open it in a new tab. To achieve this, my code looks something like this: var window = new window(); window.open("<iframe src="+base64Url+"); Interestingly, this method works perfec ...

Tips for incorporating Emoji into a messaging platform similar to SendBird (non-native, web-based application)

Hey everyone! I'm currently working on a project that involves integrating the "SendBird" messaging service. I've successfully implemented all the basic functions, but now I want to incorporate Emoji for better user experience. I'm feeling a ...

Incorporate JavaScript code into an Angular UI-Router view

I can't seem to find an answer to this question anywhere, so I'm hoping someone here can help me out. In my AngularJS app, I am using ui-router to load multiple views into the same <ui-view> element. I am trying to implement Jquery UI&apos ...

Strategies for managing the #document element within an iframe

While testing the current portal, I encountered an issue where I couldn't create any xpath locators. After some investigation, I realized that the problem stemmed from an '#document' causing the path to be cut off and redirecting the "copy x ...

Implementing a dynamic navbar in Vue.js using Firebase authentication

I've been diving into a Vue.js project that allows users to sign up and log in. I've incorporated Firebase Authentication for the login and sign up functionality. Successfully, I've implemented the feature where the navbar state changes: whe ...

Updating subdata in an array using Reactjs handleChange

I am facing an issue with my handleChange function. While I can easily change data in the same directory without any problem, I'm struggling to update sub-data. I would like to find a clean solution for this without having to add extra functions withi ...

Integrating a covert cipher into user registration

Apologies for any lack of knowledge on this framework and its elements as I am in the process of learning through practice. I have configured a simple application using angular-fullstack and am currently exploring some tasks that I would like guidance on. ...

Prevent iframe loading from occurring

I am currently working with an iframe where I upload files, but I am facing a challenge. How can I stop the loading process in the middle of it? I attempted to modify the src using a jQuery function attr(), however, it was not effective. Removing the entir ...

Alter the tab's color upon clicking the button

I am working on a multi-step form and I wanted to enhance the user experience by adding a button at the bottom of the page for easy navigation instead of relying on tabs at the top. Although the button functions as intended, there is an issue with the tab ...

What is the proper way to specify a file destination in React?

After reading this article on downloading objects from GCP Cloud storage bucket, I'm curious about setting the file destination dynamically in React. Is there a way to achieve this? The article can be found at: https://cloud.google.com/storage/docs/do ...