Chrome fails to select item in Protractor test, while Safari succeeds

While my test passes smoothly on Safari, I encountered an issue on Chrome where the following code snippet fails to work:

 it('should click the first source and get to the source preview page', function () {
    var grid_icon = element(by.css('.fa-th'));
    var sources = element.all(by.repeater('source in shownSources'));

    sources.get(0).element(by.tagName('a')).click();
    browser.pause();
    // Check url
    expect(browser.getCurrentUrl()).toContain('/source/');
});

Upon clicking the hyperlink, the URL should contain "/source/". This works perfectly in Safari, but not in Chrome.

Here is my Protractor configuration file:

exports.config = {
framework: 'jasmine2',
seleniumServerJar: '../node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar',
seleniumPort: 4444,
troubleshoot: false,
basePath: '../',
specs: ['protractor/***/**/*.js'],
baseUrl: 'http://localhost:9000',
capabilities: {
    browserName: 'chrome'
},
onPrepare: function() {
    browser.manage().window().maximize();
}
};

Edit: While the initial problem no longer exists, the test still behaves strangely. The following code works fine in Safari:

it('should add all sources to the list and show the cart icon on the source in inventory', function () {
    browser.get('/sources');
    var ordersources = element.all(by.repeater('orderSource in orderSources'));
    var sources = element.all(by.repeater('source in shownSources'));

    sources.get(0).element(by.css('a')).click();
    var add_to_cart_btn = element(by.binding('addBtnText'));
    add_to_cart_btn.click();
    browser.get('/sources');

    sources.get(1).element(by.css('a')).click();
    var add_to_cart_btn = element(by.binding('addBtnText'));
    add_to_cart_btn.click();
    browser.get('/sources');

    browser.pause();

    sources.get(2).element(by.css('a')).click();
    var add_to_cart_btn = element(by.binding('addBtnText'));
    add_to_cart_btn.click();
    browser.get('/sources');

    expect(ordersources.count()).toBe(3);

    sources.each(function (field) {
        var isInCart_symbol = field.element(by.css('.fa-cart-arrow-down'));
        expect(isInCart_symbol.getAttribute('aria-hidden')).toBe('false');
    });
});

However, in Chrome, the 'a' item isn't found the second time and the browser.sleep() is never executed, causing the next 'it' block to run prematurely.

EDIT: I managed to resolve the issue by using a different HTML element identified by the class attribute.

element.(by.css('.example'))

Answer №1

When you mention that it fails, are you referring to the expect failing? Here are 3 suggestions you could consider.

// Wait Till Url Contains
function WaitTillUrlContains(text, time, errMessage){
  if(typeof(time) ==='undefined') time = 5000;
  browser.getCurrentUrl().then(function (currentUrl) {
    browser.wait(function () {
       return browser.getCurrentUrl().then(function (newUrl) {
          var test = newUrl;
          if( test.indexOf(text) >= 0){
            // Found word
            return true;
          }

       });
    }, time , errMessage);
  });
};

(1) Include a wait before the expect statement.

it('should click the first source and get to the source preview page', function () {
    var grid_icon = element(by.css('.fa-th'));
    var sources = element.all(by.repeater('source in shownSources'));

    sources.get(0).element(by.tagName('a')).click();

    // Check url
    WaitTillUrlContains("/source/", 5000, "✗ Failed to wait for page to load");
    expect(browser.getCurrentUrl()).toContain('/source/');
});

(2) Implement a .then() function after performing the click action.

it('should click the first source and get to the source preview page', function () {
    var grid_icon = element(by.css('.fa-th'));
    var sources = element.all(by.repeater('source in shownSources'));

    sources.get(0).element(by.tagName('a')).click().then(function(){

        // Check url
        WaitTillUrlContains("/source/", 5000, "✗ Failed to wait for page to load");
        expect(browser.getCurrentUrl()).toContain('/source/');
    });
});

(3) Utilize a .then() function after retrieving the element and then proceed with the click event.

it('should click the first source and get to the source preview page', function () {
    var grid_icon = element(by.css('.fa-th'));
    var sources = element.all(by.repeater('source in shownSources'));

    sources.get(0).element(by.tagName('a')).then(function(elem){

        elem.click();
        // Check url
        WaitTillUrlContains("/source/", 5000, "✗ Failed to wait for page to load");
        expect(browser.getCurrentUrl()).toContain('/source/');
    });
});

Answer №2

The issue with your link not being found the second time is related to how you are reloading the page using browser.get(). This causes the handle for sources to be lost, resulting in webdriver not being able to identify which element to interact with.

To resolve this, you can either redefine the sources variable after the page reload or consider avoiding reloading the page altogether.

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

Monitor $localStorage for any modifications

Utilizing the ngStorage module, which can be accessed at this link: https://github.com/gsklee/ngStorage In order to maintain a lastSeenId for a real-time social feed in my hybrid app (Laravel/AngularJS), I am storing it in localStorage instead of $scope. ...

Sluggish behavior detected in hybrid AngularJS and Angular application when accessed through Safari browser

Lately, I have embarked on the task of migrating an AngularJS application to Angular 4 using the upgrade module. Within my AngularJS directives, I am utilizing a third-party library (ngFlow) for file uploads via XMLHttpRequest.send(). Everything functions ...

Exploring Twig variables in Node.js with the node-twig package

Despite following the documentation meticulously, and experimenting with various methods, I am still unable to achieve success. I have attempted using the code snippet below, trying to reference the variable in the main file like this: // None of the opti ...

Distinguishing between a regular JavaScript variable and one annotated with a dollar sign

Many responses have addressed the question of using a dollar sign in JavaScript variables. In essence, the dollar sign functions as an identifier in JavaScript variables. However, I am curious if there are other distinctions between regular variables and ...

AngularJS allows users to easily select and copy all text from both div and span elements within a specified range. This feature makes it

I am working on implementing a select all feature using AngularJS for text displayed in a structure similar to the one below. Once all the text is selected, users should be able to press ctrl + c to copy it. <div ="container"> <div class="header ...

Changing the source of the Carousel image in HTML when it is the active image

I have a unique setup with two carousels on a single page. My goal is to change the src of the first item in the carousel when the second item is clicked. Here is the code for the second carousel item: <div style="max-width: 20%; max-height: 20%;" cl ...

Utilizing cylon.js with Nest Thermostat

Experiencing errors while trying to retrieve thermostat ambient Temperature with cylon.js I have already replaced ACCESS_TOKEN with my unique access token and device id Sample Code: var Cylon = require('cylon'); Cylon.robot({ connections: { ...

Using a JavaScript loop to modify the color of the final character in a word

I am curious to find out how I can dynamically change the color of the last character of each word within a <p> tag using a Javascript loop. For example, I would like to alter the color of the "n" in "John", the "s" in "Jacques", the "r" in "Peter" ...

What is the process for converting a CSV file to JSON format using node.js?

I am working on converting a CSV file from Clash Royale into an array. The CSV file has over 40 properties in its header or data. For example: name level count upgrade cost common 14 5, 20, 50, 100 rare 12 50, 150, 400, 1000 Below is my node.j ...

Unable to fetch options for drop-down list from Database

As someone who is new to frontend application development, I am facing a challenge in populating a Select list from the database. Despite following similar approaches like the one discussed in How to populate select dropdown elements with data from API - R ...

PHP functions triggered by ajax fail to function properly when called repeatedly

I have encountered an issue with my javascript function that calls a php script upon clicking a button or link to retrieve data. Everything works well, except when I attempt to call data from an already outputted content via the same function. Let me share ...

Stopping setTimeout when leaving the current page: Is it possible?

Good evening, I am looking for advice on how to cancel a setTimeout function when a user navigates to other pages (for example, by clicking other links or pressing the back button in the browser, but not by changing tabs). I have attempted to use the windo ...

Choose your location with React: Country -> Province/State -> City?

It seems to be a common need for people to have a feature where selecting a country would then filter down the provinces or states available for that country, and then from there, narrow down to city selections. I've been searching NPM for a solution, ...

What is the best way to send an object to Vue Component?

How can I properly call a Vue Component with a data object? The todo-item tag works as expected, but the todo-item2 tag does not produce any output. I was expecting the same result. Here is the HTML code: <div id="app"> <todo-item v-bind:te ...

Discover how to retrieve the calculated percentage within CSS with the assistance of jQuery specifically designed for Webkit

I'm currently working on a simple sliding animation. However, the div that slides in is utilizing percentages for its width and right positioning. The issue arises specifically in Webkit browsers. When using jQuery to retrieve the value, it returns t ...

Learn the process of displaying and concealing elements when hovering over the parent element with Javascript

I am facing a challenge with my containing div that has a 'h1' title and a 'p' description inside it. My goal is to have the 'description paragraph' hidden while the 'title' is visible when the page loads, and to hav ...

Accessing elements in AngularJS through ng-click function

According to information provided in this answer, it is necessary to pass the $event object to the ng-click function in order to access the target element. $scope.setMaster = function(obj, $event){ console.log($event.target); } Although using event.t ...

Personalized style for text overflow property

The application is created using Angular. Within a component, we have a div containing some text: <div>abcdefghijklmnop<div> Depending on the screen size, the text should either be fully displayed or clipped. I discovered the property 'te ...

What is the most effective method for postponing the loading of JavaScript?

Incorporating a bootstrap theme into my project has required me to include several javascript files. The challenge arises when some pages load dynamic content from the server, resulting in the entire HTML not being present when the javascript files are exe ...

Troubleshooting an Ajax request in Google Scripts with a '$' variable bug

I am trying to implement an AJAX Request using the ajax() method. I have created the functions below on scripts.google.com, but encountered the following error: function AjaxCall() { var arr = { City: 'Moscow', Age: 25 }; $.ajax({ u ...