Compilation error when extending class and defining a method



  • I am getting compilation error when I am extending Loginpage class through Loginbusiness, define a method but compilation throws an error to remove illegal identifiers(eg. public) and also for arguments passed to that methods:

    Login Page:

    package com.pages.page;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.CacheLookup;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.How;
    import org.openqa.selenium.support.PageFactory;
    
    import com.utility.PageUtility;
    
    public class LoginPage {
    
        public LoginPage(WebDriver driver) {
            PageFactory.initElements(driver, this);
        }
    
        @FindBy(how = How.ID, using = PageUtility.SIGNIN_USERNAME_ID)
        @CacheLookup
        public WebElement userNameTextField;
    
        @FindBy(how = How.ID, using = PageUtility.SIGNIN_PASSWORD_ID)
        @CacheLookup
        public WebElement passwordTextField;
    
        @FindBy(how = How.ID, using = PageUtility.SIGNIN_SUBMIT_ID)
        @CacheLookup
        public WebElement signInButton;
    }  
    

    LoginBusiness:

    package com.business;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.PageFactory;
    
    import com.driver.Driver;
    import com.page.LoginPage;
    
    
    
    public class LoginBusiness extends LoginPage {
        public LoginPage loginpage;
    
        public LoginBusiness(WebDriver driver) {
            super(driver);
            // TODO Auto-generated constructor stub
    
    
        //  loginpage = PageFactory.initElements(Driver.getDriver(), LoginPage.class);
    
    
         WebElement loginToApplication(String username String password) {
    
                userNameTextField.clear();
                userNameTextField.sendKeys("aaaa");
                passwordTextField.clear();
                passwordTextField.sendKeys("4444");
                signInButton.click();
    
        }
    
        }}
    


  • Ok so there is a couple of things wrong in there:

    1. Do you really need LoginBusiness class? Seems like it all could be done in LoginPage.
    2. If you don't use the WebElements anywhere outside the LoginBusiness class they can be protected instead of public.
    3. remove the line with public LoginPage loginpage;, you already extended the class, this is probably causing the compilation issue.
    4. Inside LoginBusiness class define a private variable private WebDriver driver; and set it inside the constructor this.driver = driver;
    5. Your loginToApplication function is of type WebElement while it does not return any WebElements. Instead make it public void and don't return anything. Or make it return the page that you land on after login (and its type not void).

    Half of those errors were probably pointed out to you by the compiler...

    Adding the refactored LoginBusiness class here:

    public class LoginBusiness extends LoginPage {
    
    public LoginBusiness(WebDriver driver) {
        super(driver);
    }
    
    //  loginpage = PageFactory.initElements(Driver.getDriver(), LoginPage.class);
    
    public void loginToApplication(String username, String password) {
        userNameTextField.clear();
        userNameTextField.sendKeys("aaaa");
        passwordTextField.clear();
        passwordTextField.sendKeys("4444");
        signInButton.click();
    }
    

    }



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2