Snippets

For more snippets, click here.

Change the size of the browser window

var Dimension = Packages.minium.Dimension;

var width = 1920, height = 1080;
var dimension = new Dimension(width, height);
browser.configure().window().setSize(dimension);
var windows = $(":root").windows();
var popupWindow = windows.has(windows.find("title").withText("The title of the popup window"));
var btn = popupWindow.find(":button").withText("OK");
btn.click();
popupWindow.close();

Alerts

var alert = page.alert();

// test the message of the alert
var alertMessage = String(alert.getText());
expect(alertMessage).to.be("expected message");

// close the alert
alert.accept();

Iframes

// once you've got your frame, use it to find elements inside it
var lowerArea = $("frame").withName("lower").frames();
lowerArea.find(":input").withLabel("Name");

// iframes can be nested
var nestedFrame = $(":root").find("#myframe").frames().find("iframe").frames();

Send keyboard keys to an element

var keys = require("minium/keys");
$("#some-field").sendKeys(keys.ENTER);
$("#some-field").sendKeys(keys.chord([keys.CONTROL, keys.ENTER])); // sends Ctrl + Enter

Drag and drop files from the desktop into the browser

// creates an invisible input element of type file, to send the file to the browser
$(":root").apply(function () {
  $("<input/>").attr({ id: 'miniumUpload', type:'file' }).css("opacity", "0").appendTo("body");
});

var holder = $("#holder"); // TODO: replace this expression so that it identifies the element in which the file will be dropped
var uploadFld = $("#miniumUpload");

var file = new Packages.java.io.File("C:/file-to-upload"); // TODO: set the path to the file
uploadFld.type(file.getAbsolutePath());

holder.apply(function () {
  var files = $("#miniumUpload").get(0).files;
  var evObject = {
    target: this,
    dataTransfer : {
      files: files
    }
  };

  var ev = $.Event('drop');
  ev.originalEvent = evObject;
  ev.dataTransfer = evObject.dataTransfer;
  $(this).trigger(ev);
});

Wait a specific amount of time

var timeUnits = require("minium/timeunits");

$(":root").waitTime(5, timeUnits.SECONDS);

Save screenshot to file

browser.screenshot().saveTo('/path/to/some/folder/screenshot.png');

Check the type of an input field

if (fld.is("select")) {
  fld.select(val);
} else if (fld.is(":radio, :checkbox")) {
  fld.check();
} else {
  fld.fill(val);
}

Add new methods to Elements

Wait for an animated element to stabilize

// adds a method that waits for an animated element to stabilize
$.fn.stabilize = function (time) {
  return this.applyWebElements(function () {
    return $(this).filter(function () {
      var prev = $(this).data("prevBoundingClientRect");
      var curr = $.extend({}, this.getBoundingClientRect());

      // if stable, returns this jquery object
      if (prev && prev.right === curr.right && prev.top === curr.top && prev.width === curr.width && prev.height === curr.height) {
        return true;
      }

      // else store current bounding box and return empty
      $(this).data("prevBoundingClientRect", curr);
      return false;
    });
  });
};

$("#animated-elem").stabilize().waitForExistence();
var cookies = browser.configure().cookies();
var cookie = cookies.get("name");
var cookieValue = cookie.getValue();

Evaluate code in the page's context

$(":root").eval("$(this).find('title')");

Offsets

var offsets = require("minium/offsets");

var offset = offsets.at(
  offsets.horizontal.top.plus(20).pixels(),
  offsets.vertical.middle.plus(20).percent());

$("#some-element").click(offset);