I am using CSV reader to fetch the data from and using this data in a test function using DataProvider.
TestNG Method
#Test(dataProvider = "regCSVData", dataProviderClass = LoginData.class)
public void testLoginUsingCSVFile(LoginData loginData){
driver.get(appURL);
Login login = PageFactory.initElements(driver, Login.class);
login.loginToFwbm(loginData);
}
LoginData Class
#DataProvider(name = "regCSVData")
public static Object[][] getCSVData() throws IOException {
CSVReader csvReader = new CSVReader(new FileReader(
LoginData.class.getResource("/regdata.csv").getPath()));
List<String[]>dataList = csvReader.readAll();
Object[][]data = new Object[dataList.size()][1];
List<LoginData> logList = new ArrayList<LoginData>();
for (String[] strArray:dataList){
LoginData loginData = new LoginData();
loginData.setUserName(strArray[0].trim());
loginData.setPassword(strArray[1].trim());
logList.add(loginData);
}
for (int i=0; i<data.length;i++){
for(int j=0;j<data[i].length;j++){
data[i][j] = logList.get(i);
}
}
csvReader.close();
return data;
}
CSV File
asdf, pa2
qwerty, pa2
john, pa2
When I run this test using TestNG, I am getting Null Pointer exception
java.lang.RuntimeException: java.lang.NullPointerException
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:161)
at org.testng.internal.Parameters.handleParameters(Parameters.java:429)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
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)
Caused by: java.lang.NullPointerException
at com.fwbm.dataobject.LoginData.getCSVData(LoginData.java:49)
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.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:135)
... 20 more
Project Structure Image
You have to add the file to the classPath in your "run configuration". Then the file can be loaded using getRessource();
The best way to use QAF. In QAF, you don't need to provide implementation for parse data provider file. It is very much easy for you to use it.
You can refer documentation from QAF Data Driven
Related
I am trying to use Page Factory to separate the elements from my Base Test and Test runner but when I ran it it returns an error. Can someone help me in my code?
I already tried searching on google the fix but nothing worked
public class BaseTest {
private final String URL_STRING = "http://127.0.0.1:7622/wd/hub";
protected URL url;
public AndroidDriver<AndroidElement> driver;
private final static String APP_PACKAGE_NAME = "com.android.chrome";
private final static String APP_ACTIVITY_NAME = "com.google.android.apps.chrome.Main";
public AndroidDriver<AndroidElement> init() throws MalformedURLException {
url = new URL(URL_STRING);
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(AndroidMobileCapabilityType.PLATFORM_NAME, Platform.ANDROID);
caps.setCapability(MobileCapabilityType.UDID, "emulator-5554");
caps.setCapability(MobileCapabilityType.NO_RESET, true);
caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, APP_PACKAGE_NAME);
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, APP_ACTIVITY_NAME);
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Samsung");
driver = new AndroidDriver<AndroidElement>(url, caps);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
return driver;
}
}
public class HomePage extends BaseTest {
#AndroidFindBy(id = "com.android.chrome:id/search_box_text")
public AndroidElement searchTxt;
public HomePage() {
PageFactory.initElements(new AppiumFieldDecorator(this.driver), this);
}
public class SampleGoogle extends BaseTest {
#Test
public void Sample() throws MalformedURLException {
HomePage homePage = new HomePage();
homePage.init();
homePage.setSearch("Test");
Expected result should be Chrome will be opened on my emulator and then Test will be written in the search bar however an error is returned when I ran the code.
FAILED: Sample java.lang.IllegalArgumentException: Can not set
io.appium.java_client.android.AndroidElement field
com.google.HomePage.searchTxt to
org.openqa.selenium.remote.RemoteWebElement$$EnhancerByCGLIB$$d27c0df4
at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown
Source) at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown
Source) at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown
Source) at java.lang.reflect.Field.set(Unknown Source) at
org.openqa.selenium.support.PageFactory.proxyFields(PageFactory.java:117)
at
org.openqa.selenium.support.PageFactory.initElements(PageFactory.java:105)
at com.google.HomePage.(HomePage.java:21) at
com.testcases.SampleGoogle.Sample(SampleGoogle.java:17) 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:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661) at
org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) at
org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744) at
org.testng.TestRunner.run(TestRunner.java:602) at
org.testng.SuiteRunner.runTest(SuiteRunner.java:380) at
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) at
org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at
org.testng.SuiteRunner.run(SuiteRunner.java:289) at
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at
org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) at
org.testng.TestNG.runSuitesLocally(TestNG.java:1226) at
org.testng.TestNG.runSuites(TestNG.java:1144) at
org.testng.TestNG.run(TestNG.java:1115) at
org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)
I also got the same issue ,
My mistake :I used Capital 'W' in wd of appium URL(desired capabilities). Changed that to small 'w' (wd)and its working fine now
yesterday my test works ok, bust i want to use testng and then appeared a problem.
My main class leadTest:
public class leadTest {
WebDriver driver;
#Test
public void f() {
..
...
...
/*------------------------Go to leads page-------------------------------*/
LeadsPage ldsP = new LeadsPage(driver);
dbp.gotoLeadsPage();
ldsP.findLeadByName("nameToFind");
}
#BeforeClass
public void beforeClass() {
driver = new FirefoxDriver();
driver.get("http://getbase.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
and my class LeadsPage:
public class LeadsPage {
WebDriver driver;
String expStatus = "new";
#FindBy(id="leads-new")
WebElement addLead;
#FindBy(className = "lead-status")
WebElement UserStatus;
public void addNewLead(){
addLead.click();
}
public void checkUsrStat(){
String stat = UserStatus.getText().toLowerCase();
Assert.assertEquals(stat, expStatus.toLowerCase());
}
public void findLeadByName(String leadName){
driver.findElement(By.partialLinkText(leadName)).click();
}
public LeadsPage(WebDriver driver){
PageFactory.initElements(driver, this);
}
I have problem with this part from above:
public void findLeadByName(String leadName){
driver.findElement(By.partialLinkText(leadName)).click();
}
I have error
AILED: f
java.lang.NullPointerException
at leadTest.LeadsPage.findLeadByName(LeadsPage.java:38)
at leadTest.leadTest.f(leadTest.java:101)
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)
When i use
#FindBy(partialLinkText="nameToFind")
WebElement ntf;
and
ntf.click();
its works, but i cant do that, becouse in place nameToFind will be variable.
And i dont know what is wrong... Please help me
Driver variable not initialized: you try to call findElement() method on a null object.
One way to confirm that is to print the driver value:
System.out.println("driver=" + driver); just before the exception is thrown
public void findLeadByName(String leadName){
System.out.println("driver=" + driver);
driver.findElement(By.partialLinkText(leadName)).click();
}
also i am noticing ,
LeadsPage ldsP = new LeadsPage(driver);
dbp.gotoLeadsPage();
ldsP.findLeadByName("nameToFind");
gotoLeadsPage(); and findLeadByName(); both functions are in same class LeadsPage but using different objects dbp and ldsp
May be below changes will work.
in LeadsPage class change your findLeadByName(); function as follows,
public void findLeadByName(WebDriver driver, String leadName){
this.driver.findElement(By.partialLinkText(leadName)).click();
}
and in your class leadTest call function findLeadByName() as follows,
LeadsPage ldsP = new LeadsPage(driver);
ldsP.findLeadByName(driver ,"nameToFind");
I had started at this original post
This has been very enlightening to read. I am in the same situation and have been trying to use this as a guide. Unfortunately, I am getting a java.lang.NullPointerException which I know why, its because the page isn't getting loaded but I am not sure why.
Here is my base test class:
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class IRIS_Firefox_Test_Base {
static WebDriver driver;
static String baseURL;
#Before
public void setup() throws IOException{
baseURL = "http://localhost:3006";
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(baseURL);
}
#After
public void closeFireFox(){
driver.quit();
}
}
And my test:
import java.io.IOException;
import org.junit.Test;
import pageObjects.Login_Page;
public class POM_TC extends IRIS_Firefox_Test_Base {
#Test
public void login() throws IOException{
super.setup();
Login_Page.loginInput(driver).sendKeys("scornell");
Login_Page.passwordInput(driver).sendKeys("password");
Login_Page.loginBtn(driver).click();
}
}
I'm not really a well versed java programmer, my background is more JavaScript and C#(not a lot there either though). Maybe I'm missing something simple.
Here is my stack trace:
FAILED: login
java.lang.NullPointerException
at pageObjects.Login_Page.loginInput(Login_Page.java:13)
at com.hiiq.qa.testing.gen2.Login_Tests.POM_TCTest.login(POM_TCTest.java:11)
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)
thanks to all for their input.
I would add a page load time out:
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
This allows the page to load, giving the program time to catch up and verify that certain elements are available on the page. I would make your setup look like:
#Before
public void setup() throws IOException{
baseURL = "http://localhost:3006";
driver = new FirefoxDriver();
driver.get(baseURL);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
}
Remove the implicit wait, and add the pageLoadTimeout, also I moved the maximize portion until after the URL is called.
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);
}
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);
}
}
I just tried out the example of the Jersey tutorial which concerns AsyncResponse and can be found here: http://jersey.java.net/apidocs/snapshot/jersey/javax/ws/rs/container/AsyncResponse.html
It does not seem to work. Even the example throws an exception when executed in a simple unit test:
public class AsyncTest extends JerseyTest {
#Override
protected Application configure() {
return new ResourceConfig(MyAsync.class)
}
#Path("/async")
public static class MyAsync {
#GET
public void asyncGet(#Suspended final AsyncResponse asyncResponse) {
new Thread(new Runnable() {
#Override
public void run() {
String result = veryExpensiveOperation();
asyncResponse.resume(result);
}
private String veryExpensiveOperation() {
return "bla";
}
}).start();
}
}
#Test
public void testConvertWordToPdf() throws Exception {
String result = target().path("async").request().async().get().get(String.class);
}
}
Can somebody tell me what I am doing wrong? Thanks for any answer!
Update: I even tried the official example that can be found at http://search.maven.org/#artifactdetails|org.glassfish.jersey.examples|server-async-standalone-webapp|2.1|war and I get the same exception. The example works, if I make sure the AsyncResponse responds before the method returns. The stack trace I get:
java.util.concurrent.ExecutionException:
javax.ws.rs.ProcessingException: java.lang.NullPointerException at
com.google.common.util.concurrent.AbstractFuture$Sync.getValue(AbstractFuture.java:306)
at
com.google.common.util.concurrent.AbstractFuture$Sync.get(AbstractFuture.java:293)
at
com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:116)
at MyCompany.jersey.ConverterResourceTest.testTest(MyClass.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597) 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) at
org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:111)
Caused by: javax.ws.rs.ProcessingException:
java.lang.NullPointerException at
org.glassfish.jersey.client.ClientRuntime$1$1.failure(ClientRuntime.java:148)
at
org.glassfish.jersey.test.inmemory.internal.InMemoryConnector$3.run(InMemoryConnector.java:265)
at
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138) at
com.google.common.util.concurrent.MoreExecutors$SameThreadExecutorService.execute(MoreExecutors.java:293)
at
com.google.common.util.concurrent.AbstractListeningExecutorService.submit(AbstractListeningExecutorService.java:49)
at
org.glassfish.jersey.test.inmemory.internal.InMemoryConnector.apply(InMemoryConnector.java:257)
at
org.glassfish.jersey.client.ClientRuntime$1.run(ClientRuntime.java:156)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) at
org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) at
org.glassfish.jersey.internal.Errors.process(Errors.java:315) at
org.glassfish.jersey.internal.Errors.process(Errors.java:297) at
org.glassfish.jersey.internal.Errors.process(Errors.java:267) at
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:322)
at
org.glassfish.jersey.client.ClientRuntime$2.run(ClientRuntime.java:170)
at
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138) at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662) Caused by:
java.lang.NullPointerException at
org.glassfish.jersey.internal.util.KeyComparatorHashMap.putAll(KeyComparatorHashMap.java:509)
at
javax.ws.rs.core.AbstractMultivaluedMap.putAll(AbstractMultivaluedMap.java:332)
at
org.glassfish.jersey.test.inmemory.internal.InMemoryConnector.createClientResponse(InMemoryConnector.java:286)
at
org.glassfish.jersey.test.inmemory.internal.InMemoryConnector.apply(InMemoryConnector.java:247)
at
org.glassfish.jersey.test.inmemory.internal.InMemoryConnector$3.run(InMemoryConnector.java:261)
... 20 more
Btw, I tested this testcase in Grizzly container. It is not supported in InMemory container as the container does not implement suspend operation on the ContainerResponseWriter. InMemoryResponseWriter should throw an UnsupportedOperationException instead of returning false. This sounds like a Jersey bug.
#Test
public void testConvertWordToPdf() throws Exception {
String result = target().path("async").request().async().get().get().readEntity(String.class);
assertEquals("bla", result);
}
Here is my POM dependency:
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>