2017-06-22 6 views
-3

私は以下のコードをSelenium Webdriverで実行しています。私はクラスSalesForceApplicationMethod()m1オブジェクトを作成してから.validation()メソッドにアクセスしています。コードは、メソッド(.validation())の内部で起こっているが、それは次のエラーを与えている:Selenium Webdriverを使用してJavaでヌルポインタ例外を解決する方法

エラートレースバック:

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'インスタンスに' driver'を設定していませんでした。 –

答えて

1

ので、 SalesForceApplicationMethodクラスのドライバは初期化されていません。そのクラスに以下のコンストラクタを追加します。

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

メインクラスでは、このクラスのインスタンスを作成中にWebDriverを渡します。

SalesForceApplicationMethod m1 = new SalesForceApplicationMethod(driver); 

希望します。ありがとう。

+0

ありがとうございました。コードは正常に動作しています。ログアウトのコードが必要です –

+0

実際の問題が解決されたので、別の質問として投稿することができます –

+0

私は別の質問として投稿します。 –

0

グローバル変数を作成しました WebDriverドライバ。 その後、main関数では()あなたは、main()関数ブロックにローカル変数になります

WebDriver driver = new ChromeDriver(o); 

を書かれています。簡単なことは、同じ名前の新しいローカル変数を作成する代わりに、グローバル変数を初期化することです。そのために、ちょうどあなたのクラスの先頭で宣言されたグローバル変数を初期化します

driver = new ChromeDriver(o); 

これでmain()のブ​​ロックで上記のコード行を置き換えます。

関連する問題