【发布时间】:2017-08-16 13:16:00
【问题描述】:
这应该很简单,但我显然遗漏了一些东西:
<div>
<label>
Scenario
<select id="scenarios">
<option value="0">Default (Visa)</option>
<option value="1">Secondary (Amex)</option>
</select>
<button onclick="pickScenario()">Select</button>
</label>
<label style="padding-left: 2em;">
Custom Amount: $
<input type="text" id="custom_amount">
</label>
</div>
当我尝试使用下面的代码定位三个元素(scenarioDropdown、selectButton、customAmount)中的任何一个时,我不断返回NullPointerExceptions。我已经用 id、xpath 和 css 尝试了所有这三种方法,但下面我展示了每个元素的一种方式:
@FindBy(css = "#scenarios")
private WebElement scenariosDropdown;
@FindBy(xpath = "//button[contains(.,'Select')]")
private WebElement select;
@FindBy(how = How.ID, using = "custom_amount")
private WebElement customAmount;
private WebDriver driver;
public void selectScenario(String scenario) {
Select select = new Select(scenariosDropdown);
select.deselectAll();
select.selectByVisibleText(scenario);
}
void clickSelect() {
select.click();
}
public void enterCustomAmount(String amount) {
customAmount.clear();
customAmount.sendKeys(amount);
}
使用以下方法运行测试。
public void testWhileBroken() {
// select Scenario Two
cc.selectScenario("Secondary (Amex)");
// enter a Custom Amount
cc.enterCustomAmount("1.23");
// click Select
cc.clickSelect();
}
然后它返回:
2017 年 8 月 16 日上午 9:07:16 org.openqa.selenium.remote.ProtocolHandshake createSession INFO:检测到的方言:OSS
java.lang.NullPointerException 在 org.openqa.selenium.support.ui.Select.(Select.java:44) 在 apps.web.modules.staplespay.CreditCardsScreen.selectScenario(CreditCardsScreen.java:27)
我在这里遗漏的最明显的事情是什么?
【问题讨论】:
-
你为
Select类导入了哪个包? -
你是如何创建对象
cc -
@Tuks import org.openqa.selenium.support.ui.Select;
-
@GaurangShah select..、enter.. 和 click.. 方法在一个临时称为 CreditCardProcessing 的类中,因此 cc 来自 CreditCardsScreen cc = new CreditCardsScreen();
-
以及构造函数看起来如何
CreditCardsScreen()
标签: java selenium testing selenium-webdriver qa