Writing Our First Test

Writing Our First Test

Prerequisites :

  1. An IDE of your like (using vscode here)

  2. An executable - Selenium Webdriver

  3. Maven, Java installed

  4. A project with a sample test case using Se and testNG

I've used a simple test case i.e., hitting a URL, retrieving title and verifying it and quitting the browser.
In this we've used basic and simple Test Annotations - @Test @AfterMethod @BeforeMethod

You can create a TestNG class directly via Eclipse but, in VsCode try to create a new file as testng.xml following the below project structure.

Screen Shot 2022-04-05 at 3 47 23 PM

Lets create our first TestNG using a classic example -

  1. Add TestNG library in the pom.xml

    <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.8.7</version>
    <scope>test</scope>
    </dependency>

  2. Add Maven Surefire plugin (Optional - When trying to run testNG in VSCode via Maven) | Can be found here

  3. A Test Class

  4. testNG.xml

    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
    <suite name="Suite1" verbose="1" >
    <test name="Regression1">
    <classes>
    <class name="com.example.AppTest"/>
    </classes>
    </test>
    </suite>

Program -

package com.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;

public class Login {

    WebDriver driver;

    @BeforeTest
    public void setup() {
      System.setProperty("webdriver.chrome.driver", "/resources/chromedriver");
      driver = new ChromeDriver();
      String url = "https://opensource-demo.orangehrmlive.com/";
      driver.get(url);
      System.out.println("Starting the browser session");
    }

    @Test
    public void tests() {
          WebElement uname = driver.findElement(By.name("txtUsername"));
          uname.sendKeys("Admin");
          WebElement pwd = driver.findElement(By.id("txtPassword"));
          pwd.sendKeys("admin123");
          driver.findElement(By.id("btnLogin")).click();
          String expectedTitle = "OrangeHRM";
          String actualTitle = driver.getTitle();
          Assert.assertEquals(actualTitle, expectedTitle);
      }


    @AfterTest
    public void afterMethod() {
          System.out.println("Closing the browser session");
          driver.quit();
    }

}