Im trying to automate a Web Application through Selenium. But at a point Im getting stuck. Problem is that when I open a page and click on Search (to load all the existing saved data), a new window opens where the F12 option is not working neither can I right click on that window and select view source. If I could get the source of that window, I can use window handle. Can anybody help me to resolve this?
In my project I use something like this to a pop-up window:
public void switchToWindowOtherThan(String windowHandle) {
try {
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(windowHandle)) {
driver.switchTo().window(handle);
}
}
} catch (NoSuchWindowException nwe) {
//handle exception
}
}
In my code I would use it as follows:
switchToWindowOtherThan(driver.getWindowHandle());
Related
I am trying to achieve the below flow in selenium ,
click on a link to open new tab(child1) from parent window
close the driver (child1) after performing certain actions
Open the same link(child1) again to perform another set of actions
I am able to achieve the first two steps successfully by switching focus. But I am stuck at the third step where I am not able to focus on the same reopened tab. I get the below error,
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
public class abc {
String currentWindow = driver.getWindowHandle();
public void action1() {
//Click on main menu that open a new tab
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(currentWindow)) {
driver.switchTo().window(handle);
}
//Perform the actions
driver.close();
driver.switchTo().window(currentWindow);
}
public void action2(){
//Click on main menu which reopen the same tab
for (String handle : driver.getWindowHandles()) {
if (!handle.equalsIgnoreCase(currentWindow)) {
System.out.println("Port Response : switch focus");
driver.switchTo().window(handle);
break;
}
}
//perform a set of actions
driver.close();
driver.switchTo().window(currentWindow);
}
}//end of abc
I want to create the actions more like a reusable independent action so I would like to close all tabs and reopen them again for other set of action. Please let me know if you need any more details on this.
I am trying to write an Eclipse plugin where one of the features requires listening for when the user switches to another file in the editor via clicking.
For example, consider the screenshot below.
I want to know how to listen for when the user switching over to FakeClass.java via double-clicking on it in the Project Explorer or clicking on the tab in the editor. Furthermore, I would like to get information about the element that was clicked. Note that I am asking specifically about changing a file through the two means I asked above.
I am a beginner with Plugin development. It would be helpful to explain with that in mind. Thanks.
You can use an IPartListener to listen for changes to parts including a part being activated:
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.addPartListener(listener);
The partActivated method of the listener is probably what you want:
#Override
public void partActivated(final IWorkbenchPart part)
{
if (part instanceof IEditorPart) {
IEditorPart editor = (IEditorPart)part;
IEditorInput input = editor.getEditorInput();
IFile file = input.getAdapter(IFile.class);
if (file != null) {
// TODO handle file
}
}
}
I don't know of a way to tell why the part was activated.
I have the following in my code:
withWindow({ title == 'Google' }) {
report "08"
}
And report is leading me to the exception NoSuchWindowException. I've checked if that was a problem of the Window selector and it isn't,After some research I guessed that the problem was that the driver got messed in the way so I stored and switched my driver:
String mainHandle= driver.getWindowHandle();
driver.switchTo().window('Google');
But I kept getting the same error. So I tried:
driver.get("http://www.google.com");
And it is working but I need to do this dinamycally and automatically because the windows and popups that we are working with are hundreds and with different titles.
How can I achieve that in every windows that opens? We are generating the code with a external tool so I don't need to do "magic", only a driver.get.windowUrl or something like this will work for me, I will add the concurrence later.
If you want to get the last window handle that is opened, you can do something like this:
public String getLastWindowHandle() {
Set<String> windows = webDriver.getWindowHandles();
Iterator<String> itera = windows.iterator();
String window = null;
while (itera.hasNext()) {
window = itera.next();
}
return window;
}
with this handle, you can switch to the new window:
webDriver.switchTo().window(getLastWindowHandle());
While testing on IE11, When I click on the link to download a doc, a new blank window opens up with save and Cancel option popup. I want to hit cancel, Switch back to my current window and continue validation.
Can anyone help me how to handle this without the use of AutoIT.
If you are using selenium-webdriver with Java then you can use Sikuli-api.
Just a small introduction: Sikuli is a tool which helps to achive automation task for any software(web/standalone). Base of Sikuli is a Screenshot of the control you would like to interact with.
In your case, it is just matter of 2 buttons. Hence I would not suggest you to use seperate autoIT generated *.exe file.
Just take screenshot of OK & Cancel button.
Showing you the sample code here:
import org.sikuli.script.*;
public class Test {
Screen m_screen;
SikuliScript m_sikscr;
#Test
public void Test1() throws FindFailed
{
m_screen=new Screen();
m_screen.wait((double) 10.0);
//Click on Cancel button
m_screen.click(new Pattern("./img/CancelButton.png"));
}
}
This much should do your task.
Please Note, if at all you decide to use this method, make sure that you handle all Sikuli related dependencies in java project.
Try manually at first whether by pressing escape key it dismissing the popup.
if so follow the below code,
Robot r = null;
try {
r = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
r.keyPress(KeyEvent.VK_ESCAPE);
I have 2 browsers open and Selenium Webdriver can switch between these two. One window is in foreground and other is in background. And in the workflow, a modal dialog opens up in the background window and thus webdriver cannot perform any actions on it. Is there any possible solution apart from getting the background window into foreground?
I am using C#.
Loop through your window handles and check for you modal dialog to appear.
string current_window = driver.CurrentWindowHandle;
foreach (string window in driver.WindowHandles)
{
driver.SwitchTo().Window(window);
if (GetModal())
{
//do actions here
break;
}
}
driver.SwitchTo().Window(current_window); //To put you back where you started.
private bool GetModal()
{
Try
{
IWebElement modal = driver.FindElementByXPath("");
return true;
}
catch
{
return false;
}
}
Based on what you put this should work. If you can't find the modal then there is probably a different issue than just the window not being in focus. If you are worried about other errors then I would say catch only the specific error in the catch and let everything else float up ElementNotFound exception.
I am using below code
try{
//your code which will generate Modal Dialog
} catch (Exception e) {
if (e.getMessage().contains("Modal dialog present")) {//For Handling modal dialog in firefox
(new Robot()).keyPress(java.awt.event.KeyEvent.VK_ESCAPE);
(new Robot()).keyRelease(java.awt.event.KeyEvent.VK_ESCAPE);
}else if(e.getMessage().contains("unexpected alert open")){//For Handling modal dialog in chrome
driver.switchTo().alert().accept();
}
}