0

I'm using Selenium WebDriver with Testng (started using an .xml file) to test a site using multiple browsers.

I'm trying to create a method which will take in a parameter from the xml file, and using an IF statement, detect the browser, create the relevant driver, and return it.

The trouble I'm having is when I try to pass through the parameter to the method. If I pass through "Chrome" for example, the IF statement works fine and the driver is created. However if I use the parameter itself, the driver isn't created and the test fails the first time it is used.

Here's the set up code I'm using:

@Parameters ({"driver_property_value","driver_property_location","browser"})
@BeforeClass
public void setUp(String driverPropertyValue, String driverPropertyLocation, String browser) throws Exception {
    Setup setup = new Setup();

    //set properties
    System.setProperty(setup.driverPropertyValue(driverPropertyValue),setup.driverPropertyLocation(driverPropertyLocation));
    driver = setup.driver(browser);
    baseUrl = setup.baseURL();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

here's what it's calling:

public WebDriver driver(String browser)
{
    WebDriver value = null;

    if (browser == "chrome")
    {
    value = new ChromeDriver();
    }
    return value;
}

and here's the testng xml that I'm using to run the tests and pass the parameters in:

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd">
<suite name = "testng" verbose="1">
<parameter name="driver_property_value" value="webdriver.chrome.driver"/>
<parameter name="driver_property_location" value="C:/chromedriver.exe"/>
<parameter name="browser" value="chrome"/>
<test name="chrome_tests">
    <packages>
        <package name="com.LoginPage"/>
    </packages>
</test>

The first setup seems to be working fine, it's just the the driver selection that doesn't seem to work when using a parameter.

Any help or advice would be appreciated.

Thanks

p.s. here's a failure trace, not sure if it'll help or not.

java.lang.NullPointerException
at com.LoginPage.Login_Logout.setUp(Login_Logout.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:80)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:543)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:212)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
at org.testng.TestRunner.privateRun(TestRunner.java:753)
at org.testng.TestRunner.run(TestRunner.java:613)
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:1137)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1062)
at org.testng.TestNG.run(TestNG.java:974)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)
ZnArK
  • 1,533
  • 1
  • 12
  • 23

1 Answers1

1

Do not use the reference equality operator == to compare Strings. Use equals or equalsIgnoreCase.

The reference equality operator checks that both operands refer to the same instance of an object. With Strings and many object types, this will rarely work the way you expect.

The expression browser == "chrome" resolves to false because even if the browser variable has the value "chrome", it will most likely be a different instance of the string representing "chrome". For much more detail on what this means, please refer to this question.

So with that expression resolving to false, driver returns a null, which your setUp proceeds to use happily as though it's a valid object instance, resulting in the NullPointerException.

Change your comparison to this:

if (browser.equals("chrome")) {
   value = new ChromeDriver();
}

There are several variations to this statement. You can use equalsIgnoreCase to match "chrome" in any combination of character cases, and you could reverse the order of the literal "chrome" and the browser local variable. What this would do is prevent a NullPointerException from occurring on that line if null was passed in as the browser parameter.

if ("chrome".equalsIgnoreCase(browser)) {
   value = new ChromeDriver();
}
Community
  • 1
  • 1
Jonathon Faust
  • 12,396
  • 4
  • 50
  • 63
  • Ah I see, so the other objects were working as it was simply passing objects around which were all strings with the same text, therefore working. As soon as the IF came into play, the objects were different and not being matched. Thanks for clearing that up for me, and also for the background info, it's going to be really useful for me going forward. – user1483193 Jun 27 '12 at 09:35