How do I interact with a popup window with Mink, Selenium 2, and Behat? - selenium

I am running through an internal site with Behat and for the most part it is going really well. But the problem is that on certain parts of the site we have popup windows that come up to complete an action. In this case we hit a "Withdraw" button and a popup comes up to have you select a reason and save it.
In an ideal world, and if I had actually designed this app, the site wouldn't be using any popup windows. But I am the new guy who is supposed to implement automated functional tests (and I am learning how to do that from the ground up). So I don't really have any say over the site design at this point (though I will push for a lot of changes as time goes by).
I am running Behat with Mink and the Selenium 2 driver on an Ubuntu 12.10 system (will eventually have to run some tests on a Windows environment for testing in IE). I am also using PhantomJS for some of the tests I have setup.
Anyway, does Behat/Mink support working with popup windows somehow through the Selenium 2 driver (or through PhantomJS)? I am early in all of this automation setup and really I am just experimenting with tools. If there is a better tool that can handle this then please let me know.
My primary question is how do I get Behat/Mink to work with the popup window, check a box, fill in a field, and click the save button? I know how to do everything except get it to interact directly with the newly popped up window. Any ideas/suggestions would be welcome.
Thanks!

So it turns out that Mink includes some window switching features, but no way to identify said windows. So I wrote two functions getWindowName() and getWindowNames() that identify the current window and all open windows respectively. I committed these changes to the project in GitHub it seems that my fixes will get implemented soon into the code base.
But with these changes I am able to switch windows no problem.
Link: https://github.com/Behat/Mink/pull/341

By setting the focus of the window we can also name these windows so we can access them again in the future.
Using this method we can easily switch between popup windows and continue testing...
/**
* #Then I switch to popup :name
*
* #param $name
*/
public function iSwitchToPopup($name)
{
$this->iSetMainWindowName();
$this->getSession()->switchToWindow($name);
}
/**
* #Then I set main window name
*/
public function iSetMainWindowName()
{
$window_name = 'main_window';
$script = 'window.name = "' . $window_name . '"';
$this->getSession()->executeScript($script);
}
/**
* #Then I switch back to main window
*/
public function iSwitchBackToMainWindow()
{
$this->getSession()->switchToWindow('main_window');
}

Related

Sending automated text messages from web.skype.com, using Selenium

I’m trying to send automated text messages from Skype web client, using Selenium.
So far, I was able to authenticate myself and select the required unique recipient. It works fine both programmatically (Java bindings) and using Selenium IDE.
But I was not able to successfully add text to the message box. And without that, Skype does not even shows the send button!
In a recent past, I googled that one could send a message with something like:
input_messages = browser.find_element_by_name('messageInput')
input_messages.send_keys('Testing Output' + Keys.RETURN)
But I guess Microsoft changed the web client as this does not work anymore, namely by not showing the “Send” button unless something is written in the message box:
Using Selenium IDE, it records this set of commands (Skype authentication omitted):
However, running above script, I get what seems to be an overlapped “Test” string on top of original “Type a message”:
It seems obvious to me that Selenium IDE is not placing my “Test” string in the “right” place. And because of that, not even the send button is shown.
Here’s the “edit content” parameters:
Target
css=.notranslate
xpath=//div[2]/div[2]/div/div/div/div/div/div/div/div[2]/div/div/div/div[2]/div/div/div
Value
<div data-contents="true"><div class="" data-block="true" data-editor="f244q" data-offset-key="0-0-0"><div data-offset-key="0-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="0-0-0"><span data-text="true">Test</span></span></div></div></div>
And the Chrome Inspect of the message box:
So, any idea what Selenium IDE (or myself…) is doing wrong?...
What is the right locator and how to edit its content (to set my text message), making sure the send button is then shown and clickable (though a Keys.ENTER should then suffice).
Thank you in advance!
Windows 10 - 1903 (64-bit)
Google Chrome 85.0.4183.102 (Official Build) (64-bit)
Selenium IDE 3.17.0
chromedriver_win32-85.0.4183.87
selenium-server-standalone-3.141.59.jar
I have encountered similar problems and tried many methods:
clear the element before sendkeys
sendkeys multiple times in for-loop
I can’t tell which method is effective, so I will give you a code snippet in my real project.
// for the div element
boolean bl = false;
for (int i = 0; i < 3; i++) {
element.clear();
element.sendKeys(text);
if (element.getText().equals(text)) {
bl = true;
break;
}
}

Appium/WinAppDriver Can't Find Context Menu - But Only On Certain Machines

I'm running a set of automated UI tests using Appium/Winappdriver on Windows 10. The test framework is compiled in Visual Studio 2017 using mstest.
The problem that I am having is with tests that use a right-click to open a context menu, then select an element from the resulting menu. Locally, it works. It also works on our remote CI/CD machine. However, it does not work for the other two developers on the project, and we've spent two business days fruitlessly trying to figure out why.
We have the same Windows version (Windows 10, version 1903), we have the same Visual Studio 2017 (we also tried it with 2019, no luck), we have the same monitor resolution (1920 x 1080), we are targeting the same .NET framework (4.72), we have the same WinAppDriver, etc.
Everything else works just fine. But when the UI Test reaches that context menu, the test fails with the error "An element could not be located on the page using the given search parameters."
I used the WinAppDriver UI Recorder to find the XPath for the element. We also used it on the other user's machine and confirmed that, as far as the UI Recorder is concerned, the path is identical on both machines.
The specific call that fails:
Session.FindElementByXPath("/Pane[#ClassName=\"#32769\"][#Name=\"Desktop 1\"]/Menu[#ClassName=\"#32768\"][#Name=\"Context\"]/MenuItem[#Name=\"" + itemName + "\"]");
The WinAppDriver call on my machine (success):
{"using":"xpath","value":"/Pane[#ClassName=\"#32769\"][#Name=\"Desktop 1\"]/Menu[#ClassName=\"#32768\"][#Name=\"Context\"]/MenuItem[#Name=\"New Location\"]"}
HTTP/1.1 200 OK
Content-Length: 125
Content-Type: application/json
{"sessionId":"8970FDC1-E869-4304-A87D-D8F2CB711EA2","status":0,"value":{"ELEMENT":"42.856234.4.-2147483646.8140.18614751.1"}}
and the same call on the other user's machine (fail):
{"using":"xpath","value":"/Pane[#ClassName=\"#32769\"][#Name=\"Desktop 1\"]/Menu[#ClassName=\"#32768\"][#Name=\"Context\"]/MenuItem[#Name=\"New Location\"]"}
HTTP/1.1 404 Not Found
Content-Length: 139
Content-Type: application/json
{"status":7,"value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters."}}
Again, everything else works. Other UI tests that don't use the right-click context menus work just fine. It's only this particular area that fails.
What I've tried so far:
Using Thread.Sleep to force a long wait before making the call
Wrapping the call with a DefaultWait and polling it over a period of several seconds to see if the element becomes available during that time.
When the "An element could not be located" is thrown, retry up to a set number of times to find the element.
Lots and lots of double-checking to make sure we're both on the same version of the code, same libraries, same nuget packages, etc.
Trying a much broader locator ( Session.FindElementByName(itemName); )
The biggest head-scratcher is that when we check with UI Recorder, the element is there. When we check on my machine or the remote build machine, WinAppDriver can find it normally. But for some reason WinAppDriver can't find it on my coworker's machines.
This is a peculiar issue indeed.
I'd like to rule out the XPath selector as a potential problem here. Based on your syntax, it looks like you are using an absolute XPath. These can be extremely brittle depending on the circumstances. Not saying it's the root problem, but I would like to try a different selector to rule this out.
{"using":"xpath","value":"//MenuItem[#Name=\"New Location\"]"}
Using relative // notation tells your path to look anywhere on the page, rather than following a specific path down to the element itself.
Give this a try, and let me know if it helps at all.
For my application context menu is listed out of the DOM of actual application in inspect.exe. So switching back to desktop session after selecting the context menu worked fine for me.
var regressionChannelRow = labelProcessorSession.FindElementByName("5000");
Actions action1 = new Actions(labelProcessorSession);
regressionChannelRow.Click();
action1.ContextClick(regressionChannelRow).Perform();
Now creating a desktop session to get the "Stop" option from the context menu
AppiumOptions appCapabilities = new AppiumOptions();
appCapabilities.AddAdditionalCapability("app", "Root");
WindowsDriver<WindowsElement> desktopSession;
desktopSession = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), appCapabilities);
below is the context menu option which I need to select, remember to use desktop session here
var stopService = desktopSession.FindElementByName("Stop");
stopService.Click();
I've just replicated this issue. I was working on a test that I wrote last week, which was now getting stuck trying to find the context menu from a desktop session. I tried using various XPaths, searching by class name or just name, but it didn't seem to make any difference.
Eventually I tried closing Spotify, and that solved the issue! If you're experiencing this problem then try closing every application window possible.

Controlling level and focus of windows other apps with CGPrivate functions

Question
How to use these private functions on other windows? It would be nice to have this knowledge back in the wild. I am specifically trying to get CGSOrderWindow and CGSSetWindowLevel to work.
I was trying in the direction of:
temporarily register as the dock and then register the dock as the dock again immediately afterwards
or
code injection into the Dock process per this comment:
Also, the author of the above project seems determined to make all core functionality available as a framework. It seems to be implemented as code injection into the Dock process.
Reason I know this is possible
I have been doing work on trying to setLevel on window of another app, and focus window of another app if focused. I am posting this again with the info I learned because from my searching online, I know this was done in the past, its just the knowledge is not publicly out there anymore. The sourceforge pages are no longer there. So I was wondering if you could help me make this information public again.
This is the topic I read that gave me this information - http://cocoadev.com/HowtoControlOtherAppsWindows
Here you see comments like:
You cannot control an another app's windows from a user-level process, unfortunately.
SlavaKarpenko
You can, Slava, you just need to register as the Dock. It might be possible to temporarily register as the dock and then register the dock as the dock again immediately afterwards, not sure. I think the call you'd be wanting to investigate as CoreDockRegisterDockOwner in HIServices.framework.
FinlayDobbie
You could also use APE or similar to do control the windows, or (as mentioned above) register as the Dock (look for the private APIs with Universal Connection in their name). Has anyone found a polite way of getting the Dock to give up its universal connection? The only way I can find is to force quit the Dock and grab the universal connection when it's not looking (which prevents the dock reloading).
SamTaylor
There's an open source project up on sourceforge.net that looks much more like the window managers I've used on Unix boxes than Space.app (or Space.dock): http://wsmanager.sourceforge.net/
SteveCook
Verifying things work
This is what I learned, from the sources at bottom of this post, we see all these functions work with CGWindowIds, so how do I get that, this is how:
Get all windows with CGWindowListCopyWindowInfo. Then access each element from that array with CFArrayGetValueAtIndex and then get the CGWindowId with objectForKey:, kCGWindowNumber, and then integerValue.
Now if I try to focus or set level of a window that is OWNED by the app running the code, it works fantastic. For instance:
MY_TARGET_CGWINDOW_ID = 179;
rez_CGError = CGSOrderWindow(_CGSDefaultConnection, MY_TARGET_CGWINDOW_ID, kCGSOrderAbove, 0);
Will focus it, rez_CGError is 0. Even if the window is minimized, it is unminimized, without animation, and shown.
Now however, if I try this on a window of a different app we get some errors:
MY_TARGET_CGWINDOW_ID_of_other_app = 40;
rez_CGError = CGSOrderWindow(_CGSDefaultConnection, MY_TARGET_CGWINDOW_ID_of_other_app, kCGSOrderAbove, 0);
This fails and rez_CGError is 1000, which I suspect means "cid (CGSConnection) used does not have permission to modify target window". The same happens if I first do [app activateWithOptions: (NSApplicationActivateIgnoringOtherApps | NSApplicationActivateAllWindows)] before making the call above.
So I first get the cid of that owning window like this:
var rez_CGError = CGSGetWindowOwner(_CGSDefaultConnection, MY_TARGET_CGWINDOW_ID_of_other_app, &ownerCid);
This works good and I get ownerCid is set to a value. Then I do the focus command with this new connection:
rez_CGError = CGSOrderWindow(ownerCid, MY_TARGET_CGWINDOW_ID_of_other_app, kCGSOrderAbove, 0);
However this gives rez_CGError of 268435459, which I suspect means "current app does not have permission to use this ConnectionId (cid)". (Same happens if I call activateWithOptions first.
My Sources for the Private Functions
Here is the sources for some private functions I found - https://code.google.com/p/undocumented-goodness/source/browse/trunk/CoreGraphics/CGSPrivate.h
This one source here contains a function that is not in the above link - CGSGetConnectionIDForPSN - i test it and it exists - from - https://github.com/mnutt/libqxt/blob/767498816dfa1742a6f3aee787281745afec11b8/src/gui/qxtwindowsystem_mac.h#L80

Webdriver/Selenium Alert window issue

My company wants me to develop a "Visual" GUI style BDD function using JBehave and Selenium, which uses javascript alert/confirm popup window to prompt user what is the exact step the running test reaches, eg:
Given I goto "www.google.com"
When I login
So we want to add Javascript alert window to popup during the automation test, the popped up window has the "OK" button, so when user click the OK button, the test will continue to the next step, and so on...
My issue is: I wrote a javascript func using Selenium's executeScript API which invoke the pop up alert window:
public void stepText(String step) {
executeScript("alert('"+step+"');");
}
So I expect when I click the OK button, the popped up window will disappear and test will continue to next step... But what shocked me is that when I click it, the test throw exception and crashed...
The exception is: selenium.WebDriverException
But I found if I add the following code to make the test automatically detect the alert window and accept it by using the following usual selenium alert handle function:
Alert alert=switchTo().alert();
alert.accept();
This can make the test runs well, so it looks I can NOT manually click the alert (after I manually click, the selenium still can NOT go back to the browser...lost connection to browser?), but the automation alert handle code works...
Of course, we want to let user to manually to click alert window to control the test execution, not the automation handle alert.
I really got stuck here for a while, and did a lot googling to search, but can not find similar example online, I hope you can shed me light on it, since you are much more guru than me on JBehave and Selenium.
I will be much grateful if you can help me out.
Selenium is a browser automation tool, it does not anticipate user's interactions.
Therefore, I'd use a simple Java GUI window to present the user with messages/options. Afterall, you are testing a web application in a browser, but the program itself is Java and has nothing to do with the browser. A usual Swing option dialog should be enough.
JOptionPane.showMessageDialog(null, "Login successful.");
String loginAs = JOptionPane.showInputDialog("Login as:", "admin");
int choice = JOptionPane.showConfirmDialog(null, "Use production data?");
(note that you don't want to invoke this in the EventQueue.invokeLater() block, because you want the dialogs to be blocking)
This way, you won't interact with Selenium or the browser in any way, you won't confuse it and you'll get the user input cleanly.
That said, if you insist on using alerts, I think it's definitely doable, but as of now (June 2013, Selenium 2.33.0), I don't know how:
The issue is not reproducible on IE8. After the executeScript("alert('Something.')"); call, Selenium waits for the call to return something and then proceeds normally. So you're good on IE.
However, with FF21, Selenium fails immediatelly with UnhandledAlertException just as you said.
I tried two obvious solutions:
js.executeScript("alert('something')");
new WebDriverWait(driver, 10)
.pollingEvery(100, TimeUnit.MILLISECONDS)
.ignoring(UnhandledAlertException.class)
.until(ExpectedConditions.not(ExpectedConditions.alertIsPresent()))
.wait();
and
js.executeScript("alert('something')");
boolean alertVisible = true;
while (alertVisible) {
try {
driver.switchTo().alert();
} catch (NoAlertPresentException ignored) {
alertVisible = false;
}
}
Both make FF fail horribly with an internal JavaScript exception. Possibly a bug that might get fixed (please test it, check whether it had been reported and report it if you're interested in it), so I'll leave the solutions here for future generations.
But as I said before, it's possible that it won't get fixed, since Selenium doesn't count on manual user interactions.
Not sure how this behaves in other browsers.

Is there a way to make actions optional in Selenium IDE?

This is a bit of a newbie question, but... is there a way to make actions optional in Selenium IDE? I'll provide a use case.
In the app I'm testing, users see a "hey, you're agreeing to the ToS by logging on"-type modal window at the beginning of each session. They have to click OK to continue, and they don't see the window again until the next session.
Based on what I've seen so far, I need to have one test suite for the first test each day, and a second test suite for all the others. The second suite is exactly the same except that it doesn't have the "click okay to dismiss the initial modal window" step. Alternatively, I could just remember that my first run of the test each day will fail, and that I have to run the test again.
Both of those "solutions" seem unnecessarily awkward. Can I just make the click command optional?
Create a javascript file called user-extensions.js with the below code. Then go into the Selenium IDE Options dialog and select your user-extensions.js file, restart Selenium and you'll be able to choose TryClick which will work the same as Click but suppress any errors.
Selenium.prototype.doTryClick = function(locator) {
try {
return Selenium.prototype.doClick.call(this,locator);
} catch(err) { return null; }
};
Perhaps overdue, but for future searchers.
You could use the if and endIf statements within the IDE.
If you are using cookies to decide whether to hide the ToS dialog, you could check that a certain cookie is set and if so, skip the click.
I haven't used the selenium IDE much, but I think doing the check would be much easier if you are using a programming language. I am not sure how to do it in HTML tests.
If you are using HTML, you could have a look for Selenium IDE Flow Control and see if that can do what you need. I haven't used this myself, but if looks like it supports if statements. You could use verifyCookie to check if the cookie exists.
Hope that helps.
As aj.esler pointed it out Selenium ID Flow control is a good solution that has worked for me.
Here is the Firefox add on
I use the gotoif, here is an example about how you can use it. When skip value is 1 then it will go to the label=jump line and will not execute everything from gotoif like to label=jump .
Another extremely useful flow control add-on for the IDE is SelBlocks
It will give you the ability to use: if/else/for/foreach/while and even a way to read variables from an XML file.
Use http://wiki.openqa.org/display/SEL/flowControl addon.
Make something like this :
1.storeElementPresent | //button[#name="cookie_law_accept"] | cookie_law
2.goToIf | storedVars['cookie_law']!=true | end
3.click | //button[#name="cookie_law_accept"]
4.label | end
Explain:
1.If element is present it will be stored as a "cookie_law" with value "true"
2.If cookie_law is not "true" - go to label "end" - other way go to next step
3.Click to cookie accept button (only when itsenter code herepresent because it its not - you go to "end" label and you skip this command)
4.You go here if there is no cookie law button :)