"driver.switchTo.window() " doesn't help in switching windows/tabs - webdriver

In my test I have four tabs opened in same browser window. I want that I iterate through all
tabs, perform some task in each tab and quit entire session using driver.quit();.
This is what I tried:Pre-Condition: All four tabs are open.
ArrayList<String> windowhandles = new ArrayList<String>(driver.getWindowHandles());
System.out.println("got handles");
for (int i = 0; i<windowhandles.size();i++ ){
System.out.println("inside windowhandles for loop");
driver.switchTo().window(windowhandles.get(i));
System.out.println(driver.getTitle());
/*** perform some tasks ***/
}driver.quit();
Above code will remain at the first tab and print page title of all tabs, without switching to them. Why its not switching, what wrong am I doing?

Related

[WEBDRIVER - JAVA]Check all ELEMENTS on ALL Pages open

Have any method to check alements on ALL pages opened on new tabs?
Like:
WebDriver driverT = new FirefoxDriver()
driverT.get("http://www.x.com.br");
--CHECK ELEMENTS -- OK
--IF FIND ELEMENT, CLICK TO OPEN NEW TAB -- OK
--GO TO OTHER TAB -- OK
Robot robox = new Robot();
robox.keyPress(KeyEvent.VK_CONTROL);
robox.keyPress(KeyEvent.VK_TAB);
robox.keyRelease(KeyEvent.VK_CONTROL);
robox.keyRelease(KeyEvent.VK_TAB);
--CHECK ELEMENTS ON NEW TAB -- // HOW DO THIS?
I just need know how to see elements on page tab opened by first page
Once a new tab (or window) is opened, then you need to tell WebDriver to switch to that window.
Before the new window opens, you will want to get the current window handle of the original window:
String currentHandle = driver.getWindowHanlder();
Then, perform your actions to will click to open the new window. Once the window is opened, you will need to get the window handle of the new window.
for (String handle: driver.getWindowHandles()) {
if (handle != currentHandle) {
driver.switchTo().window(handle);
break;
}
}
Now, WebDriver is focused on the newly opened window, and then you can perform your actions against that window. To switch back to the original window, you can use:
driver.switchTo().window(currentHandle);

Avoid duplicate Tabs in QTabWidget

I have an application, in which I have added a QTabWidget.
Tabs are closable.
When I add new tab, if the tab is already added, it still add new tab and make duplicate.
I want to avoid this duplication.
If the tab is opened already then It just active that tab and not open again.
You help will be appreciated.
Thanks
To add on top of Prakash's answer, be aware that some times the tab title is not a good identifier of the content of the tab (this of course depends on the situation). For example, you might have a file manager where the current directory is the title of the tab, but there might be different directories with the same name across your filesystem.
I would follow the following strategy for identifying tab contents: Qt allows you to set dynamical properties to widgets (see QObject::setProperty), so each time you create a new tab, for example of a file manager, you might do something like
widget = ...
widget->setProperty("tab_dir_fullpath", the_full_path);
tabWidget->addWidget(widget, directory_name);
where the_full_path would be a unique identifier (in this example, the full absolute path to the current directory), which will not be displayed to the user but which you can later use to see if a given directory is already open.
Then, when opening a new tab, you should check whether the same full path is already open:
for (int k = 0; k < tabWidget->count(); ++k) {
if (tabWidget->widget(k)->property("tab_dir_fullpath").toString() == the_full_path_to_open) {
tabWidget->setCurrentIndex(k);
return;
}
}
... // open new tab, as in the previous snippet.
Use tabText(int index) to get the identifier of the each tab before adding a new tab addTab(QWidget * page, const QString & label) and compare the label texts, if already exist just setCurrentIndex of that index or else add a new tab.
Inspired by Noor Nawaz's comment, my approch is:
void MainWindow::openPanel1()
{
for(int i=0;i<ui->tabWidget->count();i++) {
if(ui->tabWidget->tabText(i) == "Panel1") {
ui->tabWidget->setCurrentIndex(i);
return;
}
}
Panel1 = new panel1Widget();
int index = ui->tabWidget->addTab(Panel1,"Panel1");
ui->tabWidget->setCurrentIndex(index);
}
Also its very good to use setTabData() instead of property which is more proper way of doing.

Opening new window in WebDriver using C#

EDIT 4:
EDIT 3
EDIT 2
string currentWindow = driver.CurrentWindowHandle;
driver.SwitchTo().Window("");
string childTitle = driver.Title;
driver.SwitchTo().Window(currentWindow);
string parentTitle = driver.Title;
the above code gives me the same title for parent window or child window.
EDIT:
<a id="ctl00_ctl00_Features_ctl03_lnkPage" class="title" target="_blank" href="websiteaddress">Stay Around</a>
how to verify the title of a newly window open and once i verified then close the opened new window?
so in my page I have a link and click on the link and it opens a new window and now I am not sure how to verify the title of that window.
here is what i have done so far.
GoToMysiteUrl();
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();
//it opens a new window
now i want to switch focus on the new window and verify the title and close the new window
back to the previous window.
The piece that most people miss when dealing with popup windows in IE is that a click on an element is asynchronous. That is to say, if you check the .WindowHandles property immediately after a click, you may lose the race condition, because you're checking for the existence of a new window before IE has had the chance to create it, and the driver has had a chance to register it exists.
Here's the C# code I would use to perform the same operation:
string foundHandle = null;
string originalWindowHandle = driver.CurrentWindowHandle;
// Get the list of existing window handles.
IList<string> existingHandles = driver.WindowHandles;
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();
// Use a timeout. Alternatively, you could use a WebDriverWait
// for this operation.
DateTime timeout = DateTime.Now.Add(TimeSpan.FromSeconds(5));
while(DateTime.Now < timeout)
{
// This method uses LINQ, so it presupposes you are running on
// .NET 3.5 or above. Alternatively, it's possible to do this
// without LINQ, but the code is more verbose.
IList<string> currentHandles = driver.WindowHandles;
IList<string> differentHandles = currentHandles.Except(existingHandles).ToList();
if (differentHandles.Count > 0)
{
// There will ordinarily only be one handle in this list,
// so it should be safe to return the first one here.
foundHandle = differentHandles[0];
break;
}
// Sleep for a very short period of time to prevent starving the driver thread.
System.Threading.Thread.Sleep(250);
}
if (string.IsNullOrEmpty(foundHandle))
{
throw new Exception("didn't find popup window within timeout");
}
driver.SwitchToWindow(foundHandle);
// Do whatever verification on the popup window you need to, then...
driver.Close();
// And switch back to the original window handle.
driver.SwitchToWindow(originalWindowHandle);
Incidentally, if you're using the .NET bindings, you have access to a PopupWindowFinder class in the WebDriver.Support.dll assembly, which uses a very similar approach to the locating popup windows. You may find that class meets your needs exactly, and can use it without modification.
GoToMysiteUrl();
IWebElement addtoList = driver.FindElement(By.XPath(_pageName));
addtoList.Click();
// Post above operation a new window would open as described in problem
// Get hold of Main window's handle
string currentWindow = Driver.CurrentWindowHandle;
// Switch to the newly opened window
Driver.SwitchTo().Window("Your Window Name");
// Perform required Actions/Assertions here and close the window
// Switch to Main window
Driver.SwitchTo().Window(currentWindow);

Qt: two actions in menu (with the same text)

I create a menu dynamically. I add several checkable actions into one menu. Sometimes actions may have the same text that user sees. It's up to user (actually user adds commands into menu).
The problem is in this case clicking works wrong. If I click on the first action (from 2 with the same texts) everything is good but if I click on the second one, both actions are selected. I don't understand why. The code where actions have been created is here:
for (int i = 0; i< currentList.size(); i++)
{
QString lanKey = currentList.at(i)->Language->toString();
QAction* lanAction = new QAction(this);
QString name ="action_" + currentList.at(i)->Id->toString();
lanAction->setObjectName(name);
lanAction->setText(lanKey);
lanAction->setCheckable(true);
lanAction->setData(i);
connect(lanAction, SIGNAL(triggered(bool)), this, SLOT(ShowSomething(bool)));
ui->menuMy->addAction(lanAction);
}
Here, lanKey is language that may be the same for different actions. Anyway click on the specific action should lead only to checking of this action. What's wrong?
The slot is here:
void VMainWindow::ShowSomething(bool IsTriggered)
{
QAction* senderAction = (QAction*)sender();
int listIndex = senderAction->data().toInt();
if (IsTriggered)
{
CreateEditor(subtitles, listIndex);
}
else
{
//hide this editor
QString name = "editor" + editorsList->Id->toString();
QDockWidget* editorDock = this->findChild<QDockWidget*>(name);
if (editorDock != 0)
{
this->removeDockWidget(editorDock);
this->setLayout(layout());
}
}
}
Thanks
The source of problem is found: it turned out that the slot finds the checked action wrong - by text, not by id.
I can't find a logical issue in the code you posted so far. Here are a couple of options which I would try in order to resolve this problem:
Limit the users possibilities when adding items to a menu so that he can't add two items with the same name.
Add qDebug() output to ShowSomething to see if there is a problem with signals&slots. For example, if the slot gets called once for the first item but twice for the second item there is a problem there.
Debug into CreateEditor step-by-step.
As the problem seems to appear only for actions with a similar name, you should make sure that you never make a lookup of an action (or something related) by its text() but rather by its data() or objectName() (assuming that currentList.at(i)->Id will always be unique)

How do I detect whether a window is hidden (cmd-h) vs closed (cmd-w) - Mac

I'm developing an AIR application which uses multiple windows. I'm running into an issue where I want to open new windows (toaster notifications for example) when the primary application window is not visible, but the behavior is different depending on how the window is closed.
When a user hides all application windows with CMD-H, opening a new window causes all application windows to come back to the foreground (instead of just that new window, like I would expect). If the user closed a window with CMD-W, however, that window does not become visible when I open a new window.
Is there a way to either 1) tell when the user uses cmd-h to hide all windows OR 2) tell whether a window is hidden using cmd-h vs. closed cmd-w?
Thanks
I actually just figured out a good answer to this problem. Apparently, the reason cmd-H and cmd-W don't trigger keyDown events are because they are capturee and stopped by the native application menu.
By default, several "normal" mac OS menu options are put into AIR applications by the framework - these include cmd-w to close the window, cmd-h to hide and shortcuts around copy/cut/paste. In order to avoid the default behavior, I either removed these menu options or changed their key equivalents (the shortcut combination that triggers them).
The code to add a preferences shortcut (cmd-,), override cmd-w, change cmd-w to cmd-shift-w, and override the cmd-h functionality looks like this:
if (NativeApplication.supportsMenu) {
var prefItem:NativeMenuItem = new NativeMenuItem("Preferences...");
prefItem.addEventListener(Event.SELECT, handlePreferencesMenuSelect);
prefItem.keyEquivalent = ",";
var closeItem:NativeMenuItem = new NativeMenuItem("Close Tab");
closeItem.addEventListener(Event.SELECT, handleCloseTabMenuSelect);
closeItem.keyEquivalent = "w";
// Add the preferences option under the first menu
// Also add a spacer line (like most other applications)
// Also change the hide command to our own handler
var baseMenu:NativeMenuItem = NativeMenuItem(NativeApplication.nativeApplication.menu.items[0]);
baseMenu.submenu.addItemAt(new NativeMenuItem("", true), 1);
baseMenu.submenu.addItemAt(prefItem, 2);
for (var idx:String in baseMenu.submenu.items) {
var menuItem:NativeMenuItem = baseMenu.submenu.items[idx];
if (menuItem && menuItem.keyEquivalent == 'h' && menuItem.keyEquivalentModifiers.length == 1) {
baseMenu.submenu.removeItemAt(int(idx));
var hideItem:NativeMenuItem = new NativeMenuItem("Hide Application");
hideItem.addEventListener(Event.SELECT, handleHideWindowSelect);
hideItem.keyEquivalent = "h";
baseMenu.submenu.addItemAt(hideItem, int(idx));
}
}
// Set the close window shortcut to cmd+shift+w, instead of cmd+w
var fileMenu:NativeMenuItem = NativeMenuItem(NativeApplication.nativeApplication.menu.items[1]);
NativeMenuItem(fileMenu.submenu.getItemAt(0)).keyEquivalent = 'W';
fileMenu.submenu.addItem(closeItem);
}
Thanks for the help figuring it out.

Resources