How to use the same browser window in multiple classes using TestNG Selenium webdriver in java?

Hi Guys,

I am trying to automate a webpage which has a login and post login has many menu item. I would like to automate it in such a way that it logs into the webpage only once and then use the different menu items. Each new menu item automation is created in a different class.

  1. I have an initialization class which is used to login to the application
  2. Class A: automates 1st menu item in the link (it is extending initialization class)
  3. Class B: automates 2nd menu item in the link (it is extending initialization class)

when I run just Class A or just Class B, it works perfectly fine. However if I try to run all together, it only runs Class A. When it goes to Class B, the driver is not able to locate the window.

Kindly help.

My Code is

init.java

package pack1;

 

public class Init {
public WebDriver driver;
ChromeOptions options;

@BeforeSuite
public void beforeSuite() throws AWTException, InterruptedException, IOException {

//Setting Chrome Driver and disabling the save password option
System.setProperty(“webdriver.chrome.driver”,”C:\\Users\\user\\Desktop\\Demo\\chromedriver.exe”);
options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put(“credentials_enable_service”, false);
prefs.put(“profile.password_manager_enabled”, false);
options.setExperimentalOption(“prefs”, prefs);
driver=new ChromeDriver(options);

//Opening the URL
driver.get(“myURL”);
driver.manage().window().maximize();

//Login to the portal
driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[1]/input”)).sendKeys(username);
driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[2]/input”)).sendKeys(password);
driver.findElement(By.xpath(“.//*[@id=’content-wrapper’]/div/div/div/div/div/div/div/form/div/div[3]/button”)).click();

}
@AfterSuite
public void afterSuite() {
//Closing the driver
// driver.close();

}

}

 

Class A

package pack1;

 

public class ClassA extends Init{

@Test (priority=0, enabled = true)
public void Setup() throws InterruptedException{

//Traversing the menu to reach contract grower setup
Thread.sleep(5000);
driver.findElement(By.linkText(“Menu1”)).click();
driver.findElement(By.linkText(“SubMenu1”)).click();

}

}

 

Class B

package pack1;

public class ClassBextends Init{

@Test (priority=0, enabled = true)
public void Setup() throws InterruptedException{

//Traversing the menu to reach contract grower setup
Thread.sleep(5000);
driver.findElement(By.linkText(“Menu2”)).click();
driver.findElement(By.linkText(“SubMenu2”)).click();

}

}

 

testing.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>
<suite name=”Suite”>
<test name=”Test”>
<classes>
<class name=”pack1.ClassA”/>
<class name=”pack1.ClassB”/>
<class name=”pack1.Init”/>
</classes>
</test> <!– Test –>
</suite> <!– Suite –>

Add Comment
  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.