var host = 'www.google.com';
module.exports = {
'some test': function(client) {
client
.url(host)
.waitForElementVisible('body', 1000)
.execute(function(newWindow){
window.open(newWindow, null, "height=1024,width=768");
}, [host])
.waitForElementVisible('body', 1000)
.window_handles(function(result) {
var temp = result.value[0];
this.switchWindow(temp);
})
.end();
}
};
Doesn't work with result.value[0] or result.value[1].
It's because window.open() needs the protocol, see https://developer.mozilla.org/en-US/docs/Web/API/Window/open.
If you assign var host = 'http://www.google.com' it works.
Thanks for the reply!
I'm having trouble using .switchWindow() to toggle between the windows when both are open.
The code above opens a new window, and the result inside the callback of .window_handles shows two unique browser handles. .switchWindow() doesn't seem to be working, though.
Maybe if you can say more about what you are trying to achieve I can help you better.
var host = 'http://www.google.com';
module.exports = {
'some test': function(client) {
client
.url(host)
.waitForElementVisible('body', 1000)
.execute(function(newWindow){
window.open('http://www.twitter.com', null, "height=1024,width=768");
}, [host])
.assert.urlContains('google')
.window_handles(function(result) {
var temp = result.value[1];
this.switchWindow(temp);
})
.assert.urlContains('twitter')
.end();
}
};
I've modified your example opening twitter in the second window to show you that the switch is working.
Awesome, the http:// protocol did it. Cheers!
Hi,
Thanks to both of you.
Can anyone please guide me how to use both Windows?
Thanks,
Hema
Hey Hema,
I think the example is pretty straight forward.
What have you got unclear?
For e.g.: 1) A window 2) B window
The above code is used to,
Thanks,
Hema
In the callback passed to window_handlesyou got the result variable, if you select value[0] you have the main window.
client.window_handles(function(result) {
var temp = result.value[0];
this.switchWindow(temp);
})
Thank you sirLisko.
It is working now. One more thing is, If we want to switch between 2 windows do we need to write this code for before every switchWindow?
client.window_handles(function(result) {
var temp = result.value[0];
this.switchWindow(temp);
})
Once again Thanks for helping.
Thanks,
Hema
I created two custom commands: switchToPrimaryWindow and switchToSecondaryWindow.
exports.command = function() {
var browser = this;
browser.windowHandles(function(result) {
browser.switchWindow(result.value[0]);
});
return browser;
};
Alternatively you can have one custom command and pass as a parameter the number of the window that you want to access.
Yes, I just found and did it.
Its working. Thank you.
Thanks,
Hema
Cool, I am glad it worked :)
Cheers,
Luca
@sirLisko I am facing another issue regarding switchWindow i.e. after switching to new window , none of the methods are working (elementPresent , click etc) except 'urlContains'.
Partial code is as below.
what could be the problem ?
Thanks
.click(".//*[@id='j_id0:jobForm:pb:render:j_id38:0:j_id39_lkwgt']")
.pause(10000)
.window_handles(function(result) {
this.verify.equal(result.value.length, 2, '2 windows should be open')
var handle = result.value[1]
this.switchWindow(handle)
this.verify.urlContains('https://invoiceit-s.na30.visual.force.com/_ui/common/data/LookupPage?lkfm=j_id0%3AjobForm&lknm=j_id0%3AjobForm%3Apb%3Arender%3Aj_id38%3A0%3Aj_id39&lktp=001&lksrch=')
this.useXpath()
this.waitForElementVisible(".//*[@id='lksrch']", 20000)
})
.pause(20000)
.end()
}
Hi @maharshioza,
I think the problem is because you are executing the assertion in window_handles, so the value of this is referenced to that scope and not the main one.
Try this instead:
.click(".//*[@id='j_id0:jobForm:pb:render:j_id38:0:j_id39_lkwgt']")
.pause(10000)
.window_handles(function(result) {
this.verify.equal(result.value.length, 2, '2 windows should be open')
var handle = result.value[1]
this.switchWindow(handle)
})
.verify.urlContains('https://invoiceit-s.na30.visual.force.com/_ui/common/data/LookupPage?lkfm=j_id0%3AjobForm&lknm=j_id0%3AjobForm%3Apb%3Arender%3Aj_id38%3A0%3Aj_id39&lktp=001&lksrch=')
.useXpath()
.waitForElementVisible(".//*[@id='lksrch']", 20000)
.pause(20000)
.end()
Let me know if this resolve your problem :)
@sirLisko Getting same Error. Is there any method to check DOM is rendered or not/ Page is fully loaded or not ?
Any other suggestion?
You can wait for the body .waitForElementVisible('body', 1000).
If you can provide a little playground I can try to give a better look at the problem :)
I am working with salesforce CRM UI.
Trying to create order in platform.
This is my code. For simplicity I changed the name of Xpath.
'Order Module' : function(browser) {
browser
.useXpath()
.waitForElementVisible("@OrderTab", 20000)
.pause(5000)
.click("@OrderTab")
.useCss()
.waitForElementVisible("input[title = 'New']", 20000)
.click("input[title = 'New']")
.useXpath()
.waitForElementVisible("@TextBox']", 20000)
.verify.urlContains('PrimaryPageUrl')
.setValue("@TextBox", "Order Name")
.click("@LinkForSecondaryPage")
.pause(5000)
.window_handles(function(result) {
this.verify.equal(result.value.length, 2, '2 windows should be open')
var handle = result.value[1]
this.switchWindow(handle)
})
.verify.urlContains('@SecondaryPageUrl')
.useXpath()
.waitForElementVisible("@TextBoxOnSecondaryPage", 20000)
.closeWindow()
.pause(2000)
.end()
}
}
Stupid question, are you using url?
Because for what I can see from this test you are not going anywhere. If so can you provide the full test otherwise I can't reproduce it.
Ok, I got your problem. The tests are independent, so in the second you are not accessing any page.
Every test starts from scratch, so everytime you have to load a page with url. If you want to avoid this you can create a custom command to avoid to repeat all the time the login process.
I hope this can help you, let me know :)
Thanks @sirLisko :)
Cheers mate, I am happy you fixed it π
As I said in my previous comment you are executing two different tests and in the second one you are not loading any page.
The problem is that I can't reproduce your example because salesforce is asking me a verification code.
Try to merge them in a single test:
module.exports = {
'test123' : function (browser) {
browser
.url('http://login.salesforce.com')
.waitForElementVisible('body', 20000)
.maximizeWindow()
.setValue("input[name='username']", '[email protected]')
.setValue("input[name='pw']",'Venugopal12')
.click("input[type = 'submit']")
.pause(1000)
.waitForElementVisible("#setupLink",20000)
.useXpath()
.waitForElementVisible(".//*[@id='01r36000000RX00_Tab']/a", 20000)
.pause(5000)
.click(".//*[@id='01r36000000RX00_Tab']/a")
.useCss()
.waitForElementVisible("input[title = 'New']", 20000)
.click("input[title = 'New']")
.useXpath()
.waitForElementVisible(".//*[@id='j_id0:jobForm:pb:render:j_id37']", 20000)
.verify.urlContains('https://invoiceit-s.na30.visual.force.com/apex/createJobStep1?retURL=%2Fa0K%2Fo&save_new=1&sfdc.override=1')
.setValue(".//*[@id='j_id0:jobForm:pb:render:j_id37']", "Order Name")
.click(".//*[@id='j_id0:jobForm:pb:render:j_id38:0:j_id39_lkwgt']")
.pause(5000)
.window_handles(function(result) {
this.verify.equal(result.value.length, 2, '2 windows should be open')
var handle = result.value[1]
this.switchWindow(handle)
})
.verify.urlContains('https://invoiceit-s.na30.visual.force.com/_ui/common/data/LookupPage?lkfm=j_id0%3AjobForm&lknm=j_id0%3AjobForm%3Apb%3Arender%3Aj_id38%3A0%3Aj_id39&lktp=001&lksrch=')
.useXpath()
.waitForElementVisible(".//*[@id = 'lksrch']", 20000)
.pause(2000)
.end()
}
}
okay. I merged it but still is not working.
Is there any bug in window_handle protocol or switchWindow method ?
Thanks for your help @sirLisko :)
No idea. I suggest you open a new ticket because here I guess I am the only one reading :)
@sirLisko Cheers, Its working now.
In new page, there are 2 frames that I didn't notice. Switch to frame results to solution.
Thanks. :) btw It took 3 days to get solution.
Cool, I am happy you solved the problem, better late than never :D
@sirLisko Thanks a lot for your help here. I have a problem locating an element in my test but i think it's related to losing focus of my current window. Not sure if you can help but I will paste my code here. Maybe im doing something wrong.
Basically for some reason after I click the class all_message-min, I can't locate any elements in the screen and I can't figure out why. Any help would be appreciated!
module.exports = {
'THEN My email is verified': function (client) {
var URL1 = 'https://www.mailinator.com/';
var email = '[email protected]';
var WINDOW1 = 'wMail';
//Opens new window
client
.execute(function (url1, window1) {
window.open(url1, window1, "height=1024,width=768");
}, [URL1, WINDOW1])
//Focus on the new window, without this the driver gets lost in the wrong one and won't find the elements
.switchWindow(WINDOW1)
.waitForElementVisible('body', 2000)
.verify.title('Mailinator')
.setValue('#inboxfield', email)
.click('button.btn.btn-dark')
.waitForElementVisible('body',3000)
.verify.visible('#query_data > div:nth-child(4)')
.verify.containsText('#query_data > div:nth-child(4)',email)
.click('.all_message-min')
.useXpath() // every selector now must be XPath
.waitForElementVisible('//a[text()="Confirm Account"]',2000) //HERE IS WHERE NIGHTWATCH GETS LOST! .click('//a[text()="Confirm Account"]')
.useCss() // we're back to CSS now
},
};
Hey @sparda2,
I check your test and the problem is that the message is loaded inside an iframe.
So what you need to do is accessing that iframe via .frame('msg_body').
.click('.all_message-min')
.frame('msg_body') // You need this
.useXpath()
.waitForElementVisible('//a[text()="Confirm Account"]',2000)
.click('//a[text()="Confirm Account"]')
Let me know how it goes :)
Thanks @sirLisko!,
I figured it out yesterday and yes that was the fix! Thanks a lot for taking the time to reply though. I will probably bother you soon about dealing with multiple windows anyway. Thanks again!
@sparda2 I have the same problem with you. Do you have any solution? I tried frame and I couldn't solve it.
@sirLisko Do you have any solution? I tried frame and I couldn't solve it. Or I'm using the wrong frameοΌ
@SIJIACAN can you show us some code? I think it's easier π
@sirLisko
I had the same question as @HLarul --having two windows A and B, doing something on window B then going back to window A. I tried to do the same thing by switch window twice, but always got error for the second one. Could you please help me out? My code looks like this:
client.windowHandles(function(result) {
var popUp= result.value[1];
this.switchWindow(popUp);
})
//doing something on window(popUp)
//then I want to switch back to original window
client.windowHandles(function(result) {
var original= result.value[0];
this.switchWindow(original); <----Error while running "switchWindow" command: "TypeError: client.api[method] is not a function"
})
@bluemoon711 hard to tell without an example I can run, especially now that I am not using Nightwatch on a daily basis π
Just from the top of my head, did you try to use client.switchWindow instead this.switchWindow?
If you have some actual code I can give it a better look :)
@sirLisko I reused your previous code for example.
var host = 'http://www.google.com';
module.exports = {
'some test': function(client) {
client
.url(host)
.waitForElementVisible('body', 1000)
.execute(function(newWindow){
window.open('http://www.twitter.com', null, "height=1024,width=768");
}, [host])
.assert.urlContains('google')
.windowHandles(function(result) {
var temp = result.value[1];
this.switchWindow(temp);
})
.assert.urlContains('twitter') <---working!
.windowHandles(function(result) {
var original = result.value[0];
this.switchWindow(original); <---failed! Error
})
.assert.urlContains('google')
}
};
@bluemoon711 I just ran a test with the code you provided βοΈand it's working as expected:
Starting selenium server... started - PID: 96220
[Prova] Test Suite
======================
Running: some test
β Element <body> was visible after 172 milliseconds.
β Testing if the URL contains "google".
β Testing if the URL contains "twitter".
β Testing if the URL contains "google".
OK. 4 assertions passed. (10.186s)
I start thinking that probably you have a problem with your setup π€
I am using selenium-server-standalone-3.14.0 and nightwatch v0.9.21.
I don't know if it can help, but I am using https://github.com/sirlisko/nightwatch-ready to run the tests.
It could be a bit outdated but you can try to see if it's working with a different configuration.
I hope it could help :)
Thanks a lot!! @ @sirLisko Yes, it is from the configuration!!
That is what I run with selenium-server-standalone-3.13.0
Running: some test
β Element
was visible after 31 milliseconds.FAILED: 1 errors and 3 passed (2.767s)
Guess what, after updating to selenium-server-standalone-3.14.0, all the tests passed!!
I have created the nightwatch to open the application, click on a help link which opens the user guide. If i open the link myself, userguide opens in new tab, while if nightwatch opens it or if i open from window launched by nightwatch, userguide is opened in new window. Quiet strange.
My main target is to focus on the new window and verify the URL in new window but focus is not going to new window but actions still keep performing on the main window.
I have used switch Window for result.value[1]
Please help on my query, im fully stuck with the switch window, it doesnt look to be working
@shreev1
If i open the link myself, userguide opens in new tab, while if nightwatch opens it or if i open from window launched by nightwatch, userguide is opened in new window.
This usually cause by the code, (depend on if you have the _target set to in the HTML, and you maybe using Firefox?
Please help on my query, im fully stuck with the switch window, it doesnt look to be working
maybe you can try to maximum the new window before you do the check.
Also check if you result.value have 2 values. (AFAIK, the windows_hanldes doesn't care it is window or tabs, it will have unique ID assign for each window/tabs)
.window_handles(function(result) {
console.info('[info]' + result.value);
})
Most helpful comment
Thanks a lot!! @ @sirLisko Yes, it is from the configuration!!
That is what I run with selenium-server-standalone-3.13.0
Running: some test
β Element
was visible after 31 milliseconds.β Testing if the URL contains "google".
β Testing if the URL contains "twitter".
Error while running "switchWindow" command: "TypeError: client.api[method] is not a function"
at NightwatchAPI.
at process._tickCallback (internal/process/next_tick.js:68:7)
FAILED: 1 errors and 3 passed (2.767s)
Guess what, after updating to selenium-server-standalone-3.14.0, all the tests passed!!