2017-07-20 25 views
0

我想將兩個不同類的登錄和搜索方法調用到主類中。我已經編寫了兩個diff類用於登錄和搜索,但是當我調用包含main方法的類中的兩個方法時,它給出了以下錯誤>>如何使用java在selenium webdriver中將一個類調用另一個類的函數/方法

線程「main」中的異常java.lang.NullPointerException 在Test_package_Ted_baker.search.search_1(search.java:14) 在Test_package_Ted_baker.SHopping.main(SHopping.java:16)

是否有人可以幫助我瞭解如何從一個調用方法解決方案班到另一班。我的代碼如下>>

主類

package Test_package_Ted_baker; 

import org.openqa.selenium.WebDriver; 

public class SHopping { 
static WebDriver driver; 
    public static void main(String[] args) throws InterruptedException { 
     // TODO Auto-generated method stub 
     login_Ted_Baker obj=new login_Ted_Baker(); 
     obj.login_to_Ted_Baker("[email protected]", "[email protected]"); 
     System.out.println("Login sucessfully"); 
     search obj1=new search(); 
     obj1.search_1(); 
obj.user_logout(); 
     System.out.println("User log out sucessfully"); 
    } 
    } 

登錄類

package Test_package_Ted_baker; 
import java.util.HashMap; 
import java.util.Map;  
import org.openqa.selenium.By; 
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.interactions.Actions; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class login_Ted_Baker {  
    WebDriver driver;  
    public static void main (String [] args) throws InterruptedException 
    { 
     login_Ted_Baker obj=new login_Ted_Baker(); 
     obj.login_to_Ted_Baker("[email protected]", "[email protected]"); 
     System.out.println("Login Sucessfully"); 
     obj.user_logout(); 
       } 

    public void usename(String Username) 
    { 
     WebElement Username_TB=driver.findElement(By.name("j_username")); 
     Username_TB.sendKeys(Username); 
    } 
    public void password(String Password) 
    { 
     WebElement Password_TB=driver.findElement(By.name("j_password")); 
     Password_TB.sendKeys(Password); 
    } 
    public void submit() 
    { 
     driver.findElement(By.xpath(".//*[@id='command']/div/input ")).click(); 
    } 
    public void login_Ted_Baker(WebDriver driver){ 

     this.driver = driver; 

    } 
    public void user_logout() throws InterruptedException 
    { 
      Thread.sleep(1000); 
      WebElement Sign_in_link=driver.findElement(By.xpath(".//*[@id='page']/header/div/nav[1]/ul/li[3]/div/a")); 
      WebElement Sign_out_button=driver.findElement(By.xpath(".//*[@id='account']/ul/li[2]/a")); 
      Thread.sleep(1000); 
      Actions Act1=new Actions(driver); 
      Act1.moveToElement(Sign_in_link).click(); 
      Act1.moveToElement(Sign_out_button).click().build().perform(); 
      System.out.println("Logout sucessfully"); 
    } 
    public void login_to_Ted_Baker(String Username,String Password) throws InterruptedException 
    { 
     System.setProperty("webdriver.chrome.driver","C:\\Chrome\\chromedriver_win32\\chromedriver.exe"); 
     //System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger"); 
     Thread.sleep(2000); 
     ChromeOptions options = new ChromeOptions(); 
     Map<String, Object> prefs = new HashMap<String, Object>(); 
     prefs.put("credentials_enable_service", false); 
     prefs.put("password_manager_enabled", false); 
     options.setExperimentalOption("prefs", prefs); 
     options.addArguments("start-maximized"); 
     options.addArguments("disable-infobars"); 
     driver = new ChromeDriver(options); 
     try{ 
     driver.get("http://www.tedbaker.com/"); 
     WebDriverWait wait = new WebDriverWait(driver, 40); 
     wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a"))); 
     if(driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).isDisplayed()) 
     { 

     driver.findElement(By.xpath("html/body/div[3]/div[2]/div/div/ul/li[1]/a")).click(); 
     } 
     driver.findElement(By.xpath(".//*[@id='page']/header/div/nav[1]/ul/li[3]/div/a")).click(); 
     Thread.sleep(2000); 
     this.usename(Username); 
     this.password(Password); 
     this.submit(); 
     }catch(Exception e) 
     { 
      System.out.println(e.getMessage()); 
     } 
    } 
} 

搜索類

package Test_package_Ted_baker; 

import org.openqa.selenium.By; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.interactions.Actions; 
import org.openqa.selenium.support.ui.Select; 

public class search { 
    WebDriver driver; 
public void search_1() throws InterruptedException 
{ 
    WebElement Search_Item=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[1]/a/span[1]")); 
    Search_Item.click(); 
    WebElement Seacrh_item_message=driver.findElement(By.id("search")); 
    WebElement Search_textbox=driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[1]/input")); 

    Search_textbox.sendKeys("Watches"); 
    driver.findElement(By.xpath("html/body/div[2]/header/div/form/div[1]/ol/li[2]/input")).click(); 
    WebElement SearchResult_pagemessage=driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/h1")); 
    driver.findElement(By.xpath("html/body/div[2]/div/div/div[1]/div/div/div/span[1]")).click(); 

    WebElement Item_Tobuy = driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[2]/div[1]/article/div[2]/header/div/h4/a")); 
    JavascriptExecutor jse1= (JavascriptExecutor)driver; 
    jse1.executeScript("window.scrollBy(0,400)", ""); 
    Item_Tobuy.click(); 

    driver.findElement(By.xpath("html/body/div[2]/div/div/div[2]/div[1]/section[2]/form/ol/li[2]/span/span/select")).click(); 
    Select dropdown1=new Select(driver.findElement(By.id("qty"))); 
    dropdown1.selectByVisibleText("1"); 
    driver.findElement(By.id("qty")).click(); 
    // driver.findElement(By.xpath("*[@classname='button add_to_cart major full_mobile colour_dark']/div[2]/div[1]/div/input[1]")).click(); 
// driver.findElement(By.xpath("//*[@id='add_to_cart_form']/div[2]/div[2]/a/span[1]/span[1]")).click(); 
    driver.findElement(By.xpath("//input[contains(@class,'add_to_cart')]")).click(); 
    Thread.sleep(1000); 
    Actions act=new Actions(driver); 
    WebElement My_cart_button=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[3]/a/span[2]")); 
    //WebElement My_cart_button=driver.findElement(By.xpath("//input[contains(@class,'My bag')]")); 
    WebElement View_bag_checkout_button=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[3]/div/ul/li/a")); 

    act.moveToElement(My_cart_button).moveToElement(View_bag_checkout_button).click().build().perform(); 
    driver.findElement(By.xpath("html/body/div[2]/div/div/div/section/table/tbody/tr/td[2]/form/ul/li[2]/a")).click(); 
    String Cart_empty_mesg=driver.findElement(By.xpath("html/body/div[1]/div/div/div/header/h1")).getText(); 
    System.out.println(Cart_empty_mesg); 
    String Actual_cart_empty_msg="Your shopping bag is empty"; 
    if(Cart_empty_mesg==Actual_cart_empty_msg) 
    { 
     System.out.println("Cart is empty, you can add product cart"); 
    } 

    } 
} 

回答

0

這聽起來刺耳,但首先你應該閱讀有關

  • 從這個linkNull Pointer Exception
  • How to debug from a StackTrace from this link
  • Why using absolute xpath, or xpaths locators in general is a bad strategyhere

從您的堆棧跟蹤,

異常線程 「main」 顯示java.lang.NullPointerException處 Test_package_Ted_baker.SHopping.main Test_package_Ted_baker.search.search_1(search.java:14)( SHopping.java:16)

我看到你在Search類代碼

WebElement Search_Item=driver.findElement(By.xpath("html/body/div[2]/header/div/nav[2]/section/ul[2]/li[1]/a/span[1]")); 

是不正確的。我懷疑你的定位器不適合這個。該元素可以很容易地使用id屬性標識,如

driver.findElement(By.id("search")); 

,或者,如果你使用的XPath,然後

driver.findElement(By.xpath("//*[@id="search"]")); 

應該做掉NPE - 但不知道,因爲你已經使用很多絕對xpaths - 這是一個可怕的做法。

相關問題