java - 无法在原始类型 void 上调用 sendKeys(String)

标签 java selenium automation data-driven-tests page-factory

我是 selenium 的新手,我正在使用页面工厂实现数据驱动的框架工作,但不断收到以下错误:

Cannot invoke sendKeys(String) on the primitive type void

尝试设置我的测试脚本时 - 用于以下内容:

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

关于java - 无法在原始类型 void 上调用 sendKeys(String),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51699709/

相关文章:

java - 让 JQuery-UI 插件与 Errai 一起使用

java - Logback 附加程序和 Postfix

java - 如何修复JAVA中的 "The constructor Timer(int, new ActionListener(){}) is undefined"错误?

java - 从 WebElement 中提取定位器并使用 pagefactory

python - 如何将 python 变量传递到 bash 脚本函数中

java - 对于从下拉框中选择的特定值,在下拉列表中显示其对应的值吗?

angularjs - 如何断言某个元素已获得焦点?

javascript - Selenium ,java,js "Exception in thread "主“org.openqa.selenium.JavascriptException : missing ) after argument list"

python - 在Windows7上安装Selenium/Python Bindings 2.35.0

javascript - 您可以自动将产品从在线商店添加到 WooCommerce 吗?