3

I am new to selenium and I am implementing a data driven frame work using page factory and keep getting the following error:

Cannot invoke sendKeys(String) on the primitive type void

when trying to set my test script- for the following:

String sEmail = ExcelUtil.getCellData (1,1);

loginpage.setEmail().sendKeys(sEmail);

Here is my java file:

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

Here is my test script-

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

    }
}
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
nchll
  • 53
  • 1
  • 5

1 Answers1

3

Return type of setEmail() method is void, so you can't call sendKeys() on this method.

This should be replaced :

loginpage.setEmail().sendKeys(sEmail);  

With :

loginpage.setEmail(sEmail);  

and your setEmail method should take a string argument like this :

public void setEmail(String email) {
    emailTextBox.sendKeys(email);
}  
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Upvoted. I'd also suggest that an `emailTextBox.clear();` be performed before the sendKeys – Bill Hileman Aug 06 '18 at 14:29
  • @BillHileman : Thanks. yeah , I will take `clear` as good suggestion. I do not know why OP do not communicate back when they just got the solution. We want to help and in return if the solution is helpful then you should be open to upvote and accept. – cruisepandey Aug 06 '18 at 14:36
  • 2
    It's one of the more frustrating things about SO in my opinion - abandoned questions. That's why I try to upvote useful answers whenever I can. – Bill Hileman Aug 06 '18 at 14:42
  • 1
    @cruisdepandey- sorry we must be on different times zones- i will give this a try today and let you know how it works! – nchll Aug 06 '18 at 15:37
  • No problem. Let us know if that was useful or some modification is required in answer. – cruisepandey Aug 06 '18 at 15:40
  • @cruisdepandey- i just tried it and it worked...thank you...its always something so simple :) sorry about the delayed response- – nchll Aug 06 '18 at 16:31