Exception in thread "main" java.lang.NullPointerException when trying to run a test case in Selenium with Java

Viewed 188

This is the Java code I have:

package test;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class FirstSeleniumTest {

public static WebDriver driver;

public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver","C:\\Users\\35196\\IdeaProjects\\selenium test\\SeleniumTest\\lib\\seleniumjars\\chromedriver.exe");

    driver = new ChromeDriver();

    driver.navigate().to("https://foodplanhealth.herokuapp.com/login");

    driver.manage().window().maximize();

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    WebElement root1 = driver.findElement(By.cssSelector("my-app"));
    WebElement shadow_root1 = expand_shadow_element(root1);

    WebElement root2 = shadow_root1.findElement(By.cssSelector("mwc-drawer"));
    WebElement shadow_root2= expand_shadow_element(root2);

    WebElement root3 = shadow_root2.findElement(By.name("appContent"));
    WebElement shadow_root3 = expand_shadow_element(root3);

    WebElement root4 = shadow_root3.findElement(By.tagName("div#main-content"));
    WebElement shadow_root4 = expand_shadow_element(root4);

    WebElement root5 = shadow_root4.findElement(By.cssSelector("div#container"));
    WebElement shadow_root5 = expand_shadow_element(root5);

    WebElement root6 = shadow_root5.findElement(By.cssSelector("login-container"));
    WebElement shadow_root6 = expand_shadow_element(root6);

    WebElement root7 = shadow_root6.findElement(By.cssSelector("mwc-textfield"));
    WebElement shadow_root7 = expand_shadow_element(root7);

    WebElement root8 = shadow_root7.findElement(By.cssSelector("mdc-text-field__ripple"));
    WebElement shadow_root8 = expand_shadow_element(root8);

    WebElement login = shadow_root8.findElement(By.cssSelector("mdc-text-field__input"));

    String js = "arguments[0].setAttribute('value','random@email.com')";
    ((JavascriptExecutor) driver).executeScript(js, login);

}

public static WebElement expand_shadow_element(WebElement element)
{
    return (WebElement)((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot", element);
}
}

The code runs fine up until line 35 WebElement root4 = shadow_root3.findElement(By.tagName("div#main-content")); where i get this error

Exception in thread "main" java.lang.NullPointerException at test.FirstSeleniumTest.main(FirstSeleniumTest.java:35)

I know the code in its entirety is not correct and incomplete, but I can't move forward because of this error. I just don't understand why it works fine up until this point but it doesn't in this. I asked someone for help and they told me the code runs fine for them, I checked the chrome driver version and it matches the browser version.

Can someone tell me what is wrong?

2 Answers

A NullPointerException always implies that the null pointer has been dereferenced, either by evaluating a period after a null reference, either by indexing a null as an array.

Let's study this case in your source line:

WebElement root4 = shadow_root3.findElement(By.tagName("div#main-content")); 

There are just two occurrences of the derefence operator (the period): We must discard the one after By, because it is a class and cannot be null. So we are left with just one culprit: shadow_root3 is surely null.

Next step: Why shadow_root3 is null? It is initialized in this previous line:

root3 = shadow_root2.findElement(By.name("appContent"));
WebElement shadow_root3 = expand_shadow_element(root3);

Method expand_shadow_element returns the shadowRoot of the inputted argument (or null if it has none), so we must conclude that the DOM element named appContent has no shadowRoot set.

From here on, you must debug your HTML page to diagnose why it has no shadowRoot, and how to set it properly.

Its not able to find element due to which it throws the null pointer exception. You may need to change the xpath or by the locator, you are identifying the element.

Below is the code to access the email text box

String cssSelectorForHost1 = "my-app[title='FoodPlan']";
String cssSelectorForHost2 = "page-login";
String cssSelectorForHost3 = "mwc-textfield[type='email']";
Thread.sleep(1000);
WebElement shadowDomHostElement0 = driver.findElement(By.cssSelector("my-app[title='FoodPlan']"));
WebElement last0 = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHostElement0);
Thread.sleep(1000);
WebElement shadowDomHostElement1 = last0.findElement(By.cssSelector("page-login"));
WebElement last1 = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHostElement1);
Thread.sleep(1000);
WebElement shadowDomHostElement2 = last1.findElement(By.cssSelector("mwc-textfield[type='email']"));
WebElement last2 = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", shadowDomHostElement2);
Thread.sleep(1000);
last2.findElement(By.cssSelector(".mdc-text-field__input"))
Related