CasperJS- Register on a site and validate the mail

2019-07-21 04:40发布

问题:

Edit : this is the windows behaviour, with linux it just fails.

First, if you succeeded navigate on gmail with casper (without random waiting time -from 20sec to 5min-), please tell me.

I want to register on our site, then validate my registration automatically with Gmail (an entire register step). Did someone do that before?

I have no problem to register, and I can login on my mailbox (Gmail) but after i have some troubles to navigate and validate my registration in Gmail, and i observe different behaviors between phantomJS and slimerJS.

In phantom it will work (without special commands), but it may take until 5 minutes before pass in the next step (waitForSelector). And with slimerjs it just stays stuck on the mailbox page.

EDIT : A strange thing : if i click manually (slimer) on a link which opens a popup, it stops being blocked and my navigation continues, it's like it can't detect the end of the step itself and can't perform the waitFor after the submit click without another interaction. Is it a refresh/reload problem?

Try that to see yourself :

casper.thenOpen('https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/&hl=en', function(){
    this.sendKeys("input#Email","your mail");
    this.sendKeys("input#Passwd","your password");
    this.click("input#signIn.rc-button-submit");
    console.log(this.getCurrentUrl());
    this.waitForSelector(".aeF",function(){//fail with linux -> timeout
        this.test.pass("ok"); //windows -> stuck in slimer, several times in phantom
        this.test.assertExists(".T-I.J-J5-Ji.T-I-KE.L3","Gmail Home ok");
        console.log("url "+this.getCurrentUrl());
    });

And i don't get any timeOut error. In slimerjs it just keeps the page opened.

If i do a waitForPopup instead of a waitForUrl, i have the error (timeout -> did not pop up), so why does a waitForUrl/waitForSelector... stay stuck ? I tried --web-security=no,--ignore-ssl-errors=true commands too (not linked but i tried --output-encoding=ISO 8859-1 too which doesn't work).

Here the differences between phantom and slimer (doc) : http://docs.slimerjs.org/0.8/differences-with-phantomjs.html (useless in this issue i think)

回答1:

Well, we finally found a way to do it : the problem is by default gmail loop on ajax requests, to check some new mails, etc... see Page polls remote url, causing casper to block in step.

Fortunately google proposes a way to avoid that, using the simplified HTML version (you can for example use a special gmail address for your tests using this version) :

That way the script works as it should.

Bonus :

/*
 * Click on an element specified by its selector and a part of its text content.
 * This method resolves some problem as random space in textNode / more flexible too.
 * Need to fix one bug though : when there is a tag in textContent of our selector.
 */
casper.clickSelectorHasText = function (selector, containsText){
    var tmp = this.getElementsInfo(selector)
        ,i
        ,l
        ,bool=false
        ;
    for (i=0,l=tmp.length;i<l; i++){
        if(tmp[i].text && tmp[i].text.indexOf(containsText)!==-1){
            this.clickLabel(tmp[i].text);
            bool=true;
            break;
        }
    }
    casper.test.assert(bool, "clickSelectorHasText done, text and selector found -> click selector " + selector +" which contains text " + containsText);
};

casper.thenOpen('https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/&hl=en', function scrapeCode(){
        //log in
        this.sendKeys("input#Email","your email");
        this.sendKeys("input#Passwd","your password");
        this.click("input#signIn.rc-button-submit");
        //wait to redirect to our mailbox
        this.waitForSelector("form[name='f']",function(){
            //check main block
            this.test.assertExists("form[name='f']","Gmail Home ok");
            this.test.assertSelectorHasText("span", "Your gmail title message");
            this.clickSelectorHasText("font", "one string which appears in font tag");
            //wait inscription message appears
            this.waitForSelector("div.msg",function(){
                this.test.assertSelectorHasText("a","the message which activates your account--> in <a>");
            });
        })
        //validate our account
        .then(function(){
            this.clickLabel("the message which activates your account--> in <a>");
            this.waitForPopup(/mail/, function(){
                this.test.pass("popup opened");
            });
            this.withPopup(/mail/, function(){
                this.viewport(1400,800);
                this.test.pass("With Popup");
                //wait something on your website (for me selector .boxValid)
                this.waitForSelector(".boxValid", function(){
                    /*
                     * Here your code after validation
                     */
                });

            });
        })

It might be possible to do it with normal gmail using event, see resource.received.