-2

I'm automating a simple login test project with cucumber + maven + testng. I can't write username and password in login page I can't login https://www.lambdatest.com/automation-demos/ website. Where is my mistake? What am I doing wrong.

   public class loginPage extends TestBase {
    
    public loginPage() {
        PageFactory.initElements(driver, this);
    }
    
    @FindBy(how = How.XPATH, using ="//input[@id='username']")
    WebElement uss;

    @FindBy(how = How.XPATH, using ="//input[@id='password']")
    WebElement pss;

    @FindBy(how = How.CSS, using=".applynow")
    WebElement buttonLogin;
    
    public void enter_username(String user) {
        uss.sendKeys(user); 
    }

    public void enter_password(String pass) {
        pss.sendKeys(pass);
    }
    public void enter_buttonLogin() {
        buttonLogin.click();
    }   
    
}

public class loginPageSteps extends TestBase{
    
    loginPage LoginPage;
    
    @Given("I open the login page")
    public void i_open_the_login_page() throws InterruptedException {
        TestBase.initialization();
    }

    @When("^I fill username \"([^\"]*)\" and password \"([^\"]*)\"$")
    public void i_fill_username_and_password(String username, String password) {
        
        LoginPage.enter_username(username);
        LoginPage.enter_password(password);
        LoginPage.enter_buttonLogin();
    }
    
    @Then("I should see the success login message \\\"([^\\\"]*)\\\"")
    public void i_should_see_the_success_login_message(String string) {
        // Write code here that turns the phrase above into concrete actions
        throw new cucumber.api.PendingException();
    }

}

My TestBase Code:

public class TestBase {
public static WebDriver driver;
public static WebDriverWait wait;
public static Properties properties;
public static String currentDirectory = System.getProperty("user.dir");

public TestBase() {

    properties = new Properties();
    try {
        properties.load(new FileInputStream(currentDirectory + "//src//test//resources//config.properties"));
    } catch (FileNotFoundException e) {
        System.out.println("File Not Found Exception");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("IO Exception");
        e.printStackTrace();
    }
}

public static void initialization() {
    String browserName = properties.getProperty("browser");
    System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
    if (browserName.equals("chrome")) {
        // driver = new ChromeDriver();
        ChromeOptions optns = new ChromeOptions();
        optns.addArguments("disable-infobars");
        driver = new ChromeDriver(optns);
        System.out.println("Browser is set to Chrome");
    } else if (browserName.equals("firefox")) {
        driver = new FirefoxDriver();
        System.out.println("Browser is set to Firefox");
    } else {
        driver = new InternetExplorerDriver();
        System.out.println("Browser is set to IE");
    }
    // driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
    // driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();
    driver.get(properties.getProperty("url"));
}

public static void closeBrowser() {
    driver.quit();
  }

 }
0
1

You should define an instance of login page first. In loginPageSteps class and in i_fill_username_and_password method, first add this line:

LoginPage = new loginPage();

then call enter_username and all other methods you write.

Note: Actually you should read more about Naming convention in java in order to help you make your application more understandable. You should name a class with capital letter and the instance of the class with lower letter. In your case it would be better to define LoginPage as the class name and loginPage as the instance of that class. Although it's not mandatory but makes your application easy to read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.