Some people including me were suffering from this issue called "OpenQA.Selenium.WebDriverException: [windowHandle] is not a top level window handle".
There are lot of question asked and answered on "how to attach to a TopLevelWindow" but literally none is talking on "how to attach to a NonTopLevelWindow". I searched a lot for a solution but there was nothing on it. But after reading a code shared in this answer on GitHub, I realized what the solution is.
I was so disgusted after it because of the simplicity of the solution! Then I thought to share it with everyone.
The solution is so simple. As the new window which is a Child Node (i.e. resides under the main application tree), we can not attach to it by creating a new session. The reason is that, it is not a top level window and we can only attach to a top level window with this process explained on GitHub.
What we need to do is just simply search for the window by its name and you will get access of that window (same as finding an UI element)
For example- This code is for such a window which is a TopLevelWindow:
# create a desktop session to find the new window
desired_caps = {}
desired_caps["app"] = "Root"
newDriver = webdriver.Remote(WindowsApplicationDriverUrl, desired_caps)
# Find the NativeWIndowHandle of the new window
newWindow = newDriver.find_element_by_name("SmarTTY - New SSH Connection")
newWindowHandle = newWindow.get_attribute("NativeWindowHandle")
# create a new session to attach the new window with its NativeWindowHandle
desired_caps = {}
desired_caps["appTopLevelWindow"] = newWindowHandle
connWinDriver = webdriver.Remote(WindowsApplicationDriverUrl, desired_caps)
And this code is for such a window which is a NonTopLevelWindow:
# Find the new window
newPopUpWindow = driverMain.find_element_by_name("SmarTTY - New SSH Connection")
In this case the first code will not work due to the window is not being a top level window. But the second code will work.
This window (window name is SmarTTY - New SSH Connection) pops open after I click a button on the previous window. The application name on which this example is based on is SmarTTY. And the above codes are Python codes for WinAppDriver.
Here is my C# code, I am adding it as desired capabilities are depricated and options should be used instead:
AppiumOptions rootSessionOptions = new AppiumOptions();
rootSessionOptions.AddAdditionalCapability("app", "Root");
rootSessionOptions.AddAdditionalCapability("deviceName", "WindowsPC");
_driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), rootSessionOptions);
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
var VSWindow = _driver.FindElementByName("Your project name without .csproj - Microsoft Visual Studio");
var VSTopLevelWindowHandle = VSWindow.GetAttribute("NativeWindowHandle");
VSTopLevelWindowHandle = (int.Parse(VSTopLevelWindowHandle)).ToString("x");
AppiumOptions VisualStudioSessionOptions = new AppiumOptions();
VisualStudioSessionOptions.AddAdditionalCapability("appTopLevelWindow", VSTopLevelWindowHandle);
_driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), VisualStudioSessionOptions);
_driver.SwitchTo().Window(_driver.WindowHandles[0]);
# After login i am not able to perform any operation
#Conftest file code
import pytest
from appium import webdriver
#pytest.fixture(scope="class")
def setUp(request):
desired_caps = {}
desired_caps["app"] = r"C:\\Program Files (x86)\\Giesecke Devrient\\Compass VMS\\Compass VMS.exe"
driver = webdriver.Remote(command_executor='http://127.0.0.1:4723',
desired_capabilities= desired_caps)
#driver.implicitly_wait(10)
request.cls.driver = driver
yield
driver.close()
#Main Code
from appium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from Utility.Base import BaseClass
# #pytest.mark.usefixtures("setUp")
class TestDe_Login(BaseClass):
def test_initialize(self):
self.driver.find_element_by_xpath("//*[#Name='Login']//*[#LocalizedControlType='text' "
"and #Name='User ID:']/..//*[#LocalizedControlType='edit']").send_keys("t2")
self.driver.find_element_by_accessibility_id("passwordTextBox").send_keys("1")
self.driver.find_element_by_name ("&Login").click()
wait = WebDriverWait(self.newDriver, 20)
wait.until(EC.presence_of_element_located((By.NAME, '&Logout'))).click()[enter image description here][1]
Related
I am at my wits end, trying to automate our tests for the Windows SaveAs-dialog.
The thing is that the automation code works on some machines but not all.
It works on my local box and a few other, but we need to make it work on all our test machines. Something is different but what I can see sofar is
Same windows version
Same dotnet --info
The test code I have tried to make work is something like
...
var app = FlaUI.Core.Application.Launch("FlaUISaveDialog.exe");
using (var automation = new UIA3Automation())
{
var window = app.GetMainWindow(automation);
var button1 = window.FindFirstDescendant(cf => cf.ByAutomationId("ClickIt"))?.AsButton();
button1?.Patterns.Invoke.PatternOrDefault.Invoke();
Thread.Sleep(2400); // Wait for window to appear!
var dialog = window.FindFirstDescendant(cf => cf.ByControlType(ControlType.Window));
Thread.Sleep(1000);
var fileNameTextBox = dialog.FindFirstDescendant(e => e.ByAutomationId("1001"));
fileNameTextBox.Focus();
fileNameTextBox.Patterns.Value.Pattern.SetValue(resultFile);
Thread.Sleep(2400);
//FlaUI.Core.Input.Keyboard.Press(VirtualKeyShort.RETURN);
var save = dialog.FindFirstChild(e => e.ByAutomationId("1").And(e.ByControlType(ControlType.Button)));
save.Focus();
var mousePoint = new Point(save.BoundingRectangle.X + save.BoundingRectangle.Width/2, save.BoundingRectangle.Y + save.BoundingRectangle.Height/2);
FlaUI.Core.Input.Keyboard.Press(VirtualKeyShort.RETURN);
//FlaUI.Core.Input.Mouse.Click(mousePoint);
//save.Patterns.Invoke.Pattern.Invoke();
Thread.Sleep(2400); // Wait for file save to complete
Assert.IsTrue(File.Exists(resultFile));
}
After another day going at this, I found that it seems to be the "SetValue" pattern that looks like it works, but doesn't change the Dialogs actual filename.
But moving the mouse,clicking and typing like below actually works:
var fileNameTextBox = dialog.FindFirstDescendant(e => e.ByAutomationId("1001"));
var mousePoint = new Point(fileNameTextBox.BoundingRectangle.X + fileNameTextBox.BoundingRectangle.Width/2, fileNameTextBox.BoundingRectangle.Y + fileNameTextBox.BoundingRectangle.Height/2);
Thread.Sleep(1000);
FlaUI.Core.Input.Mouse.MoveTo(mousePoint);
FlaUI.Core.Input.Mouse.Click(mousePoint);
Thread.Sleep(1000);
FlaUI.Core.Input.Keyboard.TypeSimultaneously(VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_A);
Thread.Sleep(1000);
FlaUI.Core.Input.Keyboard.Type(resultFile);
Thread.Sleep(1000);
FlaUI.Core.Input.Keyboard.Press(VirtualKeyShort.RETURN);
Edit #2: I seems that it is the Windows Explorer option "View File name extensions" that affects the automation behavior of the "SaveFileDialog". If we turn on "View file name extensions" the "SetValue" pattern starts working. Turn the setting off and the "SetValue" stops working! Unexpected, to say the least!
I have this error "org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element is not clickable at point (209, 760)", when I run the piece of code below in headless mode. When it is run with browser displayed I have no error and test passes fine. As you can see below, I trie with waiting, js executor, actions move to element but still no good result. I am using xpath to locate / define the element, and not coordinates. Why is this happening please and how can I solve it? Thanks in advance.
#Test(priority = 1)
public void verifyAddUserWithMarkedMandatoryFields() {
// accessing add user webpage / functionality
userListObject.getAddUserButton().click();
// inserting data to complete form
addOrEditUserPageObject.insertCredentials(userModel.getUsername(), userModel.getEmail(), "", userModel.getPassword());
// clicking Submit when becoming enabled
WebDriverWait myWaitVariable = new WebDriverWait(driver, 5);
myWaitVariable.until(ExpectedConditions.elementToBeClickable(addOrEditUserPageObject.getSubmitButtonAddOrEdit()));
// Actions actions = new Actions(driver);
// actions.moveToElement(addOrEditUserPageObject.getSubmitButtonAddOrEdit()).click().perform();
JavascriptExecutor jse = (JavascriptExecutor)driver;
// jse.executeScript("scroll(209, 760)"); // if the element is on top.
jse.executeScript("scroll(760, 209)"); // if the element is on bottom.
addOrEditUserPageObject.getSubmitButtonAddOrEdit().click();
}
You should add screen size for the headless mode, something like this:
Map<String,String> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadsPath); // Bypass default download directory in Chrome
prefs.put("safebrowsing.enabled", "false"); // Bypass warning message, keep file anyway (for .exe, .jar, etc.)
ChromeOptions opts = new ChromeOptions();
opts.setExperimentalOption("prefs", prefs);
opts.addArguments("--headless", "--disable-gpu", "--window-size=1920,1080","--ignore-certificate-errors","--no-sandbox", "--disable-dev-shm-usage");
driver = new ChromeDriver(opts);
I put much more things here, the only relevant point here is "--window-size=1920,1080", this should resolve your problem.
The rest is to show how things are managed, including other relevant settings for headless mode.
I am using following code snipped to take screenshot in selenium 2.21.
augmentedDriver = new Augmenter().augment(driver);
File scrnshot = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrnshot, new File(File_Name));
Whenever i call method containing this code, it's launching new browser instances with text "This is the initial start page for the WebDriver server.
"
driver = new new InternetExplorerDriver();
Please let me know the issue, as well as solution too.
I think, that this line:
augmentedDriver = new Augmenter().augment(driver);
Starts new Driver instance. But you need to give here:
File scrnshot = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
Not augmentedDriver but existing driver instance.
Using .NET WPF and Windows 10, is there a way to push a local toast notification onto the action center using c#? I've only seen people making custom dialogs for that but there must be a way to do it through the os.
You can use a NotifyIcon from System.Windows.Forms namespace like this:
class Test
{
private readonly NotifyIcon _notifyIcon;
public Test()
{
_notifyIcon = new NotifyIcon();
// Extracts your app's icon and uses it as notify icon
_notifyIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
// Hides the icon when the notification is closed
_notifyIcon.BalloonTipClosed += (s, e) => _notifyIcon.Visible = false;
}
public void ShowNotification()
{
_notifyIcon.Visible = true;
// Shows a notification with specified message and title
_notifyIcon.ShowBalloonTip(3000, "Title", "Message", ToolTipIcon.Info);
}
}
This should work since .NET Framework 1.1. Refer to this MSDN page for parameters of ShowBalloonTip.
As I found out, the first parameter of ShowBalloonTip (in my example that would be 3000 milliseconds) is generously ignored. Comments are appreciated ;)
I know this is an old post but I thought this might help someone that stumbles on this as I did when attempting to get Toast Notifications to work on Win 10.
This seems to be good outline to follow -
Send a local toast notification from desktop C# apps
I used that link along with this great blog post- Pop a Toast Notification in WPF using Win 10 APIs
to get my WPF app working on Win10. This is a much better solution vs the "old school" notify icon because you can add buttons to complete specific actions within your toasts even after the notification has entered the action center.
Note- the first link mentions "If you are using WiX" but it's really a requirement. You must create and install your Wix setup project before you Toasts will work. As the appUserModelId for your app needs to be registered first. The second link does not mention this unless you read my comments within it.
TIP- Once your app is installed you can verify the AppUserModelId by running this command on the run line shell:appsfolder . Make sure you are in the details view, next click View , Choose Details and ensure AppUserModeId is checked. Compare your AppUserModelId against other installed apps.
Here's a snipit of code that I used. One thing two note here, I did not install the "Notifications library" mentioned in step 7 of the first link because I prefer to use the raw XML.
private const String APP_ID = "YourCompanyName.YourAppName";
public static void CreateToast()
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(
ToastTemplateType.ToastImageAndText02);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode("This is my title!!!!!!!!!!"));
stringElements[1].AppendChild(toastXml.CreateTextNode("This is my message!!!!!!!!!!!!"));
// Specify the absolute path to an image
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + #"\Your Path To File\Your Image Name.png";
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = filePath;
// Change default audio if desired - ref - https://learn.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-audio
XmlElement audio = toastXml.CreateElement("audio");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Reminder");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.IM");
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Mail"); // sounds like default
//audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call7");
audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call2");
//audio.SetAttribute("loop", "false");
// Add the audio element
toastXml.DocumentElement.AppendChild(audio);
XmlElement actions = toastXml.CreateElement("actions");
toastXml.DocumentElement.AppendChild(actions);
// Create a simple button to display on the toast
XmlElement action = toastXml.CreateElement("action");
actions.AppendChild(action);
action.SetAttribute("content", "Show details");
action.SetAttribute("arguments", "viewdetails");
// Create the toast
ToastNotification toast = new ToastNotification(toastXml);
// Show the toast. Be sure to specify the AppUserModelId
// on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
}
UPDATE
This seems to be working fine on windows 10
https://msdn.microsoft.com/library/windows/apps/windows.ui.notifications.toastnotificationmanager.aspx
you will need to add these nugets
Install-Package WindowsAPICodePack-Core
Install-Package WindowsAPICodePack-Shell
Add reference to:
C:\Program Files (x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd
And
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
And use the following code:
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
for (int i = 0; i < stringElements.Length; i++)
{
stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
}
// Specify the absolute path to an image
string imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier("Toast Sample").Show(toast);
The original code can be found here: https://www.michaelcrump.net/pop-toast-notification-in-wpf/
I managed to gain access to the working API for windows 8 and 10 by referencing
Windows.winmd:
C:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral
This exposes Windows.UI.Notifications.
You can have a look at this post for creating a COM server that is needed in order to have notifications persisted in the AC with Win32 apps https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/10/16/quickstart-handling-toast-activations-from-win32-apps-in-windows-10/.
A working sample can be found at https://github.com/WindowsNotifications/desktop-toasts
I am trying to take screenshot of sub menu which happens on hovering in selenium using TakesScreenshot. But this is not working. Screenshot is taken but sub menu is not present in the image.
I have also tried using implicit wait after hover, but nothing worked.
Please suggest a method to capture screenshot of the sub menu.
contactUs.hoverHM();
screenshot = ((TakesScreenshot) PageFactoryBase.getSharedWebDriver()).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
This did the trick for me. I am pretty sure it will work for you.
_driver = new FirefoxDriver();
_driver.Navigate().GoToUrl("http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_event_mouseover_mouseout");
_driver.SwitchTo().Frame(_driver.FindElement(By.Id("iframeResult")));
Actions builder = new Actions(_driver);
builder.MoveToElement(_driver.FindElement(By.TagName("p"))).Build().Perform();
var screenshot = ((ITakesScreenshot)_driver).GetScreenshot();
var filename = new StringBuilder("D:\\");
filename.Append(DateTime.Now.ToString("HH_mm_ss dd-MM-yyyy" + " "));
filename.Append("test");
filename.Append(".png");
screenshot.SaveAsFile(filename.ToString(), System.Drawing.Imaging.ImageFormat.Png);
After hovering mouse on the text, it turns yellow and below is the screen shot that I took.
Below is another approach where you can use 'Print screen' Key in your Test code and get the image from the clipboard in the system.
What is have done is used KeyEvent 'PRTSC' to get the Image into the system clipboard and then get the system clipboard to write it to a file. I hope it will also copy the mouseover.
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_PRINTSCREEN);
Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable content = clip.getContents(null);
BufferedImage img = (BufferedImage)content.getTransferData(DataFlavor.imageFlavor);
ImageIO.write(img, "png", new File("D:\\test.png"));
I have tried the same scenario but for clickAndHold for hover skin. It worked for me with the help of Actions as below:
WebElement elm = driver.findElement(By.id("btn1"));
Actions builder = new Actions(driver);
Action act = builder.clickAndHold(elm).build();
act.perform();
try {
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\Img\\screenshot.png"));
} catch (IOException e) {
e.printStackTrace();
}
act = builder.release(elm).build();
act.perform();
You can instead replace clickAndHold with moveToElement Mouse hover the element. take the screenshot then release the element or move away from it.
Thanks everyone for answering on this thread.
I am able to take screenshot using robot as suggested by Vivek.
builder.moveToElement(getSharedWebDriver().findElement(By.xpath("//div[#class='brand section']/ul/li[#class='active hasflyout']"))).perform();
Robot robot = new Robot();
Point point;
point = getSharedWebDriver().findElement(By.xpath("//div[#class='brand section']/ul/li[#class='active hasflyout']")).getLocation();
int x = point.getX();
int y = point.getY();
robot.mouseMove(x,y);
In ideal case, either perform() or mouseMove() should be used. But somehow in my case, i had to use both the functions.