lundi 31 octobre 2016

How to reliably test OAuth2 login flow with protractor

I've been playing with protractor for the last few hours, the first test I've written is to test login via oauth2.

The problem is, when testing this manually, browser will remember my credentials after the first time and automatically finish the flow for me(by which I mean I don't have to type in username/password again). To go through the flow again, I typically have to clear browser data first.

I can't figure out how to do this with protractor though. With protractor, after the first time, I still have to send username and password, but protractor will automatically click login button for me. So the code below passes, but if I uncomment submitBtn.click();, the test will hang.

describe('login_with_github', function() {
    it('login with github oauth2', function() {
        browser.get('http://localhost:3001');
        element(by.css('.btn-login-with-github')).click();
        var loginField = element(by.id('login_field'));
        var passwordField = element(by.id('password'));
        var submitBtn = element(by.css('input.btn.btn-primary.btn-block'));
        browser.getAllWindowHandles().then(function(handles) {
                // switch to the newly openned window
                let oldWindowHandle = handles[0];
                let newWindowHandle = handles[1];
                browser.switchTo().window(newWindowHandle).then(function() {
                    browser.ignoreSynchronization = true;
                    expect(loginField.waitReady()).to.eventually.be.ok;
                    expect(passwordField.waitReady()).to.eventually.be.ok;
                    expect(submitBtn.waitReady()).to.eventually.be.ok;

                    loginField.sendKeys('username@gmail.com');
                    passwordField.sendKeys(password);
                    //submitBtn.click(); // can't do this after the first time.

                    browser.switchTo().window(oldWindowHandle).then(function() {
                        browser.driver.sleep(2000);

                        expect(element(by.css('.foo')).getText()).to.eventually.contain('bar');
                    });
                })
            })
       })
  })

I'm looking for a solution which can either mimic the manual test process, or resolve the protractor hanging problem, so that every time I run this spec it will fill in username/password, click signin button, wait for redirect and go back to main page.

Any advice is much appreciated!

Aucun commentaire:

Enregistrer un commentaire