2

After a lot of research, and tinkering, I can't seem to actually get my Protractor test to do anything else other than have an Angular related error, even though I am using browser to avoid Angular being detected at all.

The test involves an Angular app, opening a dropdown in it, and clicking on the link for the console; the console opens a non-Angular admin page in a separate window.

So based on the many informative SO posts I found, I first used this...

browser.driver.getAllWindowHandles().then(function(handles) {
    browser.driver.switchTo().window(handles[1]).then(function() {
//expect for new window here
            });
        });

Which appeared to work, as I could get to the window through repl pretty easily. The issue is when either of the following were added...

browser.driver.getAllWindowHandles().then(function(handles) {
    browser.driver.switchTo().window(handles[1]).then(function() {
        expect(browser.getLocationAbsUrl()).toContain('/console/login.jsp');
        expect(browser.driver.findElement(By.css('th.login')).getText()).toEqual('Login');
            });
        });

One expect check the URL and the other checks for the header element on the page, which is a table header. When I run this, I get the following:

Error while waiting for Protractor to sync with the page: "angular could not be found on the window"

When I decide to use browser.ignoreSynchronization = true, both in the function, or in a beforeEach, with or without a following afterEach setting it to false, I get the following:

JavascriptError: angular is not defined

I can't seem to get any "useful" errors to help me debug it, and trying it in repl does not help, as I get the same issue. To be comprehensive, trying my URL expect without getting the second window will give me the root, and the other will fail. Just doing one or the other will cause the same problem. Changing to regular syntax (element(by.css...)) does not change things.

Richard Erickson
  • 2,568
  • 8
  • 26
  • 39
Omar Ahmad
  • 61
  • 4
  • The method in which you are trying to get the URL isn't very 'AngularJS', for example it would be much simpler to inject '$state' into your Angular test controller and then get the url from there, $state.href() for example. You can also use "element" to access DOM elements, here's somewhere you can find it being used: http://stackoverflow.com/questions/19429025/how-to-access-a-dom-element-in-a-angular-directive – cullimorer Dec 08 '15 at 15:45

2 Answers2

2

So much for my first question...

It appears that my use of browser.getLocationAbsUrl() is meant to be used for an Angular page, and was causing my issue... Essentially, even though I believed I was using pure Webdriver calls, that call still required Angular on the page to work... As stated in another post, the use of browser.driver.getCurrentUrl() is a non-Angular call using Webdriver, and fixed the problem. Thus, the final code is the following...

browser.sleep(1000); //to wait for the page to load
        browser.driver.getAllWindowHandles().then(function(handles) {
            browser.driver.switchTo().window(handles[1]).then(function() {
                expect(browser.driver.getCurrentUrl()).toContain('/console/login.jsp');
                expect(browser.driver.findElement(By.css('th.login')).getText()).toEqual('Login');
            });
        });

This works without setting ignoreSynchronization, BTW. I realized it would probably be something relatively simple to fix it, just didn't expect I'd get it that quickly (I intended on submitting the question last night, but posted it this morning instead).

In any case, I hope this will at least be a good reference for anyone else facing the same issue.

Community
  • 1
  • 1
Omar Ahmad
  • 61
  • 4
  • This is relevant: [getLocationAbsUrl vs getCurrentUrl](http://stackoverflow.com/questions/30790032/getlocationabsurl-vs-getcurrentur). – alecxe Dec 08 '15 at 16:13
0

Seems like getLocationAbsUrl is angular abs url.
Try using the native driver getCurrentUrl instead.

-- expect(browser.getLocationAbsUrl()).toContain('/console/login.jsp');
++ expect(browser.driver.getCurrentUrl() ...

Nihau
  • 474
  • 3
  • 20
  • Thanks, I realized that and was writing up the answer when you posted this. The link to the doc page is definitely helpful though; it was trial and error that led me to find the answer myself. – Omar Ahmad Dec 08 '15 at 16:01