Instability opening new page Protractor non angular site - javascript

I have a problem when I click in non angular site and the test opens a new non angular site tab. Sometimes works (a lot of times), but sometimes shows the following error:
Unknown Error: null value in entry: name=null
This is the code:
browser.ignoreSynchronization = true;
element(by.id('go')).click().then(function () {
browser.driver.getAllWindowHandles().then(function (handles) {
browser.driver.switchTo().window(handles[1]).then(function () {
browser.wait(EC.visibilityOf(element(by.id('text'))), 15000);
expect(element(by.id('text')).getText()).toEqual('Works');
expect(element(by.css('#crumbsNav .last')).getText()).toEqual('Payment');
});
browser.driver.close();
browser.driver.switchTo().window(handles[0]);
});
How I can fix it?

UnknownError: null value in entry: name=null
Means you are trying to switch to a window that is not opened at the moment.
you should wait for the number of tabs to be more than 1 and only then switch.
So:
browser.ignoreSynchronization = true;
element(by.id('go')).click().then(function () {
browser.driver.getAllWindowHandles().then(function (handles) {
browser.wait(function(){return handles.size() > 1}, 15000);//MY ADDITION
browser.driver.switchTo().window(handles[1]).then(function () {
browser.wait(EC.visibilityOf(element(by.id('text'))), 15000);
expect(element(by.id('text')).getText()).toEqual('Works');
expect(element(by.css('#crumbsNav .last')).getText()).toEqual('Payment');
});
browser.driver.close();
browser.driver.switchTo().window(handles[0]);
});

Related

Trying to getCurrentUrl() and open it in new tab in protractor

I have to get the url and open it in 3 new tabs. But I get errors while trying:
browser.getCurrentUrl().then(url => {
browser.actions().keyDown(protractor.Key.CONTROL).sendKeys('t').perform();
browser.sleep(2000);
browser.getAllWindowHandles().then(function (handles) {
browser.switchTo().window(handles[ 1 ]); // 0 or 1 to switch between the 2 open windows // //
browser.get(url)
});
});
Error: Failed: null value in entry: name=null
These are limitation with chrome driver to simulate the same behavior at browser level. It means sendKeys doesn't work with opening new tab as you are trying in your code. There is an alternative way to achieve your goal by 'window.open()'. here is your code snippet with small modification. hope this helps you.
browser.getCurrentUrl().then(url => {
browser.executeScript('window.open()').then( () => {
browser.sleep(2000);
browser.getAllWindowHandles().then(function (handles) {
browser.switchTo().window(handles[ 1 ]); // 0 or 1 to switch between the 2 open windows // //
browser.get(url);
});
});
});

How to close print windows dialog in Protractor Test

I am doing end-to-end testing with protractor. In a certain test, I need to test like print button is creating pdf or not. So When Test clicks the button, It opens the print window dialog like below.
And now this test is not able to be finished. because of this print window. My question is how to close this print dialog in protractor? Because of it, rest of test become pending. Please help. Thanks in advance.
EDIT
I have tried like this..
var printButton=element(by.css('[class="print"]'));
/* This print button should be present first*/
expect(printButton.isPresent()).toBe(true);
browser.actions().mouseMove(printButton).perform();
printButton.click().then(function () {
// fill in the form here
browser.sleep(2000);
// For Pressing Escape key
browser.actions().sendKeys(protractor.Key.ESC).perform();
});
I thought If i got successful to press escape key, then It will resolve the issue.But No Success.
NEXT EDIT--
I have tried new Windows change like below
printButton.click().then(function () {
// fill in the form here
browser.sleep(4000);
browser.getAllWindowHandles().then(function(handles){
browser.switchTo().window(handles[1]).then(function(){
//do your stuff on the pop up window
browser.driver.close();
browser.switchTo().window(handles[0]);
});
});
});
but it shows an error in console and actually It does not open any windows. and hungs up on print dialog as previous.
Failed: unknown error: failed to close window in 20 seconds
EDIT 3
I am having this problem in angular js not in java.
EDIT 4 My Last Attempt
printButton.click().then(function () {
// fill in the form here
return browser.getAllWindowHandles().then(function (handles) {
var newWindowHandle = handles[1]; // this is your new window
return browser.switchTo().window(newWindowHandle).then(function () {
return browser.sleep(5000).then(function () {
return browser.actions().sendKeys(protractor.Key.ESCAPE).perform().then(function () {
return browser.switchTo().window(handles[0])
});
});
});
});
But It does not open a new tab for print Dialog..open print Dialog in same tab.
You need to send the escape to the right window. Also wait for the window to be open before you send it. You probably also need to switch back to handles[0].
return browser.getAllWindowHandles().then(function (handles) {
var newWindowHandle = handles[1]; // this is your new window
return browser.switchTo().window(newWindowHandle).then(function () {
return browser.sleep(5000).then(function () {
return browser.actions().sendKeys(protractor.Key.ESCAPE).perform().then(function () {
return browser.switchTo().window(handles[0])
});
});
});
});
Set the capabilities as below; this will disable the print preview pop up from chrome browser
capabilities: {
'browserName': 'chrome',
chromeOptions: {
args: ['--disable-print-preview','start-maximized']
}
},

How to open new browser window in intern js?

I am automating the UI test of my application. There are some cases when i want my test script to close the current browser and and run next test by opening new browser. The problem is that I am unable to figure out how to open new browser window in intern. remote.get(URL) doesn't do what i want to do here. Can someone please help.
I have updated my question to include code as well. My question is pretty much straight forward though. How to open new browser window using intern from inside test ?.
though if you want to see the code please comment, I will write it down .
Thanks.
// in tests/functional/index.js
define([
'intern!object',
'intern/chai!assert',
'Automation/ConfigFiles/dataurl',
'Automation/pages/login/loginpage',
'intern/dojo/node!fs',
'intern/dojo/node!leadfoot/helpers/pollUntil'
], function (registerSuite, assert, dataurl, LoginPage, fs, pollUntil) {
registerSuite(function () {
var loginPage;
var values;
return {
setup: function () {
var data = fs.readFileSync(loginpage, 'utf8');
json = JSON.parse(data);
values = json.values;
loginPage = new LoginPage(this.remote, json.locator);
return this.remote
.get(require.toUrl(json.locator.URL)).setFindTimeout(60000000000).sleep(5000)
},
beforeEach:function() {
// here i want to open new window
},
'valid loginname lands to password page':function () {
loginPage.submitLoginName(values.unamevalue);
loginPage.isPasswordPageDisplayed().then(function(isPasswordPageDisplayed) {
assert.true(isPasswordPageDisplayed, 'password page is not displayed, Invalid Login name');
})
},
'successful login': function () {
loginPage
.login(values.unamevalue, values.pwdvalue)
loginPage.isLoginSuccess().then(function (loginSuccess) {
assert.isTrue(loginSuccess, 'Login Failed');
});
},
afterEach: function () {
return this.remote.closeCurrentWindow()
}
};
});
});
You can open a new window with window.open. The trick is that you want to run that command in the remote browser. Intern (technically Leadfoot) gives you two methods for doing that on this.remote: execute and executeAsync. For example, to simply open a new window, you could do:
this.remote.execute(function () {
window.open();
})
Once you've opened a new window, you need to switch to it to interact with it. A script might look something like:
var windowHandles;
this.remote
.getAllWindowHandles()
.then(function (handles) {
windowHandles = handles;
})
.execute(function () { window.open() })
.sleep(500)
.getAllWindowHandles()
.then(function (handles) {
// compare the new handles to windowHandles to figure out which
// is the new window, then switch to it
return this.remote.switchToWindow(newWindowHandle);
})
.get('some_new_url')
// rest of test

How to test the rendered text of a mdToast with Protractor?

My workmate is working with Angular Material and he's using the mdToast this way: (in the else statements)
$scope.login = () => {
$scope.loading = true;
authService.login($scope.username, $scope.password, (result) => {
if (result) {
if (result === true) {
$location.path('/');
} else {
$mdToast.show($mdToast.simple().textContent($filter('capitalize')($filter('translate')('passwordNotValid'))).position('top right').hideDelay(3000));
$scope.loading = false;
$("#input_username_login").focus();
}
} else {
//error callback from server
$mdToast.show($mdToast.simple().textContent($filter('capitalize')($filter('translate')('passwordNotValid'))).position('top right').hideDelay(3000));
$scope.loading = false;
$("#input_username_login").focus();
}
});
};
I need to test the text of the resulted toast but it seems like Angular Material's mdToast uses ng-if. My workmate hasn't any HTML code for the toast (he's just using the controller above to generate it) even so when the toast is triggered the next code appears for a few seconds in the DOM:
screenshot of the generated md-toast
I've tried, among other things, slowing down the toast's disappearance with browser.wait, waitForAngular and so on, but didn't work. I'm stuck with:
it("should show error toast in English when user fails login", function(){
login.email_address_field.sendKeys('random_user#correo.com');
login.password_field.sendKeys('hardest_pass_123');
login.login_button.click();
expect(element(by.tagName('md-toast')).element(by.tagName('span')).getText()).toBe('Incorrect username and/or password');
});
I found a solution using this answer as an example. My spec is:
it("should show error toast in English when user fails login", function(){
login.email_address_field.sendKeys('random_user#correo.com');
login.password_field.sendKeys('hardest_pass_123');
login.login_button.click();
browser.ignoreSynchronization = true;
browser.sleep(1000);
expect(element(by.tagName('md-toast')).element(by.tagName('span')).getText()).toBe('Incorrect username and/or password');
browser.ignoreSynchronization = false;
});
You can use ExpectedConditions to make your script wait till the toaster message is displayed.Once the toaster is displayed then you can validate the message as well.
var EC = protractor.ExpectedConditions;
browser.wait(EC.visibilityOf(element(by.tagName('md-toast'))),5000);
expect(element(by.tagName('md-toast')).element(by.tagName('span')).getText()).toEqual('Incorrect username and/or password');
This spec worked for me and it does not use sleeps. The important thing to note here is that the browser.ignoreSynchronization flag must be set while the browser is waiting. Due to the asynchronous nature of the browser.wait, changing ignoreSynchronization must be done after the browser.wait promise resolves or else it could have no effect.
// a close button that appears on the md-toast template
const closeToastButton = $('[data-automation="toast-close"]')
const cond = protractor.ExpectedConditions
function waitToastShow() {
return browser.wait(cond.elementToBeClickable(closeToastButton), 5000)
}
function waitToastHide() {
return browser.wait(cond.invisibilityOf(closeToastButton), 5000)
}
screenshot = name => browser.takeScreenshot().then(/* save fn */)
describe('a suite ... ', () => {
it('takes screenshots of an md-toast once shown and after hidden', function () {
// ... actions that launch an md-toast using $mdToast.show({ ... })
browser.ignoreSynchronization = true
waitToastShow().then(() => {
screenshot('toast-showing.png')
waitToastHide().then(() => {
screenshot('toast-hidden.png')
browser.ignoreSynchronization = false;
})
})
});
}

Howto get new opened tab URL with protractor

Hi I'm trying to get the url from a new tab opened after click on a link, and check if I get the expected value. I can't figure out how to do it because I get this error on the chrome execution:
Failed: null value in entry: name=null
In Firefox the test passes, but the funny part is that it opens a target=_blank in a new window instead of a new tab...
Here is my code:
var scrollBar = require("./lib/scroll_bar");
describe("WAM home blog tests", function() {
var URL = 'http://dev.wam.com.es';
beforeEach(function() {
browser.driver.manage().window().maximize();
browser.get(URL);
});
it("Should find blog section after scrolling", function() {
scrollBar.doScroll(0, 1800);
expect( $("#blog-panel").isPresent() ).toBe(true);
});
it("Should redirect to blog after click on link", function() {
var EC = protractor.ExpectedConditions;
scrollBar.doScroll(0, 1800);
element.all( by.css('div.blog-post > a') ).first().click();
browser.ignoreSynchronization = true;
browser.getAllWindowHandles().then(function (handles) {
console.log(handles);
browser.switchTo().window(handles[1]);
var header = element(by.id("header"));
browser.wait(EC.visibilityOf(header), 15000);
expect(browser.driver.getCurrentUrl())
.toMatch(/^http:\/\/blog.wam.tv\/.*/);
browser.driver.close();
browser.driver.switchTo().window(handles[0]);
});
});
afterEach(function() {
browser.ignoreSynchronization = false;
});
});
What you can do is to open the link in a new browser window by making a SHIFT+click:
var elm = element.all( by.css('div.blog-post > a') ).first();
browser.actions().mouseMove(elm).keyDown(protractor.Key.SHIFT).click().keyUp(protractor.Key.SHIFT).perform();
This helped me before in a similar situation.

Categories