Java 无法在基元类型void上调用sendKeys(字符串)
我是selenium的新手,正在使用page factory实现数据驱动的框架,并不断出现以下错误: 无法在基元类型void上调用sendKeys(字符串) 尝试设置我的测试脚本时-针对以下内容:Java 无法在基元类型void上调用sendKeys(字符串),java,selenium,automation,data-driven-tests,page-factory,Java,Selenium,Automation,Data Driven Tests,Page Factory,我是selenium的新手,正在使用page factory实现数据驱动的框架,并不断出现以下错误: 无法在基元类型void上调用sendKeys(字符串) 尝试设置我的测试脚本时-针对以下内容: String sEmail=ExcelUtil.getCellData(1,1) loginpage.setEmail().sendKeys(sEmail) 这是我的java文件: package pages; import org.openqa.selenium.WebDriver; impo
String sEmail=ExcelUtil.getCellData(1,1)代码>
loginpage.setEmail().sendKeys(sEmail)代码>
这是我的java文件:
package pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class AmazonLoginPage {
WebDriver driver;
//public static void main(String[] args) {
public AmazonLoginPage (WebDriver driver) {
this.driver=driver;
}
//Using FindBy for locating Elements
@FindBy(how=How.CSS, using="#ap_email") WebElement emailTextBox;
@FindBy(how=How.CSS, using="#ap_password") WebElement passwordTextBox;
@FindBy(how=How.CSS, using="#continue") WebElement continueButton;
@FindBy(how=How.CSS, using="#signInSubmit") WebElement signInSubmitButton;
//This method is used to set email in the email text box
public void setEmail() {
emailTextBox.sendKeys();
}
//public void setEmail(String strEmail) {
//emailTextBox.sendKeys();
//This method is used Continue after entering email
public void clickOnContinueButton() {
continueButton.click();
}
//This method is used to set password in the password text box
public void setPassword (String strPassword) {
passwordTextBox.sendKeys(strPassword);
}
//This method is used to click on the Sign In Button
public void clickOnSignInSubmitButton () {
signInSubmitButton.click();
}
}
这是我的测试脚本-
package appModules;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import pages.AmazonHomePage;
import pages.AmazonLoginPage;
import utility.ExcelUtil;
public class SignIn_Action {
WebDriver driver;
public static void Execute (WebDriver driver) throws Exception {
//Get values from Excel sheet, passing parameters to getCellData method
AmazonLoginPage loginpage = PageFactory.initElements(driver, AmazonLoginPage.class);
AmazonHomePage homepage = PageFactory.initElements(driver, AmazonHomePage.class);
homepage.clickOnSignIn();
String sEmail = ExcelUtil.getCellData (1,1);
String sPassword = ExcelUtil.getCellData(1, 2);
loginpage.setEmail().sendKeys(sEmail);
loginpage.clickOnContinueButton();
loginpage.clickOnSignInSubmitButton();
}
}
setEmail()
方法的返回类型为void,因此不能在此方法上调用sendKeys()
这应该被替换:
loginpage.setEmail().sendKeys(sEmail);
与:
loginpage.setEmail(sEmail);
您的setEmail方法应该采用如下字符串参数:
public void setEmail(String email) {
emailTextBox.sendKeys(email);
}
向上投票。我还建议使用emailTextBox.clear()代码>必须在启动之前执行sendKeys@BillHileman:谢谢。是的,我会把clear
作为一个好建议。我不知道为什么OP在刚得到解决方案时不进行反馈。我们想提供帮助,作为回报,如果解决方案有帮助,那么你应该公开投票并接受。在我看来,这是最令人沮丧的事情之一——被抛弃的问题。这就是为什么我会尽可能地推荐有用的答案。@cruisdepandey-很抱歉,我们必须在不同的时区-我今天会尝试一下,让你知道它是如何工作的!没问题。让我们知道这是否有用,或者回答中需要一些修改。