为什么我在给定的代码中得到NullPointer异常

时间:2017-04-20 13:19:20

标签: java selenium

public class AmazonTestClass {

static WebDriver driver;
static By cartNumber = By.className("a-dropdown-prompt");


public static void main(String[] args) throws InterruptedException {
    driver= new FirefoxDriver();
    driver.get("http://www.amazon.co.uk");
    Thread.sleep(5000);

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

    AmazonHomePageClass home= new AmazonHomePageClass();
    home.shopAllCategories();
    home.shopSubCategory();

    SubcategoryProductPage category = new SubcategoryProductPage();
    category.clickSeeMoreBrands();
    category.clickSelectOneBrand();

    ProductsPage product = new ProductsPage();
    product.clickProduct();

    AddToBasketPage basket = new AddToBasketPage();
    basket.clickAddToBasket();

    home.enterProductInSearchBox();
    home.clickSearchButton();
    product.clickProduct();
    basket.clickAddToBasket();

    basket.clickEditBasket();

    assert(driver.findElement(cartNumber).getText().equals("2"));
    System.out.println("Quantity of items increased to 2");

}

public class AmazonHomePageClass {

WebDriver driver;
By sShopAllCategories = By.id("nav-link-shopall");
By sShopSubCategory = By.xpath("//a[contains(text(),'Bags')]");
By sSearchBox = By.id("twotabsearchtextbox");
private static String searchItem = "Coofit handbag";

By sSearchButton = By.cssSelector("input[value='Go']");

public void shopAllCategories(){

    driver.findElement(sShopAllCategories).click();
}

public void shopSubCategory(){

    driver.findElement(sShopSubCategory).click();
}

public void enterProductInSearchBox(){

    driver.findElement(sSearchBox).sendKeys(searchItem);

}

public void clickSearchButton(){

    driver.findElement(sSearchButton).click();
}

}

以下是我收到的错误消息:

Exception in thread "main" java.lang.NullPointerException
at com.practice.pageObjectModelExercise.AmazonHomePageClass.shopAllCategories(AmazonHomePageClass.java:18)
at com.practice.pageObjectModelExercise.AmazonTestClass.main(AmazonTestClass.java:22)

0 个答案:

没有答案