by Kevin Schroeder | 3:20 pm

I added a Twitter OAuth integration in @magiumlib so you can easily do authentication automation. pic.twitter.com/NjHvvRDG9G

— Kevin Schroeder (@kpschrade) April 6, 2016

This is a simple library to help browser tests perform OAuth logins to Twitter.

To install

composer require magium/twitter

To use:

use MagiumTwitterActionsAuthenticateTwitter;

class TwitterTest extends MagiumAbstractTestCase
{

    public function testLogin()
    {
        // Do something that forwards the browser to the twitter OAuth page.

         $action = $this->getAction(AuthenticateTwitter::ACTION);
        /* @var $action AuthenticateTwitter */
        $action->execute();
    }

}

Setting the username and password

There are two ways to set the username and password

In code

use MagiumTwitterIdentitiesTwitter
use MagiumTwitterActionsAuthenticateTwitter;

class TwitterTest extends MagiumAbstractTestCase
{

    public function testLogin()
    {

        $identity = $this->getIdentity(Twitter::IDENTITY);
        /* @var $identity Twitter */
        $identity->setUsername('username');
        $identity->setPassword('password');

        // Do something that forwards the browser to the twitter OAuth page.

         $action = $this->getAction(AuthenticateTwitter::ACTION);
        /* @var $action AuthenticateTwitter */
        $action->execute();
    }

}

In configuration

Create the file /configuration/Magium/Twitter/Identities/Twitter.php and enter the following:

<?php

/* @var $this MagiumTwitterIdentitiesTwitter */
$this->username = 'username';
$this->password = 'password';

Note that you should be familiar with AbstractConfigurableElements to do this

Not in Magium

This library doesn’t require Magium to run. Well, actually it does, but it doesn’t need the abstract test class. It’s a little verbose, but this is what it would look like (you could also use a DIC to manage this).

<?php

namespace TestsMagiumTwitter;

use FacebookWebDriverRemoteDesiredCapabilities;
use FacebookWebDriverRemoteRemoteWebDriver;
use FacebookWebDriverWebDriverBy;
use MagiumTwitterActionsAuthenticateTwitter;
use MagiumTwitterIdentitiesTwitter;
use MagiumUtilConfigurationClassConfigurationReader;
use MagiumUtilConfigurationConfigurationCollectorDefaultPropertyCollector;
use MagiumUtilConfigurationConfigurationReader;
use MagiumUtilConfigurationEnvironmentConfigurationReader;
use MagiumUtilConfigurationStandardConfigurationProvider;
use MagiumWebDriverExpectedCondition;
use MagiumWebDriverWebDriver;

class NoAbstractTestClassTest extends PHPUnit_Framework_TestCase
{

    public function testWithoutMagium()
    {
        // Do some stuff to get to the Twitter authenticaation page

        // Start from here

        $configurationProvider = new StandardConfigurationProvider(
            new ConfigurationReader(),
            new ClassConfigurationReader(),
            new EnvironmentConfigurationReader()
        );
        $collector = new DefaultPropertyCollector();

        $action = new AuthenticateTwitter(
            $webDriver,
            new MagiumTwitterThemesTwitter($configurationProvider, $collector),
            new MagiumTwitterIdentitiesTwitter($configurationProvider, $collector)
        );

        // Everything up to here could have been done using a DIC.

        $action->execute();
        $webDriver->quit();
    }

}

Powered by WPeMatico

Comments

No comments yet...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.