Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor Button Click and open page in new tab

I am fairly new to Protractor. I am trying to automate a scenario where I click on a button and its opens up a page in new tab and then we need to populate form in new page and submit.

Issue: when i click on button to open new page. My tests does not wait for new page to load and say test completed and success message.

I am using simple click event of that button to click the button.

element(by.id("newPlan")).click()

Am I missing something ? Do i need to do something so that my tests wait for new page to load and then I can perform some functions ?

like image 950
Sumit Avatar asked Feb 16 '15 08:02

Sumit


People also ask

How do I open a new tab with a protractor?

open(arguments[0], '_blank')", url); //opens google.com in a new tab (works fine with Chrome.

How do you click a button with a protractor?

As we click up and down the button on the mouse to perform an activity. Similarly, the mouse up and mouse down methods in Protractor are used to click up and down the primary mouse button.

Is protractor getting deprecated?

Protractor launched in 2013 before WebDriver APIs were standard and E2E tests were hard to write, and it tests Angular and AngularJS apps by running them in Google Chrome. It will continue to run until the end of 2022 when Angular 15 will be the last update.


2 Answers

You need to wait until the page opens by using callbacks. Try something in this sense:

    element(by.id("newPlan")).click().then(function () {
        browser.getAllWindowHandles().then(function (handles) {
            newWindowHandle = handles[1]; // this is your new window
            browser.switchTo().window(newWindowHandle).then(function () {
                // fill in the form here
                expect(browser.getCurrentUrl()).toMatch(/\/url/);
            });
        });
    });
like image 78
Michal Lison Avatar answered Nov 15 '22 19:11

Michal Lison


There is another more convenient way. Just make use of the functions on the browser object.

element(by.id("newPlan")).click();
browser.sleep(10000);
browser.waitForAngular();
expect(browser.getCurrentUrl()).toMatch(/\/url/)
like image 24
Blaise Avatar answered Nov 15 '22 19:11

Blaise