I am trying to run my .xml (package) file to run test suite. While running it as TestNG Suite i am getting "java.lang.NullPointerException". If i execute two test cases separately then it passes without any error but fails if i try to run package. I am unable to understand whats missing in my code or am i doing something wrong. Please help.
My TestCase1:
package Teacher;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
public class TestCase1_TeacherLogin {
public WebDriver d;
public Properties p;
File file;
FileInputStream fis;
#Test(priority=0)
public void TeacherLogin() {
//Enter UserName
d.findElement(By.id(p.getProperty("txt_username_id"))).sendKeys("username");
//Enter Password
d.findElement(By.id(p.getProperty("txt_password_id"))).sendKeys("password");
//Click on Login Button
d.findElement(By.id(p.getProperty("btn_login_id"))).click();
String LoginSuccessexp="my system";
assertEquals(d.getTitle(), LoginSuccessexp);
System.out.println("Your Login Successful");
}
#BeforeMethod
public void beforeMethod() throws IOException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\admin\\Desktop\\Software Testing\\testing softwares\\chromedriver.exe");
d=new ChromeDriver();
d.get(p.getProperty("url"));
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#BeforeTest
public void beforeTest() throws IOException {
file=new File("C:\\Users\\admin\\Desktop\\IntMarks.properties");
fis=new FileInputStream(file);
p=new Properties();
p.load(fis);
}
}
My TestCase2:
package Teacher;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
public class TestCase2_InternalExaminer extends TestCase1_TeacherLogin{
#Test(priority=1)
public void IntExaminer() {
//super.TeacherLogin();
//Click on Int Examiner link
d.findElement(By.xpath(p.getProperty("link_IntExaminor_xpath"))).click();
//Click on Link Add Absentee Entry
d.findElement(By.linkText(p.getProperty("link_AddAbsenteeEntry"))).click();
//Select Appointed College
WebElement we=d.findElement(By.id(p.getProperty("dropdown_college_id")));
Select sc=new Select(we);
sc.selectByIndex(1);
//Select Pattern
WebElement we1=d.findElement(By.id(p.getProperty("dropdown_Pattern_id")));
Select sc1=new Select(we1);
sc1.selectByIndex(1);
//Select Subject
WebElement we2=d.findElement(By.id(p.getProperty("dropdown_subjetc_id")));
Select sc2=new Select(we2);
sc2.selectByIndex(3);
//Select Paper Type
WebElement we3=d.findElement(By.id(p.getProperty("dropdown_paper_id")));
Select sc3=new Select(we3);
sc3.selectByIndex(1);
//Select Batch No
WebElement we4=d.findElement(By.id(p.getProperty("dropdown_batch_id")));
Select sc4=new Select(we4);
sc4.selectByIndex(1);
//Enter Seat No
WebElement we5=d.findElement(By.id(p.getProperty("txt_SeatNo_id")));
JavascriptExecutor js=(JavascriptExecutor)d;
js.executeScript("arguments[0].value='50027';", we5);
//Scroll Page Down
Actions ac=new Actions(d);
ac.sendKeys(Keys.PAGE_DOWN).build().perform();
//Click on Remove button
d.findElement(By.id(p.getProperty("btn_remove_id"))).click();
}
}
My XML file/package:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="My package">
<test name="My test">
<classes>
<class name="Teacher.TestCase1_TeacherLogin"/>
<class name="Teacher.TestCase2_InternalExaminer"/>
</classes>>
</test>
</suite>
Execution Report
My Console:
[RemoteTestNG] detected TestNG version 6.9.9
[TestNG] Running:
C:\Users\admin\AppData\Local\Temp\testng-eclipse-1496718797\testng-customsuite.xml
Starting ChromeDriver 2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7) on port 7843
Only local connections are allowed.
FAILED: IntExaminer
java.lang.NullPointerException
at Teacher.TestCase2_InternalExaminer.IntExaminer(TestCase2_InternalExaminer.java:20)
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: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.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
===============================================
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.XMLReporter#55a1c291: 25 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2#482cd91f: 11 ms
[TestNG] Time taken by org.testng.reporters.jq.Main#75f9eccc: 93 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter#64c87930: 85 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 17 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter#75c072cb: 10 ms
Related
I'm trying to run a Testng.xml file in Eclipse and it is successfully opening the browser and executing the test but at the end, it is throwing the above error because of which the test case is marked as failed by TestNG.
Following is the console output:
[RemoteTestNG] detected TestNG version 7.4.0
Starting ChromeDriver 105.0.5195.52
(412c95e518836d8a7d97250d62b29c2ae6a26a85-refs/branch-heads/5195#{#853}) on port 37386
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Sep 28, 2022 11:52:20 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
[TestHTMLReporter] The filename, directory name, or volume label syntax is incorrect
===============================================
Live Project Automation : Facebook
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================
The code that I'm using in testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Live Project Automation : Facebook">
<parameter name="browser" value="chrome"></parameter>
<test name="Facebook launch test">
<classes>
<class name="com.testng.tests.SimpleTestng"></class>
</classes>
</test>
</suite> <!-- Suite -->
Java class file:
package com.testng.tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.beust.jcommander.Parameter;
public class SimpleTestng {
WebDriver driver=null;
#BeforeMethod
#Parameters("browser")
public void launchBrowser(String browserName) {
if (browserName.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
driver=new ChromeDriver();
} else if(browserName.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", "Drivers\\geckodriver.exe");
driver=new FirefoxDriver();
} else {
System.setProperty("webdriver.edge.driver", "Drivers\\msedgedriver.exe");
driver=new EdgeDriver();
}
driver.manage().window().maximize();
}
#AfterMethod
public void quitDriver() {
driver.close();
}
#Test
public void LaunchFB() throws InterruptedException {
String url="https://www.facebook.com";
String title="Facebook - login or sign up";
driver.get(url);
Assert.assertEquals(driver.getTitle(), title);
Thread.sleep(4000);
}
}
Dependencies used for this:
cucumber-core-1.2.4
cucumber.html-0.2.3
cucumber-java-1.2.4
cucumber-junit-1.2.4
junit-4.11
gherkin-2.12.2
cucumber-jvm-deps-1.0.3
I recently cleaned up my dependencies folder structure and think I might have misplaced something, but my problem is an instantiation issue that doesn't make sense as it seems I have my ducks in a row on the cucumber side. Here is the stack trace:
cucumber.runtime.CucumberException: Failed to instantiate class cucumber.feature.LoginandMeetingCreation
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:46)
at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:32)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:299)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
at cucumber.runtime.Runtime.run(Runtime.java:121)
at cucumber.api.cli.Main.run(Main.java:36)
at cucumber.api.cli.Main.main(Main.java:18)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:40)
... 11 more
Caused by: java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)
at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:96)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
at cucumber.feature.LoginandMeetingCreation.<init>(LoginandMeetingCreation.java:19)
... 16 more
Here is my cucumber runner that sets up the feature files, but never executes the glue command. My package cucumber.feature contains my step definition file LoginandMeetingCreation.java file:
package cucumberInitialization;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {"pretty" , "json:target/cucumber.json"},
features = {"src/cucumber/"},
monochrome = true,
glue = {"cucumber.feature"}
)
public class cucumberRunner {
}
Feature file with steps:
Feature: Create a meeting and fill in the necessary text fields
Scenario: As a user, login and create a meeting
Given I navigated to the site
When I login and select an org
Then Create a meeting
And finally my step definitions:
package cucumber.feature;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class LoginandMeetingCreation {
WebDriver chromeDriver = null;
WebDriver ieDriver = null;
//open IE and Chrome browsers and go to Website
#Given("^I navigated to the site$")
public void navigateToWebsite() throws Throwable{
throw new PendingException();
}
//login users
#When("^I login and select an org$")
public void userLogin() throws Throwable{
throw new PendingException();
}
#Then("^Create a meeting$")
public void meetingCreation() throws Throwable{
throw new PendingException();
}
}
Any advice?
Thanks GalexMES.
You were right, I had forgotten I had deleted a bulk amount and it dawned on me I was creating an object that cannot accept a null value. When I had written WebDriverWait wait = new WebDriverWait(ieDriver, 5); ieDriver was null, which causes the NPE. Once I create the ieDriver object from a non null value the expected behavior is executed.
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
public class TestNG {
public WebDriver driver;
#Test
public void main() throws Exception {
driver.findElement(By.id("account")).click();
driver.findElement(By.id("log")).sendKeys("********");
driver.findElement(By.id("pwd")).sendKeys("*****");
driver.findElement(By.id("login")).click();
System.out.println("Login Successfully :-)");
driver.findElement(By.id("account_logout")).click();
Thread.sleep(15000);
}
#BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32 (1)\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://store.demoqa.com/");
}
#AfterMethod
public void afterMethod() {
driver.quit();
}
}
The #BeforeMethod ran successfully and launched the specified Website, but after the website has opened, it should move towards the main functionality i.e. #BeforeMethod, but unable to move further.
Please advise me, is there something wrong with the code.
Below Error we are getting:-
[TestNG] Running:
C:\Users\gmohammad\AppData\Local\Temp\testng-eclipse--1784470156\testng-customsuite.xml
Starting ChromeDriver 2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4) on port 6093
Only local connections are allowed.
FAILED CONFIGURATION: #AfterMethod afterMethod
java.lang.NullPointerException
at com.ghulam.TestNG.afterMethod(TestNG.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: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.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)
FAILED: f
java.lang.NullPointerException
at com.ghulam.TestNG.f(TestNG.java:15)
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: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.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)
===============================================
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
===============================================
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter#1a18493: 24 ms
[TestNG] Time taken by org.testng.reporters.jq.Main#c0f91d: 148 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter#de4588: 39 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2#354949: 15 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter#19b622d: 123 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 21 ms
Try with the below code..
I guess this is happening due to declaring the driver again in your #BeforeMethod method making the driver object local to that method itself.
Note :- Tried executing your code, it is failing again at "driver.findElement(By.id("account_logout")).click();".. please correct this and run again, it will work.
package testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestNG {
public WebDriver driver;
#Test
public void main() throws Exception {
driver.findElement(By.id("account")).click();
driver.findElement(By.id("log")).sendKeys("********");
driver.findElement(By.id("pwd")).sendKeys("*****");
driver.findElement(By.id("login")).click();
System.out.println("Login Successfully :-)");
driver.findElement(By.id("account_logout")).click();
Thread.sleep(15000);
}
#BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://store.demoqa.com/");
}
#AfterMethod
public void afterMethod() {
driver.quit();
}
}
I am getting following error when I run code in Selenium and Java using TestNG. On multiple blogs/sites it is mentioned to clean the project and so I did Project->Clean but still it is throwing me this error. Can some one please point me what is wrong in this code? Thanks.
package firsttestngpackage;
//import org.testng.annotations.Test;
//import org.openqa.selenium.*;
import org.testng.Assert;
import org.testng.annotations.*;
//import org.testng.asserts.*;
//import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTestNGFile {
#BeforeSuite
public void SetBrowser(){
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
}
public WebDriver driver1 = new ChromeDriver();
public String baseurl = "http://newtours.demoaut.com/";
public String ExpTitle = "Welcome: Mercury Tours";
#Test
public void CheckPageTitle() {
driver1.get(baseurl);
String ActTitle = driver1.getTitle();
Assert.assertEquals(ActTitle, ExpTitle);
driver1.quit();
}
}
Exception:
org.testng.TestNGException:
Cannot instantiate class firsttestngpackage.FirstTestNGFile
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
at org.testng.TestRunner.initMethods(TestRunner.java:409)
at org.testng.TestRunner.init(TestRunner.java:235)
at org.testng.TestRunner.init(TestRunner.java:205)
at org.testng.TestRunner.<init>(TestRunner.java:160)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
at org.testng.SuiteRunner.init(SuiteRunner.java:157)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
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.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 21 more
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:197)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:105)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:89)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:117)
at firsttestngpackage.FirstTestNGFile.<init>(FirstTestNGFile.java:21)
... 26 more
I have made changes to the above code now its working fine...
The issue was due to driver instance scope it was defined inside a method..
package firsttestngpackage;
//import org.testng.annotations.Test;
//import org.openqa.selenium.*;
import org.testng.Assert;
import org.testng.annotations.*;
//import org.testng.asserts.*;
//import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTestNGFile {
public WebDriver driver1 ;
#BeforeSuite
public void SetBrowser(){
System.setProperty("webdriver.chrome.driver", "/home/vicky/Documents/Jars/chromedriver");
driver1= new ChromeDriver();
}
public String baseurl = "http://newtours.demoaut.com/";
public String ExpTitle = "Welcome: Mercury Tours";
#Test
public void CheckPageTitle() {
driver1.get(baseurl);
String ActTitle = driver1.getTitle();
Assert.assertEquals(ActTitle, ExpTitle);
driver1.quit();
}
}
iam facing a problem i have written a method navback, which i need to use regularily to navigate back. when iam running it is throwing a error.
below is the Code.
package Examples;
import java.util.concurrent.TimeUnit;
//import org.junit.BeforeClass;
import org.testng.annotations.BeforeClass;
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.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Flipkart {
public static WebDriver driver;
Actions action = new Actions(driver);
//String ddd;
// public Example2() {
// super();
// }
#BeforeClass
public void beforeClass()
{
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);
}
#Test
public void mailSend() throws InterruptedException
{
driver.get("https://www.flipkart.com/");
driver.manage().window().maximize();
navback();
driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[2]/a")).click();
driver.navigate().back();
navback();
driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[3]/a")).click();
driver.navigate().back();
driver.navigate().refresh();
}
public void navback()
{
WebElement we = driver.findElement(By.xpath("//html/body/div/div/div[2]/div/div/ul/li/a/span"));
action.moveToElement(we).build().perform();
}
#AfterClass
public void tear()
{
// driver.quit();
}
}
Below is the Error.
org.testng.TestNGException:
Cannot instantiate class Examples.Flipkart
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
at org.testng.TestRunner.initMethods(TestRunner.java:409)
at org.testng.TestRunner.init(TestRunner.java:235)
at org.testng.TestRunner.init(TestRunner.java:205)
at org.testng.TestRunner.<init>(TestRunner.java:160)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
at org.testng.SuiteRunner.init(SuiteRunner.java:157)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
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.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 21 more
Caused by: java.lang.NullPointerException
at org.openqa.selenium.interactions.Actions.<init>(Actions.java:41)
at Examples.Flipkart.<init>(Flipkart.java:18)
... 26 more
Please some one help me, iam not able to proceed further.
Thanks
You don't initialize driver, so it is null, but you pass it to Actions.
public static WebDriver driver;
Actions action = new Actions(driver);
That throws a NullPointerException.
Caused by: java.lang.NullPointerException
at org.openqa.selenium.interactions.Actions.<init>(Actions.java:41)
Initialize driver.
Note the lifecycle. Before JUnit runs your #BeforeClass or #Before methods, it has to create the Flipkart instance. The instance field initialization expression runs at that point.
Rethink your design. Initialize action after driver has been initialized.