Wednesday, 8 June, 2016 UTC


Summary

Recently I found a pretty advanced way to automate my social media stuffs:
  1. elgalu’s docker-selenium
  2. Webdriver.io
  3. Social media platform of choice (I’ve done LinkedIn, Facebook and Instagram)
So the idea is that you launch a Docker instance with Selenium grid running on it, and you can choose between Chrome and Firefox on the fly through the Node.js based Webdriver.io code configuration.
I was able to automate myself going through hundreds of posts, and picking out ones with relevancy which may have some kind of monetary value, kind of like lead generation.
Quickly I ran into some issues while doing some Instagram unfollow automation. My script was unfollowing users from a list of 100+ every 20 seconds or so, however Instagram has some limits in place so essentially when Instagram decides I’ve done too many unfollows, it rejects my action of clicking the unfollow button.
This rejection can only be seen in the console of the client, and I needed a way for the browser – via Selenium via Webdriver.io to pass to me, the message that I’ve hit this limit. Essentially it was an AJAX response of 403 forbidden.
Achieving this communication wasn’t straightforward but the below example makes this happen. Hopefully it’s of some use to somebody out there on the internets.
client
    .execute(function() {
        // inject global variable & jQuery AJAX error logger
        fkErrorz = []
        jQuery(document).ajaxError(function(e, request, settings) {
            fkErrorz.push(e)
        })
    })
    // snip, do stuffs here
    .execute(function() {
        return fkErrorz.length
    })
    .then(function(ret) {
        var errorsCount = parseInt(ret.value)
        console.log('AJAX errors: ' + errorsCount)
        if (errorsCount > 1) {
            console.log('Exiting process due to AJAX errors')
            process.exit()
        }
    })
Surely this can be adapted for use on any kind of Selenium based testing wrapper/framework/script.
The post WebdriverIO – Injecting JavaScript to Capture AJAX Errors appeared first on Francis Kim.