Protractor testing the hover state changes in action

I am struggling to make the hover state work properly with my Protractor tests. The code I have is almost there but not quite...

  • Works perfectly in Firefox
  • Only works when I manually scroll the area into view in Chrome.
  • Fails completely in Phantom JS

obj
  .getCssValue('color')
  .then(function (color1) {
    browser
      .actions()
      .mouseMove(obj)
      .perform()
      .then(function () {
        obj
          .getCssValue('color')
          .then(function (color2) {
            expect(color1)
              .not
              .toEqual(color2);
          });
      });

Answer №1

We recently encountered a similar issue and found a solution that worked for us.

One helpful approach was to wait for an element to reach a specific CSS value using the browser.wait() method:

function waitForCssValue (elementFinder, cssProperty, cssValue) {
    return function () {
        return elementFinder.getCssValue(cssProperty).then(function (actualValue) {
            return actualValue === cssValue;
        });
    };
};

Usage example:

browser.wait(waitForCssValue(obj, 'color', color2), 5000);

In this case, we are waiting for up to 5 seconds for a CSS property color to match color2. Remember to call this after hovering over the element.


Another helpful tip is to scroll the element into view, which can resolve similar issues on Stack Overflow:

browser.executeScript("arguments[0].scrollIntoView();", obj);

Additionally, maximizing the browser window can be beneficial. This can be done in the onPrepare() function:

onPrepare: function () {
    browser.driver.manage().window().maximize();
},

Regarding PhantomJS:

It's worth noting that Protractor developers advise against using PhantomJS for end-to-end tests due to potential issues:

Note: We recommend against using PhantomJS for tests with Protractor. There are many reported issues with PhantomJS crashing and behaving differently from real browsers.

For more information, check out:

  • Using Protractor with PhantomJS

In conclusion, it may be necessary to let go of the "Fails in Phantom JS" argument.

Answer №2

Approach 1 :

To implement a simple hover effect, utilize the following function for your object.

<!DOCTYPE html>
<html>
<body>

<p onmouseover="colorin(this)" onmouseout="colorout(this)">
Testing colorin and colorout function for mouse hover
</p>


<script>
function colorout(x) {
    x.style.color = "#000000";
}

function colorin(x) {
    x.style.color = "#7FAF00";
}
</script>

</body>
</html>

Method 2 :

Incorporate waitForAngular(); in this version of obj to possibly address any delays related to angular:

obj
  .getCssValue('color')
  .then(function (color1) {
    browser.waitForAngular();
    browser
      .actions()
      .mouseMove(obj)
      .perform()
      .then(function () {
        obj
          .getCssValue('color')
          .then(function (color2) {
            expect(color1)
              .not
              .toEqual(color2);
          });
      });

Approach 3 :

Substitute .mouseMove(obj) with

.mouseMove(browser.findElement(protractor.B.id('foo')))
and customize it according to your specific codebase

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

The error message "TypeError: Cannot set property 'href' of undefined" occurred at angular.js line 12520 when trying to set $window.location.href

Has anyone tried using a directive function to redirect when clicking with ng-click? Here is the HTML code: <a ng-click="navbarlinksCtrl.clickedfr()" ng-class="{active: clickedfr()}">FR</a><br> <a ng-click="navbarlinksCtrl.clickeden( ...

Vue: The enigmatic world of ghost properties?

During my project work, I encountered the following code snippet: Main component - <ParameterModal>: <template> <modal-wrapper props="..."> <!-- ... other similar templates are present... --> <template v-else-if="moda ...

I'm perplexed by the inner workings of infinite ajax scroll in fetching additional posts

As someone who is new to JavaScript, I find it challenging to grasp the concept, especially when incorporating it with HTML. Despite this, I decided to experiment with infinite ajax scroll functionality. Below is my code snippet: var ias = jQuery.ias({ ...

Using handlebars template to render multiple objects in MongoDB with node.js and mongoskin

Dealing with an application that requires reading from two different collections in a Mongo database and passing the returned objects into a handlebars template has been quite a challenge for me. The code snippet I've been working with doesn't s ...

Node.js offers a variety of routing options and URL endpoints

app.use('/api', require('./api')); app.use('/', require('./cms')); The initial path is designated for my public API, while the latter is intended for the CMS dashboard. However, the setup is flawed as localhost:80/a ...

Maintain daily data that can be updated in JSON format

I'm working on a web application that utilizes client-side MVC with backbone.js and Spring on the server side. I have a scenario where I require data that needs to be updated daily or every couple of days. The data will be used on the client side for ...

Handling form submissions in Vue.js before navigating away from the current route

As a newcomer to VueJs, I am facing a problem with creating a form. My goal is to display an alert dialog with the message "you might lose the data, please save the data before you leave," and upon clicking 'yes', the form should be submitted and ...

The Angular multi select tree feature seems to be malfunctioning

I recently incorporated a plugin called angular-multi-select-tree from GitHub into my project. Here is how I added it: First, I installed it via bower using the command bower install angular-multi-select-tree --save. This updated the bower.json file to i ...

Just starting out - Can someone explain how to extract the text from a div class element?

I am attempting to obtain 1 WAVAX = 9.61353 PNG ($31.84) from <div class="sc-bdVaJa KpMoH css-flugrv">1 WAVAX = 9.61353 PNG ($31.84)</div> Which command should I use to achieve this? I realize it's a simple question, but as a be ...

Navigating Postback Scenarios in Selenium

Hello, I am currently working on a project that involves a scenario where if a user fails to select options from a dropdown menu and clicks the next button, an error message is displayed. I would like to verify if the displayed message matches the intended ...

Selenium Throws Element Not Interactable Instead of TimeOut Exception

When I reach the end of the pages where the next button is disabled, I want to click the apply button. There are two apply buttons - one visible and one invisible. Surprisingly, I encounter an element not interactable exception even though the button I am ...

Nested functions with async-await syntax

I'm attempting to showcase a nested countdown utilizing two nested loops. You can access the complete, functional code on this js.fiddle link. Two key segments are highlighted below: function sleep(ms) { return new Promise(resolve => setTime ...

Can the localization be embedded into an svg image?

I am currently engaged in a project that involves working with multiple languages in React using i18next. Within this project, I have a set of SVG images that contain fixed text in a specific language. Here is a snippet from one of the SVG images: <tex ...

Using CasperJS, learn how to effectively utilize the jQuery find() function

I'm looking to implement something similar to Cabybara within a function in CasperJS. My goal is to select parent divs and extract text from their child elements. Here's an example of what I want: $('div.education').find('h4&apos ...

When the state changes, initiate the animation

Currently, I am working with Vue.js and need to animate a navigation menu. My goal is to display two li elements when a user hovers over one of the navigation buttons. At the moment, I have set the data type showActivities to false by default and changed ...

Is there a way I can finish animating these divs in a diagonal motion?

I successfully made the blue and red divs move diagonally and switch positions. Now I am looking to achieve the same effect with the green and pink divs using a similar function. I am unsure about setting the position of both divs to 0 and 350 for those p ...

Shadows and reflections are not supported on Threejs PlaneGeometry

I'm relatively new to the world of 3D graphics and three.js, and I'm struggling to figure out how to achieve individually illuminated polygons on a PlaneGeometry. I've been working on applying some noise to the vertices' z-values in ord ...

Executing multiple jQuery Ajax requests with promises

I've been learning how to use promises gradually, and now I'm faced with the challenge of handling multiple promises. In my code snippet, I have two email inputs in a form that both create promises. These promises need to be processed before the ...

Encountered an error: "Unable to access property 'toString' of an undefined value within Multer

While working on uploading a text file using multer, I encountered an issue with getting the content of the file. When I tried using buffer.toString('utf8'), I received an error message saying Cannot read property 'toString' of undefine ...

Selenium: Locating the element correctly, yet unable to perform interactions with the input field

When trying to interact with an input field using the provided script: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_ ...