如何用 Cucumber 解决 Selenium 中的 java.lang.NullPointerException?

2023-12-13

Register.feature
Feature: The Registration Page

Background:
Given The User is on the Registration Page

Scenario: The User Should be able to Register
When The User Enters Contact Information
And The User Enters Mailing Information
When The User Enters User Information

ReisterSteps.java
public class RegisterSteps {

    public  WebDriver driver;

    @Given("^The User is on the Registration Page$")
    public void the_User_is_on_the_Registration_Page() throws Throwable {
        System.out.println("Background:- The User is on the Home Page");
        System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\cucumberBDDProject\\src\\Resources\\chromedriver.exe");
        //System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.newtours.demoaut.com/");
        Thread.sleep(3000);
        driver.navigate().to("http://www.newtours.demoaut.com/mercuryregister.php");
        Thread.sleep(3000);
    }

         @When("^The User Enters Contact Information$")
            public void the_User_Enters_Contact_Information() throws Throwable {
                System.out.println("Step1:- The User Enters Contact Information");
                try 
                {

                WebElement e = driver.findElement(By.name("firstName"));
                e.click();
                e.sendKeys("Manideep");
                driver.findElement(By.name("lastName")).sendKeys("Latchupatula");
                driver.findElement(By.name("phone")).sendKeys("9052324348");
                driver.findElement(By.name("userName")).sendKeys("[email protected]");
    }

        catch(Exception e)
                {
                    System.out.println("Exception has Caught");
                }
            } 
    @And("^The User Enters Mailing Information$")
    public void the_User_Enters_Mailing_Information() throws Throwable {
        System.out.println("Step2:- The User Enters Mailing Information");
        driver.findElement(By.name("address1")).sendKeys("Chinareddy Street");
        driver.findElement(By.name("address2")).sendKeys("Alajangi");
        driver.findElement(By.name("city")).sendKeys("Bobbili");
        driver.findElement(By.name("state")).sendKeys("Andhra Pradesh");
        driver.findElement(By.name("postalCode")).sendKeys("535558");
        Thread.sleep(3000);
        WebElement e = driver.findElement(By.name("country"));
        Select s = new Select(e);
        s.selectByValue("India");
        Thread.sleep(3000);

    }

    @When("^The User Enters User Information$")
    public void the_User_Enters_User_Information() throws Throwable {
        System.out.println("Step3:- The User Enters User Information");
        driver.findElement(By.name("email")).sendKeys("[email protected]");
        driver.findElement(By.name("password")).sendKeys("Pass@123");
        driver.findElement(By.name("confirmPassword")).sendKeys("Pass@123");
    }

}

Runner.java
package runners;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features="C:\\Selenium\\cucumberBDDProject\\ProjectFeatures",glue="stepDefinitions")

public class LoginRunner {


}

Error Message:
Background:- The User is on the Home Page
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 39832
Only local connections are allowed.
Aug 10, 2018 3:13:16 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Step1:- The User Enters Contact Information
Exception has Caught
Step2:- The User Enters Mailing Information
Step1:- The User is on the Login Page
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 35493
Only local connections are allowed.
Aug 10, 2018 3:13:27 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
[1533894216.869][WARNING]: Timed out connecting to Chrome, retrying...
[1533894220.887][WARNING]: Timed out connecting to Chrome, retrying...

[31mFailed scenarios:[0m
[31mRegister.feature:6 [0m# Scenario: The User Should be able to Register
[31mlogin.feature:2 [0m# Scenario: The User should be able to Login with Valid Credentials.

2 Scenarios ([31m2 failed[0m)
9 Steps ([31m2 failed[0m, [36m5 skipped[0m, [32m2 passed[0m)
0m37.335s

java.lang.NullPointerException
    at stepDefinitions.RegisterSteps.the_User_Enters_Mailing_Information(RegisterSteps.java:56)
    at ?.And The User Enters Mailing Information(Register.feature:8)

描述:

我正在使用 Maven 项目开发 Selenium。我已多次运行上述代码,但网页无法接收输入。在执行相同操作时,我收到 java.lang.NullPointerException。

我用过的包: 1.资源 2.特点 3.step定义 4跑者

笔记: 有趣的是,如果我通过创建 Java 项目来执行代码,则代码工作得绝对正常。

提前致谢:)


Within RegisterSteps您已声明为全局的类网络驱动程序实例为:

public  WebDriver driver;

但在内部继续前进void the_User_is_on_the_Registration_Page()你已经初始化了另一个方法网络驱动程序实例为:

WebDriver driver = new ChromeDriver();

哪些范围之外没有范围void the_User_is_on_the_Registration_Page()方法。因此你看到java.lang.NullPointerException

Solution

Within void the_User_is_on_the_Registration_Page()方法将其更改为:

driver = new ChromeDriver();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何用 Cucumber 解决 Selenium 中的 java.lang.NullPointerException? 的相关文章

随机推荐