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 in Selenium by Donna
324 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 by Anvi
• 13,960 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

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,110 points
1,107 views
0 votes
1 answer
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,150 points
1,231 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,150 points
977 views
0 votes
1 answer