Automatically testing 3D Secure orders on staging with Selenium

Automatically testing 3D Secure orders on staging with Selenium

Intro

Here's a Selenium script to quickly place five 3DS test orders on ATD staging.

How to run this locally

One-time setup - Mac

  1. Make sure you have composer installed
  2. Clone this git repo: https://bitbucket.org/atdtravel/selenium
  3. From the git directory, run "composer install"
  4. Download Selenium Standalone Server (it is a single .jar file)

One-time setup - Windows

  1. Install a stable 5.x version of php from here and enable the curl and openssl extensions in php.ini (which you might have to create, based on one of the distributed example .ini files)
  2. Install composer from the Windows installer here
  3. Install git from here
  4. Clone this git repo into a new folder somewhere: https://bitbucket.org/atdtravel/selenium (you might need to use the https:// url rather than the ssh:// one)
  5. From the git directory, run "composer install"
  6. Download Selenium Standalone Server (it is a single .jar file)
  7. Install ChromeDriver

Running the script

  1. Run Selenium Standalone Server with: "java -jar selenium-server-standalone-3.4.0.jar" (or whatever version you downloaded)
  2. (Windows only) Run ChromeDriver by double-clicking it
  3. From the git directory, run "php 3ds_front_end.php"

Script

<?php

require 'vendor/autoload.php';
require '_init.php';

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;

$num_tests = 5;

for ($i=0; $i<$num_tests; $i++) {

  print ("Test order " . ($i+1) . " of $num_tests...\n");

  $driver = RemoteWebDriver::create($host, DesiredCapabilities::chrome());
  $driver->manage()->timeouts()->implicitlyWait = 10;

  // Add item to cart and go to checkout:
  // See: https://atdtravel.atlassian.net/wiki/display/~rangi/2013/12/04/Ubercart+shortcut+links+-+Empty+your+cart
  $url = "http://atd.atdtravel.com/cart/add/e-p152968_q1?destination=cart/checkout";
  $driver->navigate()->to($url);

  // Wait till credit cart form loads:
  $driver->wait()->until(
    WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::id('edit-cc-number'))
  );

  // See: https://atdtravel.atlassian.net/wiki/display/phoenix/Smartpay+test+credit+cards
  $test_cc_number = "4212345678901237";

  // See: https://atdtravel.atlassian.net/wiki/display/~rangi/2015/10/13/A+snippet+to+quickly+submit+the+checkout+page
  $submit_checkout_js = 'var email = "atd-test-order@mailinator.com"; $("input[type=text]").val("Test"); $("input[type=tel]").val("12341234"); $("input[id$=-10]").val("9pm"); $("input[id$=-5]").val("12/12/2017"); $("input[name*=departure_date]").val("17/07/2017"); $("#edit-panes-customer-primary-email").val(email); $("#edit-panes-uc-termsofservice-agreement-checkout-tos-agree-popup").prop("checked", true); $("#fill-details").click(); $("#edit-cc-number").val("' . $test_cc_number . '"); $("#edit-panes-billing-billing-zone")[0].selectedIndex = 1; $("#edit-continue").click();';

  // Submit checkout form:
  $driver->executeScript($submit_checkout_js);

  // Focus on 3D Secure iframe:
  $iframe = $driver->findElement(WebDriverBy::className('smart_pay_iframe'));
  $driver->switchTo()->frame($iframe);
  $driver->wait()->until(
    WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::id('username'))
  );
  $driver->findElement(WebDriverBy::id('username'))->click();
  $driver->getKeyboard()->sendKeys('user');
  $driver->findElement(WebDriverBy::id('password'))->click();
  $driver->getKeyboard()->sendKeys('password');
  $driver->findElement(WebDriverBy::className('paySubmit'))->click();

  // Go back to top frame:
  $driver->switchTo()->defaultContent();

  $driver->close();
}

Troubleshooting

Issue: "Address already in use" when running Selenium Standalone Sever

This can happen if another instance of the program is already running. On Mac you can check this with "ps aux | grep -i selen" and on Windows you may need to look for and terminate unknown java instances in the Task Manager.

 

blog tag: