Click here to Skip to main content
15,877,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have three class files. In first class, only one test case is used for login Gmail account. rest of the classes having only one test case and both are used for navigating Inbox and sent items. I have created testng.xml to run these multiple classes. Now the problem is when first class is being executed, I could be log in Gmail account. But when it comes to second class, I am unable to navigate to Inbox as I did not perform login operation in class 2 and class 3.

So it thrown me an error Null Pointer Exception. I know the reason why I am getting this exception. But I want to perform login action only one time but it should be available to all classes which means When I am going to execute class 2 and class 3, it should not throw error. It should continue from the class 1. How I can achieve this task.

I googled a lot. None of them helped me. Please suggest me any idea if you have. I have tried to extend my class also. I got no result. Please guide me where am I struggling?
Updated 4-Mar-17 20:11pm
Member 13038083 4-Mar-17 13:31pm    
Can anyone please share the suggestion on how to achieve the above said scenario using webdriver
Thanks in Advance

You need to catch the page loaded event to know if your are already signed in to Gmail or not and then run the appropriate code: selenium page loaded event[^]

Google has an API Lib in most modern languages for gmail and all their other services. Why not use that instead? Much simpler: Gmail API Overview  |  Gmail API  |  Google Developers[^]
Share this answer
@Member 11898276

It can be achieved by adding Login steps in @BeforeSuite as below and you can create different classes and mention all in TestNG XML file.
@Parameters({"browser", "username", "password"})

public void setup(String browser, String username, String password) throws Exception{
driver = new FirefoxDriver();
}else if(browser.equalsIgnoreCase("chrome")){

public class TestBase {
public static WebDriver driver;
driver = new ChromeDriver();


TestNG XML file as below:
<!DOCTYPE suite SYSTEM "" >
<suite name="Log Suite Example" verbose="1">
<parameter name="browser" value="Chrome" />
<parameter name="username" value=""/>
	<parameter name="password" value="Test1234"/>s
		<listener class-name="pac1.ListenerClass" />

	<test name="TestNG logs sample" preserve-order="true">
	       <class name="pac1.Inbox"></class>
	       <class name="pac1.SentItems"></class>
		<class name="pac1.Spam"></class>

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900