You do not need selenium knowledge to learn protractor and Jasmine
Protractor is a test framework for Angular JS applications which uses Selenium to automate the browser behaviour (Protractor uses JS but still communicates with Selenium using WebDriverJS) .
It’s true you could still use Selenium Web Server API as an alternative to Protractor (with a language of your choice that’s supported) to talk to the Selenium Server, but you will have overheads to handle asynchronous process
This might not be the best choice for non-angular applications because you may prefer more specific synchronization approaches and the API might not be as flexible as you need.
Protractor allows tests to be organized based on Jasmine, thus allowing you to write both unit and functional tests on Jasmine.
Protractor is open source framework specifically for automation of AngularJS applications although it can be used to automate non-angular web based applications. So in case your application is designed using AngularJS for the front end, so it would be recommended to use Protractor
1. Install VSCode, VSCode is nothing but another version of Visual Studio but available for free.
2. Navigate to https://code.visualstudio.com/
3. Click download button
4. Download the VSCode software according to your operating system.
5. Install the VScode like any other exe file, go along with the recommended setting only.
6. Launch the VSCode, and UI will look like below.
We would be using end language as Javascript, so we must install NodeJs for using supporting functions and supporting packages.
1. Navigate to : https://nodejs.org/en/download/
2. Download the package according to your operating system.
3. Install the downloaded NodeJs file.
4. Once installation is complete, open cmd and type
In Java selenium, we would be using Selenium standalone jar file for executing the selenium, sometimes we may also use selenium-java jar fil.
In case of Protractor we would be installing a npm package called protractor and we would be using selenium standalone server jar as server for executing the protractor.
1. You can install all the packages from the VS code itself.
2. Open VSCode and click View -> Integrated Terminal
3. Navigate to the folder on which you want to install the protractor, if you are going in local installation.
4. Install the Protractor using below command for global installation
npm install protractor -g
OR
npm i protractor -g
5. For Local installation you should use above command without -g
npm install protractor
OR
npm i protractor
6. Global installation : Global installation is nothing but package will be installed at user level and available in all folder of your file system, you can find these packages at %appdata%/roaming/npm/node_modules
7. Local Installation : The package we install is available only to a particular folder.
8. I am installing Locally, I would recommend you to install locally
9. If everything installed successfully then you should see the stupid yellow messages.
Protractor is installed in :
Jasmine is an automated unit testing framework for JavaScript
few people may say : Jasmine follows Behavior Driven Development (BDD) framework and which is nicely integrated with angular.js
Jasmine keeps you User stories and test cases as different entities so that you can keep map of manual and automation testcases.
This is just introduction page, we are having different chapter to discuss about the Jasmine framework with protractor.
install Jasmine using below command.
# Global installation
npm install jasmine
We have to install the dependency file for jasmine, otherwise you face import error for 'describe', 'it', 'expect' statements.
npm install --save-dev @types/jasmine
TypeScript is a superset of the JavaScript language, typescript will provide more functionalities than javascript along with javascript functionalities
JavaScript is more or less the programming language of the web. It is one of the few languages you can use to create animations, dynamic content, games and pretty much anything else you can imagine, and run it on a website.
You can use javascript write the protractor code.
It has been developed by Microsoft as an extension to JavaScript, that means that all valid JavaScript code is also valid TypeScript code.
In addition, TypeScript provided certain features, such as classes, before they were formally added into JavaScript.
Typescript is scripting language with Object Oriented Programming structure
I recommend you to use TypeScript on top of JavaScript. It will save you a ton of time in the long run.
If you are familiar with any OOP language, it would become easier for you to lean typescript
To install typescript, we have to enter below command in the terminal
npm install typescript
We can install all the required Jars and Exe files using the below command
webdriver-manager update
1. Click Explorer option in VSCode:
1. Click folder icon and create a folder called Specs, which will contain all our test cases.
3. Right click on the specs folder and select New File
4. Name the file as test.ts [ We are using Typescript language]
5. Just simply create a sample print statement inside the test.ts file
6. As I explained earlier, we have to convert the TS files into JS files.
We can use below command in the terminal to compile the existing files.
tsc test.ts // earlier i wrote tsc -test.ts, it was a mistake from my side.
7. Now copy paste below code into the test.ts file.
import { browser} from 'protractor'
describe('Protractor Typescript Demo', function() {
it('title verifications', function() {
browser.get('https://angularjs.org/');
browser.getTitle().then(function(title){
console.log("The title is : "+title)
browser.sleep(5000);
})
});
});
8. Create a new file called conf.js outside the specs folder and enter below content
exports.config = {
// launch locally when fields directConnect and seleniumAddress are not provided
chromeDriver: 'D:/Eclipse progs/driverserver/chromedriver.exe',
seleniumServerJar: 'D:/Eclipse progs/jars/selenium-server-standalone-3.11.0.jar',
specs: ['D:\\Protractor\\JSFilesLocation\\test.js'],
capabilities: {
browserName: 'chrome'
}
}
You can start the run from the cmd, by writing like below but after compiling the TS files:
protractor ConfFilePath/conf.js
Few steps back we have compiled the ts file using 'tsc' command, do you prefer doing that for every change and for every files.
I donot know about your, but I donot prefer it. So with tsconfig.json help we can minimize the amount of compilation effort.
tsconfig will have all the details like what to compile and what not to compile, if we compile when do we need to save the compiled files, to standard we have to compile (es5, es6 like that).
In below file I am compiling all the TS files into Js files and placing them on JSFilesLocation folder
I am ignoring the files present in the node_modules folder, compilation will create exact folder structure in js files like TS files
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"inlineSourceMap": true,
"declaration": false,
"noImplicitAny": false,
"outDir": "JSFilesLocation"
},
"exclude": [
"node_modules"
]
}
tsc -w
Unlike other Major editor, Vscode provides the launch configurations using json format file.
We have to provide details like where is out protractor location and the conf.js file location.
conf.js file is the trigger for the Protractor framework.
Create a folder called .vscode, all the launch configurations should be present in this folder. Yes, this is an hidden folder
Inside .vscode folder crate a file called Launch.json and paste the below content into it.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "C:/Users/user/AppData/Roaming/npm/node_modules/protractor/bin/protractor",
"args": ["${workspaceRoot}/JSFilesLocation/conf.js"]
}
]
}
For running protractor/conf,js file , all you have to do is, press the run button under debug tab on VS code.
While I was writing this tutorial, I have made few mistakes while setting up the above VScode protractor, below are the mistakes I because ave made.
Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know.
Knowledge is the only thing that doubles when you spend it.
I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation
Need help: I have to see whther a element is present. If not, I need to keep clicking a refresh button till the elem is displayed. I can verify with IsPresent() promise by using then(), but if elem not displayed, I need to keep clik refresh button. The time taken by the elem to be displayed varies - from 20 sec to 46 seconds. So after every 5 seconds, I need to verify the IsPresent() and click Refresh button. So I may need to verify 15 times, which is not possible to use the IsPresent() promise - I will end up writing chaining for 15 or more times. In protractor + javascript, this is a big problem for me. Any sudo code?
Hi, I think Code is not compiled. I'm getting the below error. pls have a look. PS D:protractor demo> tsc -w tsc : The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + tsc -w + ~~~ + CategoryInfo : ObjectNotFound: (tsc:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
Hi Madhu, Please install typescript globally. npm install typescript -g
Tnx for the reply admin. I'm attempting to run the script as u suggested - It can able to lunch the browser but it doesn't enter the URL. Browser abruptly stopped. From Debug Console:- C:Program Files odejs ode.exe --inspect-brk=32569 node_modulesprotractorinprotractor 'D:protractor demo/conf.js' Debugger listening on ws://127.0.1:32569/df42d3f3-8a1f-4346-93dd-47d291e4399a [18:51:35] I/launcher - Running 1 instances of WebDriver [18:51:35] I/local - Starting selenium standalone server... [18:51:36] I/local - Selenium standalone server started at http://192.178.0.121:51125/wd/hub [18:51:40] I/local - Shutting down selenium standalone server. [18:51:40] I/launcher - 0 instance(s) of WebDriver still running [18:51:40] I/launcher - chrome #01 passed
Hi Vijay, Due special character present in you message, out commenting system was not able to accept your comment: 1. You need to navigate to folder where tsconfig.json is present then type tsc -w 2. before that check you have installed nodejs