The switch statement is not yielding any results

I am currently working on a test that involves processing a string through a switch statement. However, I am facing an issue where the integer value set in the case of the switch statement is not being passed correctly. As a result, the subsequent if statement fails to work as expected.

Here is the object for the switch:

var appsNotPurchased = 0;
this.checksHomeSublevel = function(mmCode) {
    browser.get('https://iplan-qa.meetingmatrix.com/Home/Index/' + mmCode);
    marketingObjects.level.getText().then(function(text) {
      var homeText = text;
      browser.get('https://iplan-qa.meetingmatrix.com/Home/Apps/' + mmCode);
      expect($('div.apps-subscription > span').getText()).toEqual('iPlan Level: ' + homeText);
      switch (homeText) {
        case 'Select':
          console.log(homeText);
          appsNotPurchased = 6;
          return appsNotPurchased;
          break;
        case 'Content':
          console.log(homeText);
          appsNotPurchased = 0 || 1 || 2 || 3 || 4 || 5 || 6;
          return appsNotPurchased;
          break;
      }


    });

Below is the testSpec describe function:

describe('should upload media: ', function() {

  it('should select add media', function() {
    var mmCode = "ACC0572";
    var appsNotPurchased = appsObjects.checksHomeSublevel(mmCode);
    appsObjects.checksSubLevelSelect(mmCode, appsNotPurchased);
  });

});

The object that receives the value:

    this.checksSubLevelSelect = function(mmCode, appsNotPurchased) {

      //checks and counts the apps
      apps.count().then(function(count) {
        expect(count).toEqual(7);
        for (var i = 0; i < count; i++) {
          if (appsPlace == appsNotPurchased) {
            //perform certain actions here
          } else {
            //perform other actions here
          }
          appsPlace++;
        }
      });
    };

Answer №1

To improve your code, it is recommended to return an object instead of using the || statement. Additionally, ensure that the return statement is placed outside the switch statement rather than inside it.

  • An easy solution would be to utilize a global variable named appsNotPurchased to store the value and then access it in your test specifications without needing to return it, although this may not adhere to best coding practices.
  • Another solution is to return a promise from the function since Protractor executes asynchronously.

Here is an example implementing the second solution:

this.checksHomeSublevel = function(mmCode) {
    var getval = marketingObjects.level.getText().then(function(text) {
        switch (homeText) {
          case 'Select':
            console.log(homeText);
            appsNotPurchased = [6];
            break;
          case 'Content':
            console.log(homeText);
            appsNotPurchased = [0, 1, 2, 3, 4, 5, 6]; //either use array or object
            break;
          default:
            console.log("Default");
        }
        return appsNotPurchased;
    });
    return protractor.promise.fulfilled(getval);
};

Then, you can use it as a promise in your spec:

appsObjects.checksHomeSublevel(mmCode).then(function(appsNotPurchased){
    appsObjects.checksSubLevelSelect(mmCode, appsNotPurchased);
});

Incorporate the result into your function now:

this.checksSubLevelSelect = function(mmCode, appsNotPurchased) {

      //counts the apps
      apps.count().then(function(count) {
        expect(count).toEqual(7);
        for (var i = 0; i < count; i++) {
          if (appsPlace == appsNotPurchased[i]) {
            //does something here
          } else {
            //does something here
          }
          appsPlace++;
        }
      });
    };

I hope this explanation proves helpful.

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 onChange method in React is failing to execute within the component

I am having trouble overriding the onChange method in a component. It seems like the method is not triggering on any DOM events such as onChange, onClick, or onDblClick. Below are the snippets of code where the component is rendered and the component itsel ...

The Route.get() function in Node.js is expecting a callback function, but instead received an unexpected object of type

Recently, I started coding with nodejs and express. In my file test.js located in the routes folder, I have written the following code: const express = require('express'); const router = new express.Router(); router.get('/test', (req ...

Text displaying as actionable icons

In my React project, I am utilizing material-table (https://material-table.com/#/) and have successfully imported the necessary icons. However, when viewing the action bar, the icons are displaying as plain text instead of the Material Icon. import React, ...

Is your Jquery validation malfunctioning?

I am currently facing a challenge with required validation on an asp.net page. In order to validate the inputs, I have implemented multiple controls which can be hidden or displayed based on certain conditions. These controls include checkboxlists, dropdow ...

Upon introducing the CSS loader into the webpack configuration, TinyMCE unexpectedly ceases to function

My application development journey began with cloning code from https://github.com/DMPRoadmap/roadmap. This project is based on webpack and npm. To integrate select2, I executed npm install select 2 in the lib/assets directory. I aimed to incorporate a ...

Tips on waiting for the user to click before proceeding with the rest of the automation process

I am working on automating a form filling process using code. However, I would like the automation to pause and wait for the user to manually click after reading a notice before proceeding. postalcode = driver.find_element(By.NAME, 'postalCode') ...

React error: Unable to iterate through items because property 'forEach' is undefined

I am trying to implement private routing validation using the following code: import React from 'react'; import { Route, Redirect } from 'react-router-dom'; import routes from '../../routing/routes'; export default function ...

How can you make a button in Vue only visible after all API calls have successfully retrieved the data?

Is there a way to make the report button visible only after all the data has been loaded from the latest click of the budget button? Currently, when the budget button is clicked, it retrieves budgets and displays them in a Vue grid using Kendo Grid. To spe ...

Enabling the apple-mobile-web-app-capable feature prevents SVG touches from registering

Looking to enhance touch functionality on an SVG element. The touch event detection is done using a jQuery-like selector. (Currently utilizing angular JQLite - angular.element()): .on("mousedown touch", function(event) { No problems with recognition of ...

Angular-schema-form utilizes computed values to dynamically generate form fields based on

I am facing an issue with a form where users input values and I want to display dynamic calculations based on those values. The problem is that the calculation does not update when the numbers are changed. Here is my code snippet: angular.module('cal ...

Trouble with minified scripts on a particular server

Apologies if this has already been addressed, but I've been searching for a solution for hours. I created a basic website that works perfectly on my own hosting. However, when I transfer it to new hosting (same company, same package, different server ...

Guide to selecting the initial index in search drop down with selenium using C#

IWebDriver driver = new ChromeDriver(); driver.Navigate().GoToUrl("http://www.yahoo.com"); driver.Manage().Window.Maximize(); driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); driver.FindElement(By.XPath(" ...

Two separate entities link to a shared occurrence

Two namespaces are at play here: '/' and /notification If I trigger an event in the '/' namespace, how can I listen to the same event in the '/notification' namespace? It would be helpful if someone could provide an explanati ...

Is it possible to use a custom filter along with data-ng-model? Problem arises when attempting to bind a custom filter with data-ng-model

I have created a custom filter to format input numbers into a specific format, like hh:mm. filter app.filter('formatDuration', function () { return function (duration) { if (angular.isUndefined(duration) || duration === null || dura ...

After saving any HTML, SCSS, or TS file, Angular 12 does not compile immediately

Recently I upgraded my Angular project from version 8 to 12 and then migrated to eslint. However, I have encountered an issue where the compilation does not begin immediately after saving a file. There is a delay of approximately 2 minutes before the compi ...

Gathering information from a website where the URL remains constant even after clicking on a specific button

Visit this website for bus tickets: I am working on scraping data related to bus trips from the mentioned site. However, the data I need to scrape depends on the user-selected date in my web application. Does anyone have suggestions on how I can develop ...

Singleton pattern for iFrames sharing the same origin

I have developed a web application that runs on multiple iframes within a parent window, similar to a modified version of GWT. Rather than each individual iframe accessing our backend service separately, I am attempting to have them share the data service ...

What is the best way to ensure my jQuery plugin is up to date?

I have a question regarding the functionality of this plugin I am using. My goal is to update the timer it provides. To start the countdown timer with 5000 milliseconds remaining, I use the following code: $('#CountdownTimer').countdown({ remai ...

Eliminating duplicate loading of jQuery in Django SmartSelect

I'm facing an issue with Django's app, smart select, as it tries to load jQuery on its own. I've already loaded jQuery in the header and then loaded JQuery UI related stuff. However, smartselect also loads jQuery again in the body, causing p ...

Eliminate parameter from URL

Here is the URL I am working with: http://my.site/?code=74e30ef2-109c-4b75-b8d6-89bdce1aa860 My goal is to redirect to this URL: http://my.site#/homepage To achieve this, I use the following code snippet: import { push } from 'react-router-redux& ...