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.
Related
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.
After executing the#BeforeTest its showing java.lang.NullPointerException for #Test part.
Error displayed:
Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 34173
Only local connections are allowed.
Sep 08, 2017 10:40:43 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
FAILED CONFIGURATION: #AfterTest Aftertest
java.lang.NullPointerException
at SampleTesting.Test1.Aftertest(Test1.java:71)
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.invokeConfigurationMethod(Invoker.java:523)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:224)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:146)
at org.testng.TestRunner.afterRun(TestRunner.java:958)
at org.testng.TestRunner.run(TestRunner.java:606)
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)
FAILED: Test1
java.lang.NullPointerException
at SampleTesting.Test1.Test1(Test1.java:27)
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
Configuration Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
Code Used
package SampleTesting;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.*;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.Select;
public class NameClassTest {
WebDriver driver;
ChromeOptions options;
#Test
public void Test1() {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement tabAgreement = driver.findElement(By.xpath("***Value***"));
tabAgreement.click();
WebElement btnNew = driver.findElement(By.xpath("***Value***"));
btnNew.click();
Select lstRecordType = new Select(driver.findElement(By.id***Value***));
lstRecordType.selectByVisibleText("MSA");
WebElement btnContinue = driver.findElement(By.xpath("***Value***"));
btnContinue.click();
WebElement txtAgreementName = driver.findElement(By.xpath("***Value***"));
txtAgreementName.clear();
txtAgreementName.sendKeys("***Value***");
Date date = new Date();
System.out.println("Date is "+ date);
}
#BeforeTest
public void BeforeTest() {
options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\\\Users\\UserName\\AppData\\Local\\Google\\Chrome\\User Data");
String exepath =
"C:\\\\Users\\UserNAme\\Downloads\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver",exepath);
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("***URL***");
WebElement userName = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.id("password"));
WebElement btnLogin = driver.findElement(By.id("Login"));
userName.sendKeys("****userName****");
password.sendKeys("****password****");
btnLogin.click();
}
#AfterTest
public void Aftertest() {
WebElement drpUserName = driver.findElement(By.id***Value***);
drpUserName.click();
WebElement lnkLogout = driver.findElement(By.xpath(***Value***));
lnkLogout.click();
driver.close();
}
}
TestNG.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite_SuiteName">
<test name="Test_TestName">
<classes>
<class name="SuiteName.ClassName"/>
</classes>
</test> <!-- Test_TestName -->
</suite> <!-- Suite_SuiteName -->
Its executing the block under #BeforeTest successfully but when its coming to #Test part its giving an error message - Null Pointer exception. I am not sure what mistake I made here in my code.
This is because you have mentioned WebDriver driver = new ChromeDriver(options); in #BeforeMethod annotated method. So your webdriver instance limited for that method only.
So if go in #test annotated method it will find driver not initialized so throw the NullPointerException
Just remove WebDriver from here
#BeforeTest
public void BeforeTest()
{
options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\\\Users\\UserName\\AppData\\Local\\Google\\Chrome\\User Data");
String exepath = "C:\\\\Users\\UserNAme\\Downloads\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver",exepath);
driver = new ChromeDriver(options); // remove "WebDriver" from this line
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("***URL***");
Let me know if still have issue. Thanks :)
In your #BeforeTest annotated method you have this line
WebDriver driver = new ChromeDriver(options);
This causes your code to shadow out the class level data member and so when your #Test methods try accessing the driver, you hit a NullPointerException.
To fix this, please change WebDriver driver = new ChromeDriver(options); to driver = new ChromeDriver(options); in your #BeforeTest annotated method.
On a side note, I would recommend that you either use one of the following instead of using the #BeforeTest because #BeforeTest methods are called only once per <test> tag. So if you have two test classes which are relying on the #BeforeTest method, then you are again looking at NullPointerException
#BeforeClass - so that the webdriver is instantiated once per test class and is then shared by all the #Test methods in the class. The issue would be that you would need to resort to sequential execution only, because parallel execution can cause test methods to step on each other's shoes.
#BeforeMethod - so that the webdriver is instatiated once for every #Test method. This causes a browser to be spun off for every test method. The flip side of this would be that you would need to use ThreadLocal if you want run the methods in parallel. If sequential execution is fine, then you don't need to do anything extra.
Try creating object of WebDriver outside #BeforeTests
as mentioned below,
public class nameClassTest {
WebDriver driver = new ChromeDriver(options);
#BeforeTest
public void BeforeTest()
{
}
that will resolve your issue.
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.
I am trying to run this code with TestNG but all it gives me is java.lang.NullPointerException error. Firefox does get opened but after that error comes up. What's going wrong. Please help!
Also is it mandatory to run testNG with XML.
public class GuestCheckout
{
public WebDriver driver ;
public String baseURL = "http://www.google.com";
#BeforeTest
public void setBaseURL(){
WebDriver driver = new FirefoxDriver();
driver.get(baseURL);
}
#Test(priority = 0)
public void EmailPasswordEntry() {
// Entering Email and Password values
driver.findElement(By.id("loginRegister")).click();
WebElement email = driver.findElement(By.id("head_username"));
email.clear();
email.sendKeys("abcd#gmail.com");
WebElement password = driver.findElement(By.id("head_password"));
password.clear();
password.sendKeys("123456");
driver.findElement(By.name("loginsubmit")).click();
}
#Test(priority = 1)
void AssertionVerification() {
// Verifying "My Account" text after login
String bodyText = driver.findElement(By.xpath("//span[#id='loginHeader']/descendant::a[text()='My Account']")).getText();
Assert.assertTrue(bodyText.contains("My Accountii"), "Not matched");
}
}
Error:
--------
FAILED: EmailPasswordEntry
java.lang.NullPointerException
at package_1.GuestCheckout.EmailPasswordEntry(GuestCheckout.java:31)
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 `enter code here`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)
and same for the second Test method
Since you have instantiated WebDriver in local way and trying use that as a global instance so you are getting Null Pointer Exception. Do this in this way:
public WebDriver driver ;
public String baseURL = "http://www.ggolge.com";
#BeforeTest
public void setBaseURL(){
driver = new FirefoxDriver(); // Now this is a global instance for this class
driver.get(baseURL);
}
#Test(priority = 0)
public void EmailPasswordEntry() {
// Entering Email and Password values
driver.findElement(By.id("loginRegister")).click();
WebElement email = driver.findElement(By.id("head_username"));
email.clear();
email.sendKeys("abcd#gmail.com");
WebElement password = driver.findElement(By.id("head_password"));
password.clear();
password.sendKeys("123456");
driver.findElement(By.name("loginsubmit")).click();
}
#Test(priority = 1)
void AssertionVerification() {
// Verifying "My Account" text after login
String bodyText = driver.findElement(By.xpath("//span[#id='loginHeader']/descendant::a[text()='My Account']")).getText();
Assert.assertTrue(bodyText.contains("My Accountii"), "Not matched");
}
No it is not mandatory to run using TestNg xml, if you are using some intelligent IDE like Eclipse then you can run your tests without xml.
XMLs are beneficial when you have multiple classes to run all together as a suite.
since using webdriver... try using public static
public static webdriver driver
in each class files
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... :)