This repository contains basic protractor tests that are written using typescript
Pre requisites :
- install node js
- open command prompt and perform npm install -g webdriver-manager webdriver-manager start ( This will start selenium server in your local machine )
clone this repository and perform npm install
- Install Node JS from https://nodejs.org/en/ ( LTS version )
- Add environment variable for npm ( Ex : C:\Users\cadasa\AppData\Roaming\npm )
- Follow this URL to Install and run first test using protractor -- http://www.protractortest.org/#/tutorial
-
Install visual studio code from https://code.visualstudio.com/
-
Install typescript globally using command : npm install typescript –g
-
Create a project Folder ( Ex : Protractor_Demo )
-
Creation of tsconfig.json a. CMD into Protractor_Demo folder and type tsc –init
-
Creation of package.json a. CMD into Protractor_Demo folder and type npm init –f
-
Creation of task.json a. CMD into Protractor_Demo folder and type code . This will open visual studio code b. Press F1 – This will open tasks list c. Select Configure Task Runner d. Replace “tasks” with below mentioned code "tasks": [ { "taskName": "TypeScript Compiler", "command": "tsc -P tsconfig.json", "type": "shell", "args": [ "-w" ] } ]
-
Enabling Debugging a. CMD into Protractor_Demo folder and type code . This will open visual studio code b. Press F1 – This will open tasks list c. Type and Select Open launch.json d. Replace “configurations” with below mentioned code "configurations": [ { "type": "node", "request": "launch", "name": "Launch Program", "program": "${workspaceRoot}\node_modules\protractor\bin\protractor", "outFiles": [ "${workspaceRoot}/out/**/*.js" ], "args": [ "conf.js" ] } ]
e. To debug your application use keyword F5
-
Add dependencies for the project a. CMD into Protractor_Demo folder b. install protractor as a dependency using command : npm i --D protractor c. install types as a dependency using command : npm i –D @types/jasmine
-
Create protractor.conf.ts file and add below mentioned code to it import { Config, browser } from "protractor"; export let config:Config = {
//seleniumAddress : 'http://localhost:4444/wd/hub', directConnect : true, framework :"jasmine2", specs : ['protractor.spec.js'], jasmineNodeOpts : { }, }
-
Create protractor.spec.ts file and add below mentioned code to it import { by,element,browser } from "protractor"; describe("Test Angular Site", ()=>{ beforeAll(()=>{
browser.get('https://angularjs.org/'); }); it("Test Text Field", ()=>{ expect<any>(browser.getTitle()).toBe('AngularJS — Superheroic JavaScript MVW Framework'); }); });
-
Press F1 key type Run task hit enter select option “TypeScript Compiler” – This task will compile your type script files into js files and wait for changes in ts files
-
CMD into Protractor_Demo folder and type Protractor protractor.conf.js
- Add dependencies for the project a. CMD into Protractor_Demo folder b. install Winston as a dependency using command : npm i –D winston c. install types as a dependency using command : npm i –D @types/winston
- Import Winston into your spec files ( Ex : import { log } from "winston"; )
- Wherever you need, just add a log ( Ex : log("info",'Successfully verified title'); )
- Add dependencies for the project a. CMD into Protractor_Demo folder b. install Jasmine spec reporter as a dependency using command : npm i –D jasmine-spec-reporter
- Import Jasmine-spec-reporter into your conf file ( Ex : import { SpecReporter } from "jasmine-spec-reporter"; )
- To display stack trace place below mentioned code onPrepare: function () { jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
- Configuring Maven a. To use Allure reports, we need a maven plugin b. 1. Download “ apache-maven-3.5.0-bin.zip “ maven from https://maven.apache.org/download.cgi site 2. Add environment variable M2_HOME with the value as path to apache-maven-3.5.0 folder. 3. Add environment path variable like %M2_HOME%\bin c. Open CMD and type mvn –verision If you get version details, then maven configured successfully
- Add dependencies to the project a. CMD into Protractor_Demo folder b. install allure-reporter as a dependency using command : npm i –D jasmine-allure-reporter
- To Get Allure Reporter XML Files : a. At protractor.conf.ts file import AllureReporter ( Ex : let AllureReporter = require('jasmine-allure-reporter'); ) b. Inside OnPrepare function add below mentioned code : jasmine.getEnv().addReporter(new AllureReporter({ resultsDir: 'allure-results' })); c. Run the Protractor protractor.conf.js –> Test results will be saved as xml files inside Protractor_Demo/allure-results folder
- To Get Screen shots : a. At protractor.conf.ts file, Inside OnPrepare function add below mentioned code : jasmine.getEnv().afterEach(function(done){ browser.takeScreenshot().then(function (png) { allure.createAttachment('Screenshot', function () { return new Buffer(png, 'base64') }, 'image/png')(); done(); }) }); b. Run the Protractor protractor.conf.js –> After completing the run, you can see both images and xml files
- To Generate Allure Reports : a. Go to Protractor-Demo\node_modules\jasmine-allure-reporter folder and copy pom.xml file b. CMD into Protractor_Demo folder c. Run command “mvn site -Dallure.results_pattern=allure-results” It creates target folder which contains allure report d. Open “ Protractor-Demo\target\site\allure-maven-plugin\index.html “ in firefox browser to see allure report e. To share reports with other, we need to enable Jetty server and share through it. To enable, run command mvn jetty:run - Djetty.port=8888
-
Install docker stable version from https://docs.docker.com/docker-for-windows/install/
-
After installation, you need to logout and login – After login your system will reboot to enable Hyper-V mode
-
After Reboot – Open Powershell any type docker info -- You should get something like below
-
Get selenium hub container using command docker pull selenium/hub
-
Get Selenium chrome node debug container using command docker pull selenium/node-chrome-debug
-
Get selenium firefox node debug container using command docker pull selenium/node-firefox-debug
-
Start the selenium hub using command docker run -d -P --name selenium-hub selenium/hub
-
Link the chrome node container using command docker run -d -P --link selenium-hub:hub selenium/node-chrome-debug
-
Link the firefox node container using command docker run -d -P --link selenium-hub:hub selenium/node-firefox-debug
-
Docker Compose : a. Create docker-compose.yml file and add below mentioned code version: '2'
services: Selgrid: image: selenium/hub:latest container_name: Selgrid privileged: true ports: - 4444:4444 environment: - GRID_TIMEOUT=240000 - GRID_BROWSER_TIMEOUT=240000 networks: - w2agrid_grid_internal nodechrome: image: selenium/node-chrome-debug:latest privileged: true depends_on: - w2agrid ports: - 5900 environment: - no_proxy=localhost - HUB_PORT_4444_TCP_ADDR=w2agrid - HUB_PORT_4444_TCP_PORT=4444 networks: - w2agrid_grid_internal nodefirefox: image: selenium/node-firefox-debug:latest privileged: true depends_on: - w2agrid ports: - 5901 environment: - no_proxy=localhost - HUB_PORT_4444_TCP_ADDR=w2agrid - HUB_PORT_4444_TCP_PORT=4444 networks: - w2agrid_grid_internal networks: w2agrid_grid_internal:
b. Run compose file using command docker-compose up –d
- In conf.js fie, enable selenium address. Since selenium port is configured as 4444, no changes required
- If you want to run tests on multiple browsers (ON Both the nodes) we should add below mentioned code to conf.js file multiCapabilities: [{ 'browserName': 'firefox' }, { 'browserName': 'chrome' }]
- To See all the running containers using command docker ps -aq
- To remove all the containers use command : docker rm $(docker ps -a -q)
- To remove all the images use command : docker rmi $(docker images -q)
-
Install Nodemailer using command npm i -g nodemailer
-
Create mail.ts file
const nodemailer = require('nodemailer'); // create reusable transporter object using the default SMTP transport let transporter = nodemailer.createTransport({ host: 'smtp.gmail.com', port: 465, secure: true, // secure:true for port 465, secure:false for port 587 auth: { user: '[email protected]', pass: 'Planon@521' } }); // setup email data with unicode symbols let mailOptions = { from: '[email protected]', // sender address to: '[email protected]', // list of receivers subject: 'Protractor Demo Results at http://localhost:8888', // Subject line text: 'Dear StakeHolders, Please see attachment for todays result', // plain text body html: '<b>Hello world ?</b>', // html body attachments: [{ 'filename' : 'Results.html', 'filePath' : './allure-results/Results.xml' }] }; // send mail with defined transport object transporter.sendMail(mailOptions, (error, info) => { if (error) { return console.log(error); } console.log('Message %s sent: %s', info.messageId, info.response); });
-
Create OnComplete method at conf file
onComplete : function () { let stdout:ChildProcess.stdout; let stderr:ChildProcess.stderr; let error: ChildProcess.error; console.log ('Sending test result as email'); puts(error, stdout,stderr); exec('node ./util/mail.js', puts); }
-
Create a dataprovide.ts file and create below mentioned code
export class DataProvider { plusProvider() { return [ {a: 2, b: 3, expected: '5'}, {a: '14', b: 15, expected: '29'}, {a: 12, b: '13', expected: '25'}, {a: '22', b: '13', expected: '35'}, ]; } }
-
In your test import using block ( Ex : var using = require('jasmine-data-provider'); )
-
Create using block and send data to the app
let dataprovider = new DataProvider(); let plusProvider = dataprovider.plusProvider(); let calculatormainpage = new SupercalculatorMainPage(); let elementhelper = new ElementHelper(); using(plusProvider,(data)=>{ it("Supercalculator Addition", ()=>{ calculatormainpage.addition(data.a,data.b) browser.sleep(6000); expect<any>(elementhelper.resultText().getText()).toEqual(data.expected); }); });
-
Install xlsx using command: npm i -D xlsx
-
Import the xlsx into your spec files (Ex : import { readFile, read, ColInfo, RowInfo } from "xlsx"; )
-
Create test.xlsx file and rename first sheet as "Test_Data" and add data into the cells
-
Send data to the app using for loop. Ex : let workbook = readFile('./data_provider/test.xlsx'); let sheetname = 'Test_Data'; let target = workbook.Sheets[sheetname]; for ( var i=2; i<14; i++){ let finalresult = String(target["C"+i].v); calculatormainpage.addition(target["A"+i].v, target["B"+i].v) expect(elementhelper.resultText().getText()).toEqual(finalresult); }
-
Get the Jenkins war file for windows from https://jenkins.io/download/
-
Start the Jenkins using command : java -jar jenkins.war --> It will start the Jenkins server at localhost:8080
-
Open chrome browser and go to localhost:8080 -- Jenkins main page will appear
-
Click on New Item and provide name of your project and select "Multiconfiguratio project"
-
Under Build, select "Execute Windows batch command" and provide batch commands to execute your project
Ex :
-----To Goto project folder and to start run --------
cd\
D:
cd D:\GitReporsitories\Protractor-Demo
rmdir /s /q D:\GitReporsitories\Protractor-Demo\allure-results
protractor conf\conf.js
------- To copy allure reports to jasmine allure reporter location ------------
cd\
D:
cd D:\GitReporsitories\Protractor-Demo\node_modules\jasmine-allure-reporter
rmdir /s /q D:\GitReporsitories\Protractor-Demo\node_modules\jasmine-allure-reporter\allure-results
xcopy D:\GitReporsitories\Protractor-Demo\allure-results D:\GitReporsitories\Protractor-Demo\node_modules\jasmine-allure- reporter\allure-results /i /d
mvn site -Dallure.results_pattern=allure-results
---------- To start Jetty server to publish results --------------
cd\
D:
cd D:\GitReporsitories\Protractor-Demo\node_modules\jasmine-allure-reporter
mvn jetty:run -Djetty.port=8888