question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expect.toContains() with if ...else if...else

See original GitHub issue

want to differentiate some values from list into different different arrays like $ 1 B , $ 2 B , $ 100 M ,$ 300 M , $ 105 K , $ 205 K so i tried it with expect.toContain(‘’) method

var ele = element.all(by.css('.company-network-list__header__income.ng-scope .ng-binding.ng-scope'));
var b =[] , m=[], k=[];
ele.map(function(eachName) {
	return eachName.getText().then(function(unSorted) {
if(expect(unSorted).toContain('B')){
     var b = unSorted;
}
else if(expect(unSorted).toContain('M')){
   var m = unSorted;
}
else{
  var k = unSorted;
}
});
});

for 2 values …i tried with try catch

try{
	expect(unSorted).toContain('B');
	var b = unSorted;
    }catch(e) {
	var m = unSorted;
   }

also this…

expect(unSorted).toContain('B').then(function(result) {
       if(result){
           var b= unSorted;
       }else {
             var m= unSorted;
      }  
});

Not working anything from these Can anyone please help me to solve this issue

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Apr 6, 2017

You are right, missed the toContain() part… Use: if (unSorted.indexOf(‘B’) > -1);

Google will help you for future js queries 😉

1reaction
ghostcommented, Apr 10, 2017

@hitesh-jani

First, like you have on your post: “Make sure you are not asking a usage or debugging question. If you are, use StackOverflow, Google Group discussion list, or Gitter to get help.” this is a question not a protractor issue.

Trying to help you before this issue gets closed, I don’t think expect() should be used in if…else clauses… The expect() is an assertion. Picking your first example, I don’t understand why you define var b = [] and then in the if you redefine it with different type (var b = string). Also, you return eachName.getText() but don’t return anything inside your function(unSorted).

Give it a try this way:

var ele = element.all(by.css('.company-network-list__header__income.ng-scope .ng-binding.ng-scope'));
var b =[] , m=[], k=[];
ele.each(function(eachName) {
	eachName.getText().then(function(unSorted) {
if(unSorted === 'B'){
     b.push(unSorted);
}
else if(unSorted ==='M'){
   m.push(unSorted);
}
else{
  k.push(unSorted);
}
});
});

Hope it helps and don’t forget to use StackOverflow for future questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expect - Jest
When you're writing tests, you often need to check that values meet certain conditions. expect gives you access to a number of "matchers" ......
Read more >
How to make an expect statement be condition for if statement
I want to make it into an if statement that would the url as the condition. How could I do that? if(browser.driver.getCurrentUrl().toEqual(" ...
Read more >
Untitled
An IF statement is a logical formula. ts jest cannot use import statement outside a module. describe("Suite", () => { it("test", ); prettify....
Read more >
if...else - JavaScript - MDN Web Docs - Mozilla
The if...else statement executes a statement if a specified condition is truthy. If the condition is falsy, another statement in the ...
Read more >
Expect · Jest
Everything else is truthy. .toBeUndefined() #. Use .toBeUndefined to check that a variable is undefined. For example, if you want to check ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found