org.openqa.selenium.WebDriverException: Unknown command: uploadFile - selenium

I am trying to upload a file using Safari Driver.
Here is my code:
DesiredCapabilities browserCapabillities = DesiredCapabilities.safari();
RemoteWebDriver driver = new SafariDriver(browserCapabillities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("myAppURL");
WebElement upload = driver.findElementByXPath("//input[#id='fileElementId']");
RemoteWebElement webElement = ((RemoteWebElement) upload);
LocalFileDetector detector = new LocalFileDetector();
webElement.setFileDetector(detector);
File f = detector.getLocalFile("myFilePath");
upload.sendKeys(f.getAbsolutePath()); // Generating exception:
// org.openqa.selenium.WebDriverException: Unknown command: uploadFile
driver.findElement(By.id("uploadButton")).click();
Only thing that is working for me right now is AppleScript. Thanks to Using AppleScript to choose a file in Safari. But with Apple Script I had to keep my machine unlocked.
I feel LocalFileDetector is a better solution as I would like to run my tests even when the machine is locked.
I am not sure whether the following helps ?
driver.setFileDetector(new LocalFileDetector()); // I am getting
// org.openqa.selenium.WebDriverException: Setting the file detector only
// works on remote webdriver instances obtained via RemoteWebDriver

Change
RemoteWebDriver driver = new SafariDriver(browserCapabillities);
to
RemoteWebDriver driver = new RemoteWebDriver(urlofhub,browserCapabillities);
Refer this post for an example code.

Related

Selenium does not "listen" to "webdriver.chrome.driver" property

My coworker using MacOS sent me their Selenium project, containing this:
System.setProperty("webdriver.chrome.driver", "resources\\chromedriver");
driver = new ChromeDriver();
Because I'm using Windows, I downloaded chromedriver.exe in the same folder and changed the previous lines to:
System.setProperty("webdriver.chrome.driver", "resources\\chromedriver.exe");
driver = new ChromeDriver();
However, the test doesn't launch the driver, then fails after some time and I'm getting this error message:
caused by java.io.IOException: Cannot run program "blablabla\resources\chromedriver"
So this means the project is looking for chromedriver and not chromedriver.exe. How come? How can I fix it?
Thank you!
I have a C# setup but the way it works for me is that I specify the folder (full path) where the driver is located.
Config class, notice I have specified the location of the driver and not the .exe-file.
"selenium": {
"screenShots": "C:\\ScreenShots",
"chromeDriver": "C:\\webdrivers",
"driverUrl": "https://www.google.se"
}
Driver init.
Driver = new ChromeDriver(Conf.Selenium.ChromeDriver)
{
Url = Conf.Selenium.DriverUrl,
};
Driver.Manage().Window.Maximize();

org.testng.TestNGException: Cannot instantiate class EDRTermsPackge.ContactInformationTesting

I"m trying to resolve this issue but to no avail. Here is my Source Code
public class ContactInformationTesting {
//Using or Launching Internet Explorer
String exePath = "\\Users\\jj85274\\Desktop\\IEDriverServer.exe";
//For use of IE only; Please enable for IE Browser
WebDriver driver = new InternetExplorerDriver();
#Test
public void OpenPage_Login() {
driver.get("http://cp-qa.harriscomputer.com/");
}
You need to set the System property webdriver.ie.driver before instantiating the InternetExplorerDriver object. I could not find that in your code:
Try following:
System.setProperty("webdriver.ie.driver", "<path_to_your_IEDriverServer.exe>");
WebDriver driver = new InternetExplorerDriver();
Let me know, if you still get the same error.
UPDATE: I have assumed that IEDriverServer.exe's local path is C:\Users\jj85274\Desktop\IEDriverServer.exe. You can change it, as per its location on your machine. Try following code and let me know, whether you are able to launch Internet Explorer successfully.
System.setProperty("webdriver.ie.driver", "C:\\Users\\jj85274\\Desktop\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();

Unable to instantiate Firefox using Marionette driver over Selenium and C#. os error

Could someone please help me with the following issue.
While I'm trying to initialize a browser I get the 'os error' exception.
var option = new FirefoxOptions();
option.IsMarionette = true;
var driver = new FirefoxDriver(option);
var b = new Browser(driver); // Throws an exception with a message - 'os error'
The screenshot of the exception
Plese note, the path to wires.exe is added to the system PATH. Selenium, wires, firefox are of the latest versions. I have tried running using firefox-stable and firefox-developer editions.
Thanks.
So I ran into the 'os error' issue when I was trying to get Marionette working. The source of the issue in my case was I was trying to use some NuGet package called 'Mozilla Firefox Webdriver 0.6.0.1' which I believe had a very old version of the (now called) geckodriver.exe.
I downloaded the latest version of the driver from here https://github.com/mozilla/geckodriver/releases
renamed to wires.exe and put in my working directory
then I had to initiate the driver using the following code.
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
Driver = new FirefoxDriver(service);
The way you initated the driver was giving me an entity not found exception.
Hope this helps
Maybe DesiredCapabilities would work.
DesiredCapabilities capabilities = DesiredCapabilities.Firefox();
capabilities.SetCapability("marionette", true);
var driver = new FirefoxDriver(capabilities);

How to run selenium code

I have the code written for selenium, but I do not know how to make selenium read the code. I know that you have to put it in a certain folder somewhere though. I have never used this program or anything like it before. I just need to know how to make selenium read my code. I have no idea what I'm doing here.
public class GoogleTest {
WebDriver driver = new ChromeDriver();
String baseUrl = "google.com";;
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
}
You need to write the following line before creating the driver object:
System.setProperty("webdriver.chrome.driver", "C:/path/to/chrome/driver/chrome.exe");
Try following code in same order:
System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "google.com";;
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
.
.
You may need to download the chrome driver as well.
link: https://code.google.com/p/selenium/wiki/ChromeDriver

Using Selenium to test Safari gives GridException

I am using the code from this site (http://darrellgrainger.blogspot.com/2011/02/using-selenium-20-with-webdriver-and.html) to run Selenium tests in Safari 5. The code goes like this:
Selenium sel = new DefaultSelenium("localhost", 4444, "*safari", baseURL);
CommandExecutor executor = new SeleneseCommandExecutor(sel);
DesiredCapabilities dc = new DesiredCapabilities();
WebDriver browser = new RemoteWebDriver(executor, dc);
browser.get("http://www.google.com");
WebElement input = browser.findElement(By.name("q"));
input.sendKeys("Selenium");
So I start a Selenium server standalone version on the localhost machine and I register a test node (also on localhost) to the Selenium hub. Then I start the test. I then get the following exception: org.openqa.selenium.WebDriverException: Could not start Selenium session: org%2Eopenqa%2Egrid%2Ecommon%2Eexception%2EGridException%3A+Error+forwarding+the+new+session+The+server+returned+an+error+%3A+
I don't know what error. There is no server output on the console. Does anyone have ideas? I used the newest version (2.17.0) of Selenium.
EDIT: I just tried "firefox" instead of safari and it outputs the same exception. So actually it's not the fault of safari. Maybe there is something wrong with executing Selenium 1 code via the grid?
Try This:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("safari");
capabilities.setJavascriptEnabled(true);
CommandExecutor executor = new SeleneseCommandExecutor(new URL("http://localhost:5555/"), new URL("http://www.google.com/"), capabilities);
WebDriver driver = new RemoteWebDriver(executor, capabilities);
driver.get("http://google.com");
Don't create DefaultSelenium object. The above code works well for me with Safari browser.