【发布时间】: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