Skip to content

Commit a788890

Browse files
authored
Merge pull request #2 from 4msha/updatedNewRepo
new updated format
2 parents 7a96f0b + 417aaf0 commit a788890

File tree

4 files changed

+524
-113
lines changed

4 files changed

+524
-113
lines changed

README.md

+50-110
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,31 @@
11
# NodeJS Test Tutorial
22

3-
![LambdaTest Logo](https://www.lambdatest.com/images/logo.svg)
4-
53
![NodeJS Framework](https://cdn.lambdatest.com/support/docs/wp-content/uploads/2019/03/run-nodejs-tests-on-selenium-grid-cloud.jpg)
4+
Node.js is an efficient, light weight and cross platform runtime environment for executing JavaScript code. npm(node package manager) is the largest ecosystem of open source libraries. LambdaTest enables node.js scripts to run on the Selenium automation grid. This tutorial will help you run Nodejs automation scripts over LambdaTest Selenium Grid.
65

7-
Node.js is an efficient, light weight and cross platform runtime environment for executing JavaScript code. npm(node package manager) is the largest ecosystem of open source libraries. LambdaTest enables node.js scripts to run on the Selenium automation grid.
8-
9-
This tutorial will help you run Nodejs automation scripts over LambdaTest Selenium Grid.
10-
11-
## Prerequisites for Node.js Tutorial
6+
## Prerequisites for Node.js Tutorial
127

13-
1. [Download Visual Studio](https://visualstudio.microsoft.com/downloads/) (IDE) for your operating system.
14-
2. **Node.js and Package Manager (npm)** : Install Node.js from their [official website](https://nodejs.org/en/download/) Or Install Node.js using command line. Go to the terminal or command prompt & run the below command.
8+
1. Install npm.
159

1610
```
17-
$ install node
11+
sudo apt install npm
1812
```
1913

20-
To verify the node version
14+
2. Install NodeJS.
2115

2216
```
23-
$ node -v
17+
sudo apt install nodejs
2418
```
2519

26-
If node isn’t of the latest version then you can update it using the below command.
27-
28-
```
29-
$ npm install npm@latest -g
30-
```
20+
## Steps to Run your First Test
3121

32-
3. Install **Selenium Dependencies**
22+
Step 1. Clone the NodeJs Selenium Repository.
3323

3424
```
35-
npm install selenium-webdriver
25+
git clone https://github.com/4msha/nodejs-selenium-sample.git
3626
```
3727

38-
4. **LambdaTest Authentication Credentials**: Make sure you have your LambdaTest credentials with you to run test automation scripts with Jest on LambdaTest Selenium Grid. You can obtain these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/) or through [LambdaTest Profile](https://accounts.lambdatest.com/detail/profile).
39-
40-
Set LambdaTest Username and Access Key in environment variables.
28+
Step 2. Export the Lambda-test Credentials. You can get these from your automation dashboard.
4129

4230
<p align="center">
4331
<b>For Linux/macOS:</b>:
@@ -46,7 +34,6 @@ Set LambdaTest Username and Access Key in environment variables.
4634
export LT_USERNAME="YOUR_USERNAME"
4735
export LT_ACCESS_KEY="YOUR ACCESS KEY"
4836
```
49-
5037
<p align="center">
5138
<b>For Windows:</b>
5239

@@ -55,60 +42,46 @@ set LT_USERNAME="YOUR_USERNAME"
5542
set LT_ACCESS_KEY="YOUR ACCESS KEY"
5643
```
5744

58-
## Setting Up The Project In Visual Studio IDE
59-
60-
* **Step 1** : After installation of the Visual Studio IDE, create a folder in your local system to save all the projects.
61-
62-
* **Step 2** : Install the below extensions for JavaScript from ‘Extensions’ in VScode Editor.
63-
64-
* Code Runner
65-
* JavaScript( ES6) code snippet
66-
* ES Lint
45+
Step 3. Inside nodejs.selenium repository install necessary packages.
6746

68-
* **Step 3** : Press ‘Ctrl+Shift+P’ and search for git:clone. Paste the URL of this repository([https://github.com/LambdaTest/nodejs-selenium-sample](https://github.com/LambdaTest/nodejs-selenium-sample)) to clone.
69-
70-
* **Step 4** : Press ENTER and save the TestCafe project in the above created folder.
71-
72-
* **Step 5**: Create a project directory named *nodejs-selenium-sample*.
73-
74-
**Step 6**: Create a file named **index.js** inside the project **nodejs-selenium-sample**. This index.js will be used to specify the nodejs test script that will be executed over the LambdTest Selenium Grid on cloud.
75-
76-
## Executing First node.js Testing Script
77-
78-
### Test Scenario
79-
80-
The test script will do the following actions:
81-
82-
1. Invoke the browser launch.
83-
2. Go to [www.google.com](http://www.google.com).
84-
3. Type LambdaTest in the search box.
85-
4. Fetch the title of the web page.
86-
5. Close the browser and display the fetched title in the console.
47+
```
48+
cd nodejs-selenium-sample
49+
npm i
50+
```
8751

88-
That’s it. Before we deep dive into the test script, we need to declare our desired capabilities. These desired capabilities will help us define the testing environment such as browser version, operating system, and more. You can leverage [LambdaTest Desired Capabilities Generator](https://www.lambdatest.com/capabilities-generator/) to specify the desired capabilities class.
52+
Step 4. To run your First Test.
8953

90-
### [LambdaTest Desired Capabilities Generator](https://www.lambdatest.com/capabilities-generator/)
54+
```
55+
npm test
56+
or node index.js
57+
```
9158

92-
Let us fetch the desired capabilities class from the LambdaTest Desired Capabilities Generator to run the script on LambdaTest cloud-based Selenium Grid.
59+
## See the Results
9360

94-
![LambdaTest Desired Capabilities](https://github.com/LambdaTest/nodejs-selenium-sample/blob/master/tutorial-images/lambdaTest%20desired%20capabilities%20generator.png)
61+
You can check your test results on the [Automation Dashboard](https://automation.lambdatest.com/build).
62+
![Automation Testing Logs](https://github.com/LambdaTest/nodejs-selenium-sample/blob/master/tutorial-images/automation%20testing%20logs.PNG)
9563

96-
With the capability generator, you can specify a variety of configurations in multiple programming languages.
64+
## Understanding the Test.
9765

98-
With that said, let us look into the test script **index.js**.
66+
1. Importing Selenium Wedriver,setting up username, access-key, and grid-host.
9967

10068
```
10169
const webdriver = require('selenium-webdriver');
102-
70+
10371
// USERNAME & KEY can be found at automation dashboard
10472
const USERNAME = 'shwetas';
10573
const KEY = 'xhfQswR5454';
106-
74+
10775
// gridUrl: gridUrl can be found at automation dashboard
10876
const GRID_HOST = 'hub.lambdatest.com/wd/hub';
109-
77+
78+
```
79+
80+
2. Define the Test function and capabilities.
81+
82+
```
11083
function searchTextOnGoogle() {
111-
84+
11285
// Setup Input capabilities
11386
const capabilities = {
11487
platform: 'windows 10',
@@ -123,17 +96,28 @@ function searchTextOnGoogle() {
12396
name: 'Test 1', // name of the test
12497
build: 'NodeJS build' // name of the build
12598
}
126-
99+
127100
// URL: https://{username}:{accessToken}@beta-hub.lambdatest.com/wd/hub
101+
}
102+
searchTextOnGoogle()
103+
```
104+
105+
3. Next, we create the grid url and create the selenium driver object.
106+
107+
```
128108
const gridUrl = 'https://' + USERNAME + ':' + KEY + '@' + GRID_HOST;
129-
130-
// setup and build selenium driver object
109+
110+
// setup and build selenium driver object
131111
const driver = new webdriver.Builder()
132112
.usingServer(gridUrl)
133113
.withCapabilities(capabilities)
134114
.build();
135-
136-
// navigate to a url, search for a text and get title of page
115+
```
116+
117+
4. At last, we try to search LambdaTest on google.
118+
119+
```
120+
// navigate to a url, search for a text and get title of page
137121
driver.get('https://www.google.com/ncr').then(function() {
138122
driver.findElement(webdriver.By.name('q')).sendKeys('LambdaTest\n').then(function() {
139123
driver.getTitle().then(function(title) {
@@ -149,51 +133,7 @@ function searchTextOnGoogle() {
149133
driver.executeScript('lambda-status=failed');
150134
driver.quit();
151135
});
152-
}
153-
searchTextOnGoogle()
154-
```
155-
156-
## Running The Test
157-
158-
Open the command line in the same directory where the GitHub repository for nodejs tutorial is cloned & run the below command.
159-
160-
```
161-
node index.js
162-
```
163-
164-
Once the test case is executed, you can assess its status over the LambdaTest Automation Dashboard. You can figure out whether the test has passed or failed. You can also review various kinds of test logs such as network logs, command logs, raw Selenium logs, and even video recording of the entire test execution.
165-
166-
![Automation Testing Logs](https://github.com/LambdaTest/nodejs-selenium-sample/blob/master/tutorial-images/automation%20testing%20logs.PNG)
167-
168-
If you notice your console output in the command prompt or terminal. You will find the below output.
169-
170-
![Command Prompt](https://github.com/LambdaTest/nodejs-selenium-sample/blob/master/tutorial-images/cmd.PNG)
171-
172-
### Test Your Locally Hosted Web Applications
173-
174-
You can also perform cross browser testing of your web application which is locally hosted using Lambda Tunnel. Lambda Tunnel establishes a secure shell connection between your localhost and our cloud servers to help you execute Selenium test automation for privately hosted projects. All you need to do is set the tunnel variable as true in your desired capabilities for running your Selenium script with Lambda Tunnel.
175-
176-
* Set **tunnel = true**
177-
* Set **truetunnelName = 'Identifier name'** (recommended in case of more than 1 tunnels are connected)
178-
179-
For example, to run the above test script through your localhost, your desired capabilities will be:
180-
181136
```
182-
const capabilities = {
183-
platform: 'windows 10',
184-
browserName: 'firefox',
185-
version: 'latest',
186-
resolution: '1280x800',
187-
network: false,
188-
visual: false,
189-
console: false,
190-
video: true,
191-
tunnel: true, // flag to run Selenium script on Lambda Tunnel
192-
name: 'Test 1',
193-
build: 'NodeJS build'
194-
```
195-
196-
It is important to note that you would need to download the Lambda Tunnel binary file for your operating system. Refer to our support documentation for more information on [Lambda Tunnel](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/).
197137

198138
### Want To Run Lambda Tunnel Without Using Command Line?
199139

index.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ const webdriver = require('selenium-webdriver');
2121
*/
2222

2323
// username: Username can be found at automation dashboard
24-
const USERNAME = '{username}';
24+
const USERNAME = process.env.LT_USERNAME;
2525

2626
// AccessKey: AccessKey can be generated from automation dashboard or profile section
27-
const KEY = '{accessKey}';
27+
const KEY = process.env.LT_ACCESS_KEY;
2828

2929
// gridUrl: gridUrl can be found at automation dashboard
3030
const GRID_HOST = 'hub.lambdatest.com/wd/hub';
3131

32+
3233
function searchTextOnGoogle() {
3334

3435
// Setup Input capabilities
@@ -37,6 +38,7 @@ function searchTextOnGoogle() {
3738
browserName: 'chrome',
3839
version: '67.0',
3940
resolution: '1280x800',
41+
geoLocation : "US",
4042
network: true,
4143
visual: true,
4244
console: true,
@@ -63,10 +65,18 @@ function searchTextOnGoogle() {
6365
driver.executeScript('lambda-status=passed');
6466
driver.quit();
6567
}, 5000);
68+
}).catch(function(err){
69+
console.log("Cannot find title."+err)
70+
driver.executeScript('lambda-status=failed');
71+
driver.quit();
6672
});
73+
}).catch(function(err){
74+
console.log("Cannot find element."+err)
75+
driver.executeScript('lambda-status=failed');
76+
driver.quit();
6777
});
6878
}).catch(function(err){
69-
console.log("test failed with reason "+err)
79+
console.log("Test failed with reason "+err)
7080
driver.executeScript('lambda-status=failed');
7181
driver.quit();
7282
});

0 commit comments

Comments
 (0)