Получение нулевого значения для элемента объекта страницы в Selenium - PullRequest
0 голосов
/ 02 октября 2019

Я работаю в проекте на основе огурца, TestNG, Selenium, и у меня есть страница PageObject, как показано ниже

package com.testing.pageobject;

import java.util.List;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.testing.components.SearchResultComponent;
import com.testing.stepdefinitions.BaseClass;

public class ShoppingPage extends BaseClass {



    public ShoppingPage(RemoteWebDriver driver) {
        super();
        this.driver=driver;
        PageFactory.initElements(driver, this);

    }

    @FindBy(xpath="//div[@class='sh-dlr__list-result']")
    private List<SearchResultComponent> SearchResult;

    @FindBy(xpath="//span[@class='qa708e IYWnmd']")
    private List<WebElement> ResultListView;

    @FindBy(xpath="//span[@class='Ytbez']")
    private List<WebElement> ResultGridView;

    public List<SearchResultComponent> getSearchResult() {
        return SearchResult;
    }

    public List<WebElement> getResultListView() {
        return ResultListView;
    }

    public List<WebElement> getResultGridView() {
        return ResultGridView;
    }   

}

, и страница моего компонента, как показано ниже

package com.testing.components;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.testing.stepdefinitions.BaseClass;

public class SearchResultComponent extends BaseClass {



    public SearchResultComponent(RemoteWebDriver driver) {
         super();
         this.driver=driver;
         PageFactory.initElements(driver, this);
    }

    @FindBy(xpath="//a[@class='AGVhpb']")
    private WebElement productName;

    @FindBy(xpath="//span[@class='O8U6h']")
    private WebElement productPrice;

    @FindBy(xpath="//div[@class='vq3ore']")
    private WebElement productStars;

    @FindBy(xpath="//img[@class='TL92Hc']")
    private WebElement productImage;

    @FindBy(xpath="//div[@class='hBUZL CPJBKe']")
    private WebElement productDescription;

    public WebElement getProductName() {
        return productName;
    }

    public WebElement getProductPrice() {
        return productPrice;
    }

    public WebElement getProductStars() {
        return productStars;
    }

    public WebElement getProductImage() {
        return productImage;
    }

    public WebElement getProductDescription() {
        return productDescription;
    }




}

Мой шаг огурцаопределение:

@When("search for product less than {int}")
public void search_for_product_less_than(Integer int1) {
    ShoppingPage myshopping = new ShoppingPage(driver);
    List<SearchResultComponent> SearchResults = myshopping.getSearchResult();
    for(SearchResultComponent myResult:SearchResults) {
        System.out.println(myResult.getProductName());
    }
}

Постановка задачи:

При попытке получить getSearchResult () в определении шага я получаю ошибку с нулевой точкой. Не уверен, почему какие-то мысли, как это исправить?

Ответы [ 3 ]

1 голос
/ 03 октября 2019

В классе Shoppingpage используйте WebElement в качестве типа вместо имени класса SearchResultComponent, как указано ниже.

@FindBy(xpath="//div[@class='sh-dlr__list-result']")
private List<WebElement> SearchResult;

public List<WebElement> getSearchResult() {
    return SearchResult;
}

Также используйте webelement в определении шага огурца

   @When("search for product less than {int}")
   public static void search_for_product_less_than(Integer int1) {
        ShoppingPage myshopping = new ShoppingPage(driver);
        List<WebElement> SearchResults = myshopping.getSearchResult();
        for(WebElement myResult:SearchResults) {
            System.out.println(myResult.getText());
        }
    }
0 голосов
/ 03 октября 2019

Спасибо @Yosuva A. Я обновил его до WebElement, и теперь он работает, как показано ниже

public void search_for_product_less_than(Integer int1) {
    ShoppingPage myshopping = new ShoppingPage(driver);
    SearchResultComponent resultcomp = new SearchResultComponent(driver);

    List<WebElement> PriceResult = resultcomp.getProductPrice();
    List<WebElement> ProductName = resultcomp.getProductName();
    for(int i=0;i<PriceResult.size();i++) {
        String price = PriceResult.get(i).getText().replace("$", "");
        System.out.println("Price" + Double.valueOf(price));
        if(Double.valueOf(price)<13) {
            System.out.println(price);
            System.out.println(ProductName.get(i).getText());

        }
    }
}  
0 голосов
/ 02 октября 2019

Причина в том, что нужный элемент не найден на странице. У этого могут быть разные причины.

1) Вы вызываете new ShoppingPage(driver) слишком рано, когда фрагмент страницы с "//span[@class='qa708e IYWnmd']" еще не загружен.

2) Возможно, что XPath определеннеправильно и на странице нет такого элемента. Проверьте свой XPath.

...