Howto get new opened tab URL with protractor - javascript

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.

Related

How can I make a confirm dialogue appear in the newly opened tab?

I want to open a new tab (with an URL from an array).
This works so far.
But when the new page is loaded or when the new tab is opened i want a confirm dialogue on the newly opened tab.
I would be very happy if you could help me.
Thank you in advance. :)
Here´s the code I´m trying to change:
<script>
var urlArray = [
'http://google.com',
'http://www.yahoo.com',
'http://gmail.com'
],
timeToCloseWindow = 3000;
function work() {
if(urlArray.length==0) return;
var url = urlArray.shift();
var openWindow = window.open(url);
var answer = confirm("Do you like this website?")
setTimeout(function () {
openWindow.close();
work();
}, timeToCloseWindow);
}
work();
</script>
Try this. This should open a window, wait 3 seconds, then ask if you like this website. If you hit OK, nothing further will open, but if you answer cancel, the next website will open and the process repeats until the user hits OK once or cancels all available websites.
var urlArray = [
'http://google.com',
'http://www.yahoo.com',
'http://gmail.com'
],
timeToCloseWindow = 3000;
function work() {
if(urlArray.length==0) return;
var url = urlArray.shift();
var openWindow = window.open(url);
setTimeout(function () {
if(!confirm("Do you like this website?")) {
openWindow.close();
work();
}
}, timeToCloseWindow);
}
work();

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 get get URL of new page using casperJS

I am using casperJS to get links when clicking a button. The links are returned with window.open in javaScript.
The code I have written logs all the pages after clicking button, but phantom is not exiting in terminal window. Also some pages only show about:blank, especially the last ones.
var casper = require('casper').create();
var page = require('webpage').create();
var address = 'http://www.example.com';
page.open(address, function() {
page.onPageCreated = function(newPage) {
newPage.onClosing = function(closingPage) {
console.log('A child page is closing: ' + closingPage.url);
/* if i set phantom.exit() it will only log the first page url.
Problem: I need all page urls. */
}
}
page.evaluate(function() {
$(".button").click();
});
}
The method "getCurrentUrl()" will return the current url. Here is an easy (not really meaningful) example:
casper.test.begin('My Test', 1 , function suite(test) {
casper.start().viewport(1600,1000).thenOpen("https://blabla.de", function() {
var mylink = this.getElementInfo("#linkid").text;
this.clickLabel(mylink);
});
casper.waitForSelector("#page2", function() {
this.echo(this.getCurrentUrl());
});
casper.run(function() {
test.done();
});
});
You can get the URL of the current page in CasperJS using one of the following methods:
casper.getCurrentUrl():
Retrieves the current page URL. Note that the URL will be URL-decoded.
casper.page.url:
Accesses the existing PhantomJS WebPage instance (page), and gets the current URL of the web page.
casper.evaluate(function () { return location.href; }):
Evaluates the page URL in the Page DOM Environment.

Instability opening new page Protractor non angular site

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

Protractor E2E Testing Error : Object [object Object] has no method 'getWindowHandle'

I am trying to check the pop up for facebook login opening on click of button .
Error : Object [object Object] has no method 'getWindowHandle'.
Code Snippet generating error :
describe('Tests', function() {
var ptor;
var handlePromise;
var util = require('util');
beforeEach(function() {
ptor = protractor.getInstance();
handlePromise = ptor.getAllWindowHandles();
var handlesDone = false;
ptor.get('/SiteB_Upgrade_Device/app/index.html#/Recommendations#page');
ptor.findElement(by.id('fb')).click();
ptor.ignoreSynchronization = true;
});
describe('login', function() {
return it('should switch to popUp\'s handle', function() {
handlePromise.then(function(handles) {
var popUpHandle = handles[0];
var handle = browser.driver.switchTo().window(popUpHandle).getWindowHandle();
expect(handle).toEqual(popUpHandle);
});
},30000);
});
});
Here is what I currently use to navigate through popups/tabs :
// do stuff that will trigger the popup
// ...
browser.getAllWindowHandles().then(function (handles) {
// switch to the popup
browser.switchTo().window(handles[1]);
// make sure the popup is now focused
expect(browser.getCurrentUrl()).toEqual('popup/url');
// do stuff with the popup
// ...
// go back to the main window
browser.switchTo().window(handles[0]);
// make sure we are back to the main window
expect(browser.getCurrentUrl()).toEqual('original/url');
});
You just need to make sure that your popup is really a new window and not juste some kind of popover ( in which case you can just target it with css selectors ).
Another thing to keep in mind when you change tabs/popups it that the target page might not have angularjs loaded in it, which will render protractor useles. If you face this case you can simply use browser.driver as a replacement for browser to navigate a non angular page.
Hope this helps.

Categories