Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor - get browser title as string

I'd like to store initial device title, which is accessible via GUI in browser as page title, and reuse it later in the code. E.g. I want to change name of device if it is set to some particular name only, and to store an old device name in browser console log. Code might look like this:

    var some_name = 'Some Name';
    var title = browser.getTitle();
    console.log(title);
    if (title.equals('Some Name')){
    some_name = 'Some Other Name';
    }
    setDeviceName(some_name);

unfortunately, to that protractor responds with

    TypeError: title.equals is not a function

How is it possible to extract browser title as string? Thanks.

UPDATE:

Thanks everyone, the solution due to igniteram1 is

    var some_name = 'Some Name';
    some_name = browser.getTitle().then(function(webpagetitle){
      if (webpagetitle === 'Some Name'){
        return 'Some Other Name';
      }else{
        return 'Some Name'
      } 
    });
    expect(some_name).toEqual('Some Other Name');
like image 686
Emil Avatar asked Jul 21 '16 12:07

Emil


People also ask

How do I get a browser title on a protractor?

Protractor Browser Commands - Get current page title in Protractor. Purpose: The getTitle() function in ProtractorBrowser class is used to get the current page title.

How do you find links to a website using a protractor?

We can find all the links on a webpage using tagName 'a'. Usually all the links are formed with an anchor tag 'a' and all the links will have 'href' attribute with URL value.

How to close browser window in Protractor?

window(handles[1]); browser. driver. close();


1 Answers

In addition to @Sudharshan Selvraj's answer,its good practice to use strict equality === instead of == and to return the value.

var some_name = 'Some Name';
some_name = browser.getTitle().then(function(webpagetitle){
  if (webpagetitle === 'Some Name'){
    return 'Some Other Name';
  }else{
    return 'Some Name';
  }
 });
expect(some_name).toEqual('Some Other Name'); 
like image 122
Ram Pasala Avatar answered Oct 27 '22 14:10

Ram Pasala