Wednesday, July 29, 2015

Selenium : How to retrieve text from a Textbox in Selenium WebDriver using C#

Sometimes we need to retrieved data in a form for edit and for this we search data by using a search keyword. For example, we need to edit employee personal information and in that case we need to search the Employee information using Employee ID. All the data are displayed in a text box and we verify that proper data is displayed in the textbox for this employee id. Selenium WebDriver uses different method to retrieve data from textbox for assertion.

Selenium WebDriver has GetAttribute() method to capture textbox data.   

image

Common syntax to use this command,

ElementName.GetAttribute(“value”);

Here “value” is the argument and it returns the value of the attribute of that element. 

For example in C#,

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 verify the data displayed at FirstName and LastName field. 

Page link : http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/form-template-update/defaultcs.aspx

image

Step 1 : Open Visual Studio and create a new unit test project.

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  TestMethod1().

image

Note that we need to add following namespaces for webdriver.

image

Step 4 : Write the code for getting First name and last name from textboxes.

image

Note that we use GetAttribute("value") method here that returns the text displayed on that element and this element is a textbox.

Step 5 : Write the code for verification. 

image

Step 6 :  Now close the driver. The TestMethod1() will be,

image

Step 7 : Now build the application by going to Build > Build Solution and go to test explorer and run the application by right clicking on the test name and click on Run Selected Tests.

image 

Test will run and it capture the value from textbox and verify expected value.

image

Happy scripting with Selenium WebDriver…!!!

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#.

Friday, July 3, 2015

Sahi : How to install and run Sahi on your windows machine

Sahi is an automation testing tool to automate web application. It has excellent record and playback feature to record and run application. In our previous post we have discuss about Sahi’s feature. Here we will show on how to install Sahi on our machine.

Download Sahi: Sahi has a community edition as well as commercial edition. Here we download Community edition and install it.

image

  • Sahi Download starts.

Install Sahi: After finishing download, go to the installer directory and follow the following steps.

Step1 : Double click on the installer file named “install_sahi_v50_……jar” file.

image

Sahi installation wizard is displayed.

image

Step 2 : Click on Next button.

image

Step 3 : It shows the software list that will be installed. Click on Next button

image

Step 4 : License agreement page is displayed.

  • Select the options “I accept the terms of this license agreement”.
  • Click on Next button.

image

Step 5 : Select the installation path where Sahi will be installed and click on Next button.

image

Step 6 : Click on OK button.

image

Step 7 : Select the packs that we want to install. By default all packs are selected. Grayed packs are mandatory. Click on Next button.

image

Step 8 : Sahi installation starts.

image

Step 9 : After finishing installation click on Next button.

image

Step 10 : Shortcut selection page is displayed.

  • Select the option “Create shortcuts in the Start-Menu”.
  • Select the option “Create additional shortcuts on the desktop”.
  • Select “Current User”.
  • Keep the application name “Sahi”.
  • Click on Next button.

image

Step 11 : This is the last page of the installation wizard. Following messages are displayed.

  • “Installation has completed successfully”.
  • “An uninstaller program has been created in”. It shows the path of uninstaller file.
  • A button is displayed showing “Generate an automatic installation Script”. Click on this button will generate a automatic installer.

Click on Done button. Sahi wizard window will be closed.

image

Step 12 : Note that an icon is displayed at the desktop. Double click on the icon.

image

Step 13 : Three windows are displayed.

  • Sahi Dashboard : This is the main sahi dashboard window where the browser list is displayed. We can select any browser here to record Sahi. Generally all the installed browsers are displayed at Sahi dashboard. Sometimes it might not be displayed due to installation path issue. We will submit a separate post on this issue.
  • Sahi needs to change browser proxy settings. For this it shows this window. Clicking on Continue button will close the window. You can close sahi clicking on Quit Sahi button.
  • Sahi Command Prompt : This window displays error message if there is any error happens. It also displays Sahi run status.

image

In our Next post we show on how to automate a web application using Sahi.

Happy scripting with Sahi as a Sahi manner…!!!

Sahi : What is Sahi?

Sahi is an automation tool to automate web application. It has both open source and commercial version. It has easy record and playback feature.

Record & Playback : It has an easy record and playback feature and it can automat any web application. It is easy for a non technical tester to record.

Cross Browser Testing : Script recording in one browser can run on other available browsers. So it supports cross browser testing.

Web Element Identification : It can locate elements very easily with its on build API. Also it can locate any dynamic ID without taking help on Xpath or tools. It can automate application built in using ExtJS, ZK, Dojo, YUI or any other framework.

AJAX : Sahi can automate web application built it AJAX. It does not need to use any Wait statement.

Rich Inbuilt Reports and Logs : It supplies complete info of script execution. It can report the exact line of script that fails. It provides graph reports and end to end result. Reports can be customizable.

Fast Parallel Batch Playback : Together of multiple scripts can make a suit and it can run multiple script in one machine or multiple machine at a time. It can run from Command prompt and continuous Integration also.

Simple Powerful Scripting : Sahi Script is based on JavaScript. So it can use the power of JavaScript. It can call any Java code or library. It can communicate Databases, Excel sheets, CSV files for data driven testing.

Inbuilt Excel Framework : It has inbuilt excel where tester can write simple test case in excel file and it will execute the script using the excel file.

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#.

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...