Search Here!

Tuesday, April 24, 2012

Selenium : Upload File



This Coded UI method helps you to upload file through Browse button and handles with Window Componet to 'Select File' , the Coded UI record and play actions need to integrate with C#.


using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using System.Threading;

public void ImportScoMetadataFile()
        {
            BrowserWindow bwindow;
            bwindow = BrowserWindow.Locate("Browse Window Name");
            UITestControl Browse = new UITestControl(bwindow);
            Browse.TechnologyName = "Web";
            Browse.FilterProperties.Add(HtmlDocument.PropertyNames.Title, "Browse Window Name");
            Browse.SearchProperties.Add(HtmlFileInput.PropertyNames.Id, "txtSpreadsheetFile");
            Browse.SearchProperties.Add(HtmlFileInput.PropertyNames.Name, "txtSpreadsheetFile");
            Browse.FilterProperties.Add(HtmlFileInput.PropertyNames.ControlDefinition, "onkeydown=\"return false;\" id=txtSpreadsh");
            Mouse.DoubleClick(Browse);
            WinWindow popup = new WinWindow();
            popup.TechnologyName = "MSAA";
            popup.SearchProperties.Add(WinWindow.PropertyNames.Name, "Choose", PropertyExpressionOperator.Contains);
            popup.SearchProperties.Add(WinWindow.PropertyNames.ClassName, "#32770");
            Thread.Sleep(2000);
            if (popup.Exists)
            {
                WinEdit choose_Edit = new WinEdit(popup);
                choose_Edit.SearchProperties.Add(WinEdit.PropertyNames.Name, "File name:", PropertyExpressionOperator.Contains);
                choose_Edit.Text = ConfigurationManager.AppSettings["UploadFilePath"]; // Provide file path from app.config
                WinButton Browsebutton = new WinButton(popup);
                Browsebutton.SearchProperties[WinButton.PropertyNames.Name] = "Open";
                Mouse.Click(Browsebutton);
            }

No comments:

Post a Comment