Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

How to Clear Browser Cookies with Selenium Web Driver

This code below applies to Selenium .Net.

Clearing browser cookies before starting your test is important to test the application in real life scenarios. If you are using Selenium Web Driver for your web test automation, you can use this method below to clear all cookies.

Let's assume that your Selenium Web Driver name is _webDriver; You can create a void method below and then call the method before you navigate to the application URL.

public void ClearBrowserCache()
{
_webDriver.Manage().Cookies.DeleteAllCookies(); //delete all cookies
Thread.Sleep(5000); //wait 5 seconds to clear cookies.

}
Print

Send Selenium Test Result Through Outlook Attachment

You need to use these assemblies below:

using System;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.IO;
using Microsoft.Office.Interop.Outlook;
using System.Net.Mime;
using System.Collections;
using Attachment = Microsoft.Office.Interop.Outlook.Attachment;
using Exception = System.Exception;

public static void OutlookSendEmail(string recepientAddress,string messageSubject,string messageBody,string attachmentfile,string attachmentDisplayName)
{
            try
            {
                // Create the Outlook application by using inline initialization.
                Application oApp = new Application();

                //Create the new message by using the simplest approach.
                MailItem oMsg = (MailItem)oApp.CreateItem(OlItemType.olMailItem);

                //Add a recipient.
                // TODO: Change the following recipient where appropriate.
                Recipient oRecip = (Recipient)oMsg.Recipients.Add(recepientAddress);
                oRecip.Resolve();

                //Set the basic properties.
                oMsg.Subject = messageSubject;
                oMsg.Body = messageBody;

Print

Selenium Guest Book Test Case Java Code

 

package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Seleniumguestbooktest {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://www.seleniummaster.com/seleniumguestbook/Guestbook.aspx";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
Print

Selenium Guest Book Test Case C# Code

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    [TestFixture]
    public class Seleniumguestbooktest
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
       
        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "http://www.seleniummaster.com/seleniumguestbook/Guestbook.aspx";
            verificationErrors = new StringBuilder();
        }

Print

Testing form with Selenium Code Sample Java

 

package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SeleniumTestForm {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://www.seleniummaster.com/seleniumformtest/registrationform.aspx";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
Print

Testing form with Selenium Code Sample C#

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    [TestFixture]
    public class SeleniumTestForm
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
       
        [SetUp]
        public void SetupTest()
        {
            driver = new FirefoxDriver();
            baseURL = "http://www.seleniummaster.com/seleniumformtest/registrationform.aspx";
            verificationErrors = new StringBuilder();
        }
 

Print

Testing form with Selenium

Most web applications have multiple forms by which users interact with the web server. Test cases are developed based on the forms that are associated with business scenarios. This site provides a sample registration form and Selenium test code for you to test the application.

Note: the registration form is a dummy form that does not collect any user information. It is used only for testing.

URL: http://www.seleniummaster.com/seleniumformtest/registrationform.aspx

Test Case: Authorized users are able to register (Let's assume that you have already logged in the system).

Step Action Test Data Expected Result Pass/Fail Note
1 Open web browser and navigate to the registration form URL Web Browser: FireFox

URL: http://www.seleniummaster.com/

seleniumformtest/registrationform.aspx

Browser opens and the registration form loads.    
2 Enter first name First name: ABCDEFG System accepts first name    
3 Enter last name Last name: HIGKLMN System accepts last name    
4 Enter email address Email: This email address is being protected from spambots. You need JavaScript enabled to view it. System accepts email address    
5 Enter phone number Phone: 123-456-7890 System accepts phone number    
6 Select a book from the drop down list Book: Selenium Web Driver Selenium Web Driver is selected.    
7 Select an Operating System from the radio button options Option: Windows XP Windows XP is selected    
8 Click on the Register button   The button should be clicked.    
9 Verify the registration confirmation   The page should display the confirmation "Thank you for your registration"