1

I want to pass this code to my other classes so I don't have to keep pasting it.

This is the class containing the code:

package utility;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class BrowserType {
    public static WebDriver driver;

    @Parameters("browser")
    @Test
    public static void CallBrowser(String browser) {

        if(browser.equalsIgnoreCase("firefox")) {

              driver = new FirefoxDriver();

          // If browser is IE, then do this   

          }else if (browser.equalsIgnoreCase("chrome")) { 

              // Here I am setting up the path for my IEDriver

              {System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
               driver = new ChromeDriver();

              driver.get(Constant.URL);

          } 

    }
}

I want to pass CallBrowser to SetUp which is below:

package automationFramework;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import utility.Constant;
import appModule.SignIn_Action;

public class SignIn {

    public WebDriver driver;



@BeforeMethod
@Parameters("browser")
public void SetUp(String browser) {

     if(browser.equalsIgnoreCase("firefox")) {

          driver = new FirefoxDriver();

      // If browser is Chrome, then do this   

      }else if (browser.equalsIgnoreCase("chrome")) { 

          { System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
          driver = new ChromeDriver();

          { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}
          {driver.manage().window().maximize();}
           driver.get(Constant.URL);
      }
      } 

@Test
public  void signIn() {

    SignIn_Action.Execute(driver, Constant.DevStudentUsername, Constant.DevStudentPassword);      
 }  

@AfterMethod
public void Teardown() {
    driver.quit();

} 

  }

I tried just calling the static class, but then @BeforeMethod is throwing a configuration error, I'm sure I am just doing it wrong with passing driver, and parameters.

Can someone please explain the changes I need to make to both classes to make it work correctly?

These are the errors:

FAILED CONFIGURATION: @AfterMethod Teardown
java.lang.NullPointerException
    at automationFramework.SignIn.Teardown(SignIn.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:786)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

FAILED: signIn
java.lang.NullPointerException
    at appModule.SignIn_Action.Execute(SignIn_Action.java:27)
    at automationFramework.SignIn.signIn(SignIn.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
    Configuration Failures: 1, Skips: 0
===============================================


===============================================
Suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================

This is how i am trying to call the code:

public WebDriver driver;



@BeforeMethod
@Parameters("browser")
public void SetUp(String browser) {

     BrowserType.CallBrowser(browser);
      } 
Mosam Mehta
  • 1,658
  • 6
  • 25
  • 34
Elsid
  • 243
  • 2
  • 10
  • 25

1 Answers1

0

Your issue is that your driver is not initialized when you call teardown. You need to make sure it is initialized in all cases. I would go through your code with a debugger and check to make sure that when you hit the setup and teardown method that the driver is in fact initialized at setup and exists at tear down. You also have two drivers present when things run. One is your static driver that you declare in the first part of your code, and then you have another non-static driver located with your actual tests. I would fix that as it may be causing issues.

The easiest way to solve your issue is to make sure the driver is ALWAYS initialized during setup, even if the string does not match either option.

public void SetUp(String browser) {

     if(browser.equalsIgnoreCase("firefox")) {

          driver = new FirefoxDriver();

      // If browser is Chrome, then do this   

      }else { 

          System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");
          driver = new ChromeDriver();

          { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}
          {driver.manage().window().maximize();}
           driver.get(Constant.URL);
      }
} 
Mo H.
  • 1,788
  • 1
  • 18
  • 28
  • yeah there is some sort of issue but it is really confusing, since I don't have much java knowledge. I got part of it to work by removing the static driver and changing it to webdriver driver = new firefoxdriver(); which runs in set up correctly but now I don't know how to pass that driver to the rest of the class such as test & aftermethod. – Elsid Apr 22 '15 at 17:50
  • Can you provide an example? Thanks – Elsid Apr 22 '15 at 17:55
  • @Elsid here is a rough example, there are better ways to do it – Mo H. Apr 22 '15 at 17:56
  • thanks Mo, that is what I have now I was trying to replace all that code by moving it to another class and calling it. – Elsid Apr 22 '15 at 17:59
  • That is what I originally had, yes that works, but I was trying to move the Setup code to another class and calling it, It doesn't work when i try to call it, since I'm probably calling it wrong. – Elsid Apr 22 '15 at 18:05