How can I make my protractor test pass? - angularjs

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

Related

React & Jest global.confirm not updating between tests

I have an existing application that I am trying to write tests for. I have a component function that uses a window.confirm box to get user input (not my code) like this:
if (window.confirm('Are you sure to unassign?')) {
NetworkWebAPIUtils.unassignSeat(this.state.seatnetwork.seat.id, this.state.seatnetwork.network.id, this.props.rank);
}
I am trying to write tests for both paths:
it('should call endpoint when user selects yes', function () {
global.confirm = jest.fn(() => true);
seatUpdateRow.instance().handleChangeAssigned({target: {value: true}});
expect(NetworkWebAPIUtils.unassignSeat.mock.calls.length).toBe(1);
expect(NetworkWebAPIUtils.unassignSeat.mock.calls[0].length).toBe(3);
expect(NetworkWebAPIUtils.unassignSeat.mock.calls[0][0]).toBe(1234);
expect(NetworkWebAPIUtils.unassignSeat.mock.calls[0][1]).toBe(5678);
expect(NetworkWebAPIUtils.unassignSeat.mock.calls[0][2]).toBe(1);
});
it('should not call endpoint when user selects no', function () {
global.confirm = jest.fn(() => false);
seatUpdateRow.instance().handleChangeAssigned({target: {value: true}});
expect(NetworkWebAPIUtils.unassignSeat).not.toHaveBeenCalled();
});
The problem is that global.confirm will not update for the second test. If I set the first test to false then it obviously fails but the second one passes. If I set the first to true then the first passes but the second fails because global.confirm = jest.fn(() => false) doesn't actually cause window.confirm to evaluate to false. If I comment out the first then the second passes just fine.
I have tried mocking window.confirm and I get the exact same behavior.
It was an obvious issue. I forgot to clear the mock calls from NetworkWebAPIUtils.unassignSeat.
afterEach(function () {
NetworkWebAPIUtils.unassignSeat.mockClear();
});

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

click() doesn't work in IE 11 but works in Chrome on the same elements

I have two clickable elements on my webpage, which is a login page. One is a remember-me checkbox, and the other is a login button. The code below executes successfully on chrome. However, when I run the test against IE 11, the tests actually pass! But the buttons are never clicked. Only the 'will recieve a login error' test fails with IE because the error message is never presented since the login button is never clicked.
Here is the code:
//run test
describe('Test my Login webpage', function () {
var usernameInput = element(by.id('UserName'));
var passwordInput = element(by.id('Password'));
var loginButton = element(by.tagName('button'));
it('will open the login webpage', function () {
browser.get('http://<my website>/Account/Login');
});
it('will click the remember-me checkbox', function () {
expect(element(by.id('RememberMe')).isSelected()).not.toBeTruthy();
element(by.id('RememberMe')).click();
});
it('will type a username', function () {
expect(element(by.id('UserName')).isDisplayed()).toBe(true);
usernameInput.sendKeys('testing...').then(function () {
expect(element(by.id('UserName')).getText()).toBe('');
});
});
it ('will type a password', function () {
expect(element(by.id('UserName')).isDisplayed()).toBe(true);
passwordInput.sendKeys('fakePassword').then(function () {
expect(element(by.id('Password')).getText()).toContain('');
});
});
it('will click login button', function () {
expect(loginButton.isDisplayed()).toBe(true);
loginButton.click();
browser.waitForAngular();
usernameInput.clear();
usernameInput.sendKeys('my second attempt');
});
it('will recieve a login error', function () {
expect(element(by.css('.error-msg')).isDisplayed()).toBe(true);
//expect(element(by.class('error-msg')).getText()).toMatch('Username or password was invalid, please retry');
});
});
You could try putting an explicit wait in before you .click(). IE is notorious for being slower to load objects, and thus more synchronization is needed.

How to correctly step through tests on different pages in Protractor e2e test?

We want to use protractor to visit a page, run some tests, and then go to another page for more tests. But the later navigation appears to interfere the tests on the previous page.
Here is the structure:
describe('When user is an admin', function () {
browser.get(browser.baseUrl + 'login');
loginAs(roles.admin);
wait();
describe('After log in', function () {
it('should redirect to the home page', function () {
expect(browser.getCurrentUrl()).toBe(browser.baseUrl + 'home');
expect(true).toBeTruthy();
});
it('should contain a "Locations" tab', function () {
var tabLocations = element(by.id('tabLocations'));
expect(tabLocations.isPresent()).toBeTruthy();
});
it('should contain a link to location list', function () {
var linkLocation = element(by.linkText('Manage Locations'));
expect(linkLocation.isPresent()).toBeTruthy();
});
});
});
Pasted above is the tests on the first page.
Then we want to run the tests on another page: (full coded pasted below)
describe('When user is an admin', function () {
browser.get(browser.baseUrl + 'login');
loginAs(roles.admin);
wait();
describe('After log in', function () {
it('should redirect to the home page', function () {
expect(browser.getCurrentUrl()).toBe(browser.baseUrl + 'home');
expect(true).toBeTruthy();
});
it('should contain a "Locations" tab', function () {
var tabLocations = element(by.id('tabLocations'));
expect(tabLocations.isPresent()).toBeTruthy();
});
it('should contain a link to location list', function () {
var linkLocation = element(by.linkText('Manage Locations'));
expect(linkLocation.isPresent()).toBeTruthy();
});
});
describe('at location manage page', function () {
var listPage = new ListPage();
listPage.visit('Manage Locations');
wait();
it('should be at the location list page', function () {
expect(browser.getCurrentUrl()).toBe(browser.baseUrl + 'locations/list');
expect(true).toBeTruthy();
});
});
});
It appears the navigation in my second describe runs before the execution of it statements in the first describe, therefore my tests on the first page failed.
I've tried placing the second describe ("at location manage page") inside the first describe ("After log in"), but still got the same problem.
So my question is, what is the correct way to arrange the tests in protractor, so as the navigation in later tests will be held until the previous tests are completed?
Do not put code inside a describe. Put it inside a beforeAll(function(){}) (jasmine 2) or a beforeEach(function() {})
That should fix your problem.

How to run a protractor tests with different input parameters

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

Resources