johntitus / node-horseman

Run PhantomJS from Node
MIT License
1.45k stars 124 forks source link

Way to wait for new Tab to load #260

Open jerilseb opened 7 years ago

jerilseb commented 7 years ago

Hi, I am opening a new tab via .click() on a button. Currently, I'm doing this

    return new Promise( (resolve, reject) => {
            horseman
                .click('#button1')
            .wait(5000)
            .tabCount()
            .then(tabCount => {
                if(tabCount != 2) reject('New Tab not opened');
            })
            .switchToTab(1) 
    })

I cannot use openTab(url) since I don't have the url, and the tab creation is done through javascript.

Is there a better way to do this? Waiting for a fixed time doesn't seem like a good idea.

Thanks in advance.

awlayton commented 7 years ago

You could use switchToNewTab: true in the constructor, then instead of waiting for there to be two tabs, just wait for a selector or something that should be in the page loaded in the new tab.

awlayton commented 7 years ago

Also as a side note, there is no need to wrap the horseman stuff in new Promise, it is already a Promise.