Your ultimate guide

C# With NUnit _ C# Selenium

 Launch Chrome Browser:

To work on Chrome browser First we need to download “chromedriver.exe”(On NuGet package manager). After that by using following code we can launch the chrome browser.

 

IWebDriver driver = new ChromeDriver();

 

Here IWebDriver is interface, (get more knowledge in internet)

 

Generally the By using Chrome browser version to will download relevant chromedriver. If browser is updated to new version then we need to update the chromedriver also.

To overcome this problem there is “WebDriver Manager”, this WebDriver Manager package check the browser version internally based on our browser version it will automatically download the browser driver.

For that download “WebDriver Manager” in NuGet package manager, then follow the below code to access the browser.

For Chrome:

new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());

IWebDriver driver = new ChromeDriver();

 

For Firefox:

new WebDriverManager.DriverManager().SetUpDriver(new FirefoxConfig());

IWebDriver driver = new FirefoxDriver();

 

For Edge:

new WebDriverManager.DriverManager().SetUpDriver(new EdgeConfig());

IWebDriver driver = new EdgeDriver();

 

 

here we are creating object for that we use new

WebDriverManager is namespace

Dirvermanager() is the Class

SetUpDriver() is the method

Based on your browser which you want to use Config the object for that browser.

 

driver.Url(“https://www.google.com”);

(To get the Url in the browser)

TestContext.Progress.WriteLine(driver.Url);

(To print the Url on console (in NUnit we use TestContext.Progess to print result on output) )

 

 

 

driver.Title;

Title is property

(To get the title of the web page)

Example: TestContext.Progress.WriteLine(driver.Title);

 

driver.Close();

driver.Quit();

driver.Manage().Window.Maximize();

Manage() is method

Window is property

Maximize() is method

driver.PageSource;

PageSource is the Property

 

 

 

 

driver.FindElement(By.ID(“Id Value”));

(Locators: ClassName, CssSelector, Equals, Id, LinkText, Name, PartialLinkText, ReferenceEquals, TagName, XPath)

FindElement is method this will help us to find the element based on locator that you provide.

 

driver.FindElement(By.ID(“Id Value”)).SendKeys(“xxxxx”);

driver.FindElement(By.ID(“Id Value”)).Clear();

Clear the information whatever information on that text box.

driver.FindElement(By.ID(“Id Value”)).Click();

 

 

 

CssSelector:

tagName[attribute=’value’]

driver.FindElement(By.CssSelector(“tagName[attribute=’value’]”));

 

XPath

//tagName[@attribute=’value’]

driver.FindElement(By.XPath(“//tagName[@attribute=’value’]”));

 

Thread.Sleep(3000);

 

driver.FindElement(By.ClassName(“class value”)).Text;

To get the Visible text of a web element we use Text method

 

 

 

IWebElement link = driver.FindElement(By.LinkText(“Gmail”));

 

 

String hrefAtt = link.GetAttribute(“href”);

String expectedUrl = “https://gmail.com”

Assert.AreEqual(expectedUrl, hrefAtt);

(Assert is a class

AreEqual is a method

These are used to check the expected data and actual data is match or not)

 

Xpath:

Parent to child traverse: (own)

(“//div[@class=’form-group’][5]/lable/span/input”)

CssSelector:

Example :

<input class=”term” id=’dollar’>

driver.FindElement(By.CssSelector(“#dollar”));       //# idValue

driver.FindElement(By.CssSelector(“.term”));          //dot className

 

 

<lable class=”term” >

            <spam css=’1’>

                        <input value=”sign in”> </input>

            </spam>            

            <spam css=’2’>

                        <input value = “bye”></input>

            </spam>

</lable>

 

à.term spam:nth-child(1) input

Here we suppurate every tag with <space>, and multiple tags has same name then we use  “nth-child(1)” here 1 means  select 1st tag.

 

 

No comments:

Post a Comment