Not able to get windowhandle of already open IE window - c#

I am trying to automate citrix login process at my organization.
I am using ChromeDriver ( selenium ) to automate few steps which is successful till now.
But it downloads .ica file which I need to double click and which opens fresh IE browser instance.
I am not able to get handle of this instance. I want to enter username and password programmatically in this newly opened IE window.
My code is as below.
// Code which downloads ica file
var idCitrix = chromedriver.FindElementById("IdOfDOMElement");
idCitrix.Click();
// Below code will ALWAYS OPEN IE FRESH BROWSER INSTANCE (IE 11)
Process.Start("LOCATION OF .ICA FILR FROM DOWNLOADS FOLDER");
//How to get handle of this newly opened IE window ??

Related

C# selenium chrome driver, every time I run my program I need to scan QR code from my mobile

ChromeOptions options = new ChromeOptions();
options.AddArguments("--user-data-dir=C:\\Users\\Myname\\AppData\\Local\\Google\\Chrome\\User Data");
options.AddArguments("--profile-directory=Profile 3");
IWebDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://web.whatsapp.com/");
How can I store the whatsApp login session, So that every time I run a program I don't need to scan the QR code again?
When you quit the webdriver, the cookies, cache, history, etc are deleted. So if you want to be already logged in when you start the webdriver, you have to use the cookies from a profile on the normal browser in which you are already logged in. To do so you have to:
create a new chrome profile in the normal browser
login to whatsapp
load the profile in selenium
In this way when you start the webdriver and load the page you will be already logged in.
To create a new profile, open your chrome browser, click on the profile icon on top right, then click "Add" and then click "Continue without an account".
Then write a name for your profile and be sure check the box "Create a desktop shortcut". I suggest you to choose a color so that you will immediately see if it works when selenium open the browser window.
Open chrome from the new desktop icon (mine is called "PythonSelenium - Chrome.exe") and login to instagram.
After you logged in, close the browser and open the properties of the new desktop shortcut. Take note of the name of the profile directory, which in my case is "Profile 3".
Now we have to tell selenium to open the chrome driver using this new profile we've just created, which is logged in twitch. To do this we need the path of the folder where the profile is stored. By default the path is something like C:\Users\your_username\AppData\Local\Google\Chrome\User Data, if you are not sure about it check where chrome is installed in your computer.
Then run this code (remember to substitute Profile 3 with the name of your profile from step 3)
ChromeOptions options = new ChromeOptions();
options.AddArguments("--no-sandbox")
options.AddArguments("--remote-debugging-port=9222")
options.AddArguments("--disable-dev-shm-using")
options.AddArguments("--disable-setuid-sandbox")
options.AddArguments("--user-data-dir=C:\Users\your_username\AppData\Local\Google\Chrome\User Data");
options.AddArguments("--profile-directory=Profile 3"); // <-- substitute Profile 3 with your profile name
IWebDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://web.whatsapp.com/");

Selenium C# Test with Chrome Headless - Chrome File Upload Window not working

I have a test which uploads a profile image to the web application. In order to upload the image file, an upload button is clicked which opens a file upload window as shown in the screenshot below.
The test then sends keys of the image file path to the file upload window in order to upload the file. When the test is run in chrome headless mode, the file upload window is not found and the send keys step fails.
If the test is run in normal mode (without headless option) the test runs fine without any issue. What could be the problem? Is there any way to fix this? Following code is used to set focus and send keys to the browser file upload window.
var dialogHWnd = FindWindow("#32770", "Open"); // Title for modal. IE: "Choose File to Upload"
var setFocus = SetForegroundWindow(dialogHWnd);
SendKeys.SendWait(#picPath);
Thread.Sleep(5000); //wait
SendKeys.SendWait(#"{Enter}");
Reporter.LogTestStepForBugLogger(Status.Info, $"Sent keys {picPath} and Enter to upload the Profile Picture.");
With selenium, you can just sent the full path of the image to the input element that is responsible for the upload.
Check the html code and search for relevant probably with "hidden" attribute or something common for this upload section.
Then just use:
driver.findElement(by.xpath('input xpath or id ..').sendKeys('path of the image in your system');

Chrome Driver 2.36 Login issue thread is waiting at Driver.URL C#

Meta -
OS: Windows 10, Server 2012
Browser: Chrome
Version: 65.0.3325.181 (Official Build) (64-bit)
Web driver: 2.36
Dotnet Framework: 4.6
Expected Behavior -
In my application I'm using window AD for authentication. Whenever I open application in chrome browser it ask for authentication and a login pop-up window(attached screenshot) gets open. In the pop-up window, I enter my credential and click on login button. Once successfully logged-in I'm able access my application. Expecting same behavior in automation.
Actual Behavior -
Same thing i have automated with the help of selenium driver. I had used selenium web driver 2.34 for Chrome and Chrome version was 64, with this combination login functionality was working fine. Now recently I have upgraded web driver to 2.36 and Chrome to 65, post this upgrade login functionality is not working. Now in application when login pop-up window opens and application wait for user inputs
i'm using auto hot key "WindowsAuthenticationChrome" to enter the credentials, now application gets hang at driver.url="some url" thread gets blocked and unable to launch auto hotkey and after some time it throw command timeout exception. and when i manually enter credentials or close the popup thread get released and execute next line of code.
This issue is coming after version upgrade.
Steps to reproduce -
Created sample console application
class Program
{
static void Main(string[] args)
{
string[] arguments = new string[] { "no-sandbox", "test-type", "ignore-certificate-errors", "disable-popup-blocking" } ;
var chromeOptions = new ChromeOptions() { BinaryLocation = #"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" };
chromeOptions.AddArguments(arguments);
var chromedriver = new ChromeDriver(#"C:\TestCases\Drivers", chromeOptions, new System.TimeSpan(0, 10, 0));
//chromedriver.Url = "actual url" login pop-up window come;
chromedriver.Url = "https://www.google.co.in/";
}
}
In sample code i'm creating object of web driver with some capabilities, when i'm trying to open my application url where login pop-up window come code gets hang after some time it throw command timeout exception however when i'm trying to open simple page ("https://www.google.co.in") where popup window is not coming code is working fine.
Any help would be grateful!

ShellClass.BrowseForFolder(...) DialogBox not showing

I have a .aspx page in my project inside that there is one <a> link for download .txt files.
Am using Shell32.ShellClass and Shell32.Folder2 for showing Browse For Folder for ask user to Where do you want to save files instead it download on default Downloads folder.
Below is my C# code for showing DialogBox.
Shell32.ShellClass shell = new Shell32.ShellClass();
Shell32.Folder2 flder = (Shell32.Folder2)shell.BrowseForFolder(0, "Select destination folder", 0,"Desktop");
if (flder == null)
{
dlgResult = DialogResult.Cancel;
}
else
{
strPath = flder.Self.Path;
dlgResult = DialogResult.OK;
File.WriteAllText(strPath + "\\NewFile.txt", "file content abc tex...");
}
Problem: Above code working fine in Local but when I host website on IIS it's not working and not showing DialogBox.
Is there any specific Settings or Configurations in IIS for that ? or why it's not working when it host in IIS?
please give me suggestions.
Thanks.
Your C# code runs on the server, not in the web page. When you open a dialog box in this way, it runs in the context of the calling code. When running as part of IIS express (or any web server running as you), it opens in your Windows desktop because it is running as you. When running in IIS, it is running as a different account not attached to your desktop, so it will not show.
I am not aware of a way to invoke the "Save As" functionality to save a file to a different folder in JavaScript. The execCommand function may work but it varies from browser to browser. Some versions of Internet Explorer also limit what file types can be downloaded this way.

Run process.start() in a network

I have written a win application that works on a network.
In one of its forms user can browse and select file from local computer and add it to a list, so the application copies these files to a folder in My Network Places that no users can access to it but one that i have created already for my application so i have his username and password.Every things works fine up to here.
In this form user can also open a file by select it and press open button, so the file should open in an application that relates to its extension(for example test.xlsx should open in Exel.exe).I have used Process.Start() to do this but for each extension i receive an individual error(for example "Access is denied" for NotePad and "RunTime error" for AdobeReader and "Not enough memory" for Excel).
What is my mistake?
Note : I have used ImpersonatUser to logon that user in my application.
Edit : I have used following code to open the file :
Using(WindowsImpersonationContext impersonateUser = LogonMethod())
{
ProcessStartInfo pInfo = new ProcessStartInfo(filePathWithExtension);
pInfo.Domain = MyDomainName;
pInfo.UseShellExecute = false;
Process.Start(pInfo);
}
Note : My LogonMethod uses LogonUser method of advapi32.dll.
The behavior you are seeing is almost expected.
it looks like you are not launching application directly, but rater using association by file name. I don't believe you'll get application launched under account you want. You can check what account application is run unrder using task manager.
Most application are not tested to run in "run as" context, so they may work correctly or fail randomly.
I could not solve this problem, so i have used another way.I have copied the file to a temp folder and then used Process.Start to open this new file.

Categories