Skip to content

Commit bffc06b

Browse files
committed
Code CleanUp and Readme Fix
1 parent 686a0cd commit bffc06b

6 files changed

Lines changed: 103 additions & 73 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules
2+
./package-lock.json

README.md

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,20 @@ Now you can add the `GRID HOST` which is used to connect your current test suite
8585

8686
```js
8787
// index.js
88-
const GRID_HOST = 'hub.lambdatest.com/wd/hub';
89-
const gridUrl = 'https://' + USERNAME + ':' + KEY + '@' + GRID_HOST;
90-
const driver = new webdriver.Builder().usingServer(gridUrl).withCapabilities(capabilities).build();
88+
// username: Username can be found at automation dashboard
89+
const username = process.env.LT_USERNAME;
90+
91+
// AccessKey: AccessKey can be generated from automation dashboard or profile section
92+
const accessKey = process.env.LT_ACCESS_KEY;
93+
94+
const gridUrl = 'https://' + username + ':' + accessKey + '@hub.lambdatest.com/wd/hub';
95+
96+
// Setup and build selenium driver object
97+
const driver = new webdriver.Builder()
98+
.usingServer(gridUrl)
99+
.withCapabilities(capabilities)
100+
.build();
101+
91102
```
92103
The above grid connect will create the `Webdriver` for the test suite to execute the `Selenium` commands for your test.
93104

@@ -99,18 +110,24 @@ In the test script, you need to update your test capabilities. In this code, we
99110

100111
```js
101112
// index.js
102-
const capabilities = {
103-
build: 'NodeJS build', // Name of the build
104-
name: 'Test 1', // Name of the test
105-
platform: 'windows 10', // Name of Operating System
106-
browserName: 'chrome', // Name of the browser
107-
version: '67.0', // Version of the browser
108-
resolution: '1280x800', // Resolution of the screen
109-
network: true, // Enable to capture browser network logs
110-
visual: true, // Enable to capture screenshot on every command
111-
console: true, // Enable to capture the console log
112-
video: true // Enable to capture the video recording of the test
113-
}
113+
// Setup capabilities, Know more about LamdbdaTest Capabilities: https://www.lambdatest.com/capabilities-generator/
114+
const capabilities = {
115+
"browserName": "Chrome",
116+
// "browserVersion": "latest", #Uncomment to Specify Browser Version
117+
"LT:Options": {
118+
name: 'NodeJS Get Set Go', // name of the test
119+
build: 'NodeJS Loves LambdaTest', // name of the build
120+
"project": "Build-With-LambdaTtest",
121+
"w3c": true,
122+
"plugin": "NodeJS",
123+
"customData": {
124+
"buildNumber": "1234",
125+
"environment": "Staging",
126+
"apiVersion": "v1.2.3",
127+
"releaseTag": "v1.2.3-rc1"
128+
},
129+
130+
}
114131
```
115132
> **Note:** 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=nodejs-selenium-sample)**.
116133
@@ -119,25 +136,28 @@ const capabilities = {
119136
Now you write your selenium test cases in your `index.js` file:
120137
121138
```js
122-
function searchTextOnGoogle() {
123-
// navigate to a url, search for a text and get title of page
124-
driver.get('https://www.google.com/ncr').then(function () {
125-
driver.findElement(webdriver.By.name('q')).sendKeys('LambdaTest\n').then(function () {
126-
driver.getTitle().then(function (title) {
127-
setTimeout(function () {
128-
console.log(title);
129-
driver.executeScript('lambda-status=passed');
130-
driver.quit();
131-
}, 5000);
132-
});
133-
});
134-
}).catch(function (err) {
135-
console.log("test failed with reason " + err)
136-
driver.executeScript('lambda-status=failed');
137-
driver.quit();
138-
});
139+
async function todoTest() {
140+
try {
141+
// Navigate to a URL, click on the first and second list items and add a new one in the list.
142+
await driver.get('https://lambdatest.github.io/sample-todo-app/');
143+
await driver.findElement(webdriver.By.name('li1')).click();
144+
console.log("Successfully clicked first list item.");
145+
await driver.findElement(webdriver.By.name('li2')).click();
146+
console.log("Successfully clicked second list item.");
147+
148+
await driver.findElement(webdriver.By.id('sampletodotext')).sendKeys('Complete Lambdatest Tutorial\n');
149+
await driver.findElement(webdriver.By.id('addbutton')).click();
150+
console.log("Successfully added a new task.");
151+
await driver.executeScript('lambda-status=passed');
152+
} catch (err) {
153+
console.log("test failed with reason " + err);
154+
await driver.executeScript('lambda-status=failed');
155+
} finally {
156+
await driver.quit();
157+
}
139158
}
140-
searchTextOnGoogle();
159+
160+
todoTest();
141161
```
142162
143163
## Executing the Test

index.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,43 @@
33
Configuration
44
----------
55
username: Username can be found at automation dashboard
6-
accessToken: AccessToken can be generated from automation dashboard or profile section
6+
accessKey: AccessKey can be generated from automation dashboard or profile section
77
88
Result
99
-------
10-
Execute NodeJS Automation Tests on LambdaTest Distributed Selenium Grid
10+
Execute NodeJS Automation Tests on LambdaTest Cloud Grid
1111
*/
1212

1313
const webdriver = require('selenium-webdriver');
1414

1515
// username: Username can be found at automation dashboard
16-
const USERNAME = process.env.LT_USERNAME;
16+
const username = process.env.LT_USERNAME;
1717

1818
// AccessKey: AccessKey can be generated from automation dashboard or profile section
19-
const KEY = process.env.LT_ACCESS_KEY;
20-
21-
// gridUrl: gridUrl can be found at automation dashboard
22-
const GRID_HOST = 'hub.lambdatest.com/wd/hub';
23-
24-
async function searchTextOnGoogle() {
25-
// Setup Input capabilities
19+
const accessKey = process.env.LT_ACCESS_KEY;
2620

21+
async function todoTest() {
22+
// Setup Input capabilities, Know more about LamdbdaTest Capabilities: https://www.lambdatest.com/capabilities-generator/
2723
const capabilities = {
2824
"browserName": "Chrome",
29-
"browserVersion": "latest",
25+
// "browserVersion": "latest", #Uncomment to Specify Browser Version
3026
"LT:Options": {
31-
// "platformName": "Windows 10",
32-
name: 'Test 1', // name of the test
33-
build: 'NodeJS build', // name of the build
34-
"project": "Untitled",
27+
name: 'NodeJS Get Set Go', // name of the test
28+
build: 'NodeJS Loves LambdaTest', // name of the build
29+
"project": "Build-With-LambdaTtest",
3530
"w3c": true,
36-
"plugin": "node_js-node_js"
31+
"plugin": "NodeJS",
32+
"customData": {
33+
"buildNumber": "1234",
34+
"environment": "Staging",
35+
"apiVersion": "v1.2.3",
36+
"releaseTag": "v1.2.3-rc1"
37+
},
38+
3739
}
38-
};
40+
}
3941

40-
// URL: https://{username}:{accessToken}@beta-hub.lambdatest.com/wd/hub
41-
const gridUrl = 'https://' + USERNAME + ':' + KEY + '@' + GRID_HOST;
42+
const gridUrl = 'https://' + username + ':' + accessKey + '@hub.lambdatest.com/wd/hub';
4243

4344
// Setup and build selenium driver object
4445
const driver = new webdriver.Builder()
@@ -57,6 +58,7 @@ async function searchTextOnGoogle() {
5758
await driver.findElement(webdriver.By.id('sampletodotext')).sendKeys('Complete Lambdatest Tutorial\n');
5859
await driver.findElement(webdriver.By.id('addbutton')).click();
5960
console.log("Successfully added a new task.");
61+
await driver.executeScript('lambda-status=passed');
6062
} catch (err) {
6163
console.log("test failed with reason " + err);
6264
await driver.executeScript('lambda-status=failed');
@@ -65,4 +67,4 @@ async function searchTextOnGoogle() {
6567
}
6668
}
6769

68-
searchTextOnGoogle();
70+
todoTest();

package-lock.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodejs-selenium-sample",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "![LambdaTest Logo](https://www.lambdatest.com/images/logo.svg)",
55
"main": "index.js",
66
"scripts": {

test.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@ const USERNAME = process.env.LT_USERNAME;
66
const KEY = process.env.LT_ACCESS_KEY;
77
const GRID_HOST = 'hub.lambdatest.com/wd/hub';
88

9-
async function searchTextOnGoogle() {
10-
// Setup Input capabilities
9+
async function pdfWebTest() {
10+
// Setup Input capabilities, Know more about LamdbdaTest Capabilities: https://www.lambdatest.com/capabilities-generator/
1111
const capabilities = {
12-
"browserName": "Safari",
13-
// "browserVersion": "latest",
12+
"browserName": "Chrome",
13+
// "browserVersion": "latest", #Uncomment to Specify Browser Version
1414
"LT:Options": {
15-
// "platformName": "Windows 10",
16-
name: 'Test 1', // name of the test
17-
build: 'NodeJS build', // name of the build
18-
"project": "Untitled",
19-
"w3c": true,
20-
"plugin": "node_js-node_js"
15+
name: 'NodeJS Get Set Go', // name of the test
16+
build: 'NodeJS Loves LambdaTest', // name of the build
17+
"project": "Build-With-LambdaTtest",
18+
"w3c": true,
19+
"plugin": "NodeJS",
20+
"customData":
21+
{
22+
_id: "5f46aaa69adf77cfe2bb4fd6",
23+
index: "0",
24+
guid: "9451b204-12f0-4177-8fe9-fb019b3e4bf3",
25+
isActive: "False",
26+
picture: "http://placehold.it/32x32",
27+
},
28+
2129
}
22-
};
30+
};
2331

24-
25-
// URL: https://{username}:{accessToken}@beta-hub.lambdatest.com/wd/hub
2632
const gridUrl = 'https://' + USERNAME + ':' + KEY + '@' + GRID_HOST;
2733

2834
// Setup and build selenium driver object
@@ -42,7 +48,7 @@ async function searchTextOnGoogle() {
4248
await elements[0].click();
4349
console.log("Successfully clicked first list item.");
4450
}
45-
51+
driver.executeScript('lambda-status=passed');
4652

4753
// Close the browser
4854
await driver.quit();
@@ -53,4 +59,4 @@ async function searchTextOnGoogle() {
5359
}
5460
}
5561

56-
searchTextOnGoogle();
62+
pdfWebTest();

0 commit comments

Comments
 (0)