0

I am trying to automate search functionality on a website. While running the test case, Firefox browser is opened and the website is rendered correctly but still system shows null pointer on line where i call driver.get(URL); The code snippet is as follows:

    package testcases;


    import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import static org.junit.Assert.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;

    public class SaavnSearchWeb {
      private WebDriver driver;
      private String baseUrl;
      private boolean acceptNextAlert = true;
      private StringBuffer verificationErrors = new StringBuffer();

      @Before
      public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://saavn.com";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }

      @Test
      public void testSaavnSearchWeb() throws Exception {

        driver.get(baseUrl + "/s/artist/deepika-padukone-album/36LfdYhYGvc_");
        if (isElementPresent(By.id("q"))){
            driver.findElement(By.id("q")).sendKeys("cocktail");
            driver.findElement(By.linkText("Tum Hi Ho Bandhu")).click();
        }
      }

      @After
      public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
          fail(verificationErrorString);
        }
      }

      private boolean isElementPresent(By by) {
        try {
          driver.findElement(by);
          return true;
        } catch (NoSuchElementException e) {
          return false;
        }
      }

      private boolean isAlertPresent() {
        try {
          driver.switchTo().alert();
          return true;
        } catch (NoAlertPresentException e) {
          return false;
        }
      }

      private String closeAlertAndGetItsText() {
        try {
          Alert alert = driver.switchTo().alert();
          String alertText = alert.getText();
          if (acceptNextAlert) {
            alert.accept();
          } else {
            alert.dismiss();
          }
          return alertText;
        } finally {
          acceptNextAlert = true;
        }
      }
    }

The stack trace is as follow:

java.lang.NullPointerException
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:353)
    at testcases.SaavnSearchWeb.testSaavnSearchWeb(SaavnSearchWeb.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Can someone please help me with this?

Satwik Nadkarny
  • 5,086
  • 2
  • 23
  • 41
user5446733
  • 1
  • 1
  • 2
  • post the class completely if possible .. want to know how driver is declared – StrikerVillain Oct 14 '15 at 20:16
  • `public class SaavnSearchWeb { private WebDriver driver; private String baseUrl; 'before' public void setUp() throws Exception { driver = new FirefoxDriver (); baseUrl = "http://www.saavn.com"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } 'test' public void testSaavnSearchWeb() throws Exception { driver.get(baseUrl + "/s/artist/deepika-padukone-album/36LfdYhYGvc_"); if (isElementPresent(By.id("q"))){ driver.findElement(By.id("q")).sendKeys("cocktail"); driver.findElement(By.linkText("Tum Hi Ho Bandhu")).click(); } }` – user5446733 Oct 14 '15 at 20:31
  • tried your code.. No error for me. The test case passed successfully. Which version of Selenium and JUnit are you using. Also, you are running this as JUnit, right? – StrikerVillain Oct 14 '15 at 21:53
  • Agreed. Code works fine. Not sure if `By.id("q")` is a valid locator here. – AGill Oct 14 '15 at 21:58
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Louis Oct 14 '15 at 22:47
  • Junit: 4.12 selenium: 2.47 – user5446733 Oct 15 '15 at 06:49

1 Answers1

0

You are not using the right protocol for the url. Change the baseUrl to as follows:

baseUrl = "http://saavn.com";

And run the code.

AGill
  • 768
  • 8
  • 17
  • @A Gill- tried with this but same error. When i run the testcse, url is invoked correctly once but control does not go to the next line and after some time. i get null pointer exception again at the same line for driver.get(URL), but the invoked browser is still open – user5446733 Oct 14 '15 at 21:06
  • Try this.`public class SaavnSearchWeb { private WebDriver driver; private String baseUrl; @Before public void setUp() throws Exception { driver = new FirefoxDriver (); baseUrl = "http://www.saavn.com"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testSaavnSearchWeb() throws Exception { driver.get(baseUrl + "/s/artist/deepika-padukone-album/36LfdYhYGvc_"); }` – AGill Oct 14 '15 at 21:09
  • @A Gill: control does not move ahead to the next line in after this line of my code – user5446733 Oct 14 '15 at 21:11
  • Can you please post your full code and the error stack to the original question. I am sure someone will be able to solve your problem. – AGill Oct 14 '15 at 21:15
  • So you are saying, the `driver.findElement();` line is not getting executed but the you are able to go the defined url? – AGill Oct 14 '15 at 21:53
  • @A Gill: thats the isssue I am facing – user5446733 Oct 15 '15 at 05:44
  • Ok, so thats a different question now. You original question says you are getting java.lang.NullPointerException while you are trying to execute `driver.get()` but that problem seems to be resolved. Please update the question with what you want to accomplish (whats the use case) or please ask a new one. – AGill Oct 15 '15 at 17:45