Selenium webdriver 如何使用java在SeleniumWebDriver中从一个类调用函数/方法到另一个类

Selenium webdriver 如何使用java在SeleniumWebDriver中从一个类调用函数/方法到另一个类,selenium-webdriver,Selenium Webdriver,我想将login和search方法从两个不同的类调用到主类中。我已经为登录和搜索编写了两个diff类,但是当我在包含main方法的类中调用这两个方法时,它给了我一个错误,如下>> 线程“main”java.lang.NullPointerException中出现异常 在Test_package_Ted_baker.search.search_1(search.java:14) 在Test_package_Ted_baker.SHopping.main(SHopping.java:16) 有人能帮

我想将login和search方法从两个不同的类调用到主类中。我已经为登录和搜索编写了两个diff类,但是当我在包含main方法的类中调用这两个方法时,它给了我一个错误,如下>>

线程“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("ss3777010@gmail.com", "google@123");
        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("ss3777010@gmail.com", "google@123");
        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");
  }

    }  
}

登录类

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("ss3777010@gmail.com", "google@123");
        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("ss3777010@gmail.com", "google@123");
        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");
  }

    }  
}

这听起来可能有些刺耳,但首先你应该了解

  • Null指针异常
  • 如何从StackTrace进行调试
  • 为什么通常使用绝对xpath或xpaths定位器是一个糟糕的策略
从堆栈跟踪中

线程“main”java.lang.NullPointerException中的异常位于 Test_package_Ted_baker.search.search_1(search.java:14)位于 Test_package_Ted_baker.SHopping.main(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
——但不确定,因为你已经使用了很多绝对XPath——这是一个可怕的练习