Lightning Testing Service (LTS)

Introduction to Lightning Test Services with Jasmine

In previous post, we discussed what is Jasmine and simple hello world program to see how it works. In this blog post, we will discuss how Jasmine can be used in Salesforce to test Lightning Component’s Javascript code.

Note : Everything we are discussing here is completely covered as part of Live coding in Apex Salesforce Saturday organized by Amit. Would like to thank Mohith for his support and answers during the demo. It also shows How SalesforceDX can be used daily by developers to perform development.

Jasmine and Mocha support is provided as a part of Lightning Test Service (LTS) by Salesforce. Jasmine and Mocha, both are well known and industry wide solution accepted in Javascript world. of course, Salesforce don’t want to stay behind in terms of its offering to developers thats why we have new baby in town – Lightning Test Service (LTS).

You can install Lightning Test Service (LTS) in your org as a part of unamanged package from here. You can also install Lightning Test Service in SFDX using command

sfdx force:lightning:test:install

Once Jasmine is installed, you can navigate in your browser to open Jasmine test runner page using URL https://<YourDomain>.lightning.force.com/c/jasmineTests.app

SFDX Command to open Jasmine Test runner in Salesforce

sfdx force:org:open -p /c/jasmineTests.app

Let’s consider below Lightning component to be used by Lightning Test Service (LTS) 

Lightning Component using DataTable and Lightning Test Service
Lightning Component using DataTable and Lightning Test Service

This Lightning Component uses lightning:datatable component and populates it on basis of input provided.

Now we need to create a Javascript file containing Jasmine test method and upload it as a static resource.

Once, static resource is uploaded in Salesforce, we need to add reference of this static resource in JasmineTests.app . In our case, static resource name is JasmineTestDatatable.  This Test runner application comes as part of Lightning Test Service. Same way, MochaTest runner app also comes as part of LTS.

It is time to open Jasmine Test runner using URL https://<YourDomain>.lightning.force.com/c/jasmineTests.app. We would be able to see below output

Salesforce Lightning Test Service (LTS) Output
Salesforce Lightning Test Service (LTS) Output

Youtube Video of Lightning Test Service (LTS)

 

Posted

in

by


Related Posts

Comments

4 responses to “Introduction to Lightning Test Services with Jasmine”

  1. Uma Avatar
    Uma

    Hi Jitendra,

    Nice and informative article..
    What is your view on automated testing with Selenium in Lightning. I have seen your videos on Selenium testing in classic. Have you had any luck with using Selenium to test lightning pages. For eg: Creation of account to make it simple. Due to the fact that ids are generated dynamically Selenium is not able to find the target. Any ideas to overcome this issue?

    Thanks
    Uma

    1. Chiranjeevi Avatar
      Chiranjeevi

      Hi Uma,

      We can implement UI automation for the Lightning component using Selenium Web Driver. You can avoid using Id’s to locate the Web Elements. Below are the list of Xpath’s which could be useful:

      If you are trying to perform a “sendkeys” action on the leads page, you can follow similar XPath Locators that I’ve created for my project:

      By.xpath(“//fieldset[contains(@class,’uiInput’)]//span[contains(text(),’Last Name’)]/following::input[1]”);

      By.xpath(“//*[contains(@class,’inputLabel’)]//span[contains(text(),’Phone’)]/following::input[1]”);

      By.xpath(“//*[contains(@class,’inputLabel’)]//span[contains(text(),’Email’)]/following::input[1]”);

      By.xpath(“//div[contains(@class,’forceInputPicklist’)]//span[contains(text(), ‘Lead Source’)]/following::a[1]”);

      If you follow this dynamic XPath pattern, you will never have to use dynamic @id tags used for Lightning.

  2. Vishal Ranjan Avatar
    Vishal Ranjan

    Hi Jitendra,

    I am new to lightning testing services. I came across to a scenario where I have to write LTS for onblur event which is present in tag and when onblur is called, it will invoke a controller method.

    Can you please help we this scenario.

    Thanks,
    Vishal

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading