├── .gitignore
├── .gitpod.yml
├── README.md
├── conf
├── parallel.conf.js
└── single.conf.js
├── package.json
├── specs
├── parallel_test.js
└── single_test.js
└── target
└── npmlist.json
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | package-lock.json
--------------------------------------------------------------------------------
/.gitpod.yml:
--------------------------------------------------------------------------------
1 |
2 | # List the start up tasks. You can start them in parallel in multiple terminals. See https://www.gitpod.io/docs/config-start-tasks/
3 | tasks:
4 | - init: npm install -g mocha && npm install
5 | command: npm test
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Run Selenium Tests With Mocha On LambdaTest
2 |
3 |
4 |
5 |
6 |
7 |
8 | Blog
9 | ⋅
10 | Docs
11 | ⋅
12 | Learning Hub
13 | ⋅
14 | Newsletter
15 | ⋅
16 | Certifications
17 | ⋅
18 | YouTube
19 |
20 |
21 |
22 |
23 |
24 | *Learn how to use Mocha framework to configure and run your JavaScript automation testing scripts on the [LambdaTest Selenium cloud platform](https://www.lambdatest.com/selenium-automation/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample).*
25 |
26 | [
](https://accounts.lambdatest.com/register)
27 |
28 | ## Table of Contents
29 |
30 | * [Pre-requisites](#pre-requisites)
31 | * [Run Your First Test](#run-your-first-test)
32 | * [Executing the Test](#executing-the-test)
33 | * [Running Your Parallel Tests Using Mocha Framework](#running-your-parallel-tests-using-mocha-framework)
34 | * [Testing Locally Hosted or Privately Hosted Projects](#testing-locally-hosted-or-privately-hosted-projects)
35 |
36 |
37 | ## Pre-requisites
38 |
39 | Before getting started with Selenium automation testing on LambdaTest, you need to:
40 |
41 | * Download and install **NodeJS**. You should be having **NodeJS v6** or newer. Click [here](https://nodejs.org/en/) to download.
42 | * Make sure you are using the latest version of **JavaScript**.
43 | * Install **npm** from the official website by clicking [here](https://www.npmjs.com/).
44 | * Download [Selenium JavaScript bindings](https://www.selenium.dev/downloads/) from the official website. Latest versions of **Selenium Client** and **WebDriver** are ideal for running your JavaScript automation testing script on LambdaTest’s Selenium Grid.
45 |
46 | ### Installing Selenium Dependencies and tutorial repo
47 |
48 | **Step 1:** Clone the LambdaTest’s [mocha-selenium-sample repository](https://github.com/LambdaTest/mocha-selenium-sample) and navigate to the code directory as shown below:
49 | ```bash
50 | git clone https://github.com/LambdaTest/mocha-selenium-sample
51 | cd mocha-selenium-sample
52 | ```
53 | **Step 2:** Install the required project dependencies using the command below:
54 | ```bash
55 | npm i
56 | npm install selenium-webdriver
57 | ```
58 |
59 | ### Setting up Your Authentication
60 |
61 | Make sure you have your LambdaTest credentials with you to run test automation scripts on LambdaTest Selenium Grid. You can obtain these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/build/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample) or through [LambdaTest Profile](https://accounts.lambdatest.com/login/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample).
62 |
63 | Set LambdaTest `Username` and `Access Key` in environment variables.
64 |
65 | * For **Linux/macOS**:
66 | ```js
67 | export LT_USERNAME= "YOUR_LAMBDATEST_USERNAME"
68 | export LT_ACCESS_KEY= "YOUR_LAMBDATEST_ACCESS_KEY"
69 | ```
70 |
71 | * For **Windows**:
72 |
73 | ```js
74 | $env:LT_USERNAME="YOUR_LAMBDATEST_USERNAME"
75 | $env:LT_ACCESS_KEY="YOUR_LAMBDATEST_ACCESS_KEY"
76 | ```
77 |
78 | ## Run Your First Test
79 |
80 | ### Sample Test with MochaJS
81 |
82 | **Test Scenario:** Check out [single_test.js](https://github.com/LambdaTest/mocha-selenium-sample/blob/master/specs/single_test.js) file to view the sample test script for running your first test.
83 |
84 | ### Configuration of Your Test Capabilities
85 |
86 | In `conf/single.conf.js` file, you need to update your capabilities. In this code, we are passing browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via capabilities object. The capabilities object in the above code are defined as:
87 | ```js
88 | exports.capabilities = {
89 | 'build': 'Mocha-Selenium-Sample', //Build name
90 | 'name': 'Your Test Name', // Test name
91 | 'platform':'Windows 10', // OS name
92 | 'browserName': 'chrome', // Browser name
93 | 'version': 'latest', // Browser version
94 | 'visual': false, // To take step by step screenshot
95 | 'network':false, // To capture network Logs
96 | 'console':false, // To capture console logs.
97 | 'tunnel': false // If you want to run the localhost than change it to true
98 | };
99 | ```
100 | > You can generate capabilities for your test requirements with the help of our inbuilt **[Capabilities Generator tool](https://www.lambdatest.com/capabilities-generator/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample)**.
101 |
102 | ## Executing the Test
103 |
104 | The tests can be executed in the terminal using the following command.
105 | ```bash
106 | npm run single
107 | ```
108 | Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on [LambdaTest automation dashboard](https://automation.lambdatest.com/build). LambdaTest Automation Dashboard will help you view all your text logs, screenshots and video recording for your entire automation tests.
109 |
110 | ## Running Your Parallel Tests Using Mocha Framework
111 |
112 | ### Setting up the Parallel Environment
113 |
114 | **Test Scenario:** Check out the [parallel_test.js](https://github.com/LambdaTest/mocha-selenium-sample/blob/master/specs/parallel_test.js) file which would help you to run a single test on various browsers at the same time.
115 |
116 | ## Executing Parallel Tests using Mocha
117 |
118 | To run parallel tests using **Mocha**, we would have to execute the below command in the terminal:
119 | ```bash
120 | npm run parallel
121 | ```
122 | Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on [LambdaTest automation dashboard](https://automation.lambdatest.com/build/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample).
123 |
124 | ## Testing Locally Hosted or Privately Hosted Projects
125 |
126 | You can test your locally hosted or privately hosted projects with [LambdaTest Selenium grid cloud](https://www.lambdatest.com/selenium-automation/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample) using LambdaTest Tunnel app. All you would have to do is set up an SSH tunnel using LambdaTest Tunnel app and pass toggle `tunnel = True` via desired capabilities. LambdaTest Tunnel establishes a secure SSH protocol based tunnel that allows you in testing your locally hosted or privately hosted pages, even before they are made live.
127 |
128 | >Refer our [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) for more information.
129 |
130 | Here’s how you can establish LambdaTest Tunnel.
131 |
132 | >Download the binary file of:
133 | >* [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip)
134 | * [LambdaTest Tunnel for Mac](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip)
135 | * [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip)
136 |
137 | Open command prompt and navigate to the binary folder.
138 |
139 | Run the following command:
140 | ```bash
141 | LT -user {user’s login email} -key {user’s access key}
142 | ```
143 | So if your user name is lambdatest@example.com and key is 123456, the command would be:
144 | ```bash
145 | LT -user lambdatest@example.com -key 123456
146 | ```
147 | Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code shown below :
148 |
149 | **Tunnel Capability**
150 | ```js
151 | const capabilities = {
152 | tunnel: true,
153 | }
154 | ```
155 |
156 | ## Additional Links
157 |
158 | * [Advanced Configuration for Capabilities](https://www.lambdatest.com/support/docs/selenium-automation-capabilities/)
159 | * [How to test locally hosted apps](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/)
160 | * [How to integrate LambdaTest with CI/CD](https://www.lambdatest.com/support/docs/integrations-with-ci-cd-tools/)
161 |
162 | ## Tutorials 📙
163 |
164 | Check out our latest tutorials on JavaScript automation testing 👇
165 |
166 | * [How To Generate Mocha Reports With Mochawesome?](https://www.lambdatest.com/blog/how-to-generate-mocha-reports-with-mochawesome/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample)
167 | * [Mocha JavaScript Tutorial With Examples For Selenium Testing](https://www.lambdatest.com/blog/mocha-javascript-tutorial-with-examples-for-selenium-testing/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample)
168 |
169 | ## Documentation & Resources :books:
170 |
171 | Visit the following links to learn more about LambdaTest's features, setup and tutorials around test automation, mobile app testing, responsive testing, and manual testing.
172 |
173 | * [LambdaTest Documentation](https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample)
174 | * [LambdaTest Blog](https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample)
175 | * [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample)
176 |
177 | ## LambdaTest Community :busts_in_silhouette:
178 |
179 | The [LambdaTest Community](https://community.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample) allows people to interact with tech enthusiasts. Connect, ask questions, and learn from tech-savvy people. Discuss best practises in web development, testing, and DevOps with professionals from across the globe 🌎
180 |
181 | ## What's New At LambdaTest ❓
182 |
183 | To stay updated with the latest features and product add-ons, visit [Changelog](https://changelog.lambdatest.com/)
184 |
185 | ## About LambdaTest
186 |
187 | [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample) is a leading test execution and orchestration platform that is fast, reliable, scalable, and secure. It allows users to run both manual and automated testing of web and mobile apps across 3000+ different browsers, operating systems, and real device combinations. Using LambdaTest, businesses can ensure quicker developer feedback and hence achieve faster go to market. Over 500 enterprises and 1 Million + users across 130+ countries rely on LambdaTest for their testing needs.
188 |
189 | ### Features
190 |
191 | * Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments.
192 | * Real-time cross browser testing on 3000+ environments.
193 | * Test on Real device cloud
194 | * Blazing fast test automation with HyperExecute
195 | * Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale.
196 | * Smart Visual Regression Testing on cloud
197 | * 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more.
198 | * Automated Screenshot testing across multiple browsers in a single click.
199 | * Local testing of web and mobile apps.
200 | * Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems.
201 | * Geolocation testing of web and mobile apps across 53+ countries.
202 | * LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports
203 |
204 | [
](https://accounts.lambdatest.com/register)
205 |
206 | ## We are here to help you :headphones:
207 |
208 | * Got a query? we are available 24x7 to help. [Contact Us](support@lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample)
209 | * For more info, visit - [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=mocha-selenium-sample)
210 |
--------------------------------------------------------------------------------
/conf/parallel.conf.js:
--------------------------------------------------------------------------------
1 | LT_USERNAME = process.env.LT_USERNAME || "";
2 | LT_ACCESS_KEY = process.env.LT_ACCESS_KEY || "";
3 |
4 | var config = {
5 | commanCapabilities: {
6 | build: "Mocha-Selenium-Sample", //Build name
7 | tunnel: false // Make it true to run the localhost through tunnel
8 | },
9 | multiCapabilities: [
10 | {
11 | // Desired capabilities
12 | name: "Your Test Name", // Test name
13 | platform: "Windows 10", // OS name
14 | browserName: "firefox",
15 | version: "latest",
16 | visual: false, // To take step by step screenshot
17 | network: false, // To capture network Logs
18 | console: false // To capture console logs.
19 | },
20 | {
21 | name: "Your Test Name", // Test name
22 | platform: "Windows 10", // OS name
23 | browserName: "chrome",
24 | version: "75.0",
25 | visual: false, // To take step by step screenshot
26 | network: false, // To capture network Logs
27 | console: false // To capture console logs.
28 | }
29 | ]
30 | };
31 |
32 | exports.capabilities = [];
33 | // Code to support common capabilities
34 | config.multiCapabilities.forEach(function(caps) {
35 | var temp_caps = JSON.parse(JSON.stringify(config.commanCapabilities));
36 | for (var i in caps) temp_caps[i] = caps[i];
37 | exports.capabilities.push(temp_caps);
38 | });
39 |
--------------------------------------------------------------------------------
/conf/single.conf.js:
--------------------------------------------------------------------------------
1 |
2 | LT_USERNAME = process.env.LT_USERNAME || "";
3 | LT_ACCESS_KEY = process.env.LT_ACCESS_KEY || "";
4 |
5 | exports.capabilities = {
6 | 'build': 'Mocha-Selenium-Sample', //Build name
7 | 'name': 'Your Test Name', // Test name
8 | 'platform':'Windows 10', // OS name
9 | 'browserName': 'chrome', // Browser name
10 | 'version': 'latest', // Browser version
11 | 'visual': false, // To take step by step screenshot
12 | 'network':false, // To capture network Logs
13 | 'console':false, // To capture console logs.
14 | 'tunnel': false // If you want to run the localhost than change it to true
15 | };
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mocha-selenium-sample",
3 | "version": "0.0.1",
4 | "description": " Selenium examples for Mocha and LambdaTest",
5 | "scripts": {
6 | "test": "npm run single && npm run parallel",
7 | "single": "cross-env CONFIG_FILE=single ./node_modules/.bin/mocha specs/single_test.js conf/single.conf.js",
8 | "parallel": "cross-env CONFIG_FILE=parallel ./node_modules/.bin/mocha specs/parallel_test.js conf/parallel.conf.js --timeout=100000"
9 | },
10 | "keywords": [
11 | "mocha",
12 | "LambdaTest",
13 | "selenium",
14 | "examples"
15 | ],
16 | "dependencies": {
17 | "bluebird": "^3.4.6",
18 | "mocha": "^9.1.3",
19 | "selenium-webdriver": "^3.0.0-beta-3"
20 | },
21 | "devDependencies": {
22 | "cross-env": "^7.0.3"
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/specs/parallel_test.js:
--------------------------------------------------------------------------------
1 | var assert = require("assert"),
2 | webdriver = require("selenium-webdriver"),
3 | conf_file = process.argv[3] || "conf/single.conf.js";
4 |
5 | var capabilities = require("../" + conf_file).capabilities;
6 |
7 | var buildDriver = function(caps) {
8 | return new webdriver.Builder()
9 | .usingServer(
10 | "http://" +
11 | LT_USERNAME +
12 | ":" +
13 | LT_ACCESS_KEY +
14 | "@hub.lambdatest.com/wd/hub"
15 | )
16 | .withCapabilities(caps)
17 | .build();
18 | };
19 |
20 | capabilities.forEach(function(caps) {
21 |
22 | describe("Mocha Todo Test " + caps.browserName, function() {
23 | var driver;
24 | this.timeout(0);
25 |
26 | beforeEach(function(done) {
27 | caps.name = this.currentTest.title;
28 | driver = buildDriver(caps);
29 | done();
30 | });
31 |
32 | it("can find search results" + caps.browserName, function(done) {
33 | driver.get("https://lambdatest.github.io/sample-todo-app/").then(function() {
34 | driver.findElement(webdriver.By.name('li1')).click().then(function(){
35 | console.log("Successfully clicked first list item.");
36 | });
37 |
38 | driver.findElement(webdriver.By.name('li2')).click().then(function(){
39 | console.log("Successfully clicked second list item.");
40 | });
41 |
42 | driver.findElement(webdriver.By.id('sampletodotext')).sendKeys('Complete Lambdatest Tutorial\n').then(function(){
43 | driver.findElement(webdriver.By.id('addbutton')).click().then(function(){
44 | console.log("Successfully added a new task.");
45 | })
46 | });
47 | });
48 | });
49 |
50 | afterEach(function(done) {
51 | if (this.currentTest.isPassed()) {
52 | driver.executeScript("lambda-status=passed");
53 | } else {
54 | driver.executeScript("lambda-status=failed");
55 | }
56 | driver.quit().then(function() {
57 | done();
58 | });
59 | });
60 | });
61 | });
62 |
--------------------------------------------------------------------------------
/specs/single_test.js:
--------------------------------------------------------------------------------
1 | var assert = require("assert"),
2 | webdriver = require("selenium-webdriver"),
3 | conf_file = process.argv[3] || "conf/single.conf.js";
4 |
5 | var caps = require("../" + conf_file).capabilities;
6 |
7 | var buildDriver = function(caps) {
8 | return new webdriver.Builder()
9 | .usingServer(
10 | "http://" +
11 | LT_USERNAME +
12 | ":" +
13 | LT_ACCESS_KEY +
14 | "@hub.lambdatest.com/wd/hub"
15 | )
16 | .withCapabilities(caps)
17 | .build();
18 | };
19 |
20 | describe("Mocha Todo Test " + caps.browserName, function() {
21 | var driver;
22 | this.timeout(0);
23 |
24 | beforeEach(function(done) {
25 | caps.name = this.currentTest.title;
26 | driver = buildDriver(caps);
27 | done();
28 | });
29 |
30 | it("can find search results", function(done) {
31 | driver.get("https://lambdatest.github.io/sample-todo-app/").then(function() {
32 | driver.findElement(webdriver.By.name('li1')).click().then(function(){
33 | console.log("Successfully clicked first list item.");
34 | });
35 |
36 | driver.findElement(webdriver.By.name('li2')).click().then(function(){
37 | console.log("Successfully clicked second list item.");
38 | });
39 |
40 | driver.findElement(webdriver.By.id('sampletodotext')).sendKeys('Complete Lambdatest Tutorial\n').then(function(){
41 | driver.findElement(webdriver.By.id('addbutton')).click().then(function(){
42 | console.log("Successfully added a new task.");
43 | })
44 | });
45 | });
46 | });
47 |
48 | afterEach(function(done) {
49 | if (this.currentTest.isPassed()) {
50 | driver.executeScript("lambda-status=passed");
51 | } else {
52 | driver.executeScript("lambda-status=failed");
53 | }
54 | driver.quit().then(function() {
55 | done();
56 | });
57 | });
58 | });
59 |
--------------------------------------------------------------------------------
/target/npmlist.json:
--------------------------------------------------------------------------------
1 | {"version":"0.0.1","name":"mocha-selenium-sample","dependencies":{"bluebird":{"version":"3.7.2"},"mocha":{"version":"9.1.3"},"selenium-webdriver":{"version":"3.6.0"}}}
--------------------------------------------------------------------------------