Showing posts with label NuGet. Show all posts
Showing posts with label NuGet. Show all posts

Monday, July 27, 2015

Selenium : How to download file in selenium WebDriver using C#

In many of the test scenarios, we need to download file to test that file download features are working or not. At the time of writing automated script on file download we have to interact with the web element as well as browser file download dialog. Our Selenium WebDriver API can only works on clicking the button to display the File download dialog but cannot interact with the file dialog. In this situation we need to use the power of C#.

.Net has a library to handle file upload dialog. It has a SendKeys class that has a method SendWait(string keys). It sends the given key on the active application and waits for the message to be processed. It does not return any value. 

image

Common syntax to use this command,

SendKeys.SendWait(string keys);

Here keys is the string of keystrokes.

For example in C#,

image

SendKeys.SendWait() takes a string like “{Enter}” that it executes the enter key from keyboard. When File download dialog is displayed, OK button is activated by default. At this time if we press enter key from keyboard it executes the OK command.

Note that we need to add System.Windows.Forms namespace to get this method.

image

Lets do a short project on how we can implement in our test scenario. For example we have the following page where we have to download a file by clicking download link.

Page link : http://sample-videos.com/

image

Step 1 : Open Visual Studio and create a new project with Console Application type.

image

Step 2 : Add a reference for Webdriver from Nuget package manager. How to add a reference from Nuget package can be found in my another post.

image

Step 3 : Write the following code for opening page using Firefox Browser under static void Main(string[] args) method.

image

Note that we need to add following namespaces for webdriver.

image

Step 4 : Write the code for clicking on Download link of the page to open file download dialog.

image

Step 5 : Write the code to handle file upload dialog to click OK button.

image

image

Note that we need to add the following namespace SendKeys.SendWait().

image

We will not get this namespace if we do not add “System.Windows.Forms” reference from Reference manager window. We get this reference by right clicking on Reference and click on Add Reference option.

image

image

Reference section will be,

image

Note : Sometimes we need to add Thread.Sleep(30) method before SendKeys.SendWait() method to activate the OK button. We have to add System.Threading namespace to implement this statement.

image

Step 6 :  Now close the driver. Main method will be,

image

Step 7 : Run the application by clicking on Start button. It opens the browser and download file using SendKeys.SendWait() methods.

Happy scripting with Selenium  Webdriver using the power of C#.

Thursday, July 2, 2015

Selenium : How to upload file in selenium WebDriver using C#

In many of the test scenarios, we need to upload file to test that file upload features are working or not. In case of image file we need to ensure that uploaded images are properly displayed. At the time of writing automated script on file upload we have to interact with the web element as well as windows file upload dialog. Our Selenium WebDriver API can only works on clicking the button to display the File Upload dialog but cannot interact with the file dialog. In this situation we need to use the power of C#.

.Net has a library to handle file upload dialog. It has a SendKeys class that has a method SendWait(string keys). It sends the given key on the active application and waits for the message to be processed. It does not return any value. 

image

Common syntax to use this command,

SendKeys.SendWait(string keys);

Here keys is the string of keystrokes.

For example in C#,

image

First line of SendKeys.SendWait() takes a string as argument that it writes the file name text box of file upload window.

Second line of SendKeys.SendWait() takes a string like “{Enter}” that it executes the enter key from keyboard.

Note that we need to add System.Windows.Forms namespace to get this method.

image

Lets do a short project on how we can implement in our test scenario. For example we have the following page where we have to upload an image.

Page link : http://nervgh.github.io/pages/angular-file-upload/examples/image-preview/

image

Step 1 : Open Visual Studio and create a new project with Console Application type.

image

Step 2 : Add a reference for Webdriver from Nuget package manager. How to add a reference from Nuget package can be found in my another post.

image

Step 3 : Write the following code for opening page using Firefox Browser under static void Main(string[] args) method.

image

Note that we need to add following namespaces for webdriver.

image

Step 4 : Write the code for clicking on Browse button of the page to open file upload dialog.

image

Step 5 : Write the code to handle file upload dialog to browse image and click open button.

image

image

Note : Sometimes we need to add Thread.Sleep(30) method between these two SendKeys.SendWait() method to stop to press enter key until File name typing is complete. We see system sometimes try to type quickly on file name box and do missing some part from beginning and at the time if enter key pressed System throws a invalid file path error message. To prevent this message we use this statement. We have to add System.Threading namespace to implement this statement.

Note that we need to add the following namespace SendKeys.SendWait().

image

We will not get this namespace if we do not add “System.Windows.Forms” reference from Reference manager window. We get this reference by right clicking on Reference and click on Add Reference option.

image

image

Reference section will be,

image

Step 6 : Click on Upload button of the page.

image

image

Step 7 :  Now close the driver. Main method will be,

image

Step 8 : Run the application by clicking on Start button. It opens the browser and upload the image file using SendKeys.SendWait() methods.

That’s all for now. In our future post we will show on how we can upload file using Java.

Till then ….Happy scripting with Selenium  Webdriver using the power of C#.

Monday, June 29, 2015

Selenium : How to add Selenium Webdriver package to a Visual Studio project

If we automate our application using C# API of Selenium Webdriver then we need to create a Visual Studio project and add Webdriver reference to that project. In that case we need to download Selenium API for C# from download page of Selenium official site and then add reference to that project or install Selenium Webdriver package from Nuget.org using Nuget package manager. Here we will show on how we add Webdriver reference from Nuget. 

Step 1 : Go to Solution Explorer and Right click on Reference and click on Manage NuGet Packages…

Step 2 : Manage NuGet Packages window is displayed.

Step 3 : Enter the text Selenium WebDriver at search text box and press enter from Keyboard

Step 4 : Selenium WebDriver package is displayed at Search list.

Step 5 : Click on Install button.

Step 6 : Latest WebDriver will be installed and added as Reference.

 

Step 7 : Select Selenium WebDriver Support Classes package and click on Install button.

Step 8 : After finishing installation and click on Close button to close the Package Manager window.

Step 9 : Now verify that all the References are showing properly at the solution explorer.

image

Cypress: How to handle browser-based authentication pop up dialog in Cypress

Five years ago I have written a blog on how to handle browser-based authentication for selenium webdriver.   Now it is for cypress. Cypress...