Using Selenium in C# to automate Web Browser

Using Selenium in C# to automate Web Browser

Below is snippet of code using Selenium in C# to automate the following tasks

  1. To Login to codeschool using firefox browser
  2. Sign with email address using selector Name 
    1. The selector Name of element is found by using inspect
  3. Sign with password using selector ID.
    1. The selector Id of element is found using inspect 
  4. Click the submit button using selector Xpath 
    1. To find a Xpath of element (Chrome) do a inspect on the element
    2. Click that particular element and do a right click and choose Copy Xpath
  5. Test using Assert if the next page with have certain title page otherwise return false or test fail
  6. Test using Assert if specific section on the page will have certain label otherwise return false or test fail

————————————————–

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System;
using System.Linq;

namespace UnittestGoogle
{
[TestClass]
public class Googletest
{

[TestMethod]
public void TestSomething()
{
//Load FirefoxDrive to open Firefox browser
IWebDriver driver = new FirefoxDriver();
//1. Open Firefox and goto Code School Sign Page
driver.Navigate().GoToUrl("https://www.codeschool.com/users/sign_in");
//2. Select Email textbox via Name and send emailaddress
driver.FindElement(By.Name("user[login]")).SendKeys("jkanimea@gmail.com");
//3. Select Password textbox via Id and send my password login
driver.FindElement(By.Id("user_password")).SendKeys("qtpvz7661");
//4. Select Button via XPath and click
driver.FindElement(By.XPath("//*[@id=\"sign-in-form\"]/form/div[2]/button")).Click();

//5. Test if the title page will be "Dashboard | Code School" otherwise wait for 20s
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
wait.Until(d => d.Title == "Dashboard | Code School");

//6. Test If element will have "Looking for something specific?" on the 
// page return true otherwise return false
Assert.AreEqual("Looking for something specific?", driver.FindElement(By.XPath("//*[@id=\"account-status\"]/div/div[1]/div/h2")).Text);

driver.Quit();
}

}

}

 

If everything goes well, You have passed test has below !!!

Selenium01

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *