How to create a Base Class in Selenium and use it with test scripts

0 votes
How to create a Base Class in Selenium and use it with test scripts?
Jul 17, 2019 in Selenium by Donna
5,682 views

1 answer to this question.

0 votes

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();
 }
}
answered Jul 18, 2019 by Anvi
• 14,150 points

Related Questions In Selenium

0 votes
1 answer

How to create a test case with TestNG in Selenium?

Hey Deepak, to create a Test Case ...READ MORE

answered May 21, 2019 in Selenium by Abha
• 28,040 points
350 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
5,115 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
5,843 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
3,605 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,010 points
4,350 views
0 votes
1 answer

How to extract text from a web page using selenium and save it as a text file?

Hello Isha, you can checkout this code ...READ MORE

answered May 7, 2019 in Selenium by Anvi
• 14,150 points
27,092 views