2017-01-30 35 views
0

我試圖從另一個類中的兩個類訪問方法,但只調用一個類方法。在另一個類方法的調用期間,它會給出NullpointerException錯誤。請給我解決方案。無法從selenium webdriver中的多個類調用方法

代碼是在這裏--->

設置類 - >

package BasePOI; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class Setup { 

    public WebDriver driver; 

    public void Websiteopen() { 

     driver = new FirefoxDriver(); 

     driver.manage().window().maximize(); 

     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 

     driver.get("url"); 

    } 

    public Setup(WebDriver driver){ 


     this.driver=driver; 

    } 

} 

登錄頁面對象類--->

package BasePOI; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 

public class LoginPOI { 

    public WebDriver driver; 




     //home 
     By home_login=By.linkText("Login"); 
     By about_us=By.linkText("About Us"); 

     //login 
     By counselor=By.id("counselor_login"); 
     By user=By.id("user_login"); 
     By username=By.id("username"); 
     By password=By.id("password"); 
     By Login=By.name("Login"); 
     By create_account=By.name("Login"); 

     By Logout=By.linkText("Logout"); 


     public LoginPOI(WebDriver driver){ 


      this.driver=driver; 

     } 

     public void click_Login_button(){ 

      try { 

      driver.findElement(home_login).click(); 
      } 
      catch (Exception e) 
      { 
       System.out.println(e); 
      } 

     } 

     public void click_Login_counselor(){ 

      driver.findElement(counselor).click(); 


     } 

     public void click_Login_user(){ 

     driver.findElement(user).click(); 


    } 



     public void Enter_login_data(String uname,String pwd){ 

      driver.findElement(username).clear(); 
      driver.findElement(username).sendKeys(uname); 

      driver.findElement(password).clear(); 
      driver.findElement(password).sendKeys(pwd); 


     } 


     public void click_Login(){ 

      driver.findElement(Login).click(); 


     } 



} 

現在我打電話這兩類方法另一類

登錄功能類--->

package Functionlity; 

import java.util.concurrent.TimeUnit; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 

import BasePOI.LoginPOI; 
import BasePOI.Setup; 



public class Login { 



    public WebDriver driver; 



    @Test 
    public void openwebsite() throws InterruptedException{ 



     Setup a= new Setup(driver); 
     a.Websiteopen(); 

     Thread.sleep(10000); 

     LoginPOI b=new LoginPOI(driver); 
     b.click_Login_button(); 

    } 

}

這裏網站方法運行,但click_Login_button方法給我

errorerror --->

顯示java.lang.NullPointerException

+0

無法從selenium webdriver中的多個類調用方法。 – Harshal

回答

0

的錯誤是因爲driver下LoginPOI類未初始化。更改您的代碼,按照以下,並嘗試 -

  1. 設置類

    package BasePOI; 
    import java.util.concurrent.TimeUnit; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.chrome.ChromeDriver; 
    import org.openqa.selenium.firefox.FirefoxDriver; 
    
    public class Setup { 
    
        public static WebDriver driver; 
    
        public void Websiteopen() 
        { 
    
         driver = new FirefoxDriver(); 
         driver.manage().window().maximize(); 
         driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
         driver.get("url"); 
        } 
    
        public Setup(WebDriver driver) 
        { 
    
         this.driver=driver; 
        } 
        public Setup() 
        { 
        } 
        public WebDriver getDriver() 
        { 
         return this.driver; 
        } 
    
    } 
    
  2. 登錄POI

    package BasePOI; 
    import org.openqa.selenium.By; 
    import org.openqa.selenium.WebDriver; 
    
    public class LoginPOI 
    { 
        public WebDriver driver; 
    
        //home 
        By home_login=By.linkText("Login"); 
        By about_us=By.linkText("About Us"); 
    
        //login 
        By counselor=By.id("counselor_login"); 
        By user=By.id("user_login"); 
        By username=By.id("username"); 
        By password=By.id("password"); 
        By Login=By.name("Login"); 
        By create_account=By.name("Login"); 
    
        By Logout=By.linkText("Logout"); 
    
    
        public void click_Login_button(){ 
    
         try { 
          this.driver=new Setup().getDriver(); 
         driver.findElement(home_login).click(); 
         } 
         catch (Exception e) 
         { 
          System.out.println(e); 
         } 
    
        } 
        public void click_Login_counselor() 
        { 
         driver.findElement(counselor).click(); 
        } 
        public void click_Login_user() 
        { 
         driver.findElement(user).click(); 
        } 
        public void Enter_login_data(String uname,String pwd) 
        { 
         driver.findElement(username).clear(); 
         driver.findElement(username).sendKeys(uname); 
         driver.findElement(password).clear(); 
         driver.findElement(password).sendKeys(pwd); 
        } 
        public void click_Login() 
        { 
         driver.findElement(Login).click(); 
        } 
    } 
    
  3. 登錄類

    package Functionlity; 
    
    import java.util.concurrent.TimeUnit; 
    import org.openqa.selenium.By; 
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.firefox.FirefoxDriver; 
    import org.testng.annotations.BeforeTest; 
    import org.testng.annotations.Test; 
    
    public class Login 
    { 
        public WebDriver driver; 
        @Test 
        public void openwebsite() throws InterruptedException 
        { 
         Setup a= new Setup(driver); 
         a.Websiteopen(); 
         LoginPOI b=new LoginPOI(); 
         b.click_Login_button(); 
    
        } 
    } 
    

說明:

  1. 首先需要做webderiver設置下的類的靜態作出相同的驅動程序可以訪問不同的實例

  2. 創建設置類一個默認的構造函數和一個方法,該方法返回驅動程序訪問在另一個班

  3. this.driver=new Setup().getDriver();將獲得LoginPOI類(在設置類初始化驅動程序實例)驅動程序

+0

謝謝narendra。問題解決了。我是新來的,因此無法添加聲望。 – Harshal

+0

@Harshal,你可以接受答案 – NarendraR

0

你是不是初始化測試類中的驅動程序對象,這就是爲什麼調用時會拋出空指針異常的原因頁面類中的任何webdriver方法。

簡單的解決辦法是修改您的設置類和初始化設置方法

設置類

公共類設置{

public Webdriver getDriver() { 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
    driver.get("url"); 
} 

public Setup(WebDriver driver){ 
    this.driver=driver; 
}} 

測試類

的的webdriver登錄{}
public WebDriver driver; 

@BeforeMethod 
public void setup() { 
     driver = new FirefoxDriver(); 
} 

@Test 
public void openwebsite() throws InterruptedException{ 
    Setup a = new Setup(driver); 
    a.Websiteopen(); 
    LoginPOI b=new LoginPOI(driver); 
    b.click_Login_button(); 

}} 
+0

致謝代碼是工作 – Harshal

相關問題