Skip to content

Instantly share code, notes, and snippets.

@adamelso
Created May 18, 2016 10:06
Show Gist options
  • Save adamelso/b44e43baac53cf5a512a5bc392bd2c5e to your computer and use it in GitHub Desktop.
Save adamelso/b44e43baac53cf5a512a5bc392bd2c5e to your computer and use it in GitHub Desktop.
PayPal Express Checkout - Behat Mink UI test with Selenium 2 Driver
<?php
class CheckoutWithPaypalContext extends MinkContext
{
/**
* @When /^I choose to pay with PayPal$/
*/
public function iChooseToPayWithPayPal()
{
$this->findAndWaitForElementToBeVisibleThenClick("//a[@id = 'paypal']");
sleep(5);
// Okay, now we're in PayPal
$page = $this->getSession()->getPage();
$iframes = $page->findAll('xpath', '//iframe');
/**
* Get the first frame, there might be more than one.
*
* @var NodeElement $payPalIframe
*/
$payPalIframe = $iframes[0];
// Then we switch to it. Mink works on a document-by-document basis.
$this->getSession()->getDriver()->switchToIFrame($payPalIframe->getAttribute('name'));
$payPalEmail = $page->findById('email');
$payPalPassword = $page->findById('password');
$doLogin = $page->findById('btnLogin');
$payPalEmail->setValue('my_sandbox_checkout_account@evilcorp.com.xxy');
$payPalPassword->setValue('420420');
$doLogin->click();
// Ridiculous, but PayPal takes a few seconds to process.
sleep(10);
$doConfirm = null;
$iteration = 0;
while (null === $doConfirm && $iteration <= 5) {
++$iteration;
sleep(5);
$doConfirm = $this->getSession()->getPage()->findById('confirmButtonTop');
}
$doConfirm->click();
// Return back to the shop.
$this->getSession()->wait(10000, '"undefined" !== typeof someVariableIKnowExistsOnlyOnMySite');
}
}
@joshlopes
Copy link

Thank you. It worked for me.

@AnneTee
Copy link

AnneTee commented Aug 2, 2017

Awesome, thanks for this! 👍

@Nguimjeu
Copy link

Hoo many thanks for this.
I must mention that I had to switch back from the iframe to the regular window after the login

$this->getSession()->getDriver()->switchToIFrame();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment