Getting error while running selenium with testng - selenium

I am new to automation programming. I am getting below error while running my firstprogram with TestNG in Selenium.
Could you please guide me?
The error I had:
[RemoteTestNG] detected TestNG version 6.14.3
System is opening browser
FAILED CONFIGURATION: #AfterMethod afterMethod
java.lang.NullPointerException
at TestNG.afterMethod(TestNG.java:30)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:458)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
FAILED: verifytitle
java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap
at org.openqa.selenium.remote.service.DriverService$Builder.<init>(DriverService.java:259)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.<init>(ChromeDriverService.java:101)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at TestNG.verifytitle(TestNG.java:15)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableMap
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 30 more
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
Code :
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
public class TestNG {
WebDriver driver;
#Test
public void verifytitle() {
String path = "D:\\Automation\\chromedriver\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", path) ;
driver = new ChromeDriver();
driver.get("https://www.google.com");
System.out.println("Browser has opened google website");
String currenttitle = driver.getTitle();
System.out.println("Browser of title is " + currenttitle);
}
#BeforeMethod
public void beforeMethod() {
System.out.println("System is opening browser");
}
#AfterMethod
public void afterMethod() {
driver.close();
System.out.println("Browser has closed browser");
}
}

You are getting an exception java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap. It means that you don't have Guava (which is a dependency for Selenium) on your classpath. Try using the following maven dependency in your pom.xml file and build your project.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-6</version>
</dependency>
If the above doesn't work or solve your problem, then include the following dependency for guava directly in your pom as below. Note: Do not include both in your pom.xml
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
If you don't use maven, you could download the above jars from https://mvnrepository.com/ and include it in your classpath.

Related

Getting error cucumber.runtime.CucumberException: Failed to instantiate class

In my project I am trying to build framework using (Cucumber + TestNG + Maven) but during designing baseclass and integrating with pageobjects in other class the issue of cucumber.runtime.CucumberException: Failed to instantiate class arised,
In my structure I am using baseclass consisting of methods for the invocation of browser instance, teardown of browser and then accesssormethods to access browser instance across different classes
Then, in my other class, I have my pageobjects along with constructor to initialize the pageobjects
Thirdly, I have a class consisting of stepdefintion class which corresponds to feature file and in the step definition class I declared the instance of baseclass and my pageobjects class and then passing the driver through accessor methods of baseclass but despite of that my code is not working please refer code below and feature file in attachment:-
Baseclass Code:-
public class Basehelper {
protected WebDriver driver;
protected NgWebDriver ngdriver;
#Before
public void initializebrowser()
{
System.setProperty("webdriver.gecko.driver",System.getProperty("user.dir")+"\\Drivers\\geckodriver.exe");
driver=new FirefoxDriver();
ngdriver= new NgWebDriver((JavascriptExecutor)driver);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://rmi.com/");
ngdriver.waitForAngularRequestsToFinish();
driver.findElement(ByAngular.model("username")).sendKeys("rmidev");
driver.findElement(ByAngular.model("password")).sendKeys("rmidev");
driver.findElement(ByAngular.buttonText("Submit")).submit();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
}
#Before("#selectdistrict15")
public void changetodistrict15()
{
ngdriver.waitForAngularRequestsToFinish();
driver.findElement(By.id("mainMenu_changeDistrictMenu")).click();
ngdriver.waitForAngularRequestsToFinish();
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);",driver.findElement(By.id("changeDistrictMenu_jmsRusselMetals")));
driver.findElement(By.id("changeDistrictMenu_jmsRusselMetals")).click();
ngdriver.waitForAngularRequestsToFinish();
driver.findElement(By.id("jmsRusselMetals_DistTo15-JmspBlytheville")).click();
driver.findElement(By.id("app_previous")).click();
ngdriver.waitForAngularRequestsToFinish();
}
#After
public void teardown()
{
driver.close();
}
public WebDriver getbaseclassdriver()
{
return driver;
}
public NgWebDriver getbaseclassngdriver()
{
return ngdriver;
}
Stepdefinition class
public class IM801CLPageDefintion {
Basehelper h=new Basehelper();
IM801CLPageObjects obj=new IM801CLPageObjects(h.getbaseclassdriver());
#When("^we navigate menu$")
public void we_navigate_menu() throws Exception {
obj.Inventorymenu.click();
h.getbaseclassngdriver().waitForAngularRequestsToFinish();
((JavascriptExecutor)h.getbaseclassdriver()).executeScript("arguments[0].scrollIntoView(true);",obj.InventoryExceptionMenu);
obj.InventoryExceptionMenu.click();
h.getbaseclassngdriver().waitForAngularRequestsToFinish();
obj.TaggedcostMenu.click();
}
#Then("^click on enter button$")
public void click_on_enter_button() {
h.getbaseclassngdriver().waitForAngularRequestsToFinish();
obj.Enterbutton.click();
}
#Then("^click on return to main menu$")
public void click_on_return_to_main_menu() {
h.getbaseclassngdriver().waitForAngularRequestsToFinish();
obj.returnbutton.click();
}
#When("^remove the district field value$")
public void remove_the_district_field_value() {
obj.DistrictField.clear();
}
}
Runner class:-
package runner;
import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
#CucumberOptions(features={"src//test//resources//featurefiles"},glue= {"im801clsteps"},plugin={"html:target/cucumber-html-report",
"json:target/cucumber.json", "pretty:target/cucumber-pretty.txt"})
#Test
public class MainRunner extends AbstractTestNGCucumberTests
{
}
Featurefile attached
please refer here for console logs:-
[RemoteTestNG] detected TestNG version 6.10.0
[TestNG] Running:
C:\Users\keshavd\AppData\Local\Temp\testng-eclipse--2074603226\testng-customsuite.xml
[31mFailed scenarios:[0m
[31mIM801CL.feature:7 [0m# Scenario: To test the functionality of IM801CL program on district 35
[31mIM801CL.feature:13 [0m# Scenario: To test the functionality of IM801CL program on district 99
2 Scenarios ([31m2 failed[0m)
9 Steps ([31m2 failed[0m, [36m7 skipped[0m)
0m0.081s
cucumber.runtime.CucumberException: Failed to instantiate class im801clsteps.IM801CLPageDefintion
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:47)
at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:33)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:300)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.runBackground(CucumberScenario.java:59)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:42)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:63)
at cucumber.api.testng.AbstractTestNGCucumberTests.feature(AbstractTestNGCucumberTests.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:756)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
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:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:41)
... 36 more
Caused by: java.lang.NullPointerException
at im801clsteps.IM801CLPageDefintion.<init>(IM801CLPageDefintion.java:12)
... 41 more
cucumber.runtime.CucumberException: Failed to instantiate class im801clsteps.IM801CLPageDefintion
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:47)
at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:33)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:300)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.runBackground(CucumberScenario.java:59)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:42)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:63)
at cucumber.api.testng.AbstractTestNGCucumberTests.feature(AbstractTestNGCucumberTests.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:756)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
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:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:41)
... 36 more
Caused by: java.lang.NullPointerException
at im801clsteps.IM801CLPageDefintion.<init>(IM801CLPageDefintion.java:12)
... 41 more
FAILED: feature(To test the functionality of IM801CL program)
Runs Cucumber Feature
cucumber.runtime.CucumberException: cucumber.runtime.CucumberException: Failed to instantiate class im801clsteps.IM801CLPageDefintion
at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:69)
at cucumber.api.testng.AbstractTestNGCucumberTests.feature(AbstractTestNGCucumberTests.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:756)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
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:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: cucumber.runtime.CucumberException: Failed to instantiate class im801clsteps.IM801CLPageDefintion
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:47)
at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:33)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:300)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.runBackground(CucumberScenario.java:59)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:42)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:63)
... 26 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:41)
... 36 more
Caused by: java.lang.NullPointerException
at im801clsteps.IM801CLPageDefintion.<init>(IM801CLPageDefintion.java:12)
... 41 more
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.EmailableReporter2#35645047: 10 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 8 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter#5b367418: 6 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter#46b61c56: 16 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter#12d2ce03: 16 ms
[TestNG] Time taken by org.testng.reporters.jq.Main#3d9c13b5: 19 ms

Running Selenium with IE

Can someone pls help with this
package IEProjects;
import java.io.File;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
public class TestIEBrowser {
static String driverPath = "IE driver path";
public WebDriver driver;
#BeforeClass
public void setUp() {
System.out.println("my IE");
System.out.println("launching IE browser");
System.setProperty("webdriver.ie.driver","C:\\IEDriverServer_Win32_3.3.0\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
}
#Test
public void testGooglePageTitleInIEBrowser() {
driver.navigate().to("http://www.google.com");
String strPageTitle = driver.getTitle();
System.out.println("Page title: - "+strPageTitle);
Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match");
}
#AfterClass
public void tearDown() {
if(driver!=null) {
System.out.println("Closing IE browser");
driver.quit();
}
}
}
here is the error below, i appreciate your prompt reply.
FAILED: testGooglePageTitleInIEBrowser
java.lang.NullPointerException
at IEProjects.TestIEBrowser.testGooglePageTitleInIEBrowser(TestIEBrowser.java:32)
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:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
Thank you
The root cause is because driver is not initialized properly.
I think you are incorrectly import beforeClass annotation from Junit instead of testNG, and thus causing setUp method not called and resulting driver not initialized
please change import into import org.testng.annotations.BeforeClass;
The IE browser is not able to launch due to the annotation being wrong wherein you have used the JUNIT annotation instead of TestNg.

Null pointer exception in TestNG

My code is:
package ant;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
public class NewTestNG {
public WebDriver driver;
#BeforeMethod
public void LAunchbrowser() {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#Test
public void main() {
Actions action = new Actions(driver);
WebElement a= driver.findElement(By.xpath(".//*[#id='gs_htif0']"));
action.moveToElement(a).click().sendKeys("Shirt").build().perform();
driver.findElement(By.xpath("//div[#value='Search']")).click();
}
}
I'm getting NullPointerException:
FAILED: main
java.lang.NullPointerException
at org.openqa.selenium.interactions.Actions.<init>(Actions.java:44)
at ant.NewTestNG.main(NewTestNG.java:25)
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:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
What's wrong with my code?
In LaunchBrowser() you declared driver again instead of using the class instance. Change it to
#BeforeMethod
public void LaunchBrowser() {
driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
and it should work. The problem is that when you declared driver in LaunchBrowser(), the scope of that variable is inside the method only so the class variable, driver doesn't get used. So when you are outside of the method and try to reference driver, it's null... thus the exception.
You really need to spend some time learning how to debug your own programs. If you put a break point at the start of the script and stepped through it, you should have been able to find this yourself.

TestNG - Browser closes immediately after opening and test fails with error

So I have 2 projects in Eclipse with identical classes and project1 runs fine, but project2 fails with the following error:
[TestNG] Running:
/private/var/folders/p8/sjw70s_n5f37bhv94xvr7v0c0000gn/T/testng-eclipse-1676803728/testng-customsuite.xml
FAILED CONFIGURATION: #AfterMethod tearDown([TestResult name=testBlankCard status=FAILURE method=testBlankCardInfo.testBlankCard()[pri:0, instance:testBlankCardInfo#2fc14f68] output={null}])
java.lang.NullPointerException
at testBlankCardInfo.tearDown(testBlankCardInfo.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:703)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)
FAILED: testBlankCard
java.lang.NoClassDefFoundError: freemarker/template/TemplateModelException
at com.relevantcodes.extentreports.ExtentReports.<init>(ExtentReports.java:78)
at com.relevantcodes.extentreports.ExtentReports.<init>(ExtentReports.java:362)
at testBlankCardInfo.testBlankCard(testBlankCardInfo.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)
Caused by: java.lang.ClassNotFoundException: freemarker.template.TemplateModelException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 27 more
===============================================
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
===============================================
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter#3830f1c0: 8 ms
[TestNG] Time taken by org.testng.reporters.jq.Main#1eb44e46: 47 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter#1ee0005: 43 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter#76ccd017: 5 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 5 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2#4926097b: 15 ms
Project1 runs fine in eclipse, but when I run it in Terminal, I get the same error like the one above.
And I'm using Firefox if that matters.
My #AfterMethod looks like so:
#AfterMethod
public void tearDown(ITestResult result) throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
Assert.fail(verificationErrorString);
}
if(result.getStatus()==ITestResult.FAILURE)
{
logger.log(LogStatus.FAIL, "Blank Card verification");
}
report.endTest(logger);
report.flush();
driver.get(".../Desktop/Selenium/testreport.html");
}
Any ideas what I'm doing wrong?
you are using
driver.get(".../Desktop/Selenium/testreport.html");
after
driver.quit();
in tearDown. that is the reason it getting null point exception as already driver or session is closed/quit
please check this and update accordingly..
thank you,
Murali

Encountering error: java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableList$Builder

I'm new to Selenium WebDriver using EclipseIDE with TestNG. I'm currently running this sample code in Eclipse via TestNG:
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.util.List;
public class CheesecakeFactory {
FirefoxDriver driver;
#BeforeTest
public void startDriver() {
driver = new FirefoxDriver();
}
#AfterTest
public void stopDriver() {
driver.close();
}
#Test
public void listCheesecakes() {
driver.get("http://www.thecheesecakefactory.com/");
driver.findElement(By.linkText("Menu")).click();
driver.findElement(By.linkText("Cheesecake")).click();
List<WebElement> cheesecakes = driver.findElements(By.xpath("id('leftNav_levelTwo')//li"));
System.out.println(cheesecakes.size() + " cheesecakes:");
for (int i=0; i<cheesecakes.size(); i++) {
System.out.println(i+1 + ". " + cheesecakes.get(i).getText());
}
}
}
But Eclipse returns this:
[TestNG] Running:
C:\Users\ryan\AppData\Local\Temp\testng-eclipse--616826937\testng-customsuite.xml
FAILED CONFIGURATION: #BeforeTest startDriver
java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableList$Builder
at org.openqa.selenium.os.WindowsUtils.getPathsInProgramFiles(WindowsUtils.java:275)
at org.openqa.selenium.firefox.internal.Executable.locateFirefoxBinaryFromPlatform(Executable.java:148)
at org.openqa.selenium.firefox.internal.Executable.<clinit>(Executable.java:25)
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:60)
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:56)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:78)
at CheesecakeFactory.startDriver(CheesecakeFactory.java:16)
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:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.beforeRun(TestRunner.java:641)
at org.testng.TestRunner.run(TestRunner.java:609)
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:1197)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
at org.testng.TestNG.run(TestNG.java:1030)
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)
I don't understand why I'm getting this error. I've done the following:
Added the guava-12.0.jar file (along with the other jar files in the Selenium-2.25.0 webdriver) as an external jar file in Eclipse. (This jar file contains the ImmutableList$Builder class)
Added the path of this jar file in the CLASSPATH (Environment Variables>System Variables)
Am I missing something? Any help is greatly appreciated.
I guess you are using selenium-java-2.25.0.jar. You should rather use selenium-server-standalone-2.25.0.jar, it will take care of all the dependencies (i.e. required jar files).
Also you dont need to explicitly define the environment variables if the jar files are added in the Eclipse, unless you are running the test outside from eclipse.
Hope this helps... :)