How to Fix org.openqa.selenium.WebDriverException: after update selenium library - selenium

I updated Pom File
<artifactId>selenium-java</artifactId>
<version>3.13.0</version>
and
<artifactId>testng</artifactId>
<version>6.14.3</version>
Now i am trying to run simple test case look like
#BeforeMethod
public void before() {
driver=new ChromeDriver();
}
#Test
public void mytest() {
driver.get("https://www.google.com");
System.out.println(driver.getCurrentUrl());
}
#AfterMethod
public void aftermethod() {
driver.close();
}
Its give me the error
org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot navigate to invalid URL"}
(Session info: chrome=67.0.3396.99)
If we downgrade the version then its work.Can anyone help me to fixed?

Please check you add the chromdriver.exe into the PATH environment.
Or you can specify webdriver.chrome.driver in code as below:
System.setProperty("webdriver.chrome.driver", <chromeDriverPath>);
driver=new ChromeDriver();

Related

Error: Session ID is null. Using WebDriver after calling quit()? - Parallel execution - AfterMethod driver.quit/close

Find below my code
TestBase.java
protected WebDriver driver;
#BeforeMethod
public void setUpDriver() {
System.setProperty("webdriver.chrome.driver", getConfigProp.getChromeDriverPath());
driver = new ChromeDriver();
}
#AfterMethod
public void quitDriver() {
driver.close();
driver.quit();
}
MyTests.java extends TestBase.java
#Test
Test1 (){
driver.doSomething()
}
#Test
Test2 (){
driver.doSomething()
}
Error:
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
I understand it is because of driver.quit(). So I used only driver. close() in AfterMethod. Still failed. I am not using static as well. What is the Solution.
I got it working using Threadlocal driver. Thanks

Getting Null pointer exception when declaring Webdriver as null outside the method and also declaring webdriver inside the method

Can some one explain why i am getting Null pointer exception when code execution reaches login() method in below script
public class TC_01_CreateEmployee {
WebDriver driver=null;
public void launchBrowser() throws Exception
{
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
public void login()
{
driver.get("******");
driver.findElement(By.id("txtUsername")).sendKeys("****");
driver.findElement(By.id("txtPassword")).sendKeys("****");
driver.findElement(By.id("btnLogin")).click();
}
WebDriver driver=new ChromeDriver(); - This driver has a scope only within the method. The driver object which is used on the login method is still null. I am not sure why you would need 2 driver objects. you have 2 options to solve this,
public void launchBrowser() throws Exception
{
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
this.driver = driver;
Thread.sleep(2000);
}
Or
public void launchBrowser() throws Exception
{
driver=new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
It is likely that the launchBrowser() method is not being called before the login() method is being called.
An easy way around this is to define a getDriver() method that calls launchBrowser if driver is null.
private WebDriver getDriver() {
if (driver == null) {
launchBrowser();
}
return driver;
}
Then your login method looks like:
WebDriver driver = getDriver();
driver.get("******");
driver.findElement(By.id("txtUsername")).sendKeys("****");
driver.findElement(By.id("txtPassword")).sendKeys("****");
driver.findElement(By.id("btnLogin")).click();

Null pointer exception on testng

I have written the below code and getting the java null pointer exception.
please help what is the problem.
public class testngbasics {
WebDriver driver;
#BeforeMethod
public void setbrowser() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
driver.get("http://demo.guru99.com/v4/");
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
}
#Test
public void login1() {
driver.findElement(By.xpath("//input[#name='uid']")).sendKeys("mngr212595");
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("EgebYpy");
driver.findElement(By.xpath("//input[#value='LOGIN']")).click();
System.out.println(driver.getTitle());
}
#AfterMethod
public void closebrowser()
{
driver.close();
}
}
I am getting the error:
java.lang.NullPointerException it is only going to #beforemethod
annotation and not going to #test and #aftermethod
As you have already defined a global instance of WebDriver as driver in the line:
WebDriver driver;
You don't have to create another instance of WebDriver again as in:
WebDriver driver= new ChromeDriver();
And you need to replace this line as:
driver = new ChromeDriver();

Facing Issue "Cannot find class in classpath" in selenium

I am learning selenium, as part of i was trying to execute the below program. But i was getting the error " Cannot find class in classpath:practiseAutomation". The code is below:
public class practiseAutomation {
public WebDriver driver;
String baseurl="http://www.ticketnew.com/";
#BeforeTest
public void beforeTest() {
System.out.println("Executing Before Test Block");
System.setProperty("webdriver.ie.driver", "c://IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver();
driver.get(baseurl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
#Test
public void test()
{
System.out.println("Executing Test Block");
System.out.println("The Page titile is "+ driver.getTitle());
}
}
#AfterTest
public void afterTest()
{
System.out.println("Executing After Test Block");
driver.close();
}
}
Kindly help me in resolving the above issue.
In your BeforeTest method, you are creating webdriver instance which has scope in beforeTest method only. It will throw null pointer in #Test and #AfterTest methods.
As you have already declared driver globally, so just initialize it in BeforeTest method like below:-
driver=new InternetExplorerDriver();
and not WebDriver driver=new InternetExplorerDriver();

JUnit (Selenium WebDriver) to open www.google.co.uk in Chrome Browser

I had tried to run below JUnit (Selenium WebDriver) test case to open Google in Chrome browser, but it is failing with error message as
"The path to the ChromeDriver executable must be set by the
webdriver.chrome.driver system property; for more information, see
http://code.google.com/p/selenium/wiki/ChromeDriver."
As specified in that website, I downloaded ChromeDriver.exe but don't know,
Which PATH should I place that? or
How to set ChromeDriver path in webdriver.chrome.driver?
Please Advise.
My JUnit test case (changed the Firefox Driver to Chrome Driver):
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
public class Chrome_Open_Google {
private WebDriver driver;
private String baseUrl;
#Test
public void Test_Google_Chrome() throws Exception {
driver = new ChromeDriver();
baseUrl = "http://www.google.co.uk/";
driver.get(baseUrl);
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
I believe you have several options:
Either specify the folder (in which your chromedriver binary is) in your PATH system variable - here's how
Or give you application webdriver.chrome.driver as a system property by calling it with -Dwebdriver.chrome.driver=the/path/to/it parameter.
Or the same programatically: System.setProperty("webdriver.chrome.driver", "your/path/to/it");
Or this:
private static ChromeDriverService service;
private WebDriver driver;
#BeforeClass
public static void createAndStartService() {
service = new ChromeDriverService.Builder()
.usingChromeDriverExecutable(new File("path/to/my/chromedriver"))
.usingAnyFreePort()
.build();
service.start();
}
#Before
public void createDriver() {
driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
}
#After
public void tearDown() throws Exception {
driver.quit();
}
#AfterClass
public static void createAndStopService() {
service.stop();
}
System.setProperty("webdriver.chrome.driver", "your\path\to\it");
For Eg :
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\driver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();