The issue arises when the view fails to load while simulating a backend

Trying to test a specific element of the user interface requires a particular request to the backend with predefined data while allowing all other requests to pass through. Here's a more readable version in coffee script:

describe 'select2 combobox widget', ()->
  httpBackendMock = () ->
     angular.module 'httpBackendMock', ['ngMockE2E', 'fmAppApp']
        .run ($httpBackend)->
            dummyCategories = [
               {id: 1, name: 'foo', icon: '', user_id: 5},
               {id: 2, name: 'bar', icon: '', user_id: 5},
               {id: 3, name: 'baz', icon: '', user_id: 5},
               {id: 4, name: 'quax', icon: '', user_id: 5}
            ]

            $httpBackend.whenGET '/api/categories'
                .respond ()->
                    [200, dummyCategories]
            $httpBackend.whenGET /.*/
                .passThrough()
            $httpBackend.whenGET /^\/views\//
                .passThrough()
            $httpBackend.whenGET /^\/scripts\/.*/
                .passThrough()
            $httpBackend.whenGET /^\/scripts\//
                .passThrough()
            $httpBackend.whenGET /^\/bower_components\//
                .passThrough()
            $httpBackend.whenGET(/\.html$/).passThrough()

  browser
     .addMockModule 'httpBackendMock', httpBackendMock

The process involves creating a new module on top of the application module fmAppApp and using Angular ngMockE2E to inform Protractor about it.

To provide a complete picture, here is a simple statement within the describe block:

it 'should allow users to input anything', ()->
    browser.get 'http://localhost:9000/#/'
    element By.model 'category.selected'
        .click()
    input = element By.css '.ui-select-search'
    input.sendKeys 'newtestcategory'
    expect input.getAttribute('value')
        .toBe 'newtestcategory'

Running grunt protractor opens the browser, navigates to the specified URL (http://localhost:9000/#/), but results in a blank page and spec failures due to a "NoSuchElementError" for by.model("category.selected").

Unable to use Firebug for debugging, attempting to redirect logging from the browser console reveals an error message:

'http://localhost:9000/bower_components/angular/angular.js 11607:24 Error: Unexpected request: POST /api/login'

This prompts the question of whether 'api/login' should be added to passThrough() function.

Answer №1

If you want to make sure your mock is loading properly, you can insert console.log inside it. Additionally, in your Protractor test, include the following code:

browser.manage().logs().get('browser').then(function(browserLog) {
    console.log('log: ' + require('util').inspect(browserLog));
});

This will allow you to see browser logs in your Protractor console after executing browser.get.

Answer №2

Due to my oversight, I failed to provide all the necessary information initially. Omitting some code that I deemed irrelevant was actually counterproductive and caused confusion instead.

browser.get 'http://localhost:9000/#/'
element By.model 'email'
  .sendKeys 'dima@mail'
element By.model 'password'
  .sendKeys '123456'
element By.cssContainingText 'form[name=login] button', 'Войти'
  .click()

This particular code segment is crucial for logging in the user, as unauthorized users are restricted from accessing the view being tested. Although I believed I had covered all bases in terms of requests, it slipped my mind that the login request is a POST request. Fortunately, the solution was simple:

$httpBackend.whenPOST /.*/
      .passThrough()

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

Is there a way for me to delay the return from eval() until a callback is received?

A script in node.js has been created to retrieve JS code from a file and then run it through an eval(). The code that handles passing the JavaScript code to the eval function looks like this: // Read JavaScript code from a file var outputBuffer = '&ap ...

AJAX request failed to elicit a response

Recently, I've been facing an issue with my AJAX call to the API. Previously, it was functioning correctly and returning a response JSON. However, now I am unable to retrieve any JSON object. When using Mozilla, no error is shown but the response JSON ...

What could be causing the invalid_client error in response to this POST request I am making?

I am currently trying to establish a connection with an external API that has specific specifications: Host: name.of.host Content-Type: application/x-www-form-urlencoded Cache-Control: no-cache Through the utilization of the request module from npm, I am ...

Trouble with JavaScript loading in HTML webpage

<!DOCTYPE html> <html> <head> <title>Breakout Game</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <canvas width="900" height="450" class="canvas"></canvas> ...

The code is malfunctioning on this site (yet functions perfectly on a different website)

So I have a question that may seem silly, but it's worth asking. I'm attempting to create a sticky div element that stays in place when you scroll past a certain point on the page. This is the script I have: <script type="text/javascript"> ...

Guide to dynamically loading customer data into an HTML table using JavaScript

I'm looking to populate a table with data on customers including their name, customer ID, and rental cost. Can anyone help me with the JavaScript code needed to insert this data into rows of the table? Your assistance is greatly appreciated. Below is ...

AngularJS routing taking over non-root URLs after page reload is triggered

Currently, I am working on an AngularJS application that is not situated in the root location domain.tld/blog. All routing has been set up for the /blog base. The <base href="/blog"> tag has been included in the page header and html5Mode is enabled. ...

Prevent changes in Angular UI Modal scope from impacting the parent scope

I'm currently working on an Angular application where I need to control which apps from the app list are displayed on the home page. There's a section titled "Manage Apps" that allows me to manage the visible apps. Here is a link to a Plunkr exa ...

Achieving checked checkboxes based on already selected values in AngularJS

function Test1Controller($scope) { var storeid = window.localStorage.getItem("storeid"); var serverData = ["Samsung Galaxy Note", "Samsung Galaxy S6", "Samsung Galaxy Avant","Samsung Galaxy Young"]; $scope.items= [] ; var selectedvalue="Samsu ...

Javascript's ReferenceError occasionally acts inconsistently when using Firefox's scratchpad

While delving into the world of Javascript for learning purposes, I encountered an unexpected behavior. Let's explore this scenario: function hello(name) { let greet = 'Hello ' alert(greet + name) } hello('world') alert(gree ...

The functionality in Angular designed to retain data is not functioning as intended

I'm just starting out with Angular.js and I'm having trouble storing data in a single array. This array is located in a file called controllers.js and the data is coming from index.html. It's a simple form. <html > <head> &l ...

Creating an Image Slideshow on Your Website

I'm looking to create an image slideshow on my website that functions similarly to the one found at . However, I want the images to occupy the entire screen rather than having smaller images like the reference site. Can anyone offer guidance on how t ...

An error was thrown: Unexpected token ILLEGAL was detected

Working with Liferay 5.2 I would like to show a message related to the current language of Liferay. The code for this message is in Velocity language and can be found in navigation.vm file. <div id="navigation" class="sort-pages modify-pages"> ...

Using TypeScript with Node.js and Sequelize - the process of converting a value to a number and then back to a string within a map function using the OR

Currently, I am facing a challenge in performing addition on currency prices stored as an array of objects. The issue arises from the fact that the currency type can vary among 3 different types within the array of objects. The main hurdle I encounter is ...

Check input validations in Vue.js when a field loses focus

So I've created a table with multiple tr elements generated using a v-for loop The code snippet looks like this: <tr v-for="(item, index) in documentItems" :key="item.id" class="border-b border-l border-r border-black text ...

Behind the scenes, unable to launch due to Schema Error

My experience with Backstage was going smoothly until I ran into an issue after executing a yarn install this afternoon. Now, whenever I attempt to run yarn dev, it fails with the following error: [0] Loaded config from app-config.yaml [0] <i> [webpa ...

Issue with Swiper JS: The buttons on pages three and beyond are unresponsive

I'm having trouble with the functionality of the "Back" button on pages 2 and 3 of my website. Can anyone help me figure out why it's not working? My goal is to create a website that resembles a game menu, similar to Deus Ex The Fall. I'm u ...

Determine the selected radio button

----EDIT---- I am developing a jQuery mobile application and I need to determine which radio button is selected. This is the JavaScript code I'm using: function filter(){ if(document.getElementById('segment1').checked) { aler ...

collaborative data interchange in angularjs controllers

When it comes to managing shared state between controllers, I often struggle to find the best approach among the many options recommended on forums like Stack Overflow. To help clarify my thoughts, I've created a simple diagram outlining my basic idea ...

Content enclosed by directive markers

If I have a directive called my-directive, How can I access or modify the text between the directive tags within the directive code, for example: <my-directive> Custom Text </my-directive> Here is my directive code: app.directive('myDi ...