1

I have 2 variables that get the texts: locationDisplayed and bindisplayed.

I want the 3rd variable: expectedDescription concats the text of the first 2 vars.

Here is the code:

let locationDisplayed = element(by.css('.badge-primary')).getText().then(function (text) {

        }); //location to be compared with Destination field
           
        let bindisplayed = element(by.css('.badge-info')).getText().then(function (text) {

        }); //bin to be compared with Destination field
        
        let expectedDescription = locationDisplayed + " "+bindisplayed;
        expectedDescription.getText().then(function(text){
            console.log(text);

        });

But then I got: expectedDescription.getText is not a function.

Anyone knows how can I concat 2 strings, and get the string of this concatenation?

1
  • Please see the updated answer
    – PDHide
    Commented Feb 20, 2020 at 14:22

2 Answers 2

0

Function getText() could be called on a DOM element (https://www.protractortest.org/#/api?view=webdriver.WebElement.prototype.getText), but you're using this function on a string. That won't work.

If you just want to concatenate expectedDescription and locationDisplayed, you've actually done that already on this line: let expectedDescription = locationDisplayed + " "+bindisplayed;

Another way might be to use str.concat() function (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat).

4
  • hi @pavelsaman, I want to print in the console the result of this concatenation, so I can assure the result is what I want. So I am trying to do:let expectedDescription = locationDisplayed + " "+bindisplayed; expectedDescription.getText().then(function(text){ console.log(text);
    – IBrito
    Commented Feb 20, 2020 at 13:00
  • @IngriddBrito: you can print the concatenated string into the console like this console.log(expectedDescription)
    – pavelsaman
    Commented Feb 20, 2020 at 13:22
  • 1
    I just added onsole.log(expectedDescription) as you suggested and it worked. Tks!
    – IBrito
    Commented Feb 20, 2020 at 13:44
  • @IngriddBrito your code is too brittle, it breaks when the element takes more time to render. Use the proper chaining or await. In your case even if you remove then , it behaves the same way
    – PDHide
    Commented Feb 20, 2020 at 15:38
1

I think you forgot the fact that everything is a promise in protractor.

In your code, you are not waiting for the promise to be resolved.

There are two ways :

First: Using await

Add the below line in your config file:

 SELENIUM_PROMISE_MANAGER: false

Then rewrite the code as below:

    let locationDisplayed = await element(by.css('.badge-primary')).getText()

    let bindisplayed = await element(by.css('.badge-info')).getText()

    let expectedDescription = locationDisplayed + " "+bindisplayed;

    console.log(text);

Here, the await ensures that the promise is resolved before it goes to the next line else each line will act asynchronously.

Second: Chaining promise

element(by.css('.badge-primary')).getText().then(function (locationDisplayed ) {

    element(by.css('.badge-info')).getText().then(function (bindisplayed ) {
    console.log(locationDisplayed+bindisplayed);
   });      

  });

The second option works only if the script has enough time for the call back to resolve. Try using await browser.sleep(3000) at the end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.