File upload using Selenium RC

426 views
Skip to first unread message

Dinesh Chaminda

unread,
Apr 18, 2012, 11:07:25 PM4/18/12
to Selenium Users
Hi,
I want to do a file upload using Selenium RC and my C# code is as
follows
seleniumsession.Type("id=FileControl","C:\TestFile.doc");

but this didn't work then I tried AttachFile() method and it didn't
work for me too. I'm trying this in IE8 and above browsers
Please help me to resolve this.

Thanks
-Dinesh

UmaMahesh

unread,
Apr 19, 2012, 2:46:16 AM4/19/12
to seleniu...@googlegroups.com
H dinesh,
 
please try in this way, i am using webdriver.
 
driver.findElement(By.xpath("//*[@id=xxxxxxxx]")).sendKeys("c:\\Documents and Settings\\Administrator\\Desktop\\abc.doc");
 

Thanks
uma mahesh.
-Dinesh

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.




--
mahesh.......

Dinesh Chaminda

unread,
Apr 19, 2012, 7:25:26 PM4/19/12
to Selenium Users
Hi Mahesh,
Thank you for the reply,
I'm using Selenium.DefaultSelenium currently, I have few question on
WebDriver
1. If I use WebDriver then do I need to remove code I've written for
Selenium.DefaultSelenium? or is it working paralleled?
2. Do you have any sample code which uses WebDriver from the
initialization on words?
3. My current assembly version is 2.21, Do I need a different version
to to try your code?

Thanks in advance
-Dinesh

On Apr 19, 4:46 pm, UmaMahesh <umarao...@gmail.com> wrote:
> H dinesh,
>
> please try in this way, i am using webdriver.
>
> driver.findElement(By.xpath("//*[@id=xxxxxxxx]")).sendKeys("c:\\Documents
> and Settings\\Administrator\\Desktop\\abc.doc");
>
> Thanks
> uma mahesh.

Mike Riley

unread,
Apr 19, 2012, 8:03:58 PM4/19/12
to seleniu...@googlegroups.com
If you use WebDriverBackedSelenium most of your existing code can work as-is.   You will just need to change how your start the session and once you have a driver object use it to create the WebDriverBackedSelenium object.

Mike
> > To post to this group, send email to selenium-users@googlegroups.com.
> > To unsubscribe from this group, send email to
> > selenium-users+unsubscribe@googlegroups.com.

Dinesh Chaminda

unread,
Apr 19, 2012, 10:10:24 PM4/19/12
to seleniu...@googlegroups.com
Hi Mike,
   Thank you for the reply. My Selenium start as follows
I first initialize Selenium.DefaultSelenium
private Selenium.DefaultSelenium seleniumSession;

Than launch the browser with the URL, server port, server host and the browser string (IE, Crome, etc..) parameters
 seleniumSession = new DefaultSelenium(ServerHost, ServerPort,
                BrowserString, BrowserURL);

then after that I'm using  seleniumSession object to operate selenium.
Do you have a sample code which is using  WebDriverBackedSelenium  with Selenium.DefaultSelenium that would be great.

Thanks in advance
-Dinesh


Mike Riley

unread,
Apr 20, 2012, 1:22:10 PM4/20/12
to seleniu...@googlegroups.com
You can modify this:
    /**
     * This is a global that is initialized when the DOTN_Helper class is
     * instantiated. It is used to control the DOTN session and is accessed by
     * all the various methods in the classes that extend the base DOTN_Helper
     * class.  We create this object using the WebDriver object we used to start
     * the session.  You get more control over the web page using this than you
     * have with just the WebDriver, but it can run slower for some methods.
     */
    public WebDriverBackedSelenium selenium; // Used for WebDriver and RemoteWebDriver
    /**
     * This is used to create a selenium object that is backed by a Web Driver.
     */
    public RemoteWebDriver driver;  // Used for RemoteWebDriver
    /**
     * This is the browser we are using for this test.  Pulled from testParams
     */
    public String browserName;
    /**
     * This is the base URL we are using for this test.  Pulled from testParams
     */
    public String baseUrlString;

    /**
     * This is the list of browsers that we support when using WebDriver or
     * RemoteWebDriver.<p>
     * The allowed values are:
     * <ul>
     * <li>*firefox</li>
     * <li>*iexplore</li>
     * <li>googlechrome</li>
     * <li>htmlunit</li>
     * <li>safari - Not support yet by Selenium as of 2.5.0</li>
     * <li>opera</li>
     * </ul>
     */
    private final String[] BROWSERS =
    {
        "*firefox",
        "*iexplore",
        "googlechrome",
        "htmlunit",
        "safari",
        "opera"
    };

    /**
     * DOTN_Helper is a creator for the base class DOTN_Helper used by the
     * DOTN_Helper library. This version of the constructor is used if you want
     * to use the RemoteWebDriver to run the test session, but use this library.
     * It will create the WebDriver object and a DefaultSelenium object backed
     * by the RemoteWebDriver.<BR/><BR/>
     * For Firefox it will expect a directory called "Firefox#.profile" in the
     * current directory, where '#' is the version number passed in the
     * testParameters argument.  This profile is simply the directory used by
     * that version of Firefox to configure how that version should behave.  If
     * you do not supply a specific profile the Firefox browser gets invoked
     * with a blank (re: default) profile, which may not have settings you want.
     * @param testParameters
     * @throws Exception
     */
    public DOTN_Helper(TestParameters testParameters) throws Exception
    {
        String str;
        org.openqa.selenium.remote.DesiredCapabilities capability;
        String[] chromeSwitches =
        {
            "--ignore-certificate-errors"
        };
        FirefoxProfile ffProfile;

        switch (Parser.findString(browserName, BROWSERS))
        {
            case 0:
                capability = DesiredCapabilities.firefox();
                str = "Firefox" + browserVersion + ".profile";
                ffProfile = new FirefoxProfile(new File(str));
                capability.setCapability(FirefoxDriver.PROFILE, ffProfile);
                browser = FIREFOX;
                break;
            case 1:
                capability = DesiredCapabilities.internetExplorer();
                browser = INTERNET_EXPLORER;
                break;
            case 2:
                capability = DesiredCapabilities.chrome();
                capability.setCapability("chrome.switches",
                                         Arrays.asList(chromeSwitches));
                //capability.setCapability("chrome.binary",
                //    "C:\\Program Files\\Google\\Google Chrome\\chrome.exe");
                browser = CHROME;
                break;
            case 3:
                capability = DesiredCapabilities.htmlUnit();
                capability.setJavascriptEnabled(true); // Off by default for htmlUnit
                browser = HTML_UNIT;
                break;
            case 4:
                capability = DesiredCapabilities.safari();
                browser = SAFARI;
                break;
            case 5:
                capability = DesiredCapabilities.opera();
                capability.setCapability("opera.logging.level", "FINE");
                browser = OPERA;
                break;
            default:
                throw new Exception("Unknown browser name for RemoteWebDriver: " +
                                    browserName + ".");
        }   // switch

        if (platform.equalsIgnoreCase("WINDOWS") || browserName.equals(
                "*iexplore"))
            capability.setPlatform(Platform.WINDOWS);
        else
            capability.setPlatform(Platform.ANY);

        capability.setVersion(browserVersion);
        Reporter.log("Trying to start test session with: " +
                     capability.toString(), true);
        driver = new RemoteWebDriver(new URL("http://" + host + ":" + port +
                                             "/wd/hub"), capability);
        try
        {   // We have a session started, don't leave it as an orphan now
            str = "Information from navigator object in browser:" +
                  "\nuserAgent: " +
                  driver.executeScript("return window.navigator.userAgent") +
                  "\nappCodeName: " +
                  driver.executeScript("return window.navigator.appCodeName") +
                  "\nappVersion: " +
                  driver.executeScript("return window.navigator.appVersion") +
                  "\nlanguage: " +
                  driver.executeScript("return window.navigator.language") +
                  "\nplatform: " +
                  driver.executeScript("return window.navigator.platform") +
                  "\n";
            System.out.print(str);
            str = str.replace("\n", "<br />");
            Reporter.log(str);
            selenium = new WebDriverBackedSelenium(driver, baseUrlString);
            selenium.open(baseUrlString);
        }   // try
        catch (Exception ex)
        {   // We had an exception after creating the WebDriver instance
            tearDown(); // Make sure we close the browser down
            throw ex;   // Now report the exception
        }   // catch
    }   // DOTN_Helper */

Dinesh Chaminda

unread,
Apr 22, 2012, 7:15:41 PM4/22/12
to seleniu...@googlegroups.com
Thanks Mike, This worked for.
Thank you very much
-Dinesh

Dinesh Chaminda

unread,
Apr 22, 2012, 8:56:49 PM4/22/12
to seleniu...@googlegroups.com
Hi Mike,
    Now that I updated the binaries in my selenium project and now I'm using selenium 2.0 and using you sample code but tweak a bit to suite to my requirement. Now the file upload is working file however I got a new issue in selecting drop down list. In earlier version I used DefaultSelenium session Select method which was worded fine. Now it's not working. It's giving me following error message,

System.InvalidOperationException: An error occurred executing the click atom (UnexpectedJavaScriptError)
   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:line 993
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:line 805
   at OpenQA.Selenium.Remote.RemoteWebDriver.InternalExecute(String driverCommandToExecute, Dictionary`2 parameters) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:line 705
   at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary`2 parameters) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebElement.cs:line 803
   at OpenQA.Selenium.Remote.RemoteWebElement.Click() in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebElement.cs:line 329
   at OpenQA.Selenium.Support.UI.SelectElement.SetSelected(IWebElement option) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Support\UI\SelectElement.cs:line 400
   at OpenQA.Selenium.Support.UI.SelectElement.SelectByText(String text) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver.Support\UI\SelectElement.cs:line 139
   at FMOSelenium.Utilities.Session.SelectDDLByID(String locator, String value) in C:\Workspaces\dev\PHG.FMO\Branches\Dinesh\Tests\FMOSelenium\Utilities\Session.cs:line 397}

Do you have any workaround for this? if so please help me.

Thanks
-Dinesh 

Mike Riley

unread,
Apr 23, 2012, 1:03:16 PM4/23/12
to seleniu...@googlegroups.com
Yes, a few things don't work as expected, so I have wrapper routines that implement them using pure WebDriver methods.  If you do a search you will find examples of how to take screen shots that I and others have posted using the Augmenter class.

Here is the one I use for drop downs.  Note that it uses a By class locator method as an argument:

    /**
     * This method is used to select a particular item from a drop-down list on
     * a web page.  It is designed to make it simpler to handle drop-down lists
     * on web pages.  It does three things:
     * <ol>
     * <li>Wait for the list element to be present on the page for helper.timout
     *     seconds.</li>
     * <li>Selects the list item that matches <i>valueString</i> in the list.</li>
     * </ol>
     * @param ByLocator The locator for a drop-down list.
     * @param valueString The value string you wish to have chosen.  It must not
     *                    start with "value=", but should just be the value by
     *                    itself.
     * @throws Exception Any exception due to the element not being present, etc.
     * @see By
     * @see By#className(java.lang.String)
     * @see By#cssSelector(java.lang.String)
     * @see By#id(java.lang.String)
     * @see By#linkText(java.lang.String)
     * @see By#name(java.lang.String)
     * @see By#name(java.lang.String)
     * @see By#partialLinkText(java.lang.String)
     * @see By#tagName(java.lang.String)
     * @see By#xpath(java.lang.String)
     */
    public void selectFromDropDown(By ByLocator, String valueString)
            throws Exception
    {
        Select select;

        waitForElementPresent(ByLocator, timeout); // Wait until present
        select = new Select(driver.findElement(ByLocator));
        select.selectByValue(valueString);
    }   // selectFromDropDown(String locatorString, String valueString)
Reply all
Reply to author
Forward
0 new messages