首页 > 解决方案 > 如何用 Cucumber 解决 Selenium 中的 java.lang.NullPointerException?- 行家

问题描述

public class TestRunner {
    
 public WebDriver driver;
 
    @Given("User Open the web Browser and navigated to FB page")
    public void user_open_the_web_browser_and_navigated_to_fb_page() {
         WebDriverManager.chromedriver().setup();
        ChromeOptions options = new ChromeOptions();    
        options.addArguments("--disable-notifications");
        WebDriver driver =new ChromeDriver(options);
        driver.get("http://www.facebook.com");      
    }
    @When("Enter the user name and password")
    public void enter_the_user_name_and_password() {
        try {
          driver.findElement(By.id("email")).sendKeys("123476574");
           driver.findElement(By.id("pass")).sendKeys("admin123");
        }
        catch(Exception e) {
        }
        
}
**error**
WARNING: You are using deprecated Main class. Please use io.cucumber.core.cli.Main

Scenario: Test login function                              # features/login.feature:3
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689}) on port 31567
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Sep 20, 2020 9:24:02 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
  Given User Open the web Browser and navigated to FB page # stepDefintion.TestRunner.user_open_the_web_browser_and_navigated_to_fb_page()
  When Enter the user name and password                    # stepDefintion.TestRunner.enter_the_user_name_and_password()
  And Click the login button                               # stepDefintion.TestRunner.click_the_login_button()
      ***java.lang.NullPointerException
    at stepDefintion.TestRunner.click_the_login_button(TestRunner.java:42)
    at ✽.Click the login button(file:///C:/Users/sou/eclipse-workspace/fb/features/login.feature:6)***

  Then User navigated to FB home page                      # stepDefintion.TestRunner.user_navigated_to_fb_home_page()

Failed scenarios:
file:///C:/Users/sou/eclipse-workspace/fb/features/login.feature:3# Test login function

标签: javaseleniumnullpointerexception

解决方案


推荐阅读