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
12,620 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 22, 2019 in Selenium by Abha
• 28,140 points
1,215 views
0 votes
1 answer
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,500 points
13,686 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,190 points
9,149 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,910 points
7,227 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 3, 2020 in Selenium by Sri
• 3,190 points
7,879 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
33,731 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP