This is a continuation of the article series on automating protractor tests using grunt. You can see the first part here
In this part we will be doing the following tasks.
Now, if we do a grunt install on the command console, it will show as everything uptodate, since we have already installed selenium and chromedriver in the current project folder.
PS: You can see some warning. Please ignore that for now. This is because we haven't added the README.md file and repo to the project, which we will add at the end.
To demonstrate the usefulness of what we did now, lets make a copy of our project folder. Make sure that, you are not copying the node_modules folder. I copied the files to 'exampleCopy' folder. Go to this copy folder in the command console.
Now, if we do the grunt install, everything should work, right? Let's see.
It is saying that a local copy of grunt is required. So, we have to do an npm install before we call any grunt tasks. Let's do that.
1. npm install
2. grunt install
Now you are ready to do the protractor tests. So, any other person using this repo only need to run two commands "npm install" and "grunt install" and they are ready to go.
Protractor gives the options to add onPrepare function property on its configuration file. This onPrepare function will be called before each tests. Jasmine test framework(which we are using inside protractor) provides the option to add test reporter. We will get the metadata about each test by using these two configurations.
There is a node module available for generating screenshots for each tests. protractor-screenshot-reporter. However this doesn't process the meta data and gives us the result in a readable format.
I couldn't find a node module doing this for protractor. So, I created a module for generating this HTML report, on top of protractor-screenshot reporter, protractor-html-screenshot-reporter. What it does is generate an HTML report with all the details of the test like status, browser used, message, link to screenshot etc. (If you are finding some other modules which are doing the same, please add in the comments. )
Lets install protractor-html-screenshot-reporter module in our project.
We need to add this reporter in the Protractor configuration file.
1. require the protractor-html-screenshot-reporter module and assign to a variable.
2. add this reporter on the onPrepare function.
3. Run the test using grunt command. It will run the default task in the Gruntfile.
We can see that the tests are passing. Lets see if the reports are generated.
Screenshots folder is created.
Inside the screenshots folder, one json and png with a guid filename is available. This is the metadata and screenshot respectively for the single test. There is also a combined.json which combines all the metadata into a single file and a reporter.html file which shows all the test reports.
Opening the reporter.html looks like below.
Clicking on View link opens the screenshot image also.
So, now you have an html reporter ready. But, there is one issue though. Whenever you run the tests, the reporter files are getting overridden. If I want to keep a track of tests, this is not going to help.
protractor-html-screenshot-reporter provides the option of pathBuilder function property to give your own dynamic paths. We will add this in Protractor configuration file.
I am just using a basic function to give the folder name as the current date and time and browser.
Note that I am using the path module for creating the file path (path is a native node module for handling and tranforming file paths. ). Make sure that, you have required the path and assigned to a variable.
Lets run the test again using grunt command in command console. As expected tests are running successfully.
If we check the project folder we can see that, the report is now generated in the path 'screenshots/22-Feb-2014-5h-58m/chrome'. Great. Now, on seeing the folder names itself, you know when the test was run and on which browser.
We will cover that in the coming articles. Stay tuned.
Please share your comments.
All the changes are added on github repo - protractor-e2e-bootstrap
In this part we will be doing the following tasks.
- Adding the setup tasks to grunt to make the whole process easier to start.
- Adding an HTML reporter
Adding the setup command tasks
When you create a repo, you may want to clone this repo on several machines. Or, when someone else wants to use your repo to use in his/her project. When they do that, they also have to go through all the basic setup that you have done like installing selenium, chromedriver etc. We don't want that.
We are doing all our setup on the command console. Then, why not automate it? Is there any grunt plugin for that? Yes. There is. We can use grunt-shell-spawn for running all the shell commands.
Lets add grunt-shell-spawn module to our project.
Now, we will add the shell-spawn task to Gruntfile.
You can see that I have added two options. 'protractor_install' and 'npm_install'. It is better to add an npm install before we are installing chrome driver and selenium just to make sure that, everything is ready beforehand.
Now we will add the install task. I have added both 'npm_install' and 'protractor_install' to the install task.
Lets add grunt-shell-spawn module to our project.
Now, we will add the shell-spawn task to Gruntfile.
You can see that I have added two options. 'protractor_install' and 'npm_install'. It is better to add an npm install before we are installing chrome driver and selenium just to make sure that, everything is ready beforehand.
Now we will add the install task. I have added both 'npm_install' and 'protractor_install' to the install task.
Now, if we do a grunt install on the command console, it will show as everything uptodate, since we have already installed selenium and chromedriver in the current project folder.
PS: You can see some warning. Please ignore that for now. This is because we haven't added the README.md file and repo to the project, which we will add at the end.
To demonstrate the usefulness of what we did now, lets make a copy of our project folder. Make sure that, you are not copying the node_modules folder. I copied the files to 'exampleCopy' folder. Go to this copy folder in the command console.
Now, if we do the grunt install, everything should work, right? Let's see.
It is saying that a local copy of grunt is required. So, we have to do an npm install before we call any grunt tasks. Let's do that.
1. npm install
2. grunt install
Now you are ready to do the protractor tests. So, any other person using this repo only need to run two commands "npm install" and "grunt install" and they are ready to go.
Adding the HTML Reporter
When we run the tests, we can see that tests are passing or failing in the command console. What if we have hundreds of tests and also want to keep a record of the test results whenever we run the tests? We need test reporters for that.Protractor gives the options to add onPrepare function property on its configuration file. This onPrepare function will be called before each tests. Jasmine test framework(which we are using inside protractor) provides the option to add test reporter. We will get the metadata about each test by using these two configurations.
There is a node module available for generating screenshots for each tests. protractor-screenshot-reporter. However this doesn't process the meta data and gives us the result in a readable format.
I couldn't find a node module doing this for protractor. So, I created a module for generating this HTML report, on top of protractor-screenshot reporter, protractor-html-screenshot-reporter. What it does is generate an HTML report with all the details of the test like status, browser used, message, link to screenshot etc. (If you are finding some other modules which are doing the same, please add in the comments. )
Lets install protractor-html-screenshot-reporter module in our project.
We need to add this reporter in the Protractor configuration file.
1. require the protractor-html-screenshot-reporter module and assign to a variable.
2. add this reporter on the onPrepare function.
onPrepare: function() { // Add a reporter and store screenshots to `screnshots`: jasmine.getEnv().addReporter(new HtmlReporter({ baseDirectory: 'screenshots' })); },
3. Run the test using grunt command. It will run the default task in the Gruntfile.
We can see that the tests are passing. Lets see if the reports are generated.
Screenshots folder is created.
Inside the screenshots folder, one json and png with a guid filename is available. This is the metadata and screenshot respectively for the single test. There is also a combined.json which combines all the metadata into a single file and a reporter.html file which shows all the test reports.
Opening the reporter.html looks like below.
Clicking on View link opens the screenshot image also.
So, now you have an html reporter ready. But, there is one issue though. Whenever you run the tests, the reporter files are getting overridden. If I want to keep a track of tests, this is not going to help.
protractor-html-screenshot-reporter provides the option of pathBuilder function property to give your own dynamic paths. We will add this in Protractor configuration file.
onPrepare: function() { // Add a reporter and store screenshots to `screnshots`: jasmine.getEnv().addReporter(new HtmlReporter({ baseDirectory: 'screenshots', pathBuilder: function pathBuilder(spec, descriptions, results, capabilities) { var monthMap = { "1": "Jan", "2": "Feb", "3": "Mar", "4": "Apr", "5": "May", "6": "Jun", "7": "Jul", "8": "Aug", "9": "Sep", "10": "Oct", "11": "Nov", "12": "Dec" }; var currentDate = new Date(), currentHoursIn24Hour = currentDate.getHours(), currentTimeInHours = currentHoursIn24Hour>12? currentHoursIn24Hour-12: currentHoursIn24Hour, totalDateString = currentDate.getDate()+'-'+ monthMap[currentDate.getMonth()]+ '-'+(currentDate.getYear()+1900) + '-'+ currentTimeInHours+'h-' + currentDate.getMinutes()+'m'; return path.join(totalDateString,capabilities.caps_.browserName, descriptions.join('-')); } })); },
I am just using a basic function to give the folder name as the current date and time and browser.
Note that I am using the path module for creating the file path (path is a native node module for handling and tranforming file paths. ). Make sure that, you have required the path and assigned to a variable.
Lets run the test again using grunt command in command console. As expected tests are running successfully.
If we check the project folder we can see that, the report is now generated in the path 'screenshots/22-Feb-2014-5h-58m/chrome'. Great. Now, on seeing the folder names itself, you know when the test was run and on which browser.
Finally, lets make this project ready to add as a git repo.
1. It is not a good a good practice to push node_modules directory to the repo. In order to avoid node_modules to be pushed to git, we need to add it to .gitignore file.
Create a new file in the root folder with name as .gitignore.
We don't want screenshots and node_modules folder to be added in git. So, I have added both names in .gitignore.
2. We will also add the repository details to package.json.
3. Also, create a README.md file in the root folder and add some description of your project.
Finally, we are ready to push to the repo.
More Requirements
Now that we are in a good state, we can check the further requirements.- Generating tests based on style guide
- Doing CSS Regression testing
We will cover that in the coming articles. Stay tuned.
Please share your comments.
All the changes are added on github repo - protractor-e2e-bootstrap