Data Driven Test - Protractor - angularjs

I want is to edit a profile details with passing data from a json file.
Data Form having:
-First Name
-Last Name
-Current Password
-New Password
-Confirm Password, etc....
I need to change only Last Name. How can I do?
--Edited--
"This is the form which I have to edit."
I need to change Only the Last name, First name should be the same. I'm passing values through json file.
[
{
"fname":"" ,
"lname":"",
"currentpassword":"",
"newpassword":"",
"confirmpassword":""
}
]
And this is the code :
//import login details
var testData = require('./login_details.json');
//import editprofiledata
var editData = require('./editprofiledata.json');
describe ('Edit Profile Test', function(){
//browser.driver.manage().window().maximize();
browser.get("http://example");
testData.forEach (function (data) {
it ('Login to the system, and redirect to the dashboard', function() {
element(by.name('email')).clear().sendKeys(data.Email);
element(by.name('password')).clear().sendKeys(data.Password);
//**click on Login button**//
element(by.css('[ng-disabled="register.$invalid"]')).click();
//**popup message click**//
element(by.css('[ng-click="tapToast()"]')).click();
});
});
it("redirect to the Edit Profile",function () {
element(by.css('[ng-click="profileView()"]')).click();
element(by.css('[ng-click = "goToEditView(2)"]')).click();
browser.sleep(500);
});
editData.forEach (function (data) {
it("changing details", function () {
element(by.name('fname')).clear().sendKeys(data.fname);
element(by.name('lname')).clear().sendKeys(data.lname);
browser.sleep(1000);
element(by.model('userEdit.currentPassword')).clear().sendKeys(data.currentpassword);
browser.sleep(1000);
element(by.model('userEdit.password')).clear().sendKeys(data.newpassword);
browser.sleep(1000);
element(by.model('userEdit.confirmPassword')).clear().sendKeys(data.confirmpassword);
browser.sleep(1000);
element(by.css('[ng-click="editUserProfile(userEdit)"]')).click();
browser.sleep(2000);
});
});
});
How can I do this?

Not so clear question but I guess you are thinking about some data provider like:
jasmine-data-provider

Related

Page reloading for every "it" statement in protractor and printing "."

Have anyone found fix for this. I am new to protractor and got some script running. I have a login page and once logged in search for a user. My spec.js has the following structure.
describe('Smoke', function() {
//sum stuff
beforeEach(function () {
browser.get('https://login/');
});
it('should have a title', function() {
expect(browser.getTitle()).toEqual('title');
browser.pause()
});
it('and Login to MSIX', function () {
login.login(username);
});
it('search for a user', function () {
searchUser.searchForUser();
});
it ('print test result', function () {
var userN = loginName.getText().then((text) => {
return text;
})
// at this point data is still a managed promise!
userN.then((text) => {
console.log("Logged in user is: "+text);
console.log("User " +username+" logged in successfully"); // your text would be printed!
})
})
});
For every "it" statement the page reloads and I am losing the content on the page. Also if you notice the last "it" statement to print test result I am noticing "." before the output in console
**..**[12:55:55] W/element - more than one element found for locator by.model("query.identifier") - the first result will be used
**.**Logged in user is: sample user
User abc logged in successfully
**.**
You are telling it to reload the page for every it by using beforeEach:
beforeEach(function () {
browser.get('https://login/');
});
Use beforeAll instead:
beforeAll(function () {
browser.get('https://login/');
});

AngularJS - scenario.js code

In the application building tutorial on angularjs.org, step-8, testing part, what does the following lines of code mean-
element.all(by.css('.phones li a')).first().click();
expect(browser.getLocationAbsUrl()).toBe('/phones/nexus-s');
Thanks in advance!
PS:
The exact URL is- https://docs.angularjs.org/tutorial/step_08 and the code file (scenarios.js) is-
'use strict';
// Angular E2E Testing Guide:
// https://docs.angularjs.org/guide/e2e-testing
describe('PhoneCat Application', function() {
describe('phoneList', function() {
beforeEach(function() {
browser.get('index.html');
});
it('should filter the phone list as a user types into the search box', function() {
var phoneList = element.all(by.repeater('phone in $ctrl.phones'));
var query = element(by.model('$ctrl.query'));
expect(phoneList.count()).toBe(20);
query.sendKeys('nexus');
expect(phoneList.count()).toBe(1);
query.clear();
query.sendKeys('motorola');
expect(phoneList.count()).toBe(8);
});
it('should be possible to control phone order via the drop-down menu', function() {
var queryField = element(by.model('$ctrl.query'));
var orderSelect = element(by.model('$ctrl.orderProp'));
var nameOption = orderSelect.element(by.css('option[value="name"]'));
var phoneNameColumn = element.all(by.repeater('phone in $ctrl.phones').column('phone.name'));
function getNames() {
return phoneNameColumn.map(function(elem) {
return elem.getText();
});
}
queryField.sendKeys('tablet'); // Let's narrow the dataset to make the assertions shorter
expect(getNames()).toEqual([
'Motorola XOOM\u2122 with Wi-Fi',
'MOTOROLA XOOM\u2122'
]);
nameOption.click();
expect(getNames()).toEqual([
'MOTOROLA XOOM\u2122',
'Motorola XOOM\u2122 with Wi-Fi'
]);
});
it('should render phone specific links', function() {
var query = element(by.model('$ctrl.query'));
query.sendKeys('nexus');
element.all(by.css('.phones li a')).first().click();
expect(browser.getLocationAbsUrl()).toBe('/phones/nexus-s');
});
});
});
It is testing of the routing to /phones/nexus-s.
It is written in Protractor.
The first line reads the DOM and finds all the .phones li a css rules. It then takes only the first one and calls click() on it.
element.all(by.css('.phones li a')).first().click();
The second line expects the output of the function browser.getLocationAbsUrl() to be the string /phone/nexus-s
expect(browser.getLocationAbsUrl()).toBe('/phones/nexus-s');
So all in all the test framework clicks a button and expects it to be routed to a new page.

How to call one file from another file in protractor angular

This is the first file
log.js
var user = 'raghavendra#gmail.com', pwd = '123';
describe(' login page.', function() {
it('Sending a valid values:', function() {
browser.get('http://gmail.com');
expect(browser.getCurrentUrl()).toEqual()
second file
create_file.js
describe(' login page.', function() {
it('Sending a valid values:', function() {
}
expect(browser.getCurrentUrl()).toEqual()
}
How to call log.js file in create_file.js. Please help me.
I got the solution
require('/home/raghavendra/Desktop/python_pro/ng_test/e2e-tests/log.js');

protractor get url after click()

i'm new with protractor.. i need you'r help..
my code go like this..
describe('Protractor Demo Charts', function () {
var url = 'https://angularjs.org/';
it('should get the value of attribute d', function () {
browser.get(url);
element(by.css('.btn-warning')).click().then(function(text){
expect(browser.getCurrentUrl()).toContain('0BxgtL8yFJbacQmpCc1NMV3d5dnM');
}
);
});
});
my problem is that browser.getCurrentUrl() still return me the base url (the page that i came from 'https://angularjs.org/' )
how can i get the new Url (the URL AFTER the click )?
May you should wait until the page has been loaded.
Try this way :
describe('Protractor Demo Charts', function () {
var url = 'https://angularjs.org/';
it('should get the value of attribute d', function () {
browser.get(url);
browser.sleep(2000);
$('.btn-warning').click();
expect(browser.getCurrentUrl()).toContain('0BxgtL8yFJbacQmpCc1NMV3d5dnM');
});
});
From the doc:
Protractor will ensure that commands will automatically run in sync. For example, in the following code, element(by.model(...)).click() will run before browser2.$('.css').click():
browser.get('http://www.angularjs.org');
browser2.get('http://localhost:1234');
browser.sleep(5000);
element(by.model(...)).click();
browser2.$('.css').click();

Firebase - Filter by a column

I'm using Firebase for my Angular.js application.
I'm looking for the equivalent of SQL's WHERE statement for Firebase.
I have an array of TV series stored in Firebase, and I want to fetch only these that has the name that the user entered (in the example searchQuery).
Does Firebase support it? Does it have something like this?
var seriesRef = new Firebase('http://{app}.firebaseio.com/series');
var seriesObject = $firebaseObject(seriesRef.query({ name: searchQuery }));
I have some suggestions that may help here:
Check out the Firebase Query documentation. Specifically,
.orderByChild()
.equalTo()
You can use queries in conjunction with .$ref() to get the desired record.
Example
Check out this working CodePen demo.
I replicated your data in one of my public Firebase instances.
The query that you're looking for is seriesCollectionRef.orderByChild('name').equalTo(seriesName)
If you enter 'Avatar: The Last Airbender' in the input and click "Find", you'll get the matching series object.
In my example, I extended the $firebaseArray service to include a method for finding a specific series by name.
See the documentation for extending AngularFire services.
You can accomplish the same thing without extending the service, see last code snippet.
Factories
app.factory('SeriesFactory', function(SeriesArrayFactory, fbUrl) {
return function() {
const ref = new Firebase(`${fbUrl}/series`);
return new SeriesArrayFactory(ref);
}
});
app.factory('SeriesArrayFactory', function($firebaseArray, $q) {
return $firebaseArray.$extend({
findSeries: function(seriesName) {
const deferred = $q.defer();
// query by 'name'
this.$ref()
.orderByChild('name')
.equalTo(seriesName)
.once('value', function(dataSnapshot) {
if (dataSnapshot.exists()) {
const value = dataSnapshot.val();
deferred.resolve(value);
} else {
deferred.reject('Not found');
}
})
return deferred.promise;
}
});
});
Controller
app.controller('HomeController',function($scope, SeriesFactory, fbUrl) {
$scope.seriesName = '';
$scope.findSeries = function() {
const seriesCollection = new SeriesFactory();
seriesCollection
.findSeries($scope.seriesName)
.then(function(data) {
$scope.series = data;
})
.catch(function(error) {
console.error(error);
});
};
});
Without Extended Service
Here is what a controller function would look like if you weren't using the factories:
$scope.findSeriesWithoutFactory = function() {
const seriesRef = new Firebase(`${fbUrl}/series`);
const seriesCollection = $firebaseArray(seriesRef);
seriesCollection.$ref()
.orderByChild('name')
.equalTo($scope.seriesName)
.once('value', function(dataSnapshot) {
if (dataSnapshot.exists()){
$scope.series = dataSnapshot.val();
} else {
console.error('Not found.');
}
});
};
Rules
Note: It's important to note that you should add ".indexOn": "name" to your Firebase rules so that the query runs efficiently. See the Indexing Your Data portion of the Firebase Security & Rules Guide for more information. Below is an example:
"yourfirebaseapp": {
".read": "...",
".write": "...",
"series": {
".indexOn": "name"
}
}

Resources