【问题标题】:Selenium - Java - Page Factory: Read from Property file and pass value in Selenium (SendKeys)Selenium - Java - 页面工厂:从属性文件中读取并在 Selenium 中传递值(SendKeys)
【发布时间】:2020-02-28 21:04:41
【问题描述】:

我陷入了这种情况:

我尝试从属性文件(first.properties)中获取一些值并传递给 Selenium(sendKeys 方法)为此我有下一个。

我创建了一个接口来保存属性文件的字符串。

public interface Constants {

    String key_search = "search";
}

我的属性文件:

search = "Selenium Cucumber"

这是读取程序中所有属性的类(我有多个属性文件)

    public class MultiplePropertyReader {


    public static String ReadProps() {

        Properties properties = new Properties();


        try {
         //   properties.load(new FileInputStream("src/main/resources/data/zero.properties"));
            properties.load(new FileInputStream("src/main/resources/data/first.properties"));


            //First properties fields
            System.out.println("::: First Feature Property File 1 Data :::");
            System.out.println(properties.getProperty("search"));

            //Get the properties (First properties)
           String search = properties.getProperty(key_search);
           return search;


        } catch (Exception e) {
            System.out.println("No properties file found...");
            e.printStackTrace();
        }
        return null;
    }
}

在我的页面工厂类中,我有下一个代码,它将通过 sendKeys

将参数传递给特定字段
public class Page_First extends BasePage {


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


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

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

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


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

    public void startNavigation() {

      log.info("Accessing to Google");

    }

    public void search(String search) {

        searchText.sendKeys(search);
    }

    public void enterButton (){

        clickElement(searchButton);
    }
}

这是传递参数的步骤定义类的步骤

@When("I query for \"([^\"]*)\" cucumber spring selenium")
    public void I_query_for_cucumber_spring_selenium (String search)  {

        page_first.search(search);
    }

当我用 IntelliJ 运行程序时,会显示下一个问题:

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.$Proxy15.sendKeys(Unknown Source)
    at pages.Page_First.search(Page_First.java:39)
    at stepdefs.Step_First.I_query_for_cucumber_spring_selenium(Step_First.java:28)
    at ✽.When I query for "<search>" cucumber spring selenium(first.feature:10)

如果有人可以帮助我... 最好的问候。

更新:属性文件阅读器已更新,我在这里发布我的功能文件。

Feature: Navigation Test

  As a user, I would like to make a search in Google
  So I want to navigate in the page

  Scenario: Search google 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

【问题讨论】:

  • 您是否传递了功能文件中的搜索字符串?步骤def是这样写的吗?
  • 是的,我将我的搜索字符串从我的功能作为静态参数传递。 stepddef 贴在上面。

标签: java selenium-webdriver cucumber properties-file page-factory


【解决方案1】:

您似乎没有将任何值传递给

Page_First.search

你已经提到从属性文件中读取并传递值来发送密钥,但是MultiplePropertyReader.ReadProps()也没有被使用。

大概应该是这样的:

    public class MultiplePropertyReader {


        public static String ReadProps() {

            Properties properties = new Properties();


            try {
             //   properties.load(new FileInputStream("src/main/resources/data/zero.properties"));
                properties.load(new FileInputStream("src/main/resources/data/first.properties"));


                //First properties fields
                System.out.println("::: First Feature Property File 1 Data :::");
                System.out.println(properties.getProperty("search"));


                //Get the properties (First properties)
               String search = properties.getProperty(key_search);
return search;

            } catch (Exception e) {
                System.out.println("No properties file found...");
                e.printStackTrace();
            }
        }
    }

============================== Page_First================= ==============

public class Page_First extends BasePage {


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


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

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

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


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

    public void startNavigation() {

      log.info("Accessing to Google");

    }

    public void search(String search) {

        searchText.sendKeys(MultiplePropertyReader.ReadProps());
    }

    public void enterButton (){

        clickElement(searchButton);
    }
}

【讨论】:

  • 我试过了,但我遇到了同样的问题。字符串搜索 = properties.getProperty(key_search);返回搜索;它说:局部变量“搜索”是多余的
  • 能否分享更新后的代码 sn-p 和堆栈跟踪。好像声明变量有问题。
  • 我更新了属性文件阅读器类并添加了功能文件
  • 您好,我需要有关该错误的更多详细信息。你能发布堆栈跟踪吗?
  • 还有一个关于 MultiplePropertyReader.ReadProps 的问题,这里是从哪里获取“key_search”?那是一些定义的变量还是你需要作为参数传递的东西?
【解决方案2】:

使用自定义页面工厂

<dependency>
  <groupId>com.github.hemanthsridhar</groupId>
  <artifactId>custom-page-factory</artifactId>
  <version>3.0.0</version>
</dependency>

用法

@FilePath(value = PageObjectsConfig.ERROR_MSG_PAGE)
class HomePage {

  private WebDriver driver;
  public HomePage(WebDriver driver){
      this.driver = driver;
      PageFactory.initElements(new SearchWithFieldDecorator(new FileBasedElementLocatorFactory(driver, this)), this);
      }

@SearchBy
private WebElement userName;

@SearchBy
private WebElement password;

}

如果我们有一个属性文件,例如

userName_xpath=//label[text()='username']
password_id=pwd

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-13
    • 2021-03-25
    • 2023-04-02
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    相关资源
    最近更新 更多