2017-06-22 60 views
-3

我正在Selenium Webdriver中运行下面显示的代码。我正在创建类SalesForceApplicationMethod()m1对象,然后访问.validation()方法。该代码的方法(.validation())里面去,但它给了以下错误:如何解决在java中使用硒webdriver的空指针异常

错误回溯:

Exception in thread "main" java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditi‌​ons.java:212) at org.openqa.selenium.support.ui.FluentWait.(FluentWait.‌​java:102) at org.openqa.selenium.support.ui.WebDriverWait.(WebDrive‌​rWait.java:71) at 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("[email protected]"); 
    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(); 
    } 
} 
+1

你能添加完整的错误信息吗? – Asew

+0

就像@Asew指出的那样,没有完整的错误追溯,很难调试你的问题。 '代码进入方法内部(哪个方法?)',但是它会给出错误,因为方法第一行中的空指针异常(这是第一行?)并没有给我们提供任何信息。 :) – iamdanchiv

+0

@SrieedherSanthakumar您没有为'm1'实例设置''驱动程序' –

回答

1

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

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

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

SalesForceApplicationMethod m1 = new SalesForceApplicationMethod(driver); 

希望这可以帮助你。谢谢。

+0

非常感谢。代码运行良好。我想注销代码 –

+0

您可以将它作为单独问题发布,因为实际问题已得到解决 –

+0

我将作为单独问题发布。 –

0

您已创建全局变量 WebDriver驱动程序; 然后在main()函数你写

WebDriver driver = new ChromeDriver(o); 

这将是一个局部变量main()函数块。一个简单的事情就是初始化全局变量,而不是创建一个新的同名变量。对于这一点,只需更换的在main()块上面一行代码与

driver = new ChromeDriver(o); 

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