0

I have added Assertion(Assert.asserttrue(true,"login failed")) in my every tc and conditions are according to that but when login tc failed I am not getting any output on console provided in assertion, that this tc is failed due to this this reason.

Login page:

public class LoginPage {
     private WebDriver driver;

    public LoginPage(WebDriver driver) throws Exception {
        PageFactory.initElements(driver, this);
        this.driver=driver;
    }

    @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;

    public WebElement getUserNameTextField() {
        return userNameTextField;
    }

    public void setUserNameTextField(WebElement userNameTextField) {
        this.userNameTextField = userNameTextField;
    }

    public WebElement getPasswordTextField() {
        return passwordTextField;
    }

    public void setPasswordTextField(WebElement passwordTextField) {
        this.passwordTextField = passwordTextField;
    }

    public WebElement getSignInButton() {
        return signInButton;
    }


}

Login Business:

public class LoginBusiness  {

    private WebDriver driver;
    LoginPage loginPage;
    public LoginBusiness(WebDriver driver) throws Exception {
        this.driver = driver;
        loginPage = new LoginPage(driver);

    }
public void loginToApplication() throws Exception {
        loginPage.getUserNameTextField().clear();
        Properties properties = Utility.getproperties(Constant.CONFIGURATION_PROPERTIES_FILE_PATH);
        String username = properties.getProperty(Constant.USERNAME);
        loginPage.getUserNameTextField().sendKeys(username);
        String password = properties.getProperty(Constant.PASSWORD);
        loginPage.getPasswordTextField().clear();
    //  ScreenshotUtility.captureScreenShot(driver);
        loginPage.getPasswordTextField().sendKeys(password);

        loginPage.getSignInButton().click();

    }

}

Login test:

public class LoginTest{

    WebDriver driver;

     @BeforeClass
     public void launchApplication() throws Exception
       {
          driver=Driver.getDriver();
        }

     @Test
     public void loginToGmsrApplication() throws Exception {
     LoginBusiness loginBusiness= new LoginBusiness(driver);
      loginBusiness.loginToApplication();
      Assert.assertTrue(true, "login unsuccessful.");

     }   

}
    -

##

1

AssertTrue(true, "message") is always going to be successful as you're asking it to assert that true is true!

You need the first argument of assertTrue to be something that will fail if the login was not successful, e.g. the presence of an element located on the page that appears after a successful login, but not after a failed one.

E.g.

loginBusiness.loginToApplication();
Assert.assertTrue(homePage.hasLoaded(), "login unsuccessful");

with the hasLoaded method being something like

public boolean hasLoaded() {

  try {
    driver.findElement(By.id("homePageElement");
  } catch(NoSuchElementException | TimeoutException e) {
    return false;
  }

  return true;

}
  • Its not working for me – jay Jun 7 at 10:59
  • In what way isn't it working? Are you getting an error message? – anonygoose Jun 7 at 11:07
  • no error msg is coming – jay Jun 7 at 11:16
  • it just shows 15 test cases failure on console – jay Jun 7 at 11:17
  • =============================================== Test Suite Total tests run: 15, Failures: 15, Skips: 0 =============================================== – jay Jun 7 at 11:22
0

The way we test the login scenarios is after login we check anything from the next page. By anything i mean we can check :URL of the next page, Title of the next Page or an of the WebElement which is expected to appear in the second page.

So once u call the login function, check if any of these thing's are present or not. I have modified your code a bit and check if it helps you:

@Test
     public void loginToGmsrApplication() throws Exception {
     LoginBusiness loginBusiness= new LoginBusiness(driver);
     loginBusiness.loginToApplication();
     String nextPageTitle = driver.getTitle(); 
or
     String nextPageURL = driver.getCurrentURL();
     int assertValue = 0; 
/*
Now match expected url with actual URL
*/
     if(nextPageURL.equals("Expected URL"))
       {
         assertValue++;
       }
     else {
         Assert.assertFalse("Since the URL is not what is expected, hence marking the Test Case as FAILED", assertValue==0);
       }

      Assert.assertTrue(true, "login unsuccessful.");
}

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.