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

I'm having trouble understanding why my data does not appear even though there is space allocated for it automatically

I'm facing an issue with my code that is supposed to populate a table with dynamic content using react and ant.design. Everything seems to be working fine, but the actual content is not displaying - only the space for it is created. What's happe ...

Steps to include a Target property in a freshly created MouseEvent

Trying to dispatch a contextMenu event, I've noticed that in the MouseEvent interface for TypeScript, the target property is missing, even though it is documented in the contextMenu documentation. Here's my TypeScript snippet: const emulatedMou ...

What criteria should I consider when selecting a make for the createTheme components?

After reviewing the documentation for the createTheme component, I noticed examples with MuiButtonBase, MuiButton, and MuiSlider. However, when it comes to adding a button, it's simply called Button, not MuiButton. So, does this mean I just need to p ...

A guide to inputting text with Selenium in Java

Below is the output in my developer tools: Your Identity Here is an alternative version I came up with: By fieldNameIdentityPath = By.; WebElement fieldNameIdentity = driver.findElement(fieldNameIdentityPath); fieldNameIdentity.sendKeys(&quo ...

Echo a JS variable into PHP and then insert a PHP file into an HTML element - a step-by-step guide!

Greetings to the wonderful community at stackoverflow, I am currently working on a variable code that allows for easy insertion of code from another file, such as a div. I am curious if it is possible to include a PHP file using JavaScript and JS variable ...

Extracting data from websites using Python's Selenium module, focusing on dynamic links generated through Javascript

Currently, I am in the process of developing a webcrawler using Selenium and Python. However, I have encountered an issue that needs to be addressed. The crawler functions by identifying all links with ListlinkerHref = self.browser.find_elements_by_xpath( ...

PHP Header Redirect Not Redirecting Correctly

As a newcomer to PHP, I conducted some research and attempted to implement a solution found on Stack Overflow, but unfortunately, it did not work for me. My goal is to redirect users to another page after a specific code has been executed. Despite removing ...

Executing a Python script within a Django project by clicking on an HTML button

There's a Python script file located in a Django project, but it's in a different folder (let's call it otherPythons). I'm looking to execute this Python file when an HTML button is clicked using JavaScript. Only looking for solutions ...

Is it possible to insert a second hyperlink into a JavaScript-occupied anchor?

Check out my reference page at: To change the content in a 'containerarea' div, I am utilizing Dynamic Drive's "Dynamic Ajax" script. Below is an example of the anchor code used: <a href="javascript:ajaxpage('videos-maintenance/app ...

I'm having trouble understanding how to use JQuery's .live and .remove methods together

Check out this working jsfiddle, except for the function I'm troubleshooting. I am working on code that appends a table to a form when the value changes in the quantity field. I have two goals: Allow only one extra line at a time. Remove the extra ...

Pass data in JSON format from Laravel controller to AngularJS

When working with Laravel, I successfully converted data in MySQL to JSON for use in AngularJS. However, I am now unsure of how to effectively utilize these values in AngularJS. Can anyone offer assistance? View output data (hide each value) Controller ...

React JS Bootstrap Dropdown Troubleshooting

I'm facing a challenge in my React.js project while trying to implement a filter. The issue is that the list appears when I click on the button, but it doesn't disappear on the second click or if I click outside the list. Initially, I imported t ...

Switching the positions of the date and month in VueJS Datepicker

Recently, I have been utilizing the datepicker component from vuejs-datepicker. However, I encountered an issue where upon form submission, the date and month switch places. For instance, 10/08/2018 (dd/MM/yyyy) eventually displays as 08/10/2018, leading ...

Clicking to Load Images - Angular

Implementing a feature to load sets of images on button click instead of loading all at once. Although lazy load plugins are available, I decided to experiment with this approach. Here's the logic: Start with a data array called 'Images' co ...

Guide to generating a div element with its contents using JSON

On my webpage, there is a button that increases the "counter" value every time it's clicked. I am looking to achieve the following tasks: 1) How can I generate a json file for each div on my page like the example below: <div class="text1" id="1" ...

When a user clicks anywhere on the website, the active and focused class will be automatically removed

I'm currently working with Bootstrap tabs on my website. I have three tabs, and when a user clicks on one, the active and focus classes are added to indicate which tab is selected. However, I've encountered an issue where clicking anywhere else ...

What is the best way to iterate through files within a directory and its nested subdirectories using electron?

I am currently working on developing a desktop application that involves scanning through a directory, including all subdirectories, to locate files containing specific characters in their filenames. Is it feasible to accomplish this task using Electron? ...

implement a click event handler for GLmol (webGL)

Check out GLmol, a Molecular Viewer built on WebGL and Javascript by visiting You can see a demo of GLmol in action at I am interested in adding a click function to GLmol. For example, when I click on a ball, I would like to retrieve information about it ...

What is the process for incorporating Transformer instances into the buildVideoUrl() function using cloudinary-build-url?

This particular package is quite impressive, however, it seems to lack built-in support for looping gifs. Fortunately, the provided link demonstrates how custom URL sections like "e_loop" can be created. One challenge I'm facing is figuring out how t ...

The error message "TypeError: Unable to access property of undefined when using web sockets"

Exploring Isomorphic framework for React and integrating Pusher for websockets communication. I'm encountering difficulty accessing the state within the componentDidMount() function. class TopbarNotification extends Component { state = { vis ...