Baba
2018-10-29 17:19:08 UTC
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.
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.