如何使用java

时间:2017-07-20 09:48:14

标签: selenium-webdriver

我想将登录和搜索方法从两个不同的类调用到主类中。我已经编写了两个用于登录和搜索的差异类但是当我在类中包含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");
  }

    }  
}

1 个答案:

答案 0 :(得分:0)

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

    来自此link。的
  • Null Pointer Exception 来自此link
  • How to debug from a StackTrace 来自here
  • Why using absolute xpath, or xpaths locators in general is a bad strategy

从堆栈跟踪中

  

线程中的异常&#34; main&#34; java.lang.NullPointerException at   Test_package_Ted_baker.search.search_1(search.java:14)at   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 - 但不确定,因为你已经使用了很多绝对x路径 - 这是一种可怕的做法。