Unable to physically tap on the checkbox input, though able to perceive the value it holds

When running my protractor test, I encountered an issue with the following statement:

await element(by.model('publishCtrl.isPublishedInAllRegions')).click();

The test failed and returned an error message stating "ElementNotVisibleError: element not visible". This is puzzling because a previous assertion that I included for debugging purposes, which checks if the element is present, actually passed:

expect(await element(by.model('publishCtrl.isPublishedInAllRegions')).isPresent()).toBeTruthy();

Why would this happen? Interestingly, if I omit the first assertion, the subsequent assertion to read the checkbox value works fine:

expect(await element(by.model('publishCtrl.isPublishedInAllRegions')).getAttribute('value')).toBeTruthy();

For reference, the checkbox and its parent element are defined as follows:

<label ng-transclude="" pan-form-option-type="checkbox" class="p6n-checkbox p6n-checkbox-label">
  <input type="checkbox" ng-model="publishCtrl.isPublishedInAllRegions" ng-click="publishCtrl.toggleAllRegionsSelection()" ng-required="!publishCtrl.publishedRegionsCount" class="ng-pristine ng-untouched ng-valid ng-not-empty ng-valid-required" jslog="47386;track:generic_click" aria-invalid="false">
  <span class="p6n-form-label" ng-transclude=""> All regions </span> 
</label>

Answer №1

If you want to interact with radio or checkbox buttons using JavaScript executor, you can do so without worrying about the element's clickable state.

Alternatively, you can click on the label or span element that is nested within the input element directly.

For example:

Using JS click:

export async function jsClickButton(button: ElementFinder) {

    try {
        return await browser.executeScript('arguments[0].click()', button).then(async() => {
            console.log('Element has been clicked.');
        });
    } catch (error) {
        console.log('Element could not be clicked', error);
    }
}

Or simply click normally (in your specific case).

await  element(by.xpath('//input[@ng-model=\'publishCtrl.isPublishedInAllRegions\']/following-sibling::span')).click();

Answer №2

When dealing with an input tag that may have zero height, it is essentially invisible but still present in the DOM for interaction.

In this case, we need to click on the label containing the input tag, rather than directly clicking on the input tag itself.

Attempting to click on by.modal('modalvalue') of Protractor JS will target the element with a CSS locator like

[ng-model='publishCtrl.isPublishedInAllRegions']
, returning the input button instead of the label.

To locate the unique value with ng-modal in Angular, we can use an xpath approach:

  await  element(by.xpath("//a[@ng-modal='publishCtrl.isPublishedInAllRegions']/parent::label")).click()

However, if the ng-click is only configured for the input tag, which triggers onclick events, then try the suggestion above and provide feedback.

If the solution does not work, you can directly click on the anchor by executing JavaScript using a JavaScript executor when it is not visible:

var element =element(by.xpath("//a[@ng-modal='publishCtrl.isPublishedInAllRegions']");
browser.executeScript("arguments[0].click()",element);

or simply utilize jQuery for direct execution:

browser.executeScript("$(\"input[ng-modal='publishCtrl.isPublishedInAllRegions'])\").click()",element);

Answer №3

await element(by.css('input[type="checkbox"]')) .click();  

To confirm the selection

  expect(await element(by.css('input[type="checkbox"]')).getAttribute('value').isDisplayed().then((value: boolean) =>{
    expect(value).toBe(true);
    });

I trust that the information above will assist you. Consider utilizing CSS for locating elements as it is recommended for Protractor testing.

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

Issues with routeparams are preventing ng-repeat from functioning properly, without any response or resolution

For my shoppingCart project, I am working on dynamically bringing data into views. I am using routeParams in template.html but facing an issue. The data is arriving normally as checked via ng-href="#/store/{{something.name}}/{{ child.name }}" but it isn&ap ...

Troubleshooting the error message: "Unable to add or update a child row due to a foreign key constraint failure"

Currently, I am facing a challenge while trying to perform simultaneous inserts into two tables (parent and child) using PHP. Here's my approach: I first insert a unique number along with the original data into the parent table. Subsequently, I retrie ...

Embed an angular directive into HTML dynamically

I have a multitude of directives within my angular module, ranging from MyDirective1 to MyDirectiveN. Furthermore, I keep track of these directive names in MyDirectiveList: ['MyDirective1', 'MyDirective2', ..., 'MyDirectiveN' ...

How to effectively merge DefaultTheme with styled-components in TypeScript?

I am facing an issue with integrating a module developed using styled-components that exports a theme. I want to merge this exported theme with the theme of my application. In my attempt in theme.ts, I have done the following: import { theme as idCheckThe ...

In Nodejs, the value of req.headers['authorization'] is not defined when using JWT (JSON Web Token)

Below is the implementation of JWT in Node.js: const express = require("express"); const jwt = require("jsonwebtoken"); const app = express(); app.use(express.json()); const user = [ { name: "Rohan", id: 1, }, { name: "Sophie", id ...

Utilizing Angular JS within a callback function from an external source

After updating my Angular 1.3 application to version 1.6, I noticed that a couple of functions are no longer working properly. These functions are used within a vanilla JS script called from a controller to navigate to another controller and view. Below ...

JavaScript: Different ways to utilize the reduce method

Creating an array for search purposes based on a string like BBC Sport, the desired output array will be: [ 'BBC', 'BB', 'B', 'Sport', 'Spor', 'Spo', 'Sp', 'S' ] An implement ...

Having trouble getting the finally clause to work properly in Angular JS version 1.7

In my current project, I am utilizing Angular JS 1.7. Recently, I encountered an issue while using the finally clause within a promise: $http.put( url, null, { headers: { 'Content-Type': 'application/json' } } ).then( ...

Adding a dynamic form to each row in a table: A step-by-step guide

Hey there! I'm facing a challenge with dynamically generated rows in a form. I want to send only the inputs from the selected row when a checkbox is clicked. Can you help me figure this out? I tried using let rowForm = $(event.currentTarget).closest( ...

Turn off automatic vertical scrolling when refreshing thumbnails with scrollIntoView()

My Image Gallery Slider has a feature that uses ScrollIntoView() for its thumbnails, but whenever I scroll up or down the page and a new thumbnail is selected, it brings the entire page back to the location of that thumbnail. Is there a way to turn off t ...

AngularJS's ScrollTo function allows users to scroll to a specific

Trying to implement a quick nav that smoothly scrolls to different sections on the page when a link is clicked. Currently using a guide provided by Treehouse for reference. $("#quickNav a").click(function(){ var quickNavId = $(this).attr("href"); ...

Is the MVC pattern adhered to by ExpressJS?

Currently, I am diving into the world of creating an MVC Website With ExpressJS using resources from I'm curious to know if ExpressJS strictly adheres to the MVC pattern, or if it follows a different approach like Flask which focuses more on the "C" ...

Tips for handling a login window that pops up after a session expires in Selenium

While exploring the functionalities of the Polarion application, I encountered a situation where the session expires after a few minutes, prompting the login window to appear. The session seems to expire randomly, making it difficult for me to pinpoint wh ...

Issue "cannot update headers after they have been sent"

I've been working on a node.js application to retrieve movie listings using the omdb api. However, I'm encountering an error when attempting to access the /result route. The error message is as follows: Error: Can't set headers after they ar ...

What is the safest way to convert a local file path to a file:// URL in Node.js?

In my node.js application, I am faced with the task of converting local file paths into file:// urls. After some research, I came across the File URI scheme on Wikipedia and I believe that there must be a solution out there or perhaps even an npm module t ...

Ensure that each page completely loads before proceeding in Selenium

I am currently working on a simple script that reads data from various pages. The code I have written so far is as follows: def parsePage (https): driver = webdriver.Chrome("path\chromedriver.exe") driver.get(https) content = driver.page_ ...

Guidelines for iterating through a nested JSON array and extracting a search query in Angular

I'm currently working with a complex nested JSON Array and I need to filter it (based on the name property) according to what the user enters in an input tag, displaying the results as an autocomplete. I've started developing a basic version of t ...

When attempting to print a Rectangle on my webpage using JavaScript and taking user input, I encountered an error stating that 'str' is not defined

I'm trying to implement a feature where clicking on the "try it" button will prompt the user for the number of lines and then print that many lines in the form of a rectangle. However, when I run my code, nothing appears on the DOM and there is an err ...

In a peculiar occurrence, the behavior of array.splice is exhibiting unusual characteristics within an

Be sure to take a look at this plunkr for reference: http://plnkr.co/edit/FQ7m6HPGRrJ80bYpH8JB?p=preview I'm encountering an issue where, after adding an element and then deleting it from the array using the splice method, everything becomes disorg ...

Learn how to iterate over an array and display items with a specific class when clicked using jQuery

I have an array with values that I want to display one by one on the screen when the background div is clicked. However, I also want each element to fade out when clicked and then a new element to appear. Currently, the elements are being produced but th ...