0

I simplified the code i need to test to this:

<html ng-app="home" ng-strict-di=""><head>....

And i am running some protractor tests, i want to access the value of ng-app so i can compare and see which app is running in each page.

I have tried

var appName = element(by.xpath('/html/@ng-app'))

but it is not returning a usable promise or text i can compare with

appName.getText().then(function(name) {
      expect(name).toBe('home')
      });

But protractor complains:

InvalidSelectorError: invalid selector: The result of the xpath expression "/html/@ng-app" is: [object Attr]. It should be an element.

So i'm a bit baffled as how can i access my angular app name from protractor to test for app running independently of localization of labels.

Any insight into this enigma?

shadowcharly
  • 134
  • 8

2 Answers2

3

And it seems like magically, when you order your thoughts to formulate the question, the answer comes to you.

the trick is to get the html as element

var appNameHtml = element(by.xpath('/html'))

and then, in the test, get the ng-app attribute and use it:

appNameHtml.getAttribute('ng-app').then(function(value) {
    expect(value).toBe('home');
});

And bingo, you can extract the app name.

Maybe this is a very basic question, but it was driving me insane :)

shadowcharly
  • 134
  • 8
2

your answer will suffice I guess in this case. But just wanted to highlight a more generic approach in case ng-app may reside not only on html element.

var elementWithNgApp = element(by.css('*[ng-app]'))

And then wanted to add one more thing. You need not resolve the promise of getAttribute to do an expect on its value. Jasmine resolves the promise for you. You can have something like this

expect(elementWithNgApp.getAttribute('ng-app')).toBe('home');

AdityaReddy
  • 3,625
  • 12
  • 25