Java - Page Factory - Селен - Огурец: невозможно запустить тесты - PullRequest
0 голосов
/ 10 марта 2020

Привет, сообщество: У меня есть некоторые проблемы с Page Factory.

java.lang.NullPointerException
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy17.sendKeys(Unknown Source)
    at pages.page_First.searchText(page_First.java:29)
    at stepdefs.stepdefs_First.iQueryForCucumberSpringSelenium(stepdefs_First.java:34)
    at ✽.I query for "Cucumber Selenium" cucumber spring selenium(file:src/test/resources/features/first.feature:11)

Здесь моя функция:

Feature: Navigation Test

    Scenario Outline: Search google.com to verify google search is working

        Given I go to Google
        When I query for "<search>" cucumber spring selenium
        And click search
        Then google page title should become the first page

        Examples:
        | search            |
        | Cucumber Selenium |

Мой тестовый базовый класс:

public class TestBase {

    private final String OSName;
    public WebDriver driver;

    public TestBase() throws IOException {

        OSName = System.getProperty("os.name");
        System.out.println(OSName);

        Properties prop;
        if (OSName.equalsIgnoreCase("Mac")) {
            prop = new Properties();
            FileInputStream fis = new FileInputStream("\"//src//main//resources//data//config.properties\"");
            prop.load(fis);

        } else if (OSName.equalsIgnoreCase("Win")) {
            prop = new Properties();
            FileInputStream fis = new FileInputStream("\"//src//main//resources//data//config.properties\"");
            prop.load(fis);
        }
    }

    public void initialization() {

        //Takes the properties from config.properties file
        String browserName = System.getProperty("browser");
        String url = System.getProperty("url");

        System.out.println("Property loaded" + browserName);

        if (browserName.equalsIgnoreCase("chrome")) {
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();

        } else if (browserName.equalsIgnoreCase("firefox")) {
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver();
            System.out.println("In prop eles loaded FF1");

        } else if (browserName.equalsIgnoreCase("ie")) {
            WebDriverManager.iedriver().setup();
            driver = new InternetExplorerDriver();

        } else if (browserName.equalsIgnoreCase("edge")) {
            WebDriverManager.edgedriver().setup();
            driver = new EdgeDriver();

        } else if (browserName.equalsIgnoreCase("opera")) {
            WebDriverManager.operadriver().setup();
            driver = new OperaDriver();

        } else if (browserName.equalsIgnoreCase("safari")) {
            System.setProperty("webdriver.safari.driver","/usr/bin/safaridriver");
            driver = new SafariDriver();

        } else {
            System.out.println("No browser detected");
        }

        System.out.println("Property loaded" + url);
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.get(url);
    }
}

Класс фабрики страниц:

public class page_First extends BasePage {


    public page_First(WebDriver driver) throws IOException {
        PageFactory.initElements(driver, this);
    }


    //////////////////////////////////////////////WEB ELEMENTS//////////////////////////////////////////////////////////

    @FindBy(name = "q")
    private WebElement searchText;

    @FindBy(name="btnK")
    private WebElement searchButton;

    //////////////////////////////////////////////BASE METHODS//////////////////////////////////////////////////////////

    public void searchText(String search)  {

        searchText.sendKeys(search);
    }

    public void enterButton (){

        clickElement(searchButton);
    }
}

И мой класс определения шагов:

public class stepdefs_First {

    private WebDriver driver;

    private page_First page_first = PageFactory.initElements(driver, page_First.class);

    public stepdefs_First() throws IOException {
    }


    @Given("I go to Google")
    public void iGoToGoogle() {

        System.out.println("navigating through Googl");
    }

    @When("I query for {string} cucumber spring selenium")
    public void iQueryForCucumberSpringSelenium(String search) throws Exception {

        page_first.searchText(search);
    }

    @And("click search")
    public void clickSearch() {

        page_first.enterButton();

    }

    @Then("google page title should become the first page")
    public void googlePageTitleShouldBecomeTheFirstPage() {

        System.out.println("All OK");
    }
}

Браузер не открывается, и он сталкивается с проблемами в page_first.searchText (search );

Я получаю браузер из зависимости от WebDriverManager в Maven (bonygarcia), а также браузер, url и os из config.properties.

Пожалуйста, если кто-нибудь может помочь меня. Заранее спасибо

1 Ответ

0 голосов
/ 16 марта 2020

Кажется, что search param не совпадает ни с одним и развивается до нуля. Не могли бы вы попробовать

    @When("I query for \"([^\"]*)\" cucumber spring selenium")

вместо

    @When("I query for {string} cucumber spring selenium")
...