Sunday 7 June 2015

Upload a file using selenium web driver in c#

        selenium can not identify the Input tag having attribute as file. Also selenium is not able to handle windows popup like Open File Dialog.
Ex: 
<input type="file" id="browse1" name="browse"> </file>
So To handle such kind of situation in selenium we have to use third party tools like Autoit, Point Position etc....

Prerequisite/Requirements:

  1. Visual Studio

  2. NUnit: Testing Framework.

  3. Point Position software / Tool: To find the X and Y coordinate of "Browse" Button.    

    point Position Tool: download it from this link:  http://freewareapp.com/point-position_download/

    Point Position Tool:
        It is used to to find the x & y coordinate of any control with respect to scree resolution.
        It has four corner keep any corner on button or any control whose x & Y coordinates you want to   find and click it on corner. it will shows the x and y coordinate of it. 
    see in below screen shot.
    It shows the x & Y coordinate of Open button by using Point Position Tool which is present of OpenFileDialog Window. 



    using System;
    using NUnit.Framework;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Firefox;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Support.UI;
    using OpenQA.Selenium.Support;
    using System.Threading;
    using System.Windows;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.IO;

    namespace
    UploadingFileOnNaukri
    {
        [TestFixture]   
        public class UploadingFile

        {
            IWebDriver driver = null ;
            IWebElement element = null ;
            ChromeOptions options = null ;
          
            [SetUp]
            public void SetUp()

            {
                options = new ChromeOptions();
                options.AddArgument( " start-maximized " );
                driver = new ChromeDriver("G:\\Selenium_Csharp\\Jar\\chromedriver_win32", options);
                driver.Navigate().GoToUrl("http://my.naukri.com/manager/createacc2.php?othersrcp=11499&wExp=N");
            }

            [Test]
            public void TestUploadingFileMethod()

            {
                Thread.Sleep(300);
                ((IJavaScriptExecutor)driver).ExecuteScript(" window.scrollTo(0,600) ; " ) ;
               
                //explicitly write the X , Y coordinate of Browse button on Naukri by using Point Position Tool.
                Thread.Sleep(5000);
                int x = 529;
                int y = 339;
                MouseEvents.LeftMouseClick(x, y);
               
                MouseEvents.LeftMouseClick(x, y);
                Thread.Sleep(2000);

                //click on d-drive
                MouseEvents.LeftMouseClick(491, 401);
               
                //click on Avinash folder which is present in D:\ drive.
                Thread.Sleep(3000);
                MouseEvents.LeftMouseClick(616, 243);

                //click on Open button of File Dialog
                Thread.Sleep(3000);
                MouseEvents.LeftMouseClick(886,552);

                //Now select the file name FileUpload.txt
                Thread.Sleep(3000);
                MouseEvents.LeftMouseClick(623,241);

                //Now finally click on open Button to open / upload the selected file.
                Thread.Sleep(3000);
                MouseEvents.LeftMouseClick(886,552);
                Thread.Sleep(7000);
               
            }

            [TearDown]
            public void TearDown()
            {
                element = null;
                driver.Quit();
            }
        }
    }


    Add another File in existing project
    Right Click on project -> Add new class -> Name as MouseEvents.cs
    Write the below code into it:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Drawing;


    namespace UploadingFileOnNaukri
    {
        class MouseEvents
        {
            private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
            private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, uint dwExtraInf);


            public static void LeftMouseClick(int xpos, int ypos)
            {
                int x = Convert.ToInt16(xpos) ;   //set x position
                int y = Convert.ToInt16(ypos) ;  //set y position
                Cursor.Position = new Point( x ,  y ) ;
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0) ; //make left button down
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) ; //make left button up

            }
        }
    }




    See Also Below Links:



















1 comment:

  1. Selenium Automation Blogs By Avinash Pande: Upload A File Using Selenium Web Driver In C >>>>> Download Now

    >>>>> Download Full

    Selenium Automation Blogs By Avinash Pande: Upload A File Using Selenium Web Driver In C >>>>> Download LINK

    >>>>> Download Now

    Selenium Automation Blogs By Avinash Pande: Upload A File Using Selenium Web Driver In C >>>>> Download Full

    >>>>> Download LINK tB

    ReplyDelete