3

I am trying to upload file to Safari(8.0.8) using webdriver. Can anyone confirm it is possible or not? I was searching this problem and I cant find clear information.

My test environment: I run test at my local PC with Win7 and browser start at Selenium Grid which is running on MAC machine (hub + node run at MAC Yosemite 10.10.5)

First, I try to upload direct file on MAC. But it is not working.

Browser.Driver.FindElement(By.Id("inputID")).SendKeys("/Users/administrator/Desktop/file.txt");

Next, I try to use LocalFileDetetor but it also doesn't work:

driver.FileDetector = new LocalFileDetector();
Browser.Driver.FindElement(By.Id("inputID")).SendKeys("c:\\file.txt");

Next, I try to use: WebDriverBackedSelenium:

ISelenium safari = new WebDriverBackedSelenium(webDriver, "http://systemname/");
safari.Start();
safari.AttachFile("xpath=//input[@id='inputID']", "e:\\file2.txt");

But it doesn't work too. Stack trace:

Selenium.SeleniumException : WebDriver exception thrown ----> OpenQA.Selenium.InvalidElementStateException : Element must be user-editable in order to clear it. (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 7 milliseconds Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16' System info: host: 'mac.domain.company.com', ip: '192.168.136.67', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.10.5', java.version: '1.8.0_51' Driver info: org.openqa.selenium.safari.SafariDriver Capabilities [{browserName=safari, takesScreenshot=true, javascriptEnabled=true, version=8.0.8, cssSelectorsEnabled=true, platform=MAC, secureSsl=true}] Session ID: null

It doesn't work because it is Safari or there is problem with grid/safari/remote host or with file path(something with / )?

3 Answers3

0

Looks like it is not supported https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/4220

Jeff
  • 1,039
  • 1
  • 11
  • 20
0

You can upload using OSAScript. Please do the following:

  1. Create .scpt file in mac with below code activate application "Safari" tell application "Safari" tell document 1 do JavaScript "document.getElementsByTagName('label')[0].click()" delay 2 end tell end tell tell application "System Events" keystroke "G" using {command down, shift down} delay 2 keystroke "/Users/melamc/Downloads/upload.jpeg" delay 2 keystroke return delay 2 keystroke return delay 2 end tell
  2. trigger this file whenever you required (write a code to run this file programetically

I hope this will help you, try and let me know

0

I have done a lot of research to accomplish the uploading of file in safari browser on mac and fortunately I came up with the below solution.

Following are the possible prerequisites to match the given solution:

  • Programming Environment: c#
  • Automation Environment: Selenium WebDriver, Selenium Grid
  • Browser: Safari (12)
  • OS: MAC (High Sierra)
  • Hub: Windows machine, Node: Mac machine

In the context of the above to upload the file one can implement the following lines of code.

DesiredCapabilities dc = new DesiredCapabilities();

dc.SetCapability(CapabilityType.BrowserName, "safari");
dc.SetCapability(CapabilityType.Version, "12");                       

Driver = new RemoteWebDriver(new Uri("http://Node_Ip_Address:Port/wd/hub/"), dc);

Below lines of code do the magic, (This also works for Chrome, and Firefox.)

IAllowsFileDetection AllowsDetection = Driver as IAllowsFileDetection;
if (AllowsDetection != null)
{
    AllowsDetection.FileDetector = new LocalFileDetector();
}

In java, the equivalent of the above is

Driver.setFileDetector(new LocalFileDetector());

Reference: https://saucelabs.com/blog/selenium-tips-uploading-files-in-remote-webdriver

The code line written for java has not been tested from my end as I am currently working in c#.

Happy Testing.