Tag Archives: Javascript

Using PhantomJS to read a file and output to a file.

1. Download PhantomJS from here: http://phantomjs.org/

2. On Windows, unzip the archive to your drive and put the path to the phantonjs.exe in your Environment Variables Path:

C:\phantomjs-1.9.0-windows\;

3. Save this as readFile.js

/*jslint indent: 4*/
/*globals document, phantom*/
'use strict';

var fs = require('fs'),
    system = require('system');

if (system.args.length < 2) {
    console.log("Usage: readFile.js FILE");
    phantom.exit(1);
}

var content = '',
    f = null,
    lines = null,
    eol = system.os.name == 'windows' ? "\r\n" : "\n";

try {
    f = fs.open(system.args[1], "r");
    content = f.read();
} catch (e) {
    console.log(e);
}

if (f) {
    f.close();
}

if (content) {
    lines = content.split(eol);
    for (var i = 0, len = lines.length; i < len; i++) {
        console.log(lines[i]);
    }
}

phantom.exit();

4. Execute it like this:

phantomjs readFile.js filetoread > somefile.txt

You can use this script to open log files, parse out important lines, and save to an HTML file for instance.


Select element with jQuery where the id contains specific text.

I have needed to do this on occasion with long auto-generated ids.

$j("[id$='logActivitiesCheckbox']");

To get a WebElement object back from WebDriver I use this method:

  public WebElement getWebElementFromJquery(String jQuery) {
    WebElement jqElement = null;
    JavascriptExecutor js = (JavascriptExecutor) driver;
    String query = "return " + jQuery+ ".get(0);";
    try {
      jqElement = (WebElement) js.executeScript(query);
    } catch (Exception e) {
      logger.error("jQuery() " + e.getMessage());
    }
    return jqElement;
  }

Learnings in Webdriver using C#.Net

Webdriver Tips and Techniques

The Automation Tester

My experiance with test automation

Learn WebDriver

This WordPress.com site is the cat’s pajamas

Assert Selenium

Selenium Automation in a Right Way