I am implementing code to write automation test for MMC application. I am trying to launch MMC application, then click on "File" Menu item and then "New" sub menu item of "File". I am able to launch MMC application and successfully click on "File" Menu item but clicking on "File" sub menu Item "New" fails with "An element could not be located on the page using the given search parameters.".
Snippet of the code -
session.FindElementByName("File").Click();
Thread.Sleep(TimeSpan.FromSeconds(5));
Actions actions = new Actions(session);
var item = session.FindElementByXPath("//MenuItem[@Name='New']");
actions.Click(item);
actions.Perform();
WinAppDriver log corresponding to failure -
==========================================
POST /session/DAAD9969-58E9-4D90-9753-113685106FB1/element HTTP/1.1
Accept: application/json, image/png
Content-Length: 51
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723
{"using":"xpath","value":"//MenuItem[@Name='New']"}
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."}}
==========================================
I have also tried following alternate ways of clicking on "New" sub menu item but that failed too -
i> session.FindElementByXPath(".//MenuItem[@Name='New']").Click();
ii> session.FindElementByXPath(".//MenuItem[@AutomationId='57600']");
iii> session.FindElementByAccessibilityId("57600").Click();
iv> Xpath code generated by UI recorder -
string newpath = "/Pane[@ClassName =\"#32769\"][@Name=\"Desktop 1\"]/Menu[@ClassName=\"#32768\"*/][@Name=\"Context\"]/MenuItem[@Name=\"New\"]";
session.FindElementByXPath(newpath).Click();
Make sure value of @ClassName is "#32768", not "#32768"*/. Did you manually create the following invalid xapth?
string newpath = "/Pane[@ClassName ="#32769"][@name="Desktop 1"]/Menu[@ClassName="#32768"*/][@name="Context"]/MenuItem[@name="New"]";
Try get xpaths from UIRecorder via "Copy Full XPath" menu or "Generate and copy C# code to Clipboard" button.
I believe you should be able to use the code given below with little tinkering for your example.
I wrote this source code and much more for my upcoming course about automation testing with WinAppDriver.
[TestMethod]
public void MenuTest()
{
var allMenus = sessionWinForm.FindElementsByTagName("MenuItem");
Debug.WriteLine($"All menu items found by search: {allMenus.Count}");
WebDriverWait wdv = new WebDriverWait(sessionWinForm, TimeSpan.FromSeconds(10));
foreach (var mainMenuItem in allMenus)
{
if(mainMenuItem.GetAttribute("Name").Equals("File"))
{
mainMenuItem.Click();
var newMenu = mainMenuItem.FindElementByName("New");
wdv.Until(x => newMenu.Displayed);
newMenu.Click();
}
}
}
Thanks Akram. How do I create sessionWinForm mentioned in the code snippet ?
On Sat, Jun 15, 2019, 1:40 AM Naeem Akram notifications@github.com wrote:
I believe you should be able to use the code given below with little
tinkering for your example.
I wrote this source code and much more for my upcoming course about
automation testing with WinAppDriver.[TestMethod]
public void MenuTest()
{
var allMenus = sessionWinForm.FindElementsByTagName("MenuItem");Debug.WriteLine($"All menu items found by search: {allMenus.Count}"); WebDriverWait wdv = new WebDriverWait(sessionWinForm, TimeSpan.FromSeconds(10)); foreach (var mainMenuItem in allMenus) { if(mainMenuItem.GetAttribute("Name").Equals("File")) { mainMenuItem.Click(); var newMenu = mainMenuItem.FindElementByName("New"); wdv.Until(x => newMenu.Displayed); newMenu.Click(); } } }—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/microsoft/WinAppDriver/issues/733?email_source=notifications&email_token=AGSJLQV36WQZYCJC2H7MBV3P2P3KLA5CNFSM4HXJN6NKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXX24PA#issuecomment-502246972,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGSJLQTX4XCOAEVFDR2ACILP2P3KLANCNFSM4HXJN6NA
.
Try using the "session" object which is already shown in your own code.
I have already tried with "session" object. With session object, I am able
to click on "File" Menu item but still not on "New" Menu item which is a
sub menu item of "File".
Error is - "An element could not be located on the page using the given
search parameters."
I have tried using "session" object and also "MMCSession" object which is
attached to the Desktop session (in the code below).
Here is the code -
if (session == null)
{
// Create a new session to bring up an instance of the
MMC application
DesiredCapabilities appCapabilities = new
DesiredCapabilities();
appCapabilities.SetCapability("app", MMCAppId);
appCapabilities.SetCapability("deviceName", "WindowsPC");
appCapabilities.SetCapability("platformName", "Windows");
session = new WindowsDriver
Uri(WindowsApplicationDriverUrl), appCapabilities);
Assert.IsNotNull(session);
//// Set implicit timeout to 1.5 seconds to make element
search to retry every 500 ms for at most three times
session.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(1.5));
DesiredCapabilities appCapabilities1 = new
DesiredCapabilities();
appCapabilities1.SetCapability("app", "Root");
appCapabilities1.SetCapability("deviceName",
"WindowsPC");
DesktopSession = new WindowsDriver
Uri("http://127.0.0.1:4723"), appCapabilities1);
var MMCWindow =
DesktopSession.FindElementByClassName("MMCMainFrame");
var MMCTopLevelWindowHandle =
MMCWindow.GetAttribute("NativeWindowHandle");
MMCTopLevelWindowHandle =
(int.Parse(MMCTopLevelWindowHandle)).ToString("x"); // Convert to Hex
// Create session by attaching to MMC top level window
DesiredCapabilities appCapabilities2 = new
DesiredCapabilities();
appCapabilities2.SetCapability("appTopLevelWindow",
MMCTopLevelWindowHandle);
MMCSession = new WindowsDriver
Uri(WindowsApplicationDriverUrl), appCapabilities2);
}
[TestMethod]
public void menuitem()
{
var allMenus = session.FindElementsByTagName("MenuItem");
Console.WriteLine("All Menu items found by search:
{allMenus.Count}");
WebDriverWait wdv = new WebDriverWait(session,
TimeSpan.FromSeconds(10));
foreach (var mainMenuItem in allMenus)
{
if (mainMenuItem.GetAttribute("Name") != null)
{
if
(mainMenuItem.GetAttribute("Name").Equals("File"))
{
mainMenuItem.Click();
Thread.Sleep(1000);
var newMenu =
mainMenuItem.FindElementByXPath("//MenuItem[@name=\"New\"]"); <-----
It fails here ---->
newMenu =
mainMenuItem.FindElementByName("New");
wdv.Until(x => newMenu.Displayed);
newMenu.Click();
}
}
}
}
I have tried accessing the menu item "New" using ByName and
By AccessibilityId also but it fails to locate the element.
On Sat, Jun 15, 2019 at 7:57 PM Naeem Akram notifications@github.com
wrote:
Try using the "session" object which is already shown in your own code.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/microsoft/WinAppDriver/issues/733?email_source=notifications&email_token=AGSJLQUNUE6GJ2SRPOSWJBTP2T35TA5CNFSM4HXJN6NKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXYZH5Y#issuecomment-502371319,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGSJLQRVFOM475WCYLF3FPTP2T35TANCNFSM4HXJN6NA
.
Listen... Don't search use this statement
mainMenuItem.FindElementByXPath
Instead, add an if condition in the loop to find out the new menu.
Why? Because the new menu item is already present in the list of menu items which you got.
Have you tried to print all elements in the list to the debug console?
You may add a check on "Displayed" property of mainMenuItem and Name attribute.
For example(this syntax is not exact)
if(mainMenuItem.GetAttribute("Name").Equals("New") && mainMenuItem.Displayed)
mainMenuItem.Click();
I hope this helps.
I could resolve this issue by using DesktopSession -
WindowsElement popup = DesktopSession.FindElementByClassName("#32768")
var elem = popup.FindElementByName("Add/Remove Snap-in...");
elem.Click();
Thanks for your suggestion Akram. I had tried that but that didn't work in
my case.
On Mon, Jun 17, 2019 at 2:56 AM Naeem Akram notifications@github.com
wrote:
Listen... Don't search use this statement
mainMenuItem.FindElementByXPathInstead, add an if condition in the loop to find out the new menu.
Why? Because the new menu item is already present in the list of menu
items which you got.
Have you tried to print all elements in the list to the debug console?
You may add a check on "Displayed" property of mainMenuItem and Name
attribute.For example(this syntax is not exact)
if(mainMenuItem.GetAttribute("Name").Equals("New") && mainMenuItem.Displayed)
mainMenuItem.Click();I hope this helps.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/microsoft/WinAppDriver/issues/733?email_source=notifications&email_token=AGSJLQWE7WSIV2Y6225FE7TP25NVVA5CNFSM4HXJN6NKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODX2VIUA#issuecomment-502617168,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGSJLQTDMLY2BBMDPZERQVDP25NVVANCNFSM4HXJN6NA
.
Great news, looks like you let go of the menu approach and used a UI element which was already painted. Smart approach :+1:
@yogeshrs, Great solution! I have been searching the google for a possible solution to this exact problem and found the one you implemented worked the best. Thanks for posting!
Most helpful comment
I could resolve this issue by using DesktopSession -
WindowsElement popup = DesktopSession.FindElementByClassName("#32768")
var elem = popup.FindElementByName("Add/Remove Snap-in...");
elem.Click();
Thanks for your suggestion Akram. I had tried that but that didn't work in
my case.
On Mon, Jun 17, 2019 at 2:56 AM Naeem Akram notifications@github.com
wrote: