Click here to Skip to main content
15,891,204 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?
Posted
Updated 4-Mar-17 20:11pm
Comments
Member 13038083 4-Mar-17 13:31pm    
Hi,
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
 
v2
@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.
@BeforeSuite
@Parameters({"browser", "username", "password"})

public void setup(String browser, String username, String password) throws Exception{
if(browser.equalsIgnoreCase("firefox")){
driver = new FirefoxDriver();
}else if(browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe")

public class TestBase {
public static WebDriver driver;
;
driver = new ChromeDriver();
}
driver.get("https://www.google.co.in/");

driver.findElement(By.xpath("//*[@id='gbw']/div/div/div[1]/div[1]/a")).click();
driver.findElement(By.id("Email")).sendKeys(username);
driver.findElement(By.id("next")).click();
Thread.sleep(2000);
driver.findElement(By.id("Passwd")).sendKeys(password);
driver.findElement(By.id("signIn")).click();
System.out.println(driver.getTitle());
}


TestNG XML file as below:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Log Suite Example" verbose="1">
<parameter name="browser" value="Chrome" />
<parameter name="username" value="Test@mailinator.com"/>
	<parameter name="password" value="Test1234"/>s
	<listeners>
		<listener class-name="pac1.ListenerClass" />
	</listeners>

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

</suite>
 
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