Tuesday, 3 November 2015

Basic Selenium functions with C#

Basic Selenium functions with C#


public OpenQA.Selenium.IWebDriver iWebDriver;
        public static int isloggedin = 0;
        public StreamWriter sw;
        public string desc;
        public string mode;
        
        public IWebDriver StartTest()
        {
            if (isloggedin == 0)
            {
                iWebDriver = new FirefoxDriver();
                iWebDriver.Manage().Window.Maximize();
                login();
                isloggedin = 1;
            }
            return iWebDriver;
        }

        public int RandomNumber()
        {
            Random r = new Random();
            return r.Next(1, 1000);
        }

        public void login()
        {
            iWebDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(300));
            iWebDriver.Navigate().GoToUrl(dataFile("URL"));

            // Enter user name

            iWebDriver.FindElement(By.Id("txtEmail")).Clear();
            iWebDriver.FindElement(By.Id("txtEmail")).SendKeys(dataFile("UserName"));
            System.Threading.Thread.Sleep(5);

            // Enter password
            iWebDriver.FindElement(By.Id("txtPassword")).Clear();
            iWebDriver.FindElement(By.Id("txtPassword")).SendKeys(dataFile("Password"));

            // Click Submit button
            iWebDriver.FindElement(By.Id("lnkSubmit")).Click();
            System.Threading.Thread.Sleep(5000);
        }


Sample Script for Framework:


using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System.Threading;
using System.IO;

namespace SeleniumTest
{
      public class TC_001 : Logging
    {
        public String startTime;
        public String endTime;
        public String status;
        Logging c1 = new Logging();
        
        public void CreateUser(IWebDriver WebDriver)
        {
            startTime = DateTime.Now.ToString();
            if (excel(this.GetType().Name).Equals("Y"))
            {
                
                WebDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(420));

                try
                {
                    Executionlog(DateTime.Now.ToString(), module(this.GetType().Name), this.GetType().Name, "Starting Test");

                    WebDriver.Navigate().GoToUrl("Example Website");
                    WebDriver.FindElement(By.XPath("//a[contains(text(),'NEW CONTACT')]")).Click();
                    WebDriver.FindElement(By.Id("new-contact")).Click();
                    System.Threading.Thread.Sleep(5000);
                    new SelectElement(WebDriver.FindElement(By.Id("newcontact-location"))).SelectByIndex(1);
                    WebDriver.FindElement(By.XPath("//input[@title='FIRST NAME']")).Clear();
                    WebDriver.FindElement(By.XPath("//input[@title='FIRST NAME']")).SendKeys("Tony" + RandomNumber());
                    WebDriver.FindElement(By.XPath("//input[@title='LAST NAME']")).Clear();
                    WebDriver.FindElement(By.XPath("//input[@title='LAST NAME']")).SendKeys("Stark" + RandomNumber());
                    WebDriver.FindElement(By.XPath("//a[text()='Finish']")).Click();
                    System.Threading.Thread.Sleep(5000);
                    WebDriver.FindElement(By.XPath("//a[text()='Customer Info']")).Click();

                    Executionlog(DateTime.Now.ToString(), module(this.GetType().Name), this.GetType().Name, "Customer Created Successfully");

                    System.Threading.Thread.Sleep(5000);
                    status = "Pass";

                }
                catch (Exception e)
                {
                    endTime = DateTime.Now.ToString();
                    status = "Fail";
                    ErrorLog(DateTime.Now.ToString(), module(this.GetType().Name), this.GetType().Name, e.StackTrace.ToString());
                    
                }

                if (status.Equals("Pass"))
                {
                    status = "Pass";
                }
                else
                {
                    status = "Fail";
                }
                endTime = DateTime.Now.ToString();
                Executionlog(DateTime.Now.ToString(), module(this.GetType().Name), this.GetType().Name, status);
                addSummary(startTime, endTime, description(this.GetType().Name), module(this.GetType().Name), this.GetType().Name, status);
          }
            else
            {
                endTime = DateTime.Now.ToString();
                Executionlog(DateTime.Now.ToString(), module(this.GetType().Name), this.GetType().Name, "Not Executed");
                addSummary(startTime, endTime, description(this.GetType().Name), module(this.GetType().Name), this.GetType().Name, "Not Executed");
            }
        }

     }
}

No comments:

Post a Comment