Your ultimate guide

Showing posts with label C# Automation. Show all posts
Showing posts with label C# Automation. Show all posts

Locators


How to Inspect a web element:
    On every web page, you will find various web elements, and each of these elements is made up of tags, attributes, and values.
    Example: <tag attribute='values'>
  • Right-click any part of the page and Choose Inspect or Shortcut key F12.
  • Select the Element tab (by default it is selected).
  • Note: This inspect tab is called DOM(Document Object Model)
    


What is findElement?
    It is the method to locate elements on the webpage.
What is By?
    By is the mechanism used to locate elements within the document with the help of locator value.
What is a Locator?
    It is an address that identifies web elements within documents with the help of locator value.

Locators: There are eight types of locators.
  1. id
  2. name
  3. XPath
  4. CSS Selector
  5. Class Name
  6. Tag Name
  7. Link Text
  8. Partial Link Text
1. id Locator:
    If an element in your web page has an 'id' attribute, it can be identified using the value of that attribute.

    Syntax (Java): driver.findElement(By.id("id_value"))
    Syntax (C#): driver.FindElement(By.Id("id_value"))
    Syntax (Python): driver.find_element(By.ID, "id_value")

2. name Locator:
    If an element in your web page has a 'name' attribute, it can be identified using the value of that attribute.

    Syntax (Java): driver.findElement(By.name("name_value"))
    Syntax (C#): driver.FindElement(By.Name("name_value"))
    Syntax (Python): driver.find_element(By.NAME, "name_value")

3. XPath Locator:
    XPath is an XML path used for navigation through the HTML structure of the page.
    There are two types of XPath
    1. Absolute XPath
    2. Relative XPath
To learn more about XPath Click Here.

    Syntax (Java): driver.findElement(By.xpath("//xpath"))
    Syntax (C#): driver.FindElement(By.XPath("//xpath"))
    Syntax (Python): driver.find_element(By.XPATH, "//xpath")
Note: we can copy XPath directly from the DOM, Right-Click on the particular element field(on - DOM) then Select copy in and click on Copy XPath.

4. CSS Selector Locator:
    CSS is used to create style rules for web pages and can be used to identify any web element.
To learn how to write CSS Selector Click Here.

    Syntax (Java): driver.findElement(By.cssSelector("value"))
    Syntax (C#): driver.FindElement(By.CssSelector("value"))
    Syntax (Python): driver.find_element(By.CSS_SELECTOR, "value")

5. Class Name Locator:
    If an element in your web page has a 'class' attribute, it can be identified using the value of that attribute.

    Syntax (Java): driver.findElement(By.className("class_value"))
    Syntax (C#): driver.FindElement(By.ClassName("class_value"))
    Syntax (Python): driver.find_element(By.CLASS_NAME, "class_value")

6. Tag Name Locator:
    This Locator is mainly used to get the list of web elements with the particular tag name. We will use this locator very rarely, because here we will assign the values based on the tag, not an attribute. 

    Syntax (Java): driver.findElements(By.tagName("a"))
    Syntax (C#): driver.FindElements(By.TagName("a"))
    Syntax (Python): driver.find_elements(By.TAG_NAME, "a")

7. Link Text Locator:
    Text (Visible Text) used in Hyperlinks can also locate elements.

    Syntax (Java): driver.findElement(By.linkText("text"))
    Syntax (C#): driver.FindElement(By.LinkText("text"))
    Syntax (Python): driver.find_element(By.LINK_TEXT, "text")

8. Partial Link Locator:
    A part of the text(Visible Text) in the Hyperlink can also identify an element.

    Syntax (Java): driver.findElement(By.partialLinkText("text"))
    Syntax (C#): driver.FindElement(By.PartialLinkText("text"))
    Syntax (Python): driver.find_element(By.PARTIAL_LINK_TEXT, "text")




Creating NUnit Projects



What is NUnit?
    NUnit is an Open source unit testing framework for Microsoft .NET, it works the same as Junit or TestNG framework in Java.

In Java, we have a framework called JUnit, TestNG
In Python, we have Framework PyTest
In JavaScript, we have MOKA, JASMIN
For C# it is NUnit.

NUnit Features:
  • Tests can be run in parallel
  • Every Test Case can be added to one or more categories or to allow selective running
  • Skip the tests
  • Order can be set to tests
  • Parameterization­­­­
  • Data-driven Testing


Creating NUnit Projects:
  • Right-click on the Solution folder Select Add  Click on New Project


  • Select Project type as Test.
  • Select the project template as NUnit Test Project  Click on Next.
  • And give the Project name and Location  Click on Next.
  • Select the Target Framework  Click on Create.


 

When we open NUnit Project, we will see one default Class with the name on UnitTest.cs.

It contains one Example program ⬇:

namespace NUnitSelenium

{
    public class Tests
    {
        [SetUp]
        public void Setup()
        {
        }
        
        [Test]
        public void Test1()
        {
            Assert.Pass();
        }
    }
}

[SetUp]:

    If you have pre-requests for the test like launching the browser, setting the implicit time out, maximizing, cleaning up the data, and hitting the URL ….. all pre-requests we mention here.

[Test]:

    Test Attribute holds the core logic. Whatever the functionalities we perform we write in the Test attribute.

[TearDown]:

    The last thing which will happen after the Test Attribute run is, Close the browser, delete the cookies and delete the data …. And so on.

 

 

 

How to run any NUnit test:

  • First, we need to build the entire project. For that Click on Build menu > then select Build solution.
  • After the build is done then we access the test explorer. (Click on Test  menu > then Click on Test Explorer) 

  • Now In the Test Explorer, we can run the Test attributes individually:
  • For that, Right-click on the Test (on the Explorer tab) then click on Run.

  • To see the output, On the output tab select Show output from: as Tests.



- Next Page: C# Selenium

Selenium with C# along with NUnit Framework

Selenium With C# along with NUnit Framework:

0. Difference Between C# and Java

"The content on this page is currently being developed and is not yet complete. Please check again later for updates." 
 
1. Visual Studio Setup
2. C# Basics, and about NuGet
3. C# Basics 2
4. Creating NUnit Project








The rest of the Content will be Uploaded Soon!