·
Selenium:
Selenium is basically used to automate the testing
across various web browsers. It supports various browsers like Chrome,
Mozilla, Firefox, Safari, and IE, and you can very easily automate browser
testing across these browsers using Selenium WebDriver.
·
C# has an IDE – VISUAL STUDIO
open visual studio
------------------------------------------------------------------------------------------------------------------------
1) select on a create a new project.
---> in dropdown select
c#
2) next in search box ,
search NUnit Test Project and choose
NUnit Test Project
and give name for project.
3)next goto nuget package manager and install
->Selenium.WebDriver
->Selenium.WebDriver.ChromeDriver //this
is used to intaract with browser version
related to browser
4) next open .cs file in application
using
NUnit.Framework;
using OpenQA.Selenium;
using
OpenQA.Selenium.Chrome;
namespace TestProjectselenium
{
public class Tests
{
//Hooks in
Nunit
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
System.Console.WriteLine("load browser");
IWebDriver webDriver = new ChromeDriver();
//Navigate to
site
webDriver.Navigate().GoToUrl("https://localhost:44327/customer");
System.Threading.Thread.Sleep(20000);
webDriver.FindElement(By.Name("CustomerId")).SendKeys("5674157434905");
webDriver.FindElement(By.Name("CustomerName")).SendKeys("vawmsdffdffdgdfdfhisdf");
webDriver.FindElement(By.Name("CustomerEmail")).SendKeys("vamwsdffdhddffgfdfsi@gmail.com");
//var txtname =
webDriver.FindElement(By.Id("CustomerName"));
//Assert.That(txtname.Displayed,Is.True);
//txtname.SendKeys("DSF");
//click on CustomerList
System.Threading.Thread.Sleep(5000);
webDriver.FindElement(By.Id("btnCustomerList")).Click();
//btnClear
//btnCancel
System.Threading.Thread.Sleep(10000);
//
webDriver.Close(); close driver
}
[TearDown]
public void
EndTest()
{
//close the
browser
System.Threading.Thread.Sleep(1000);
webDriver.Close();
Console.WriteLine("closed browser");
//myclass.some();
}
}
}
}
Run:
1) Goto
view
2) Testexplorer
3) Right
click on test method and click on run.
using
NUnit.Framework;
using
OpenQA.Selenium;
using
OpenQA.Selenium.Chrome;
using System;
namespace TestProjectselenium
{
public class Tests
{
//create the
reference for the browser
IWebDriver webDriver = new ChromeDriver(); //match version
[SetUp]
public void
Initialize()
{
System.Threading.Thread.Sleep(2000);
//open the
browser
webDriver.Navigate().GoToUrl("https://localhost:44327/customer");
Console.WriteLine("opened browser");
webDriver.Manage().Window.Maximize();
}
[Test]
public void ExecuteTest()
{
//perform
browser operations
//click on
btnRegisterForm
webDriver.FindElement(By.Id("btnRegisterForm")).Click();
System.Threading.Thread.Sleep(2000);
webDriver.FindElement(By.Name("CustomerId")).SendKeys("10035");
webDriver.FindElement(By.Name("CustomerName")).SendKeys("rajesh");
webDriver.FindElement(By.Name("CustomerEmail")).SendKeys("rajeshmamilla123@gmail.com");
//click on submitbutton
System.Threading.Thread.Sleep(2000);
webDriver.FindElement(By.Id("btnsubmit")).Click();
System.Threading.Thread.Sleep(6000);
Assert.AreEqual(webDriver.FindElement(By.Id("textaftersubmit")).Text,"submittedsuccessfully");
Console.WriteLine("executed test");
}
[TearDown]
public void
EndTest()
{
//close the
browser
System.Threading.Thread.Sleep(1000);
webDriver.Close();
Console.WriteLine("closed browser");
//myclass.some();
}
}
}
Comments
Post a Comment