Discussion:
[testng-users] Running Cucumber framework from TestNG.xml file seems to be skipping TestNG annotations and not initializing the remote driver.
Baba
2018-10-29 17:19:08 UTC
Permalink
I keep getting a null pointer exception as I believe the "@BeforeMethod"
annotation in my base class is not being executed therefore the driver is
not being initialized even when I'm running my framework from TestNG.xml
file.

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 class which extends the base 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


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 go about fixing this?

Thank you.
--
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.
Sameer Jethvani
2018-10-30 10:40:06 UTC
Permalink
debug your class by placing break point and see why driver object remains
null . Moreover looking at stacktrace would throw more lights
Post by Baba
annotation in my base class is not being executed therefore the driver is
not being initialized even when I'm running my framework from TestNG.xml
file.
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);
}
}
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!!");
}
}
@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
<?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 go about fixing this?
Thank you.
--
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.
Anand Somani
2018-10-30 11:43:23 UTC
Permalink
Write minimum code, just to check all component working together properly
or not then add hub and other configuration.

Thanks & Regards,
Anand Somani
Post by Sameer Jethvani
debug your class by placing break point and see why driver object remains
null . Moreover looking at stacktrace would throw more lights
Post by Baba
annotation in my base class is not being executed therefore the driver is
not being initialized even when I'm running my framework from TestNG.xml
file.
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);
}
}
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!!");
}
}
@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
<?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 go about fixing this?
Thank you.
--
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
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
--
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.
Krishnan Mahadevan
2018-10-30 14:31:44 UTC
Permalink
There are so many discrepancies in your question. So please help fix the following and repost a proper sample along with the stacktrace of the issue:


Your question refers to using a @BeforeMethod annotation. But the sample you shared is using a @BeforeTest annotation (which will get executed only once per <test> tag)
All of the class references in your testng.xml is referring to cucumberOptions.TestRunner, but you haven’t shown us what that class looks like.


Thanks & Regards

Krishnan Mahadevan



"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

My Scribblings @ http://wakened-cognition.blogspot.com/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/



From: <testng-***@googlegroups.com> on behalf of Baba <***@gmail.com>
Reply-To: <testng-***@googlegroups.com>
Date: Tuesday, October 30, 2018 at 1:20 PM
To: testng-users <testng-***@googlegroups.com>
Subject: [testng-users] Running Cucumber framework from TestNG.xml file seems to be skipping TestNG annotations and not initializing the remote driver.



I keep getting a null pointer exception as I believe the "@BeforeMethod" annotation in my base class is not being executed therefore the driver is not being initialized even when I'm running my framework from TestNG.xml file.



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 class which extends the base 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


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 go about fixing this?



Thank you.
--
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.
--
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...