-1

I have a test i'm writing which reads in a string and then takes that string and applies it to a switch statement. I then match the string to the case and set an integer value which I pass back to the spec page which then passes the int value to another test that I use for an if statement. I cannot get the int to pass so the if statement will not work properly.

The object for 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;
      }


    });

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 I am passing the value to:

    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) {
            //does something here
          } else {
            //does something here
          }
          appsPlace++;
        }
      });
    };
John C
  • 3,052
  • 3
  • 34
  • 47
Nicole Phillips
  • 753
  • 1
  • 18
  • 41
  • 2
    I recommend to use a bit more white space in your code, it makes it a loteasier to read (at least for me :-) – LionC Sep 24 '15 at 13:44
  • @Lion ok I'll update get rid of some of the code. How is that? – Nicole Phillips Sep 24 '15 at 13:44
  • 3
    You should add a `default: return 'None of the above';` case and remove the `break;` statements after your `return` statements (`return` will `break` it anyway). If it returns that sentence above, you know your cases aren't matching. Hint: **always** provide a `default` to a `switch` statement. – somethinghere Sep 24 '15 at 13:49
  • 5
    Is the homeText always 'Select' or 'Content'? Since you don't have a default return value. And is `appsNotPurchased = 0 || 1 || 2 || 3 || 4 || 5 || 6;` correct? Since it will always be 1, being the first truthy value. – Shilly Sep 24 '15 at 13:50
  • @Shilly I was just about to mention that. – somethinghere Sep 24 '15 at 13:50
  • @Shilly depends which website I load. In this case Select. yes that is correct. Those are or statements aren't they? So while my for loop is incremented the if statement will run 7. – Nicole Phillips Sep 24 '15 at 13:52
  • 3
    @NicolePhillips: The second part of what Shilly's saying is that `0 || 1 || 2 || 3 || 4 || 5 || 6` is just a really long and unclear way to write `1`. The value will always be `1`. There is nothing conditional in there. – T.J. Crowder Sep 24 '15 at 13:53
  • @T.J. I don't want it to write just 1. I want 0,1,2,3,4,5,6 to run through the if statement and in the case of Select never hit else. I have other cases. – Nicole Phillips Sep 24 '15 at 13:55
  • Since your switch isn't returning anything, the case is likely that homeText isn't always 'Select' or 'Content'. Have you checked if the homeText is not like 'select' or 'content', uncapitalized. Try console.log before the swicth statement, or have a `default:` that also logs. – Shilly Sep 24 '15 at 13:56
  • 7
    @NicolePhillips Thats not how this works. The `||` or operator will return the first value thats isn't `falsy`, therefor the number `1` – somethinghere Sep 24 '15 at 13:56
  • Then your switch should always return something and the problem isn't the switch. – Shilly Sep 24 '15 at 13:57
  • @somethinghere so if statements cannot be run with Ors? Or can that only be done when coded in the object itself? – Nicole Phillips Sep 24 '15 at 13:58
  • 1
    @NicolePhillips Statements can have `||`s, but how you've written it isn't how it works, based on what you say you want. – Dave Newton Sep 24 '15 at 13:58
  • @NicolePhillips try returning an object in place of the `||` statement. And when you mean that no integer is passing, doesn't integer 6 pass back to the function? – giri-sh Sep 24 '15 at 13:58
  • 1
    @NicolePhillips: *"so if statements cannot be run with Ors"* Yes they can. That isn't remotely how you do that. For one thing, that series of `||` is nowhere near any `if`. – T.J. Crowder Sep 24 '15 at 13:59
  • @NicolePhillips if statements can contain `or` values, but every number apart from zero, when turned into a boolean, is _automatically_ true. So when evaluating 1, or 2, or 3, or 4, will always return the first value that isn't falsy, or '1'. What you have there is not an _if statement_ its a backup variable declaration. As in `var falsyVar = false; var myVar = falsyVar || "So I will be the value!";` – somethinghere Sep 24 '15 at 13:59
  • 1
    @NicolePhillips Use this to learn how to improve your question, not how to delete it. – somethinghere Sep 24 '15 at 14:02
  • It's an ok question, the problem is the structure probably. From what I understand you're trying to do, the call to `this.checksSubLevelSelect()` should be somewhere in your switch instead of the 0 || 1 || 2.... part. The title is probably misleading though, since your switch statement should always return 6 for 'Select' and 1 for 'Content', as written. – Shilly Sep 24 '15 at 14:04
  • 1
    @NicolePhillips: I suggest stepping back from your current task and working through some JavaScript tutorials. Then come back to your task and if you're still having trouble, post a new question with the minimum necessary code and a clear statement of what inputs you have and what results you expect, and what you're getting instead. We may well be able to help. And don't worry, we were all new at this once! Sometimes more than once! – T.J. Crowder Sep 24 '15 at 14:05
  • 1
    Plese try implementing a `default` for your `switch` as many have suggested and add a `alert(homeText)` to it to see if the value passed to your switch statement _really really really_ is what you think it will be, as in all likelihood it is not. – somethinghere Sep 24 '15 at 14:05
  • @T.J. I do this for a living and was thrown into a task I cannot step back from. The answer was I should put my or statements into an array as I was confused on a java vs js or statement, which in Java, would work. Perhaps I shouldn't have tagged js people to assist in automation testing of angularjs code. I had stolen the switch statement from a website and stuck it in my code to see if it would work. Really all I needed was someone to kindly explain, thank you. – Nicole Phillips Sep 24 '15 at 18:57
  • @NicolePhillips: No, that line wouldn't work in Java, either; it won't even compile. First step if you do this for a living: Read up on what you're working with. – T.J. Crowder Sep 24 '15 at 20:56
  • @T.J. refer to this link http://stackoverflow.com/questions/1795808/and-and-or-in-if-statements -or in if statements do in fact work in Java. I have done some reading, a lot actually and am doing the best that I can given the circumstances. Thank you for your help. – Nicole Phillips Sep 25 '15 at 11:50
  • @NicolePhillips: I didn't say anything about "or in if statements" not working in Java. I said **that line**, which you were suggesting works in Java, not only doesn't work in Java, it won't even compile. – T.J. Crowder Sep 25 '15 at 11:51
  • @T.J. I meant or statements in general in if statements, not the above code. There was so many answers, some of them which is now discouraging me from using this site for help, I was lost. What I was asking was how to pass that entire line of or statements. If it couldn't be done a helpful way of going about it what was said by Girish below. Thank you for your candor and help, good day sir. – Nicole Phillips Sep 25 '15 at 11:55

1 Answers1

5

You should be returning an object instead of || statement. Also return statement should be written outside switch rather than inside it.

  • Simplest solution would be to use a global variable appsNotPurchased which stores the value and then you can use it in your test specs without returning. But that would be a poor coding standard.
  • Second solution would be to return a promise of the function as protractor executes asynchronously.

Here's an example of 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);
};

and then use it like a promise in your spec -

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

Use above result in 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++;
        }
      });
    };

Hope it helps.

giri-sh
  • 6,934
  • 2
  • 25
  • 50
  • Actually, a return statement _can_ be inside your switch as it will act as a `break` of the `switch` and will immediately end the encapsulating function as well. – somethinghere Sep 24 '15 at 14:12
  • True @somethinghere but its not a good practice. Also if at all there's something that needs to be executed after switch statement will be skipped. – giri-sh Sep 24 '15 at 14:13
  • 1
    Yeah sure, it will skip anything after it, but in this very special case I would say that not assigning anything to a variable like `appsNotPurchased` and simply returned the value would be more readable. I find this clearer: `return [0, 1, 2, 3, 4, 5, 6];`. And I am going out on a limb and say that this is not bad practise, and it might even be good practise depending on the use case (as long as _every_ `case` `return`s that is). – somethinghere Sep 24 '15 at 14:14
  • 2
    @Girish Neither of these worked for me. I made a global array variable and changed to an array and stopped returning, that worked. The next answer I got 'Cannot read property 'then' of undefined' – Nicole Phillips Sep 24 '15 at 15:33
  • 2
    @NicolePhillips, updated code to return the promise in a proper way and also updated the global variable part too, its the simplest solution one could think of. Let me know if you still get error for the promise. Thanks. – giri-sh Sep 24 '15 at 18:36
  • @Girish it worked thank-you!!! I'm excited you showed me that I can use that on my specs page in so many different areas!! – Nicole Phillips Sep 24 '15 at 18:44