Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

ptor.getCurrentUrl() is not working correct #57

Closed
apankov1 opened this issue Aug 27, 2013 · 13 comments
Closed

ptor.getCurrentUrl() is not working correct #57

apankov1 opened this issue Aug 27, 2013 · 13 comments

Comments

@apankov1
Copy link

Hello,

I've just started looking at protractor for our angularjs apps and looks very promising.

I want to use ptor.getCurrentUrl() to get url after login to make sure that I actually got logged in. My code looks something like this:

var util = require('util');

describe('Sign In Page', function() {
    var ptor;

    beforeEach(function() {
        ptor = protractor.getInstance();
        ptor.get('#/login/');
        ptor.waitForAngular();
    });

    it ('Login', function(){
        ptor.findElement(protractor.By.css('input[name=email]')).sendKeys("***");
        ptor.findElement(protractor.By.css('input[name=password]')).sendKeys("***");
        ptor.waitForAngular();
        var currentUrl = ptor.getCurrentUrl();
        console.log(currentUrl);
    }, 20000);

    it('afterAll', function(){
    });
});

I'm using

    var currentUrl = ptor.getCurrentUrl();
    console.log(currentUrl);

to see what exactly gets stored and in console I see following result:

APankov$ node_modules/.bin/protractor conf.js
Using the selenium server at http://localhost:4444/wd/hub
{ then: [Function: then],
  cancel: [Function: cancel],
  isPending: [Function: isPending] }

What am doing wrong in this case?

Thank you

@juliemr
Copy link
Member

juliemr commented Aug 27, 2013

Hello! ptor.getCurrentUrl() is returning a promise that will resolve to the value of the current URL, not the URL itself. Check out https://code.google.com/p/selenium/wiki/WebDriverJs#Understanding_the_API for an introduction to promises in WebDriver. Specific documentation for getCurrentUrl() is available in the webdriver code at https://code.google.com/p/selenium/source/browse/javascript/webdriver/webdriver.js#621

@juliemr
Copy link
Member

juliemr commented Sep 11, 2013

Closing as there seem to be no further questions. Please reopen if something comes up!

@juliemr juliemr closed this as completed Sep 11, 2013
@apankov1
Copy link
Author

Hey @juliemr, I've changed my code a little bit:

var util = require('util');

var Faker = require('../node_modules/Faker');

describe('Check Url after SignUp', function (){
    var ptor;

beforeEach(function() {
    ptor = protractor.getInstance();
    ptor.get('#/');
    var randomFirsName = Faker.Name.firstName();
    var randomLastName = Faker.Name.lastName();
    var randomEmail = "test+" + randomFirsName +"@2domen.com";
    var signUpPassword ="qatest1";
    ptor.findElement(protractor.By.css('input[name=first_name]')).sendKeys(randomFirsName);
    ptor.findElement(protractor.By.css('input[name=last_name]')).sendKeys(randomLastName);
    ptor.findElement(protractor.By.css('input[name=email]')).sendKeys(randomEmail);
    ptor.findElement(protractor.By.css('input[name=password]')).sendKeys(signUpPassword);
    ptor.findElement(protractor.By.css('button.green.large')).click();
    ptor.waitForAngular();
}, 10000);

it('Check Current URL', function() {
    ptor.getCurrentUrl().
        then(function(url) {
            expect(url).toBe('https://application.com/#/home');
        });
}, 10000);

});

application.com is my BaseUrl in conf.js.
But I'm getting error that URL doesn't match. Basically it takes base URL not a URL after i signup when user gets signed up

What's wrong with the code?

Thank you

@juliemr
Copy link
Member

juliemr commented Sep 13, 2013

I don't understand your last sentence, sorry. What is the URL that you are
getting?
On Sep 12, 2013 5:55 PM, "Andrew" notifications@github.com wrote:

Hey @juliemr https://github.com/juliemr, I've changed my code a little
bit:

var util = require('util');

var Faker = require('../node_modules/Faker');

describe('Check Url after SignUp', function (){
var ptor;

beforeEach(function() {
ptor = protractor.getInstance();
ptor.get('#/');
var randomFirsName = Faker.Name.firstName();
var randomLastName = Faker.Name.lastName();
var randomEmail = "test+" + randomFirsName +"@2domen.com";
var signUpPassword ="qatest1";
ptor.findElement(protractor.By.css('input[name=first_name]')).sendKeys(randomFirsName);
ptor.findElement(protractor.By.css('input[name=last_name]')).sendKeys(randomLastName);
ptor.findElement(protractor.By.css('input[name=email]')).sendKeys(randomEmail);
ptor.findElement(protractor.By.css('input[name=password]')).sendKeys(signUpPassword);
ptor.findElement(protractor.By.css('button.green.large')).click();
ptor.waitForAngular();
}, 10000);

it('Check Current URL', function() {
ptor.getCurrentUrl().
then(function(url) {
expect(url).toBe('https://application.com/#/home');
});
}, 10000);

});

application.com is my BaseUrl in conf.js.
But I'm getting error that URL doesn't match. Basically it takes base URL
not a URL after i signup when user gets signed up

What's wrong with the code?

Thank you


Reply to this email directly or view it on GitHubhttps://github.com//issues/57#issuecomment-24366633
.

@apankov1
Copy link
Author

I apologize, I should be more specific.

I have baseURL in conf.js file, which is a basic sign up page.
Which looks like https://myapp-qa.domen.com/#/
I'm passing my gmail name + generated by Faker name to create unique users.
After user signs up he should be redirected to the main page of the app and url should look like https://myapp-qa.domen.com/#/home. This URL is hardcoded for now.

Basically what happens protractor doesn't wait until home page gets loaded, grabs baseURL and then my test fails.

I hope it's more clear now.

Thank you.

A

On Sep 12, 2013, at 8:59 PM, Julie Ralph notifications@github.com wrote:

I don't understand your last sentence, sorry. What is the URL that you are
getting?
On Sep 12, 2013 5:55 PM, "Andrew" notifications@github.com wrote:

Hey @juliemr https://github.com/juliemr, I've changed my code a little
bit:

var util = require('util');

var Faker = require('../node_modules/Faker');

describe('Check Url after SignUp', function (){
var ptor;

beforeEach(function() {
ptor = protractor.getInstance();
ptor.get('#/');
var randomFirsName = Faker.Name.firstName();
var randomLastName = Faker.Name.lastName();
var randomEmail = "test+" + randomFirsName +"@2domen.com";
var signUpPassword ="qatest1";
ptor.findElement(protractor.By.css('input[name=first_name]')).sendKeys(randomFirsName);
ptor.findElement(protractor.By.css('input[name=last_name]')).sendKeys(randomLastName);
ptor.findElement(protractor.By.css('input[name=email]')).sendKeys(randomEmail);
ptor.findElement(protractor.By.css('input[name=password]')).sendKeys(signUpPassword);
ptor.findElement(protractor.By.css('button.green.large')).click();
ptor.waitForAngular();
}, 10000);

it('Check Current URL', function() {
ptor.getCurrentUrl().
then(function(url) {
expect(url).toBe('https://application.com/#/home');
});
}, 10000);

});

application.com is my BaseUrl in conf.js.
But I'm getting error that URL doesn't match. Basically it takes base URL
not a URL after i signup when user gets signed up

What's wrong with the code?

Thank you


Reply to this email directly or view it on GitHubhttps://github.com//issues/57#issuecomment-24366633
.


Reply to this email directly or view it on GitHub.

@apankov1
Copy link
Author

@juliemr, it looks like I'm having same issue as described here #88

@zeeshanjan82
Copy link

Hi , I have fixed the issue using the following function to wait for the URL to change
function waitForUrlToChangeTo(newUrl) {
var currentUrl;

return browser.getCurrentUrl().then(function storeCurrentUrl(url) {
        currentUrl = url;
    }
).then(function () {
        return browser.wait(function () {
            return browser.getCurrentUrl().then(function (url) {
                return (url !== newUrl);
            });
        });
    }
);

}

@tot-ra
Copy link

tot-ra commented Nov 5, 2014

I've got a problem with IE10/IE11 that both returned URL without hash, whereas webkit returned with hash. Switched to a more cross-browser hack:

browser.executeScript('return window.location.hash;').then(function(v){
    console.log(v);
});

@bitliner
Copy link

bitliner commented Jul 7, 2015

On ie11 browser.getCurrentUrl() does not return the current url, but the previous url.

The hack from @tot-ra works.

To be honest, all the APIs from protractor suck.
The best way looks like to run always javascript code inside the browser.
So may problems I am getting with protractor...

@bitliner
Copy link

@ctamas you don't understand the way protractor and js selenium api manages promises.
and getCurrentUrl() always worked, while in ie11 don't.

I guess the way js selenium API or protractor manage promises has problems in synchronization, or it is a way that is not a good one to manage promises.

@bitliner
Copy link

@ctamas the point is that it is not clear when to resolve promises with then and when not, and selenium api communicates like there is no need to resolve promises with then, while there is this need as we are demonstrating in this thread.

@bitliner
Copy link

I think the confusion comes from this sentence

The promise manager maintains a queue of scheduled tasks, executing each once the one before it in the queue is finished.

and from the fact that someone - not only me apparently - expects a different behavior from getCurrentUrl()

My fault I did not get the semantic of getCurrentUrl()

@anwani
Copy link

anwani commented Dec 6, 2016

i am using the statement browser.get(URL); but the url is not launched, rather just the browser window is opened by the previous statement browser.manage().window().setSize(1600, 1000);
which was defined in onprepare method on conf.js file

is there any solution to this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants