Antony Naveen Antony Naveen - 23 days ago 8
Java Question

I want to automate invalid and valid credential

I am doing selenium automation with page factory design pattern for a web app. Now I want to do automate valid valid, invalid invalid, valid invalid credential for a login page. how is it ?
My compete code is

package com.docmgr.Pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.WebElement;
public class LoginPage
{
WebDriver driver;
public LoginPage(WebDriver driver)
{
this.driver=driver;
}
@FindBy(how=How.NAME,using="username")
@CacheLookup
WebElement username;
@FindBy(how=How.NAME,using="password")
@CacheLookup
WebElement password;
@FindBy(how=How.CLASS_NAME,using="button")
@CacheLookup
WebElement button;
@FindBy(how=How.LINK_TEXT,using="Forgot Password")
@CacheLookup
WebElement fp;
public void login_Doc(String uid,String pas)
{
username.sendKeys(uid);
password.sendKeys(pas);
button.click();
}
}


package com.docmgr.TestCases;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;

import com.docmgr.Pages.LoginPage;

import Helper.BrowserFactory;

public class LoginTest
{
@Test
public void chechValidUser()
{
System.setProperty("firefox.webdriver.marionette","pathToGeckodriver");
WebDriver driver=BrowserFactory.startBrowser("firefox","54.68.159.876/docmgr");
LoginPage login=PageFactory.initElements(driver,LoginPage.class);
login.login_Doc("jgsdg","123");

}
}


package Helper;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BrowserFactory
{
static WebDriver driver;
public static WebDriver startBrowser(String browsName,String url)
{
if(browsName.equals("firefox"))
{
driver=new FirefoxDriver();
}

driver.manage().window().maximize();
driver.get(url);
return driver;
}




}

Answer

Pass an extra parameter for login_Doc method which denotes that the give user name and password is valid or not. Look at below example.

public void login_Doc(String uid,String pas,boolean isValidCredential)
{
    username.sendKeys(uid);
    password.sendKeys(pas);
    button.click();
    if(isValidCredential == true){
      // check if user is logged in successfully and click on logout button
   } else {
      //check appropriate error message is displayed
    }
}

and call the login.login_Doc method as,

  login.login_Doc("admin","admin",true); //valid credential
  login.login_Doc("admin","admin123",false); //invalid credential
Comments