Discussion:
[testng-users] Test throwing NullPointerException when running through TestNG.xml file - not executing TestNG annotations to initialise driver.
Baba
2018-10-29 17:27:00 UTC
Permalink
Hi,

When I run my framework via TestNG.xml file, it appears as though it is
ignoring the "base" class which I am inheriting the driver from and it is
not executing the @BeforeMethod. Does anyone know why it would ignore
TestNG annotations? The framework is also using Cucumber.

Base class:

public class base {
public WebDriver driver;
private Local l;

@BeforeTest(alwaysRun=true)
@org.testng.annotations.Parameters(value={"config", "environment"})
public void initialize(String config_file, String environment) throws Exception {
JSONParser parser = new JSONParser();
JSONObject config = (JSONObject) parser.parse(new FileReader("src/main/java/resources/" + config_file));
JSONObject envs = (JSONObject) config.get("environments");

DesiredCapabilities capabilities = new DesiredCapabilities();

Map<String, String> envCapabilities = (Map<String, String>) envs.get(environment);
Iterator it = envCapabilities.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
}

Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
it = commonCapabilities.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if(capabilities.getCapability(pair.getKey().toString()) == null){
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
}
}

String username = System.getenv("BROWSERSTACK_USERNAME");
if(username == null) {
username = (String) config.get("user");
}

String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
if(accessKey == null) {
accessKey = (String) config.get("key");
}

String app = System.getenv("BROWSERSTACK_APP_ID");
if(app != null && !app.isEmpty()) {
capabilities.setCapability("app", app);
}

if(capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true"){
l = new Local();
Map<String, String> options = new HashMap<String, String>();
options.put("key", accessKey);
l.start(options);
}

driver = new RemoteWebDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);
}


Java test class:

public class AsosLogin extends base {

@Test
@Given("^user goes to the ASOS website$")
public void user_goes_to_the_ASOS_website() throws Throwable {
driver.get("https://www.google.com/ncr");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("BrowserStack");
element.submit();
Thread.sleep(5000);

Assert.assertEquals("BrowserStack - Google Search", driver.getTitle());
}

@Test
@When("^user logs in$")
public void user_logs_in() throws Throwable {
System.out.println("user logs in");
}

@Test
@Then("^they should be logged in$")
public void they_should_be_logged_in() throws Throwable {
System.out.println("user should be logged in!!");
}
}


Feature File:

@AsosLogin
Feature: Logging into ASOS

Scenario: Going to ASOS website
Given user goes to the ASOS website
When user logs in
Then they should be logged in


Runner File:

@CucumberOptions(
features = "src/test/java/features",
glue = "stepDefinitions"
)

public class TestRunner extends AbstractTestNGCucumberTests {

}


TestNG.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel" thread-count="4" parallel="tests">
<test name="SingleTestChrome">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="chrome"/>
<classes>
<class name="cucumberOptions.TestRunner"/>
</classes>
</test>

<test name="SingleTestFirefox">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="firefox"/>
<classes>
<class name="cucumberOptions.TestRunner"/>
</classes>
</test>

<test name="SingleTestSafari">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="safari"/>
<classes>
<class name="cucumberOptions.TestRunner"/>
</classes>
</test>

<test name="SingleTestIE">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="ie"/>
<classes>
<class name="cucumberOptions.TestRunner"/>
</classes>
</test>
</suite>


Does anyone know how I can resolve this issue? Thanks!
--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+***@googlegroups.com.
To post to this group, send email to testng-***@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Loading...