This question already has answers here:
NullpointerException while invoking SendKeys through Selenium using PageFactory with Page Object
(2 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
public class NewTest
{
#FindBy(id="btnSearch")
public WebElement search;
public WebDriver driver;
#Test
public void openMyBlog() {
driver.manage().window().maximize();
driver.get("url");
search.click();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#BeforeClass
public void beforeClass() {
System.setProperty("webdriver.chrome.driver", "D:\\Driver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
}
#AfterClass
public void afterClass() {
driver.close();
}
}
Received an error as below:
[RemoteTestNG] detected TestNG version 6.14.2
Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) on port 44992
Only local connections are allowed.
Jan 29, 2019 12:59:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
FAILED: openMyBlog
java.lang.NullPointerException
at testngproject.NewTest.openMyBlog(NewTest.java:28)
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:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
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: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
===============================================
I tried my best to get rid of the issue, but failed to do so .can any one help me with the resolution to fix this null pointer exception that is received
I tried my best to get rid of the issue, but failed to do so .can any one help me with the resolution to fix this null pointer exception that is received
.
Add the below code in your program and try to re-run:
NewTest() {
PageFactory.initElements(driver, this);
}
As you are using POM(Page Object Model), you need to initialize all the elements before using it and PageFactory.initElements() will do that.
Related
I am trying to automate using web driver,testng and page factory. But I am facing null pointer exception.
Below is the code for the same.
HomePage page object class
This is page factory class
package POM;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class HomePage
{
WebDriver driver;
private static WebElement element = null;
public HomePage(WebDriver driver)
{
this.driver = driver;
}
public static WebElement signin(WebDriver driver)
{
element=driver.findElement(By.xpath("/html/body/div/div[1]/header/div[2]/div/div/nav/div[1]/a"));
return element;
}
public static WebElement emailCreate(WebDriver driver)
{
element=driver.findElement(By.id("email_create"));
return element;
}
public static WebElement submitCreate(WebDriver driver)
{
driver.findElement(By.id("SubmitCreate"));
return element;
}
}
This is Test Class for executin actual test.
Test case class
package POM;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import POM.HomePage;
public class MyTest {
private static WebDriver driver = null;
#BeforeMethod
public void beforeMethod()
{
System.setProperty("webdriver.chrome.driver", "C:/chromedriver_win32/chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.automationpractice.com");
System.out.println("Hi");
driver.manage().window().maximize();
}
#Test
public void login() throws InterruptedException
{
System.out.println("login");
HomePage.signin(driver).click();
String title = driver.getTitle();
System.out.println(title);
HomePage.emailCreate(driver).sendKeys("ulkah#xpanxion.co.in");
Thread.sleep(2000);
HomePage.submitCreate(driver).click();
}
#AfterMethod
public void afterMethod()
{
driver.close();
}
}
Exception
I am getting below exception.
[RemoteTestNG] detected TestNG version 6.14.2
Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 42928
Only local connections are allowed.
Apr 19, 2018 2:48:27 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Hi
login
FAILED CONFIGURATION: #AfterMethod afterMethod
java.lang.NullPointerException
at POM.MyTest.afterMethod(MyTest.java:41)
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:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:455)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
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:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
FAILED: login
java.lang.NullPointerException
at POM.HomePage.signin(HomePage.java:16)
at POM.MyTest.login(MyTest.java:30)
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:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
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: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
Configuration Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
Can anyone please help me resolve this error?
it seems that driver == null, that is why you got those exceptions.
Try replacing
private static WebDriver driver = null;
By
private static WebDriver driver = new ChromeDriver();
Or in your beforeMethod replacing
WebDriver driver=new ChromeDriver();
By
driver=new ChromeDriver();
because you're actually creating a second driver variable.
The easiest solution is you should not declare/create the "driver" object twice.
in your test case you have created driver object twice
1st - Class variable:
private static WebDriver driver = null;
2nd - in #Before method:
WebDriver driver=new ChromeDriver();
remove the "WebDriver" from second instance and let it to be
"driver = new ChromeDriver();"
That's all, your null pointer exception will disappear and code will execute.
I found few similar issues here(
SessionNotFoundException: Session ID is null. Using WebDriver after calling quit()? (Selenium)
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
)
but none of them did not help me to solve my issue.
Any help or advice will be appreciated.
In my case I am using TestNG.
I have created Main Class
package base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
public class MainClass {
public WebDriver driver;
public Properties CONFIG;
public Properties XPATH;
#BeforeSuite
public void initialization() throws IOException {
String chromeDriverLocation = System.getProperty("user.dir")+"/src/main/java/driverlocation/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverLocation);
driver = new ChromeDriver();
System.out.println("Web Driver initilization has started...");
System.out.println("In the before suite...");
CONFIG = new Properties();
String configLocation = System.getProperty("user.dir")+"/src/main/java/base/Config.properties";
FileInputStream ip = new FileInputStream(configLocation);
CONFIG.load(ip);
XPATH = new Properties();
String xpathLocation = System.getProperty("user.dir")+"/src/main/java/base/xpath.properties";
FileInputStream xpathIp = new FileInputStream(xpathLocation);
XPATH.load(xpathIp);
}
#AfterSuite
public void tearDown() {
System.out.println("Web Driver is closing...");
System.out.println("it is epic...");
driver.quit();
}
}
And I have class with test which extends Main.Class
package Test.TileSearch;
import org.testng.annotations.Test;
import base.MainClass;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.testng.annotations.AfterMethod;
public class Tile1City1 extends MainClass {
#BeforeMethod
public void setup() throws Exception {
driver.get(CONFIG.getProperty("urlcentura"));
driver.manage().window().maximize();
}
#AfterMethod
public void tearDown() {
driver.quit();
}
#Test
public void main1() throws Exception
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
if(!driver.findElements(By.partialLinkText("Tile")).isEmpty()){
System.out.println("Tile is available there");
}else{
System.out.println("Tile is not available there");
}
}
#Test
public void main1() throws Exception
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
driver.findElement(By.xpath().click();
if(!driver.findElements(By.partialLinkText("Tile")).isEmpty()){
System.out.println("Tile is available there");
}else{
System.out.println("Tile is not available there");
}
}
}
And here is error in console
[RemoteTestNG] detected TestNG version 6.13.1
Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 11302
Only local connections are allowed.
Feb 06, 2018 8:13:19 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Web Driver initilization has started...
In the before suite...
Abaco is available there
FAILED CONFIGURATION: #BeforeMethod setup
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T18:33:54.468Z'
System info: host: 'EARL', ip: '192.168.1.85', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:131)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
at Test.CenturaSearch.AbacoMontreal.setup(AbacoMontreal.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:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:451)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:516)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
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:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
at org.testng.TestNG.runSuites(TestNG.java:1028)
at org.testng.TestNG.run(TestNG.java:996)
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)
SKIPPED CONFIGURATION: #AfterMethod tearDown
PASSED: main
SKIPPED: main1
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T18:33:54.468Z'
System info: host: 'EARL', ip: '192.168.1.85', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:131)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
at Test.CenturaSearch.AbacoMontreal.setup(AbacoMontreal.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:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:451)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:516)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
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:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
at org.testng.TestNG.runSuites(TestNG.java:1028)
at org.testng.TestNG.run(TestNG.java:996)
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: 2, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================
Only one test is running, other always skips.
Basically the problem is that I am using static attributes for WebDriver, which shouldn't be shared between different test runs.
Anyone knows how to fix that?
It looks like you're quitting the driver in Tile1City1
#AfterMethod
public void tearDown() {
driver.quit();
}
So you open the driver in a #BeforeSuite, then quit it after one test and never re-open it. I may be misreading the code, but that appears to be what is happening.
If you want a new driver per test, you need to also open your driver each test in a #BeforeMethod.
If you want to re-use the same driver for each test, remove the code I quoted from Tile1City1
inside Driver
`public static void closeDriver() {
if (driver != null) { driver.quit(); driver = null; }
}`
and use this one in #AfterMethod
Driver.closeDriver();
I'm running a TestNG Selenium Suite via TestNG and utilizing the Applitools Eyes visual comparison tool. I'm running into a
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Long
error when my code hits eyes.checkWindow();
Eyes version 3.2
Code:
public class HelloWorld2 {
WebDriver driver;
Eyes eyes;
String testName;
#Test
public void VLPRun() throws MalformedURLException, InterruptedException {
// AppliTools Setup ---------------------------------------------
eyes = new Eyes();
eyes.setApiKey("###########################");
eyes.setForceFullPageScreenshot(false);
eyes.setMatchLevel(MatchLevel.LAYOUT);
// Launch Driver Eyes Instance ---------------------------------------------
driver = eyes.open(driver, "VLPs", "2017-mazda3-sedan", new RectangleSize(1000, 580));
// Perform Tests ---------------------------------------------
driver.get("https://www.google.com");
Thread.sleep(2000);
eyes.checkWindow();
// End the test.
eyes.close();
}
#AfterClass
public void tearDown() {
driver.quit();
eyes.abortIfNotClosed();
}
}
Any ideas?
Stacktrace:
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Long
at com.applitools.eyes.selenium.EyesSeleniumUtils.getCurrentScrollPosition(EyesSeleniumUtils.java:221)
at com.applitools.eyes.selenium.ScrollPositionProvider.getCurrentPosition(ScrollPositionProvider.java:29)
at com.applitools.eyes.selenium.FullPageCaptureAlgorithm.getStitchedRegion(FullPageCaptureAlgorithm.java:192)
at com.applitools.eyes.selenium.Eyes.getScreenshot(Eyes.java:1900)
at com.applitools.eyes.EyesBase.getAppOutputWithScreenshot(EyesBase.java:1397)
at com.applitools.eyes.EyesBase.access$000(EyesBase.java:16)
at com.applitools.eyes.EyesBase$1.getAppOutput(EyesBase.java:960)
at com.applitools.eyes.MatchWindowTask.matchWindow(MatchWindowTask.java:117)
at com.applitools.eyes.EyesBase.checkWindowBase(EyesBase.java:968)
at com.applitools.eyes.selenium.Eyes.checkWindow(Eyes.java:360)
at com.applitools.eyes.selenium.Eyes.checkWindow(Eyes.java:336)
at mazdaVLPImport.VLPTest.start(VLPTest.java:83)
at mazdaVLPImport.Experiment.Mazda_Window_600_400(Experiment.java:26)
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)
When driver launching the browser it is always getting open with local host
and then navigating to given URL but driver is not getting the current page
title instead always showing local host title "This page can’t be
displayed". I am posting my code and error i am getting, please tell me
where i am wrong.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class TestIEBrowser {
public WebDriver driver;
#BeforeClass
public void setUp() {
System.out.println("*******************");
System.out.println("launching IE browser");
System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer_Win32_2.39.0\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
}
#Test(priority=1)
public void testGooglePageTitleInIEBrowser() {
driver.navigate().to("http://www.google.com");
driver.manage().timeouts().implicitlyWait( 30, TimeUnit.SECONDS );
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();
}
} }
Error
launching IE browser
Started InternetExplorerDriver server (32-bit) 2.39.0.0
Listening on port 47579
Jul 31, 2017 5:43:08 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Page title: - This page can’t be displayed
Closing IE browser
FAILED: testGooglePageTitleInIEBrowser
java.lang.AssertionError: Page title doesn't match expected [true] but found [false]
at org.testng.Assert.fail(Assert.java:93)
at org.testng.Assert.failNotEquals(Assert.java:512)
at org.testng.Assert.assertTrue(Assert.java:41)
at TestIEBrowser.testGooglePageTitleInIEBrowser(TestIEBrowser.java:30)
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)
Try to use driver.get() instead of driver.navigate().
Also you could try to use
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(title.isDisplayed)
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();
}
}