gpt4 book ai didi

java - Selenium - Java - 页面工厂 : Read from Property file and pass value in Selenium (SendKeys)

转载 作者:行者123 更新时间:2023-12-01 18:11:39 25 4
gpt4 key购买 nike

我陷入了这种境地:

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

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

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

最佳答案

您似乎没有向

传递任何值

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

关于java - Selenium - Java - 页面工厂 : Read from Property file and pass value in Selenium (SendKeys),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60459222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com