Sei sulla pagina 1di 9

Exception Handling in c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpTestingExamples.ExamplePrograms
{
class ExceptionDemo1
{
public void addition(int x,int y)
{
int Res = x + y;
Console.WriteLine("Addition Result :" + Res);
}

public void substraction(int x, int y)


{
int Res = x - y;
Console.WriteLine("Substraction Result :" + Res);
}

public void multiplication(int x, int y)


{
int Res = x * y;
Console.WriteLine("Multiplication Result :" + Res);
}

public void division(int x, int y)


{
try
{
int Res = x / y;
Console.WriteLine("Division Result :" + Res);
}catch(DivideByZeroException e)
{
Console.WriteLine(e.ToString());
}
finally
{
Console.WriteLine("This Line Execute always ...");
}
}
public void findFactorial(int n)
{
int fact = 1;
for(int i=n;i>=1;i--)
{
fact *= i;
}
Console.WriteLine("The Factroial of " + n + " is " +
fact);
}

public void displayEven()


{
Console.WriteLine("Even Numbers :");
for (int i=10;i<=20;i++)
{
if ((i % 2)==0)
{
Console.WriteLine("Even Number :" + i);
}
}
}
}
}

using CSharpTestingExamples.ExamplePrograms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpTestingExamples
{
class Program
{
static void Main(string[] args)
{
ExceptionDemo1 obj = new ExceptionDemo1();
obj.displayEven();
obj.addition(30, 40);
obj.substraction(120, 100);
obj.division(25, 0);
obj.multiplication(12, 10);
obj.findFactorial(4);

Console.ReadLine();

}
}
}

Multiple Catch blocks:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpTestingExamples.ExamplePrograms
{
class ExceptionDemo1
{
public void addition(int x,int y)
{
int Res = x + y;
Console.WriteLine("Addition Result :" + Res);
}

public void substraction(int x, int y)


{
int Res = x - y;
Console.WriteLine("Substraction Result :" + Res);
}

public void multiplication(int x, int y)


{
int Res = x * y;
Console.WriteLine("Multiplication Result :" + Res);
}
public void division(int x, int y)
{
try
{
int Res = x / y;
Console.WriteLine("Division Result :" + Res);
}
catch (DivideByZeroException e)
{
Console.WriteLine(e.ToString());
}
catch (Exception e1)
{
Console.WriteLine(e1.ToString());
}
finally
{
Console.WriteLine("This Statement Execute always
...");
}
}

public void findFactorial(int n)


{
int fact = 1;
for(int i=n;i>=1;i--)
{
fact *= i;
}
Console.WriteLine("The Factroial of " + n + " is " +
fact);
}

public void displayEven()


{
Console.WriteLine("Even Numbers :");
for (int i=10;i<=20;i++)
{
if ((i % 2)==0)
{
Console.WriteLine("Even Number :" + i);
}
}
}
}
}
using CSharpTestingExamples.ExamplePrograms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpTestingExamples
{
class Program
{
static void Main(string[] args)
{
ExceptionDemo1 obj = new ExceptionDemo1();
obj.displayEven();
obj.addition(30, 40);
obj.substraction(120, 100);
obj.division(25, 0);
obj.multiplication(12, 10);
obj.findFactorial(4);

Console.ReadLine();

}
}
}

throw keyword in Exception handling:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpTestingExamples.ExamplePrograms
{
class ExceptionDemo2
{
public int StrLengh(string str)
{
if (str==null)
{
throw new Exception("null value has given input to the
method !!!!!!!!!!");
}
return str.Length;
}

}
}

using CSharpTestingExamples.ExamplePrograms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpTestingExamples
{
class Program
{
static void Main(string[] args)
{
ExceptionDemo2 obj = new ExceptionDemo2();
int val1=obj.StrLengh("program");
Console.WriteLine("# of Chars :" + val1);

try {
int val2 = obj.StrLengh(null);
Console.WriteLine("# of Chars :" + val2);
}catch(Exception e)
{
Console.WriteLine(e.Message);
}
int val3 = obj.StrLengh("Language");
Console.WriteLine("# of Chars :" + val3);

Console.ReadLine();

}
}
}

First Selenium Tests :

using CSharpTestingExamples.ExamplePrograms;
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium.Chrome;
using System.Threading;
namespace CSharpTestingExamples
{
class Program
{
public static IWebDriver oBrowser = null;
static void Main(string[] args)
{
intialize();
navigate();
login();
createUser();
deleteUser();
logout();
closeApplication();
}
public static void intialize()
{
oBrowser = new ChromeDriver();
Thread.Sleep(5000);
oBrowser.Manage().Window.Maximize();
}

static void navigate()


{
oBrowser.Navigate().GoToUrl("http://localhost/login.do");
Thread.Sleep(5000);
}

static void login()


{
oBrowser.FindElement(By.Id("username")).SendKeys("admin");
oBrowser.FindElement(By.Name("pwd")).SendKeys("manager");

oBrowser.FindElement(By.XPath("//*[@id='loginButton']/div")).Click();
Thread.Sleep(5000);
}

static void logout()


{
oBrowser.FindElement(By.LinkText("Logout")).Click();
Thread.Sleep(5000);
}
static void closeApplication()
{
oBrowser.Quit();
}

static void createUser()


{

oBrowser.FindElement(By.XPath("//*[@id='topnav']/tbody/tr[1]/td[5]/a/d
iv[2]")).Click();
Thread.Sleep(4000);

oBrowser.FindElement(By.Id("gettingStartedShortcutsPanelId")).Click();
Thread.Sleep(2000);
oBrowser.FindElement(By.ClassName("buttonText")).Click();
Thread.Sleep(2000);

oBrowser.FindElement(By.Name("firstName")).SendKeys("demo");
Thread.Sleep(1000);

oBrowser.FindElement(By.Name("lastName")).SendKeys("User1");
Thread.Sleep(1000);
oBrowser.FindElement(By.Name("email")).SendKeys("demo@gmail.com");
Thread.Sleep(1000);

oBrowser.FindElement(By.Name("username")).SendKeys("demoUser1");
Thread.Sleep(1000);

oBrowser.FindElement(By.Name("password")).SendKeys("Welcome1");
Thread.Sleep(1000);

oBrowser.FindElement(By.Name("passwordCopy")).SendKeys("Welcome1");
Thread.Sleep(1000);
oBrowser.FindElement(By.ClassName("buttonTitle")).Click();
Thread.Sleep(4000);
}

static void deleteUser()


{
oBrowser.FindElement(By.XPath("//span[text()='User1,
demo']")).Click();
Thread.Sleep(4000);

oBrowser.FindElement(By.Id("userDataLightBox_deleteBtn")).Click();
Thread.Sleep(4000);
IAlert oAlert=oBrowser.SwitchTo().Alert();
oAlert.Accept();
Thread.Sleep(4000);
}
}
}

Potrebbero piacerti anche