locked
Code for image in Coded UI Testing RRS feed

  • Question

  • Whats the code IF i want to upload a new image while using Coded UI testing.Can I use assertions for this.
    Tuesday, May 1, 2012 3:57 PM

Answers

  • Hi Faltu123,

    Here is the code snippet against a FileUpload control in web application, if this isn’t meet your requirement then please let me know:

                string targetAddress = @"http://localhost:8515/WebSite2/image_show.aspx";
                BrowserWindow browser = BrowserWindow.Launch(targetAddress);
                HtmlDocument doc = new HtmlDocument(browser);
                doc.SearchProperties.Add(HtmlDocument.PropertyNames.ControlType,"Document");
                doc.SearchProperties.Add(HtmlDocument.PropertyNames.TagName,"BODY");
    
                HtmlFileInput fileUpload = new HtmlFileInput(doc);
                fileUpload.SearchProperties.Add(HtmlFileInput.PropertyNames.ControlType,"FileInput");
                fileUpload.SearchProperties.Add(HtmlFileInput.PropertyNames.Id,"fileUpload");
                fileUpload.TechnologyName = "Web";
                fileUpload.Find();
               
    
    
                Mouse.DoubleClick(fileUpload);
    
                WinWindow ChooseFileDialog = new WinWindow();
                ChooseFileDialog.SearchProperties.Add(WinWindow.PropertyNames.ControlType,"Window");
                ChooseFileDialog.SearchProperties.Add(WinWindow.PropertyNames.ClassName,"#32770");
                ChooseFileDialog.TechnologyName = "MSAA";
                ChooseFileDialog.Find();
               
                WinEdit fileName = new WinEdit(ChooseFileDialog);
                fileName.SearchProperties.Add(WinEdit.PropertyNames.Name,"File name:");
                fileName.TechnologyName = "MSAA";
    
                
                string nameStr = @"Desert.jpg";
    
    
                fileName.Text = nameStr;
    
                WinSplitButton openbutton = new WinSplitButton(ChooseFileDialog);
                openbutton.SearchProperties.Add(WinSplitButton.PropertyNames.Name, "Open");
                Mouse.Click(openbutton);
    
                HtmlButton uploadButton = new HtmlButton(doc);
                uploadButton.SearchProperties.Add(HtmlButton.PropertyNames.Id,"btn01");
                uploadButton.SearchProperties.Add(HtmlButton.PropertyNames.TagName,"INPUT");
                uploadButton.SearchProperties.Add(HtmlButton.PropertyNames.Type,"submit");
                uploadButton.TechnologyName = "Web";
                
                Mouse.Click(uploadButton);

    Also you could built a data-driven test by this code, all you need to do is iterate the file name value from datasource file and give this value to fileName.Text property.

    For assertion, I'm not sure which part you want to assert. Do you want to assert if the image uploaded is shown or not? If this is what your requirement then you could refer to the code snippet of Vicky provided in following thread, hope this could give you a light:

    Hope this could help you.


    Tony Xiao [MSFT]
    MSDN Community Support | Feedback to us



    Wednesday, May 2, 2012 8:52 AM
    Moderator

All replies

  • Hi Faltu123,

    Here is the code snippet against a FileUpload control in web application, if this isn’t meet your requirement then please let me know:

                string targetAddress = @"http://localhost:8515/WebSite2/image_show.aspx";
                BrowserWindow browser = BrowserWindow.Launch(targetAddress);
                HtmlDocument doc = new HtmlDocument(browser);
                doc.SearchProperties.Add(HtmlDocument.PropertyNames.ControlType,"Document");
                doc.SearchProperties.Add(HtmlDocument.PropertyNames.TagName,"BODY");
    
                HtmlFileInput fileUpload = new HtmlFileInput(doc);
                fileUpload.SearchProperties.Add(HtmlFileInput.PropertyNames.ControlType,"FileInput");
                fileUpload.SearchProperties.Add(HtmlFileInput.PropertyNames.Id,"fileUpload");
                fileUpload.TechnologyName = "Web";
                fileUpload.Find();
               
    
    
                Mouse.DoubleClick(fileUpload);
    
                WinWindow ChooseFileDialog = new WinWindow();
                ChooseFileDialog.SearchProperties.Add(WinWindow.PropertyNames.ControlType,"Window");
                ChooseFileDialog.SearchProperties.Add(WinWindow.PropertyNames.ClassName,"#32770");
                ChooseFileDialog.TechnologyName = "MSAA";
                ChooseFileDialog.Find();
               
                WinEdit fileName = new WinEdit(ChooseFileDialog);
                fileName.SearchProperties.Add(WinEdit.PropertyNames.Name,"File name:");
                fileName.TechnologyName = "MSAA";
    
                
                string nameStr = @"Desert.jpg";
    
    
                fileName.Text = nameStr;
    
                WinSplitButton openbutton = new WinSplitButton(ChooseFileDialog);
                openbutton.SearchProperties.Add(WinSplitButton.PropertyNames.Name, "Open");
                Mouse.Click(openbutton);
    
                HtmlButton uploadButton = new HtmlButton(doc);
                uploadButton.SearchProperties.Add(HtmlButton.PropertyNames.Id,"btn01");
                uploadButton.SearchProperties.Add(HtmlButton.PropertyNames.TagName,"INPUT");
                uploadButton.SearchProperties.Add(HtmlButton.PropertyNames.Type,"submit");
                uploadButton.TechnologyName = "Web";
                
                Mouse.Click(uploadButton);

    Also you could built a data-driven test by this code, all you need to do is iterate the file name value from datasource file and give this value to fileName.Text property.

    For assertion, I'm not sure which part you want to assert. Do you want to assert if the image uploaded is shown or not? If this is what your requirement then you could refer to the code snippet of Vicky provided in following thread, hope this could give you a light:

    Hope this could help you.


    Tony Xiao [MSFT]
    MSDN Community Support | Feedback to us



    Wednesday, May 2, 2012 8:52 AM
    Moderator
  • Thanks Tony.I will try this solution
    Wednesday, May 2, 2012 2:12 PM