0

i was automating the angular application login page using selenium web driver and i need some solution for validating

i have tried to identify the element and found but, while validating the fields for login, it by passes the if condition

here is my code

un = driver.findElement(By.xpath("//*[@id='mat-input-0']"));

un.sendKeys("");

Thread.sleep(500);

boolean une = un.getText().isEmpty();

pw = driver.findElement(By.xpath("//*[@id='mat-input-1']"));

pw.sendKeys("Password123");

Thread.sleep(1000);

boolean pwe = pw.getText().isEmpty();

//System.out.println("text--->"+un.getText());

//System.out.println(pw);

//WebElement element=pw;

//String u = un.getText();

//String p = un.getText();

//if(un!=null && pw!=null && (!un.getText().isEmpty()&&!pw.getText().isEmpty()))
//if(un!=null && pw!=null && (une!=true && pwe!=true))
//if((un!=null && un.getText()!=null &&  !un.getText().isEmpty()) && (pw!=null && pw.getText()!=null && !pw.getText().isEmpty()))

if(un!=null && pw!=null && (une=false) && (pwe=false))

{ 
    driver.findElement(By.xpath("//button[@class='eta-float mat-raised-button mat-primary']")).click();

    Thread.sleep(1000);

    //logout menu
    driver.findElement(By.xpath("/html/body/app-root/div/mat-toolbar/mat-toolbar-row/button[3]")).click();

    Thread.sleep(2000);

    //logout option
    driver.findElement(By.xpath("/html/body/div/div[2]/div/div/div/button[1]"));

    driver.quit();
}
else
{
    System.out.println("Please provide valid credentials!");
}

    everytime only else part is executed
4
  • i was checking the un and pwd field not null and it's not empty but everytime, else part is alone executed Commented Dec 12, 2018 at 11:10
  • pw.sendKeys(""); Commented Dec 12, 2018 at 11:20
  • how to check this condition Commented Dec 12, 2018 at 11:20
  • how to validate the fields and can you specify the conditions to check Commented Dec 12, 2018 at 11:25

2 Answers 2

1

There is a logical error in your program. Instead of = you have to use ==

if(un!=null && pw!=null && (une=false) && (pwe=false)) // assignment operator
if(un!=null && pw!=null && (une==false) && (pwe==false)) // comparison operator

so this may causing the condition to fail always and hence else part is executing always

for WebElement we can use like below if both elements are displayed and they contains text

if(un.isDisplayed()&&pw.isDisplayed()&&(!une) && (!pwe))
Sign up to request clarification or add additional context in comments.

5 Comments

@Parth - check new solution- but your existing code was wrong.
amit, how to valdiate empty string passed by sendkeys
pw.sendkeys() - what does this mean and what does this mean pw.sendkeys("")
pw.sendkeys("password2") means pw is a input html element and you are trying to type text "password2"in pw.
how to pass empty value through sendkeys or how to validate no data passed through sendkeys
0

There are a few issues with your code:

  1. un and pw will never be null. If .findElement() doesn't find the element, it will throw an ElementNotFoundException, not return null so you can skip those conditions.

  2. Don't validate whether either field is empty. There's really no point. Even if both fields are NOT empty and the username or password is incorrect, you will still have to deal with an unsuccessful login. Skip trying to validate empty fields and instead attempt the login and if it fails (you can tell this by looking for some login fail message), then fail the test.

  3. If you are looking for an element by ID, then use By.id() NOT By.xpath().

  4. Prefer CSS selectors over XPath because they are faster, better supported, and are easier to read.

  5. It's a bad practice to use Thread.sleep(). Do some googling to see the reasons why. Instead use WebDriverWait.

  6. Don't use XPaths with more than ~3 levels or that start at HTML. They are likely to break with any small change in the HTML.

  7. You should be using TestNG or JUnit to do your validations and logging. Don't write your own or just write messages to the console. It's not maintainable.

Your code should look something like

String username = "";
String password = "Password123";

driver.findElement(By.id("mat-input-0")).sendKeys(username);
driver.findElement(By.id("mat-input-1")).sendKeys(password);
driver.findElement(By.cssSelector("button.eta-float.mat-raised-button.mat-primary")).click(); // I'm assuming this is the login button?
// check for a successful login message
if (driver.findElements(loginSuccessMessageLocator).isEmpty())
{
    // login success message NOT found
    // fail the test
}
else
{
    // login success message found
    driver.findElement(By.xpath("/html/body/app-root/div/mat-toolbar/mat-toolbar-row/button[3]")).click();
    driver.findElement(By.xpath("/html/body/div/div[2]/div/div/div/button[1]")); // this doesn't do anything... is it supposed to click?
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.