How to call one file from another file in protractor angular - angularjs

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');

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/');
});

Data Driven Test - Protractor

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

Testing component that opens md-dialog

I am trying to write a unit test for an Angular component that opens a dialog, but am unable to do so because I cannot trigger the closing of the dialog.
How can I cause the md dialog to resolve from the test case?
I have created a repository with a basic example where the problem can be reproduced, and copied the central bits below. There is an index.html to manually verify that the code is working, a test case that displays the problem and an example of how the tests are written in the md code.
Repository - https://github.com/gseabrook/md-dialog-test-issue
The component is extremely basic
angular
.module('test', ['ngMaterial'])
.component('dialogTest', {
template: '<button ng-click="showDialog()">Show Dialog</button>',
controller: function($scope, $mdDialog) {
var self = this;
$scope.showDialog = function() {
self.dialogOpen = true;
var confirm = $mdDialog.confirm()
.title('Dialog title')
.ok('OK')
.cancel('Cancel');
$mdDialog.show(confirm).then(function(result) {
self.dialogOpen = false;
}, function() {
self.dialogOpen = false;
});
}
}
});
And the test is also very simple
it("should open then close the dialog", function() {
var controller = element.controller("dialogTest");
expect(controller.dialogOpen).toEqual(undefined);
expect(element.find('button').length).toEqual(1);
element.find('button').triggerHandler('click');
expect(controller.dialogOpen).toBeTruthy();
rootScope.$apply();
material.flushInterimElement();
element.find('button').eq(2).triggerHandler('click');
rootScope.$apply();
material.flushInterimElement();
expect(controller.dialogOpen).toBeFalsy();
});
I managed to resolve the issue by setting the root element as the problem seemed to be related to element being compiled in the test being unconnected with the root element that angular-material appended the dialog too.
I've updated the github repository with the full code, but the important bits are
beforeEach(module(function($provide) {
rootElem = angular.element("<div></div>")
$provide.value('$rootElement', rootElem);
}));
beforeEach(inject(function(_$rootScope_, _$compile_, $mdDialog, _$material_) {
...
element = getCompiledElement();
angular.element(window.document.body).append(rootElem);
angular.element(rootElem).append(element);
}));

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();

protractor expect currenturl fails

I'm trying to get an e2e test running against my local server and test that the resulting url (after a navigational button has been clicked) is the correct result. However the resulting url is always false.
My code is shown below:
HTML:
//http://localhost/#/current_Page
<html>
<head><title></title></head>
<body>
//should change the current url to
//http://localhost/#/new_page
<button class="button" ng-click="change_page()">Change Page</button>
</html>
TEST CODE:
var protractor = require('protractor');
require('protractor/jasminewd');
describe('Tests', function() {
var ptor;
describe('Test 1', function() {
var ptor = protractor.getInstance();
ptor.get('#/current_page');
it('change page and current url', function() {
ptor.findElement(protractor.By.className('.button').click().then(function() {
expect(ptor.currentUrl()).toContain('#/new_page');
});
});
}, 30000);
});
The issue is the current url after clicking the button remains #/current_url and does not change to the expected result #/new_page.
Does anyone know where I have gone wrong?
After search for the answer to this question I figured it out myself
The current url does not fail, I was not waiting for the promise to return to angular. The ammended code below shows where I had gone wrong
var protractor = require('protractor');
require('protractor/jasminewd');
describe('Tests', function() {
var ptor;
describe('Test 1', function() {
var ptor = protractor.getInstance();
ptor.get('#/current_page');
it('change page and current url', function() {
ptor.findElement(protractor.By.className('.button').click().then(function() {
ptor.waitForAngular();
expect(ptor.currentUrl()).toContain('#/new_page');
});
});
}, 30000);
});
This then waits for angular to route to the new page and update any bindings and then proceeds to check the expected result which is now what I would expect it to be.
Please be advised that this does not solve all issues relating to unexpected getCurrentUrl() results. if using driver.findElement() you may need to refer to JulieMR's answer to this question
I hope this helps someone stuck on this issue.
In Protractor 1.5.0 protractor.getInstance(); isn't working anymore, so you have to use browser instead.
var protractor = require('protractor');
require('protractor/jasminewd');
describe('Tests', function() {
describe('Test 1', function() {
browser.get('#/current_page');
it('change page and current url', function() {
ptor.findElement(protractor.By.className('.button').click().then(function() {
browser.waitForAngular();
expect(browser.getCurrentUrl()).toContain('#/new_page');
});
});
}, 30000);
});
You can also write a custom expected condition to wait for current url to being equal a desired one. Besides, use browser and element notations:
browser.get("#/current_page");
it("change page and current url", function() {
element(by.css(".button")).click();
browser.wait(urlChanged("#/new_page")), 5000);
});
where urlChanged is:
var urlChanged = function(url) {
return function () {
return browser.getCurrentUrl().then(function(actualUrl) {
return actualUrl.indexOf(url) >= 0;
});
};
};
Or, a Protractor>=4.0.0 solution and the urlContains expected condition:
element(by.css(".button")).click();
var EC = protractor.ExpectedConditions;
browser.wait(EC.urlContains("#/new_page"), 5000);

Resources