Jasmine

Applies to CrossBrowserTesting SaaS, last modified on November 26, 2020

Jasmine is a behavior-driven development framework. It is non-dependent on other JavaScript frameworks and makes writing tests easy. In this guide we will use Jasmine for testing using the Selenium WebDriver and the JavaScript programming language.

Get set up

  • Install Selenium

    To get started, install Selenium bindings for JavaScript using the command:

    npm install selenium-webdriver
  • Install Jasmine

    Then let's get Jasmine installed:

    npm install --save-dev jasmine
  • Initialize project

    Initialize a new Jasmine project:

    node_modules/jasmine/bin/jasmine init

Write tests

Create a file spec/test.js and copy the following script into it. Be sure to add your CBT username and authkey to the script. To get yours, sign up for a free trial or purchase a plan.

spec/test.js

selenium = require('selenium-webdriver');
var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
jasmine.getEnv().defaultTimeoutInterval = 60000; // in microseconds.
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;

describe('Selenium Tutorial', function() {

    beforeEach(function(done) {
        
        caps = {
            'name' : 'Jasmine Example',
            'platform' : 'Windows',
            'browserName' : 'Chrome',
            'version' : '75',
            'record_video' : 'true',
            'username' : 'YOUR_USERNAME',
            'password' : 'YOUR_AUTHKEY'
        };
            
        var remoteHub = 'http://hub.crossbrowsertesting.com:80/wd/hub';
    
         this.driver = new selenium.Builder().
            usingServer(remoteHub).
            withCapabilities(caps).
            build();

        this.driver.get('http://crossbrowsertesting.github.io/login-form.html').then(done);
    });

    // Close the website after each test is run (so that it is opened fresh each time)
    afterEach(function(done) {
        this.driver.quit().then(done);
    });

    it('Should be on the home page', function(done) {

        this.driver.findElement(selenium.By.id("username")).sendKeys("tester@crossbrowsertesting.com");
        this.driver.findElement(selenium.By.xpath("//*[@type=\"password\"]")).sendKeys("test123");
        this.driver.findElement(selenium.By.css("button[type=submit]")).click();
        var element = this.driver.wait(selenium.until.elementLocated(selenium.By.id("logged-in-message")), 10000);

        element.getAttribute('id').then(function(id) {
            expect(id).toBe('logged-in-message');
            console.log("The test was successful");
            done();
        });

    });

});

To run your test, use the command:

node_modules/jasmine/bin/jasmine spec/test.js

Congratulations! You have successfully configured an automated test using Jasmine.

For examples and source code to this tutorial, check out our  Jasmine GitHub Repository

If you have any trouble, feel free to get in touch. We are always happy to help!

See Also

Test Frameworks and Tools
About Selenium Testing
Selenium and JavaScript
CucumberJS
InternJS
Jest
Karma
Mocha
NightwatchJS
Protractor
WebDriverIO and CrossBrowserTesting

Highlight search results