简体   繁体   English

Java - 页面工厂 - Selenium - Cucumber:无法运行测试

[英]Java - Page Factory - Selenium - Cucumber: Impossible to Run Tests

Hi community: I have some issues with 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)

Here, my feature:在这里,我的特点:

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 |

My Test Base Class:我的测试基类:

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);
    }
}

Page Factory Class:页面工厂类:

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);
    }
}

And my Step Definition class:还有我的 Step Definition 类:

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");
    }
}

The browser does not open, and it is facing issues in the page_first.searchText(search);浏览器打不开,在page_first.searchText(search)中遇到问题

I'm getting the browser from WebDriverManager dependency in Maven (bonygarcia), and the browser, url and os from a config.properties.我从 Maven (bonygarcia) 中的 WebDriverManager 依赖项获取浏览器,并从 config.properties 获取浏览器、url 和 os。

Please if anybody could help me.请如果有人可以帮助我。 Thanks in advance提前致谢

Seems that search param does not match any and evolves to null.似乎search参数不匹配任何并演变为空值。 Could you try你能不能试试

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

instead of代替

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Cucumber 与 Java Selenium 和 Gradle 并行运行测试 - Run tests in parallel using Cucumber with Java Selenium with Gradle 在带有 Java 的 Selenium 中使用 @Factory 注释后无法运行测试 - Not able to run tests after using @Factory annotation in Selenium with Java 使用 Page Factory Design 和 Page Object Model 进行测试,使用 Selenium 和 ZD5238788375ZA21833187880E1EA218 打开浏览器的两个实例 - Tests using Page Factory Design and Page Object Model opens two instances of the browser using Selenium and Java 使用黄瓜运行多个测试功能以进行 java selenium 宁静测试 - Running multiple tests features with cucumber for java selenium serenity tests 在 Java 中以串行方式运行 cucumber 测试 - Run cucumber tests in a serial fashion in Java 通过命令行运行 Java/Selenium/Cucumber 测试 - Running Java/Selenium/Cucumber tests via command line 使用webdriver-factory运行Selenium Webdriver TestNG测试 - Run Selenium Webdriver TestNG tests using webdriver-factory 硒/黄瓜测试未在 Maven 中运行 - Selenium/Cucumber tests not running in Maven 为什么黄瓜会找到“ runTest.java”来运行测试? - Why cucumber will find the “runTest.java” to run tests? 如何以编程方式使用TestNG运行Selenium Java测试? - How to run Selenium Java tests with TestNG programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM