38734

Running multiple classes in TestNG

Question:

I am trying to automate a scenario wherein, I want to Login once into the application & then do manipulations without having to re-login again.

Consider that, I have the code to login into the application in the @BeforeSuite method in a specific class.

public class TestNGClass1 { public static WebDriver driver; @BeforeSuite public static void setUp(){ System.setProperty("webdriver.chrome.driver", "D://Softwares//chromedriver.exe"); driver = new ChromeDriver(); //driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("https://www.myfitnesspal.com"); } @AfterSuite public static void close(){ driver.close(); } }

I have my @test method in TestNGClass2 which basically tries to click on some login buttons.

public class TestNGClass2 extends TestNGClass1 { public static WebDriver driver; @Test public static void login(){ System.out.println("Entering the searchQuery Box"); WebElement signUpWithEmailBtn = driver.findElement(By.xpath(".//*[@id='join']/a[2]")); System.out.println("srchTxtBox Box"); signUpWithEmailBtn.click(); } }

I have another class TestNGClass3 which has another @Test method which needs to be run after TestNGClass2 is completed.

public class TestNGClass3 extends TestNGClass1{ public static WebDriver driver; @Test public static void signIn(){ WebElement emailAddress = driver.findElement(By.id("user_email")); emailAddress.clear(); emailAddress.sendKeys("asdsa@gmail.com"); WebElement password = driver.findElement(By.id("user_password")); password.clear(); password.sendKeys("sdass"); WebElement continueBtn = driver.findElement(By.id("submit")); continueBtn.click(); } }

testng.xml file is as follows:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Regression Test"> <classes> <class name="com.test.TestNGClass2" /> <class name="com.test.TestNGClass3" /> </classes> </test> <!-- Test --> </suite> <!-- Suite -->

Is my approach right, since I'm getting "Null Pointer" exception when the code reaches the 'login' method of TestNGClass2 ?

Answer1:

I think you just need to get rid of this line in both your TestNGClass2 and TestNGClass3

public static WebDriver driver;

You're already storing the driver in the base class TestNGClass1, so when you have that line in your other classes, you're basically hiding the one that is instantiated.

I'd also consider changing the base class access modifier to protected since you probably don't want classes that aren't children of that base class to access the driver.

Recommend

  • Creating Wordpress like shortcode in laravel blade
  • How to start JBoss from Jenkins?
  • Need help about sound processing
  • How do I remove a member from my iOS Developer Team?
  • Avoid adding methods and properties to custom object
  • CSS border-image - critical
  • How to use get all NSLocalization using genstrings while preserved current translations
  • App not appearing in iCloud “Manage Storage” list
  • How to optimize multithreaded program for use in LSF?
  • How to structure an adjacency list for this A* program
  • Managing ObjectContext in Workflow Foundation 4
  • Checking if URL exists - HTTP Request always returns an exception
  • Select2 does not create name attribute
  • How to use Exception Handling in Selenium Webdriver?
  • Object doesn't support property or method 'valid'
  • Breaking down CRUD In Use Case Modeling
  • git post-receive hook to update multiple servers
  • symfony render json_array entity type and save using form
  • JPA CDI Injecting DAO into an Entity
  • SSIS Designer is running VERY slowly
  • How to add the custom button on google's(device) native application in android?
  • Ruby and class variables in inherit class
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Compare struct to a constant in C
  • Most efficient way to move table rows from one table to another
  • Django Haystack Rebuild Index
  • How to delay loading a property with linq to sql external mapping?
  • does jqgrid support a multiple checkbox list for editing
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Optimizing database types to compact database (SQLite)
  • TFS: Get latest causes slow project reloading
  • Cross-Platform Protobuf Serialization
  • Running a C# exe file
  • JSON with duplicate key names losing information when parsed
  • Do I've to free mysql result after storing it?
  • Jquery - Jquery Wysiwyg return html as a string
  • Turn off referential integrity in Derby? is it possible?
  • Reading document lines to the user (python)