Aldrin Combalicer Sy
Aldrin Combalicer Sy

Reputation: 51

How to use "extends" in Selenium Webdriver (JAVA)

Any idea on how can I execute 2 classes one after the other in 1 browser. I tried to use "extends" in my 2nd class but I think there is something missing in my code. Any idea on how can I fix this?

My 1st Class

package TestNg_Package;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

 public class T3EmployeeMaintenance{
 public static WebDriver driver = new FirefoxDriver(); 

  @BeforeClass
  public void invokeBrowser() {

  //driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
  driver.get("http://xxxxxxxxxxxxxx.net");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  }

  @Test(priority = 1)
  public void Login() {

   driver.findElement(By.name("login_id")).sendKeys("admin");
   driver.findElement(By.name("password")).sendKeys("12345678");
   driver.findElement(By.name("buttonAction")).click();     
   }

  @Test(priority = 2)
  public void GoToEmpMain(){
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);      
  driver.findElement(By.cssSelector(".fi-archive")).click();
  driver.findElement(By.linkText("Employee Maintenance")).click();
   }
}

My 2nd Class

 package TestNg_Package;

 import java.util.concurrent.TimeUnit;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.firefox.FirefoxDriver;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;

 public class T3DetailedTimeTrackingReport extends T3EmployeeMaintenance{

  @Test(priority = 1)
  public void GoToDTTR(){
  driver.findElement(By.cssSelector("i.fi-graph-bar")).click();
  driver.findElement(By.linkText("Detailed Time Tracking Report")).click();
   }

  @Test(priority = 2)
  public void ClickDropdown(){
  driver.findElement(By.xpath("//*[@id='panel-1063-innerCt']")).click();
    WebElement ele = driver.findElement(By.id("ext-gen1204"));
    ele.click();
  }
}

My TestNg.XML Code

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite One">
 <test name="Test One" > 
  <classes> 
  <class name="TestNg_Package.T3EmployeeMaintenance" /> 
   <class name="TestNg_Package.T3DetailedTimeTrackingReport" /> 
 </classes> 
 </test> 
 </suite> 

My goal is after executing the "Class 1 @Test(Priority=2)" the "Class 2 @Test(Priority=1)" will execute right away.

Upvotes: 0

Views: 5744

Answers (3)

Aarbi
Aarbi

Reputation: 1

You have to finish class at the end. in your html simply add at the end of each class you declared.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite One">
 <test name="Test One" > 
  <classes> 
  <class name="TestNg_Package.T3EmployeeMaintenance"> </class> 
   <class name="TestNg_Package.T3DetailedTimeTrackingReport"> </class>
 </classes> 
 </test> `
 </suite> 

Upvotes: 0

Roman Łomowski
Roman Łomowski

Reputation: 141

  1. T3DetailedTimeTrackingReport contains 4 test methods:
    • Login
    • GoToEmpMain
    • GoToDTTR
    • ClickDropdown

is this what you wanted ?

  1. @BeforeClass annotated method is executed before every test class. So two classes -> two executions

  2. Having separate drivers for each test class helps test isolation so maybe it is worth to run each class in fresh unharmed driver ?

  3. Please consider releasing resources aquired for your tests ( close() method )

  4. If you stil want to perform all tests on the same driver you could do it like this:

    @BeforeSuite
    public void invokeBrowser() {
      //set up your driver here
    }
    
    @AfterSuite(alwaysRun=true)
    public void closeBrowser() {
      driver.close()
    }
    
  5. If you decide to make each test class to use fresh driver (for the purpose of isolation):

    @BeforeClass
    public static void makeSureBrowserReady() {
      driver = new FirefoxDriver();
      //set up your driver here
    }
    
    @AfterClass(alwaysRun=true)
    public static void closeDriver() {
      driver.close()
    }
    
  6. Your question seems more related to testNG than selenium/xml. Here is some doc about it http://testng.org/doc/documentation-main.html

Upvotes: 1

murali selenium
murali selenium

Reputation: 3927

Create new class and start browser there. Extend this class to those two classes

 public class Setup {

public static WebDriver driver;

@BeforeSuite
public void startUp(){

    driver=new FirefoxDriver();
    driver.manage().window().maximize();

}

@AfterSuite
public void tearDown(){

    //driver.quit();
}
}

Please delete public static WebDriver driver = new FirefoxDriver(); in T3EmployeeMaintenance

    public class T3EmployeeMaintenance extends  Setup{
 //public static WebDriver driver = new FirefoxDriver(); //no need of this now

 @BeforeClass
 public void invokeBrowser() {
  //...

for other class also

 public class T3DetailedTimeTrackingReport extends Setup{
 //...

Thank You, Murali

Upvotes: 0

Related Questions