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

问题描述 投票:0回答:1

我想从两个不同的类调用登录和搜索方法到主类中。我已经编写了两个用于登录和搜索的 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("[email protected]", "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("[email protected]", "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");
  }

    }  
}
selenium-webdriver
1个回答
0
投票

这可能听起来很刺耳,但首先你应该阅读

  • Null Pointer Exception
    来自此链接
  • How to debug from a StackTrace
    来自此链接
  • Why using absolute xpath, or xpaths locators in general is a bad strategy
    来自这里

从你的堆栈跟踪中,

线程“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 - 这是一个可怕的做法。

© www.soinside.com 2019 - 2024. All rights reserved.