简体   繁体   中英

Selenium web driver test in Edge browser throws null pointer exception

While running the below Selenium Web driver test in java for Edge browser, it throws null pointer exception. But the test runs successfully for the chrome browser, could someone please advise about the problem here ?

java.lang.NullPointerException at payment.tests.BaseTest.beforeMethod(BaseTest.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:135) at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:64) at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:364)

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" thread-count="1" parallel="tests" >
<test name="RepayCalculatorTestChrome">
<parameter name="browser" value="Chrome" />
<packages>
    <package name="payment.tests" />
</packages>
</test>
<test name="RepayCalculatorTestEdge">
<parameter name="browser" value="Edge" />
<packages>
    <package name="payment.tests"/>
</packages>
</test>
</suite>

BaseTest.java

public class BaseTest {

    public WebDriver driver;
    public EdgeDriver edgeDriver;

    @BeforeMethod
    @Parameters("browser")
    public void beforeMethod(String browser) {
        //Check if parameter passed from TestNG is 'Edge'
        String localDir = System.getProperty("user.dir");
        if(browser.equalsIgnoreCase("edge")){
            //set path to msedgedriver.exe
            System.setProperty("webdriver.edge.driver",localDir + "\\resources\\msedgedriver.exe");
            System.out.println("Edge Driver started...");
            edgeDriver.manage().window().maximize();
            edgeDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            edgeDriver.get("test_url_here");
        }
        //Check if parameter passed from Testng is 'chrome'
        else if(browser.equalsIgnoreCase("chrome")){
            //set path to chromedriver.exe
            System.setProperty("webdriver.chrome.driver",localDir + "\\resources\\chromedriver.exe");
            System.out.println("Chrome Driver started...");
            ChromeOptions options = new ChromeOptions();
            //options.addArguments("--headless");
            driver = new ChromeDriver(options);
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            driver.get("test_url_here");
        }
        
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null || edgeDriver !=null) {
            driver.quit();
            System.out.println("Driver get instantiated. Quitting..");
        } else {
            System.out.println("Driver is null so nothing to do");
        }
    }

}

RepayCalculatorTest.java

public class RepayCalculatorTest extends BaseTest {
    
    String borrowAmount = "750000";
    String interestAmount = "2";
    String loanTerm = "30";
    
    @Test(enabled = true)
    public void loanRepayCalculator() throws InterruptedException {
        RepayCalculatorPage repayCalculator = new RepayCalculatorPage(driver);
        repayCalculator.setBorrowAmount(borrowAmount);
        WebElement webElement1 = driver.findElement(By.xpath("//input[@placeholder='Enter interest rate']"));
        webElement1.clear();
        repaymentCalculator.setInterestAmount(interestAmount);
        WebElement webElement2= driver.findElement(By.xpath("//input[@placeholder='Enter loan term']"));
        webElement2.clear();
        repaymentCalculator.setLoanTerm(loanTerm);
        repaymentCalculator.getCalculateLoanPaymentBtn();
        //driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        WebElement resultElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='calculatedresult']//div[@class='_3Kmts']//div[@class='ant-row _2kHnl']//div[contains(@class,'z-k7K ant-col-xs-24')]//p[@class='_3TRls _2fRgA']")));
        String resultText = resultElement.getText();
        System.out.println("EMI amount:"+resultText);
        Assert.assertEquals("$2,772 / month*", resultText);
    }

}

There are few issues in your code.

First:

Here:

if(browser.equalsIgnoreCase("edge")){
    //set path to msedgedriver.exe
    System.setProperty("webdriver.edge.driver",localDir + "\\resources\\msedgedriver.exe");
    System.out.println("Edge Driver started...");
    edgeDriver.manage().window().maximize();
    edgeDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    edgeDriver.get("test_url_here");
}

you call edgeDriver.manage() however edgeDriver field has not been initialized.

Second:

Even if you fix your bug with edgeDriver field initialization. Your tests work with driver field. When you run it for chrome you initialize driver field so that your tests work fine.

When you run your code for Edge, your code initializes edgeDriver leaving your driver field referring to null . Since your tests work with driver field which is left referring to null , you get NullPointerException .

This code should work better:

@BeforeMethod
@Parameters("browser")
public void beforeMethod(String browser) {
    //Check if parameter passed from TestNG is 'Edge'
    String localDir = System.getProperty("user.dir");
    if(browser.equalsIgnoreCase("edge")){
        //set path to msedgedriver.exe
        System.setProperty("webdriver.edge.driver",localDir + "\\resources\\msedgedriver.exe");
        System.out.println("Edge Driver started...");
        driver = new EdgeDriver();
    }
    //Check if parameter passed from Testng is 'chrome'
    else if(browser.equalsIgnoreCase("chrome")){
        //set path to chromedriver.exe
        System.setProperty("webdriver.chrome.driver",localDir + "\\resources\\chromedriver.exe");
        System.out.println("Chrome Driver started...");
        ChromeOptions options = new ChromeOptions();
        //options.addArguments("--headless");
        driver = new ChromeDriver(options);
    }
    
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get("test_url_here");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM