Galen JavaScript API

load

load( jsFilePath )

Loads and executes the javascript in specified file

  • jsFilePath - string type. A path to a javascript file.
load("mycomponent.js"); load("someOtherStuff.js"); // some more js code

Important to note that Galen loads the specified javascript only once. So if your tests in multiple files load the same javascript file – it will be executed only once.

Sometimes you might have different folder levels and fromt their you need to access the script from the root folder of a project. Instead of having ../../somescript.js you could use slash in the beginning:

load("/somescript.js");

createDriver

createDriver( [ url, browserSize, browserType ] )

Creates a WebDriver instance. Also creates a RemoteWebDriver in Grid in case it was configured in config file

  • url - String type. A url to be loaded in a browser
  • browserSize - String type (e.g. “650x480”). Dimensions of browser window size
  • browserType - String type. A name of a browser. Could be one of these values: firefox, chrome, ie, phantomjs
var driver = createDriver("http://galenframework.com", "640x480", "firefox"); // can also be called like this var driver = createDriver("http://galenframework.com"); // or just like this var driver = createDriver();

createGridDriver

createGridDriver( gridHubUrl, settings )

  • gridHubUrl - String type. A url to Selenium Grid Hub
  • settings - Object type. A set of key/value pairs that specify criteria to fetch a Selenium Grid node
    • browser - a name of a browser
    • browserVersion - a version of a browser
    • platform - a name of a platform (LINUX, WINDOWS, VISTA, XP etc.)
    • size - a browser window size. After driver instantiation it will change the size of the windows. This property should not be used in case of mobile node. It is only working on desktop browsers.
    • desiredCapabilities - Object type. A set of key/value pairs that provide desired capabilities to driver.
var driver = createGridDriver("http://mygrid/wd/hub", { browser: "iphone", desiredCapabilities: { platform: "OS X 10.8", version: "6.0", deviceName: "iPhone", "device-orientation": "portrait" } });

resize

resize( driver, size )
Changes the size of browser window

  • driver - an instance of WebDriver
  • size - a string with size (e.g. “1024x768”)
resize(driver, "1024x768");

checkLayout

checkLayout( driver, specFile, [ tagsToInclude, tagsToExclude ] )

  • driver - WebDriver type. An instance of WebDriver
  • specFile - String type. A path to spec file
  • tagsToInclude - String[] type (optional) - An array of tags that should be included when checking layout
  • tagsToExclude - String[] type (optional) - An array of tags that should be excluded when checking layout
test("Home page", function() { var driver = createDriver("http://galenframework.com", "1024x768"); checkLayout(driver, "homePage.gspec", ["all", "desktop"]); });

You can also use checkLayout with a single argument like this
checkLayout( settings )

  • settings - PlainObject. A set of key/value that specifies different properties for checking layout.
    • driver - WebDriver type. An instance of WebDriver
    • spec - String type. A path to spec file
    • tags - String[] type (optional) - An array of tags that should be included when checking layout
    • excludedTags - String[] type (optional) - An array of tags that should be excluded when checking layout
    • screenshot - String type (optional) - A path to a screenshot. In case this property is provided Galen will not make a screenshot and will use the provided screenshot instead in reports and for image analysis
    • properties - Properties type (optional). Used for providing custom properties when parsing a spec file.
    • vars - Map type (optional) - A set of key value pairs which will be used in spec as variables.
    • objects - Map type (optional) - a set of object locators which can be referenced from spec
checkLayout({ driver: driver, spec: "specs/homepage.gspec", tags: ["mobile", "iphone"], vars: { customVariable: "custom value" }, objects: { header: "#header", footer: "xpath: //div[@id='footer']" } });

checkPageSpecLayout

checkPageSpecLayout( driver, pageSpec, [ tagsToInclude, tagsToExclude ] )

  • driver - WebDriver type. An instance of WebDriver
  • pageSpec - PageSpec type. A path to spec file
  • tagsToInclude - String[] type (optional) - An array of tags that should be included when checking layout
  • tagsToExclude - String[] type (optional) - An array of tags that should be excluded when checking layout
test("Home page", function() { var driver = createDriver("http://galenframework.com", "1024x768"); var pageSpec = parsePageSpec({ driver: driver, spec: "homePage.gspec", tags: ["all", "desktop"] }); checkPageSpecLayout(driver, pageSpec, ["all", "desktop"]); });

You can also use checkPageSpecLayout with a single argument like this
checkPageSpecLayout( settings )

  • settings - PlainObject. A set of key/value that specifies different properties for checking layout.
    • driver - WebDriver type. An instance of WebDriver
    • pageSpec - PageSpec type. A path to spec file
    • tags - String[] type (optional) - An array of tags that should be included when checking layout
    • excludedTags - String[] type (optional) - An array of tags that should be excluded when checking layout
    • screenshot - String type (optional) - A path to a screenshot. In case this property is provided Galen will not make a screenshot and will use the provided screenshot instead in reports and for image analysis
    • properties - Properties type (optional). Used for providing custom properties when parsing a spec file.
    • vars - Map type (optional) - A set of key value pairs which will be used in spec as variables.
    • objects - Map type (optional) - a set of object locators which can be referenced from spec
checkPageSpecLayout({ driver: driver, pageSpec: pageSpec, tags: ["mobile", "iphone"], vars: { customVariable: "custom value" }, objects: { header: "#header", footer: "xpath: //div[@id='footer']" } });

parsePageSpec

parsePageSpec( settings )

Takes same arguments as checkLayout function but instead of checking layout it parses a page spec and return a PageSpec object.

  • settings - PlainObject. A set of key/value that specifies different properties for checking layout.
    • driver - WebDriver type. An instance of WebDriver
    • spec - String type. A path to spec file
    • tags - String[] type (optional) - An array of tags that should be included when checking layout
    • excludedTags - String[] type (optional) - An array of tags that should be excluded when checking layout
    • screenshot - String type (optional) - A path to a screenshot. In case this property is provided Galen will not make a screenshot and will use the provided screenshot instead in reports and for image analysis
    • properties - Properties type (optional). Used for providing custom properties when parsing a spec file.
    • vars - Map type (optional) - A set of key value pairs which will be used in spec as variables.
    • objects - Map type (optional) - a set of object locators which can be referenced from spec
var pageSpec = parsePageSpec({ driver: driver, spec: "specs/homepage.gspec", tags: ["mobile", "iphone"], vars: { customVariable: "custom value" }, objects: { header: "#header", footer: "xpath: //div[@id='footer']" } });

loadProperties

loadProperties( filePath )

Reads a specified property file and returns Java Properties object

  • filePath - String type. A path to a properties file
var properties = loadProperties("/path/to/file/mycustom.properties"); var name = properties.get("user.name");

readFile

readFile( filePath )

Reads the content of specified file and returns a String with file content

  • filePath - String type. A path to a file
var content = readFile("/path/to/file/names.txt");

listDirectory listDirectory( directoryPath )

Returns an array of files and directories in specified path

var list = listDirectory("/path/to/directory");

makeDirectory

makeDirectory( directoryPath )

Creates directories in all specified path. Returns true if the directory was created, false – otherwise.

makeDirectory("/path/to/new/directory");

fileExists

fileExists( filePath )

Checks whether the specified directory or file exists and returns true or false.

var path = "someFile.txt"; var text = ""; if (fileExists(path)) { text = readFile(path); }

isDirectory

isDirectory( directoryPath )

Returns true if the given path is a directory, false – otherwise

var path = "/opt/logs"; if (!isDirectory(path)) { throw new Error("The give log path is a not directory"); }

retry

retry( tries, callback )

Executes the specified callback with a specified amount of tries in case of error. If a callback is executed without error – it stops. Returns the object that is returned from the callback.

  • tries - Amount of times it should retry the specified callback
  • callback - function. Takes one number argument that represents amount of tries left
var driver = retry(3, function (triesLeft) { return createDriver("http://example.com"); });

takeScreenshot

takeScreenshot( driver )

Takes screenshot from specified driver and return a Java File instance with path to create screenshot

  • driver - a WebDriver instance
var file = takeScreenshot(driver); var path = file.getAbsolutePath();

cookie( driver, cookieValue )

Sets the specified cookie in the page

  • driver - a WebDriver instance
  • cookieValue - String type. A cookie that should be set in a browser
cookie(driver, "UserName=John; path=/");

inject

inject( driver, jsScript )

Inject the specified javascript on the page

  • driver - a WebDriver instance
  • jsScript - String type. A script that should be injected in a browser
inject(driver, "document.getElementById('mybutton').innerHTML = 'my button';");

createTestDataProvider

createTestDataProvider( variableName )

Creates a data provider and returns it as a function

  • variableName - a name for a data variable which will be accessible in data field of the test instance
this.featureSwitched = createTestDataProvider("featureSwitches"); featureSwitched("Feature1", function () { test("Home page test", function () { console.log("All my features are here " + this.data.featureSwitches); }); });

dumpPage

dumpPage(settings)

Creates a dump for a current open page in a browser

  • settings - an object that contains the following properties:
    • driver - an instance of WebDriver
    • name - a name of a page
    • spec - a path to a spec file
    • exportPath - a path to a folder where Galen should store page dump
    • maxWidth - a maximum width of an element for which it should create an image sample
    • maxHeight - a maximum height of an element for which it should create an image sample
    • onlyImages - a boolean argument. If set to true then only images will be saved to specified exportPath without any report files.
    • excludedObjects - an array of object names that should be excluded from page dump
dumpPage({ driver: driver, name: "Home page", spec: "specs/homepage.gspec", exportPath: "dumps/homepage", maxWidth: 200, maxHeight: 200, onlyImages: false, excludedObjects: ["header", "footer"] });

logged

logged(text, callback)

Wraps the callback into report section for html report

  • text - a report section text
  • callback - the callback that will be called within report section

loggedFunction

loggedFunction(textExpression, callback)

Creates a function that will be logged in html report once it is called. The textExpression argument is used for generating the report text message based on the function arguments that were used during the call.

  • textExpression - an expression that is used to generate log text based on the arguments of the initial function call. To refference the argument in the expression you can use ${...} syntax in expression. Each argument can be refferenced with undescore and argument index. E.g. of valid expression "Log as ${_1} with password ${_2}"
  • callback - a callback function that will be executed once the returned function is called. All the arguments passed to the returned function will be passed to a callback
this.LoginPage = $page("Login", { emailTextfield: ".email", passwordTextfield: ".password", submitButton: "#submit", loginAs: loggedFunction("Log-in as ${_1} with password ${_2}", function (email, password) { this.emailTextfield.typeText(email); this.passwordTextfield.typeText(password); this.submitButton.click(); }); }); var loginPage = new LoginPage(driver); loginPage.loginAs("testuser@example.com", "qwerty");

Comments

We are moving all the discussions to Google Groups. From this moment, if you have problems with your test code or some issues with installation, please ask your questions in https://groups.google.com/forum/#!forum/galen-framework. This way more people will be able to help you.

comments powered by Disqus