Handling Choose file window in Selenium with C# without using AutoIT - c#

I am trying to upload files into a website using selenium which when Clicked on Choose file opens a Native windows, so for this purpose I have been using AutoIT which seems not very reliable when I go for parallel execution.
Since I am using Selenium with C# I thought of finding some solution through which I can handle that Native window but I am unable to find any solution please can anybody tell me some reliable way to do this particular automation.

This worked for me so hopefully it'll work for you. I use it in an extension method here, but you can use as/in a normal method. So this uses C# libraries to enter the path in the dialog and presses enter when done.
string idPath = "C:/text.txt"; //Path to the file you are trying to upload
var button = driver.FindElement(By.Id("blah"));
button.Click()
driver.WaitOnAPage(1); //simple wait method
SendKeys.SendWait(#idPath); //this code sends the path to the file upload dialog
CommonMethods.WaitOnAPage(1);//simple wait method
SendKeys.SendWait(#"{Enter}"); //simulates pressing enter button

There are multiple ways to go on this:
IWebElement element = driver.FindElement(By.Id("your_path_to_file_here"));
element.SendKeys("C:\\Some_Folder\\MyFile.txt");
Using IJavaScriptExecutor:
IWebElement element = driver.FindElement(By.Id("your_path_to_file_here"));
string script = "arguments[0].value='" + "C:\\\\temp\\\\file.txt" + "';";
((IJavascriptExecutor)driver).executeScript(script, element);
Both options insert the text directly in your file path input and afterwards you can just click the Upload button.

Related

Get to Data in Clipboard from Selenium Client

I want to access some data which is copied to the clipboard on a website when you click a button (Jscript)
I use Remotewebdriver (ChromeDriver) to control the testcase but couldn't find out how to access this information.
Anybody knows how?
If possible with c# but a java solution may also work.
Thx
So I could not figure out how to do this so my way was to render a textarea with Jquery and CTRL+V the text into that box just to then get the value back with webdriver. Not pretty but it works
var javaScript = "$( \".d-modal-footer\").html('<div><textarea id=errormessagecopy maxlength=\"5000\" cols=\"80\" rows=\"40\"></textarea></div>');";
WebDriver.ExecuteJavaScript(javaScript);
var errormessagecopy = WebDriver.FindElement(By.Id("errormessagecopy"));
new Actions(WebDriver).MoveToElement(errormessagecopy).Click().KeyDown(OpenQA.Selenium.Keys.Control).SendKeys("v").KeyUp(OpenQA.Selenium.Keys.Control).Perform();
var errorText = WebDriver.ExecuteJavaScript<string>("return $(\"#errormessagecopy\").val();");
You can use https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API but as described in the documentation to read the value from it you need to grant permission in the browser. After you have permission simply execute js script on-page from selenium and get that value.

Click event not working for Firefox but is for chrome

The click event is not firing in firefox but works ok in chrome.
The test fails with the error: "Element not found on page."
Below is the code and HTML for the button I want to click.
Browser.ElementClickById("ctl00_ContentPlaceHolderBody_lvProducts_ctrl0_ctrl1_btnAddProductToCart_input");
and inside the elementclickbyid i have:
driver.FindElement(By.Id(elementID)).Click();
HTML code is:
event
You could try working around with a Javascript click.
// declare JS executor
var executor = (IJavaScriptExecutor)Driver;
// locate the input
var input = Driver.FindElement(By.XPath("//input[#type='submit']"));
// execute JS to click
executor.ExecuteScript("arguments[0].click();", input);
I've seen cases where regular Click(); does not work across browsers -- these cases are rare, but using JS click usually works across multiple browsers when I run into this issue.
driver.findElement(By.xpath("//input[#type='submit']")).click();
i am sure you are trying to use browser class to keep your methods there, but try to use xpath not id. just use this code to click what you need. don't use page object model or anything else. don't save it in your browser class under click method. just in your main code use this code to click. and before to run it make sure that you have only one type submit. if its gonna show to you 2 types then use this code
driver.findElement(By.xpath("//input[#type='submit'][1]")).click();
number 1 says click to first submit if the button which you need second then follow the logic and change the number to 2
driver.findElement(By.xpath("//input[#type='submit'][2]")).click();
for better answer share your code class and also URL where you are trying to click button and also which element you are trying to click

How to choose and upload a local file to a website using Geckofx in C#?

I'm using Geckofx in my winform application to fill a form on a website. One of the controls is a button "Choose Files" which lets you select and upload a local file. I want to automate this process by doing this fully via code.
I managed to click this button via code:
Gecko.DOM.GeckoButtonElement button = new Gecko.DOM.GeckoButtonElement(doc.GetElementsByClassName("choose_files_btn").First().DomObject);
button.Click();
As a result a file dialog is opened automatically but I want to automate the file choosing part and clicking 'OK' as well. I tried inspecting the webpage to find whether I could assign the path of my local file to some Gecko element but couldn't find anything of this sort.
I also thought about handling the event of opening the file dialog but couldn't find any event handler in Gecko. I found Gecko.LauncherDialog.Download event handler which is used for handling downloading a file using Geckofx browser. But there's no such event handler for uploading files using Geckofx browser, if there is and I missed it, do tell.
Maybe I can use an event handler not from Gecko but from System, if I write an event handler which will catch every open file dialog event but I don't know whether that's even possible.
Here is a solution, to upload without showing the file upload dialog:
GeckoHtmlElement el = webbrowser.DomDocument.GetElementsByTagName("input").FirstOrDefault(elz => elz.GetAttribute("type") == "file"); //inpout type file element
var fileNames = new IntPtr[1];
fileNames[0] = new Gecko.CustomMarshalers.WStringMarshaler().MarshalManagedToNative(file); //file = path to file you want to upload
var domInput = Xpcom.QueryInterface<nsIDOMHTMLInputElement>(el.DOMHtmlElement);
domInput.MozSetFileNameArray(fileNames, (uint)fileNames.Length);
Marshal.ReleaseComObject(domInput);
DomEventArgs ev = webbrowser.Document.CreateEvent("HTMLEvents");
var webEvent = new Event(webbrowser.Window.DomWindow, ev.DomEvent as nsISupports);
webEvent.InitEvent("change", true, true);
el.GetEventTarget().DispatchEvent(ev);
new Gecko.CustomMarshalers.WStringMarshaler().CleanUpNativeData(fileNames[0]); //delete everything

Check extension of choosing picture in SP 2013 Picture Library

I need to check pictures extension when user has chosen picture and try to upload it into picture library.
I've found the way to edit master page with js script, but i haven't to edit master page. Then i try to use event receiver Adding, but it can't get the name of file or file path. I used:
var file = properties.ListItem.File.Name; //properties.ListItem - returns null
AfterProperties also returns null.
The another way i see is to edit Adding Picture form with js:
I think is's simplest way but i can't found information about it.
Problem: how to set js script to the form (see picture) or how to do such actions with another way
It can be realized with event receiver ItemAdding and with
properties.AfterUrl
The same answered question with code in:
Customizing upload file functionality in SharePoint picture library
In your case, a simple JS form validation will do.
First I would prevent default action of the submit button.
Then, I would analyze the contents of the filename textbox and check the extention. If it's not one of the listed in the validation script, return false + alert "THis file extention is not supported".
Why would you want to use event receiver? It catches the event on server side.

WatiN: MsHtmlBrowser will not TypeText

From the WatiN website:
// Open a new Internet Explorer window and
// goto the google website.
IE ie = new IE("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
// Uncomment the following line if you want to close
// Internet Explorer and the console window immediately.
//ie.Close();
The above sample works just fine. However, since I do not want to open up a browser window, I modified the above code to use MsHtmlBrowser:
// goto the google website.
var ie = new MsHtmlBrowser();
ie.GoTo("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
The TypeText line is throwing an exception. Any idea what's wrong?
The MsHtmlBrowser is only there to find elements and read their attribute values. There is no support for clicking a link, typing text, firing events, no session state or any other way of interact like with a normal browser. So us it for scrapping only.
HTH,
Jeroen

Categories

Resources