jasmine and protractor test failing due to use of byID

My HTML markup is all set and valid. While using WebStorm to debug my test cases, I am able to view this specific element without any issues...

<a id="privacyPolicy1234" on-tap="goPrivacyPolicy()" class="disable-user-behavior">Privacy Policy</a>

Despite the above successful inspection, my Jasmine test seems to be struggling to locate the same element.

it("should display a privacyPolicy ", function() {

    privacyPolicyElement = element(by.id('privacyPolicy1234'));
    expect(privacyPolicyElement.getText()).toContain("Privacy Policy");

However, an error keeps popping up:

 Message:
    NoSuchElementError: No element found using locator: By.id("privacyPolicy1234")

In an attempt to troubleshoot, I even tested it in a single line format but received a false output when expecting true.

expect(element(by.id('privacyPolicy1234')).isPresent()).toBe(true);

Answer №1

Opt for an explicit wait instead of using browser.sleep() in your test, as it leads to unreliability and slower performance. An example would be waiting for the presence of the "privacy policy" element:

var EC = protractor.ExpectedConditions,
    privacyPolicyElement = element(by.id('privacyPolicy1234'));

browser.wait(EC.presenceOf(privacyPolicyElement), 5000);

expect(privacyPolicyElement.getText()).toContain("Privacy Policy");

Keep in mind that with this method, Protractor will wait for a maximum of 5 seconds, checking for the element every 500 ms (default). Once the condition is satisfied, the wait stops. If the element still isn't present after 5 seconds, a timeout exception will be thrown.

Answer №2

Discovering that I'm conducting a pre-load test on the page..

To address this, I included

it("should display a privacy policy", function() {
    browser.sleep(2000);
    privacyPolicyElement = element(by.id('privacyPolicy1234'));

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

jQuery performs perfectly in Chrome but encounters issues in IE9/IE8 and other older versions of Internet

I've implemented this lengthy jQuery script to enable dynamic dropdown loading and updating when selections are changed. However, I'm facing issues in Internet Explorer where the script loads the dropdowns initially but doesn't trigger oncha ...

Mastering the Art of Clicking on Text Buttons with Selenium in Python

Hey there, I'm having some trouble trying to click on a select button using both xpath and css selector. It just doesn't seem to work. browser.find_elements_by_xpath('//div[@class="section-select-all"]').click() browser.find_elements_b ...

Sending JavaScript functions to PHP files via Ajax

My Current Project: I am currently developing a script that provides users with choices and generates new options based on their selections. To achieve this, I have created two scripts - one for the HTML structure of my page and another for fetching serve ...

Tips for adding a bounding box to an image received from the server

I've got a python server that is returning a collection of bounding boxes post OCR processing (using Tesseract or similar). "bbox": [{ "x1": 223, "y1": 426, "x2": 1550, &q ...

Developing a custom function to retrieve an array as a callback

I'm currently diving into the world of Node.js Struggling with implementing my own callback function in a certain method. It may seem like a straightforward task, but I'm finding it quite challenging to grasp. This specific function takes an ad ...

Tips for stopping Infinite Scroll from loading pages when the tab is inactive

I'm currently developing a website using Bootstrap 4 and jQuery, which consists of 3 tabs, each containing masonry elements loaded on scroll with the Infinite Scroll plugin. I'm trying to figure out a way for the Infinite Scroll to only load cont ...

Ways to incorporate a conditional statement within a dropdown menu

When a user interacts with a dropdown, I want to conditionally show different choices based on their selection. If the user clicks on a checkbox, a specific set of options will appear; if they click on a radio button, another set of options will appear. h ...

There was an error while parsing JSON on line 1, where it was expecting either a '{' or '[' to start the input

I've been struggling to extract data from my PHP array that is encoded using json_encode. I'm new to JQuery and have been attempting this for a few days without success. This code snippet is not producing the desired results: $.post('coord ...

Overlaying content: Innovative dropdown menu design

My goal is to create a dropdown box that overlays the content of a div when an icon within it is clicked. However, currently, the dropdown box is pushing down the content of the div instead of overlaying it. I have tried adjusting my CSS by setting some el ...

What is the best way to retrieve all string constants from HTML or JSX code?

UPDATE: I recently developed my own babel plugin CLI tool named i18nize-react :D I am currently in the process of translating an existing react application from English to another language. The string constants in the JSX are all hardcoded. Is there a sim ...

What is the explanation for the outcome "-9 >> 2 = -3"?

What is the reason behind 9 >> 2 = 2 compared to -9 >> 2 = -3 ? Wouldn't it make more sense for it to be -2 instead? ...

Using Django's if statement class to locate elements with Selenium via XPATH in Python

Currently, I am conducting a test on a basic notification page in Django using Selenium to verify if notifications are properly marked as read. In order to achieve this, my notifications model features a boolean field that defaults to False if the notifica ...

The functionality of Jquery-chosen appears to be malfunctioning when applied to a select element

I am encountering an unusual issue with Jquery-Chosen. There is a multi-select box within a pop-up where the options are populated using an ajax call. Strangely, Jquery-Chosen does not seem to work on it. However, if I use a static multi-select box in the ...

What is the best way to execute tests in different environments with Protractor?

Is it possible to execute specifications in various environments? Maybe by adjusting the protractor-config file? Could we do something along the lines of specs: ['../tests/*.js', server1], ['../more_tests/*.js', server2] within the ...

What are some methods for transferring the state variable's value from one component to another in React?

I have the following scenario in my code: there is a Form.js component that interacts with an API, stores the response in the walletAssets state variable, and now I want to create a separate Display.js component to present this data. How can I pass the v ...

What are the available methods for incorporating node.js dependencies into a Python package?

At the moment, I am working with a few Python packages that are not yet published and are used locally. For development purposes, I install these packages on Linux using a Bash script into an activated virtual environment. Here's how I do it: cd /roo ...

A guide to retrieving the XPATH or CSSPath of a web element using Selenium WebDriver in Java

I need to populate input fields with values that do not have any identifiers such as name, id or class. <form class="formclass"> <input type="text"> <input type="password"> </form> ...

Error: Preflight request returned a 405 HTTP status code when querying Ionic + CI backend

I am currently working on my first app using ionic with a codeigniter backend. However, I am encountering the "Response for preflight has invalid HTTP status code 405" issue in ionic + CI backend. Can anyone help me solve this problem? This is my controll ...

Using Python to enter text into an autocomplete field with Selenium

I am encountering an issue while attempting to enter text into an autocomplete feature using the send_keys command. Although I am able to input text into the field, the autosuggest box does not open as expected. Strangely, when I perform the same action ma ...

experimenting with a TypeScript annotation

I have created a simple decorator that can trigger either stopPropagation() or preventDefault() based on certain conditions. I have thoroughly tested this decorator in my application and am confident that it works correctly. However, I encountered an issue ...