Hi Donna, you can use following code snippet to get an idea how to create Base class in Selenium:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
public class BaseClass
{
WebDriver driver;
@BeforeClass
public void setupApplication()
{
Reporter.log("=====Browser Session Started=====", true);
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://gmail.com");
Reporter.log("=====Application Started=====", true);
}
@AfterClass
public void closeApplication()
{
driver.quit();
Reporter.log("=====Browser Session End=====", true);
}
}
Now, use this Base Class in Actual test script like this:
import org.openqa.selenium.By;
import org.testng.annotations.Test;
public class InvalidLogin extends BaseClass
{
@Test(description="This TestCase will perform valid login")
public void loginToApplication() throws Throwable
{
driver.findElement(By.name("email")).sendKeys("Admin1");
driver.findElement(By.id("password")).sendKeys("admin1");
driver.findElement(By.id("submit")).click();
driver.navigate().back();
Thread.sleep(5000);
}
@Test(description="This TC will perform invalid login")
public void loginToApplication1()
{
driver.findElement(By.name("email")).sendKeys("admin1");
driver.findElement(By.id("password")).sendKeys("admin2");
driver.findElement(By.id("submit")).click();
}
}