How to run a protractor tests with different input parameters - angularjs

I am testing an angular application and I would like to feed my search tests with different parameters and run the test for all parameters.
In my case I have a search test:
require("./myconfig.js");
describe('change number of items displayed by page', function () {
var loginPage = require("./Pages/login-page.js");
var searchPage = require("./Pages/search-page.js");
var ptor;
ptor = protractor.getInstance();
it('should display 20 items per page', function () {
searchPage.home.click();
searchPage.searchTxt.sendKeys(Parameter);
searchPage.searchBtn.click();
var result = ptor.findElements(protractor.By.repeater('object in objects'));
result.then(function (arr) {
expect(arr.length).toEqual(20);
});
}, 50000);
});
is there a way using Jasmine and protractor to call parmaeter from an XML file? Tahnks for your help

Try xml2js xml parser in Node and use in your test.
> npm install xml2js

Related

Shareds Behavior Jasmine + Protractor

I'm new with Protractor and jasmine framework to test an AngularJS Application.
I whould like to reuse some of my scenarios, like Login feature can call in all scenarios of my suite.
This is my login_test.js
module.exports = function(testName, testFn) {
const loginPage = pages.login;
const mainPage = pages.main;
var protractor;
describe('common Login suite', function() {
browser.ignoreSynchronization = true;
beforeEach(function() {
});
afterAll(function() {
browser.manage().deleteAllCookies();
});
it(testName, function() {
browser.get('http://localhost:9000/');
loginPage.typeUserName('bxxxxx');
loginPage.typePassword('xxxxxx');
loginPage.clickLogin();
});
});
}
And here I have the remote_terminal feature, here I need call Login feature to perfom login in my scenario.
var loginSuite = require('./login_test.js');
loginSuite('login Suite terminal feature', function(browser) {
describe('description', function() {
console.log('describe');
it('it', function() {
console.log('it');
});
});
});
But when this spec (remote_terminal) is called to run I got this message on my console
Started
Spec started
.
common Login suite
✓ login Suite terminal feature
As you can see the describe and IT in the remote_terminal spec aren't ran.
Demo:
http://pavelbogomolenko.github.io/dry-principles-with-protractor.html
You can use pageObjects to achieve reusable code accross your test,
Let us create a new js file for login page as login_page.js as below,
var login_page = function(){
this.performLogin = function(){
//add code for performing login operation.
}
}
module.exports = new login_page();
Now you can import the login_page.js in all 'specs' and can call the performLogin() method whenever required.
Look at the example spec.js below,
describe("Test pageobject code",function(){
var login_page = require("login_page.js");
beforeAll(function(){
login_page.performLogin();
})
it("simple test to perform after login action is performed",function()
//do whatever you want to test
})
})

protractor E2E TEST multiple file upload using ng-file-upload

I was writing E2E test-case using protractor for angularjs application. My application has multiple file uploading as one of its features. So, to write E2E test-case for my application, I have to automate multiple file-uploading.
I am able to upload single file using protractor but my application requires more than 1 file for seamless working.
browser.get(localUrl);
var button = element(by.css('[ngf-select]'));
button.click();
var input = element(by.css('input[type="file"]'));
input.sendKeys([ absolutePath, absolutePath1, absolutePath2]);
Regards
Ajay
Have you tried
browser.get(localUrl);
var button = element(by.css('[ngf-select]'));
button.click();
var input = element(by.css('input[type="file"]'));
input.sendKeys( absolutePath + "\n" + absolutePath1 + "\n" + absolutePath2);
Your requirement seems to be like Data Driven approach. So, keep all input file paths in below function arrayOfData() and it block will iterate till all files are uploaded. You can follow the below code:
describe('Data driven test spec', function () {
function arrayOfData() {
return [
{
"absolutePath": "/PathToFile1",
},
{
"absolutePath": "/PathToFile2",
},
{
"absolutePath": "/PathToFile3",
},
]
}
beforeAll(function(){
browser.get(localUrl);
})
using(arrayofData, function (inputData) {
it('test case logic to be executed for each set of data', function () {
var button = element(by.css('[ngf-select]'));
var input = element(by.css('input[type="file"]'));
button.click();
input.sendKeys(inputData.absolutePath);
});
});
});

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 can I make my protractor test pass?

I created a simple protractor test for my angular app. When I click a button an input value gets set:
$scope.fillForm=function(){
console.log('fillform');
$scope.myText='hoera';
};
The e2e test expects the input to be populated with 'hoera':
describe('fill in form e2e test', function () {
it('should test form', function () {
browser.get('http://localhost:63342/protractor_new/index.html');
element(by.buttonText('go')).click().then(function () {
var el = element(by.model('myText')).getText();
expect(el).toEqual('hoera');
});
});
});
When I run the test with 'protractor conf' I get:
Expected '' to equal 'hoera'.
I would expect something like : expected 'hoera' to equal 'hoera'? How can I make it pass maybe there is a delay before angular sets the value? Here is a link to the code: https://github.com/dimitri-a/protractor_new
You were close. Your get and click should be added to the controlFlow, so no need to have a then on the click. But you DO need a then on your getText. This should work...
describe('fill in form e2e test', function () {
it('should test form', function () {
browser.get('http://localhost:63342/protractor_new/index.html');
element(by.buttonText('go')).click();
element(by.model('myText')).getText().then(function(el) {
expect(el).toEqual('hoera');
});
});
});

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

Resources