【问题标题】:how to resolve Null pointer exception in java using selenium webdriver如何使用 selenium webdriver 解决 Java 中的空指针异常
【发布时间】:2017-06-22 09:26:49
【问题描述】:

我正在 Selenium Webdriver 中运行下面显示的代码。我正在创建SalesForceApplicationMethod() 类的m1 对象,然后访问.validation() 方法。代码在方法内部(.validation()),但它给出了以下错误:

错误追溯:

在 org.openqa.selenium.support.ui.FluentWait.(FluentWait) 的 com.google.common.base.Preconditions.checkNotNull(Preconditi‌​ons.java:212) 的线程“main”中出现异常 java.lang.NullPointerException .‌​java:102) 在 org.openqa.selenium.support.ui.WebDriverWait.(WebDrive‌​rWait.java:71) 在 org.openqa.selenium.support.ui.WebDriverWait.(WebDrive‌​rWait.java:45 ) at com.syntel.pratice.SalesForceApplicationMethod.validation(Sa‌​lesForceApplicationM‌​ethod.java:27) at com.syntel.pratice.SalesForceApplicationMethod.main(SalesFor‌​ceApplicationMethod.‌​java:88)

我的代码:

package com.syntel.pratice;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
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 org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SalesForceApplicationMethod
{
  WebDriver driver;
  public void validation()
  {

    // App launcher clicking
    WebDriverWait wait = new WebDriverWait(driver,25);
    WebElement ele = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='label slds-truncate slds-text-link']")));
    ele.click();

    // New button
    WebElement newBtn1 = driver.findElement(By.xpath("//div[contains(text(),'New')]"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", newBtn1);

    // Account name - first account
    driver.findElement(By.xpath("//input[@class='input uiInput uiInputText 
    uiInput--default uiInput--input']")).sendKeys("srieedherks");

    // Click Save
    driver.findElement(By.xpath("//button[@class='slds-button slds-button--neutral uiButton--default uiButton--brand uiButton forceActionButton']")).click();

    // Clicking new contact button
    WebDriverWait wait1 = new WebDriverWait(driver, 20); 
    WebElement newbt = driver.findElement(By.xpath("//div[contains(text(),'New 
    Contact')]"));
    JavascriptExecutor exe = (JavascriptExecutor)driver;
    exe.executeScript("arguments[0].click();", newbt);
    WebElement ele1 = driver.findElement(By.linkText("--None--"));
    ele1.sendKeys("Mr.");
    System.out.println("Selecting ");

    // Entering first name
    driver.findElement(By.xpath("//input[@class='compoundBorderBottom form-
    element__row input']")).sendKeys("srieedher");

    // Entering last name
    driver.findElement(By.xpath("//input[@class='compoundBLRadius 
    compoundBRRadius form-element__row input']")).sendKeys("santhakumar");

    // Click Save
    driver.findElement(By.xpath("//button[@class='slds-button slds-button--
    brand cuf-publisherShareButton undefined uiButton']")).click();

    // Logout view profile
    driver.findElement(By.xpath("//img[@src='https://c.ap5.content.force.com/pro
    filephoto/005/T/1']")).click();
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("arguments[0].click();", newbt);

    // Click log out   
    driver.findElement(By.xpath("//a[contains(@class, 'profile-link-label 
    logout uiOutputURL')]")).click();
  }

  public static void main(String[] args) 
  {
    // TODO Auto-generated method stub
    System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
    ChromeOptions o = new ChromeOptions();
    o.addArguments("disable-extensions");
    o.addArguments("--start-maximized");

    WebDriver driver = new ChromeDriver(o);
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.get("https://login.salesforce.com/");
    driver.findElement(By.id("username")).sendKeys("srieedher@force.com");
    driver.findElement(By.id("password")).sendKeys("Sriee678$");
    driver.findElement(By.id("Login")).click();

    Scanner s = new Scanner(System.in);
    System.out.println("Enter your otp:");
    String i = s.next().toString();
    System.out.println("the OTP is : "+i);
    driver.findElement(By.id("smc")).sendKeys(i);
    driver.findElement(By.id("save")).click();
    driver.findElement(By.xpath("//div[@class='slds-icon-Waffle']")).click();

    SalesForceApplicationMethod m1 = new  SalesForceApplicationMethod();
    m1.validation();
  }
}

【问题讨论】:

  • 你能添加完整的错误信息吗?
  • 就像@Asew 指出的那样,没有完整的错误回溯,很难调试您的问题。 The code is going inside the method(哪种方法?)but it is giving error as Null Pointer Exception in first line of method(这是第一行?)没有给我们任何信息。 :)
  • @SrieedherSanthakumar 您没有为m1 实例设置driver
  • @Asew,错误是 org.openqa.selenium 的 com.google.common.base.Preconditions.checkNotNull(Preconditions.java:212) 的线程“main”java.lang.NullPointerException 中的异常。 support.ui.FluentWait.(FluentWait.java:102) 在 org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:71) 在 org.openqa.selenium.support.ui。 WebDriverWait.(WebDriverWait.java:45) at com.syntel.pratice.SalesForceApplicationMethod.validation(SalesForceApplicationMethod.java:27) at com.syntel.pratice.SalesForceApplicationMethod.main(SalesForceApplicationMethod.java:88) 请帮帮我。
  • @iamdanchiv,错误是 org.openqa.selenium 的 com.google.common.base.Preconditions.checkNotNull(Preconditions.java:212) 的线程“main”java.lang.NullPointerException 中的异常。 support.ui.FluentWait.(FluentWait.java:102) 在 org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:71) 在 org.openqa.selenium.support.ui。 WebDriverWait.(WebDriverWait.java:45) 在 com.syntel.pratice.SalesForceApplicationMethod.validation(SalesForceApplicationMethod.java:27) 在 com.syntel.pratice.SalesForceApplicationMethod.main(SalesForceApplicationMethod.java:88)

标签: java selenium selenium-webdriver automated-tests


【解决方案1】:

因为SalesForceApplicationMethod 类中的驱动程序未初始化。在该类中添加以下构造函数。

public SalesForceApplicationMethod(WebDriver ldriver)
{
    this.driver=ldriver;
}

在创建此类实例的主类中,传递您的WebDriver。

SalesForceApplicationMethod m1 = new  SalesForceApplicationMethod(driver);

希望这对您有所帮助。谢谢。

【讨论】:

  • 非常感谢。代码运行良好。我想要注销代码
  • 由于实际问题已经解决,您能否将其作为单独的问题发布
  • 我将作为一个单独的问题发布。
  • 仅在 90 分钟内提出的问题。我会在 90 分钟后提问。
【解决方案2】:

你已经创建了一个全局变量 WebDriver 驱动程序; 然后在函数 main() 中你写了

WebDriver driver = new ChromeDriver(o);

这将是 main() 函数块的局部变量。可以做的一件简单的事情是初始化全局变量,而不是创建一个新的同名局部变量。为此,只需将 main() 块中的上述代码行替换为

   driver = new ChromeDriver(o);

这将初始化在类开头声明的全局变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2022-12-05
    相关资源
    最近更新 更多