1

I'm working on Selenium WebDriver using Java, where I need to check a scenario when entered invalid username and password, the application should throw warning message. The warning message will not display if correct credentials are entered. I have written below a piece of code to verify warning message on wrong login credentials. But, when I enter valid details, my code don't skip to the else block, but instead fails saying that it can't identify element ("//*[@class='small-9 small-pull-1 column content']").

Code :

try {           
             if (new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "No Password Found for"))){

            String text = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");
            System.out.println(text);

            if(text.contentEquals("No Password Found for")){
                    driver.navigate().refresh();

                Assert.fail("Unable to login to application");
            }
            }

             else if(new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "Your Online/Mobile Banking User ID has been blocked. Please go to “Forgot Password” option to unblock it."))){

               String retext = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");

            System.out.println(retext);

            if(retext.contentEquals("Your Online/Mobile Banking User ID has been blocked. Please go to “Forgot Password” option to unblock it.")) {
                driver.navigate().refresh();

                Assert.fail("Unable to login to application");
            }


        }       

             else if (new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "Time Out"))){
            String timeouttext = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");

            if(timeouttext.contentEquals("You have specified an invalid User Name or Password. Please check and try again")){
                driver.navigate().refresh();

                                    Assert.fail("Unable to login to application");      }


        }

HTML Block with failed message:

<div class="column">
    <div class="form-field ">
        <div class="popup-login-error visible -full error" id="j_idt65">
            <div class="row"><span class="icon-cancel-thin color-white" messagepopupclose=""></span>
                <div class="small-2 column icon"><i class="icon-exclamation-circle"></i></div>
                <div class="small-9 small-pull-1 column content">You have specified an invalid User Name or Password. Please check and try again</div>
                <div class="small-9 small-pull-1 column content">No Password Found for </div>
            </div>
        </div>
    </div>
</div>
Aditya
  • 457
  • 2
  • 8
  • 27

2 Answers2

1

I have optimized your code block as follows :

  • Bring out driver.findElement(By.name("submitted")).click(); out of try{} block in any case you have to click() on it.
  • Induce WebDriverWait for the text invalid User Name or Password to be present in the HTML DOM
  • Kept the xpath for error text invalid User Name or Password unchanged.
  • Replaced getText() with getAttribute("innerHTML")
  • Kept your contentEquals() and Assert.fail() logic untouched.
  • Removed driver.navigate().refresh(); as it may invite StaleElementReferenceException
  • Incase your try{} Fails for the error message your program will be printing Dashboard is displayed
  • Here is your working code block :

    //code block
    driver.findElement(By.name("submitted")).click();
    try {
        new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//*[@class='small-9 small-pull-1 column content']"), "invalid User Name or Password"));
        String text = driver.findElement(By.xpath("//*[@class='small-9 small-pull-1 column content']")).getAttribute("innerHTML");
        System.out.println(text);
        if (text.contentEquals("You have specified an invalid User Name or Password. Please check and try again") || text.contentEquals("Time Out") || text.contentEquals("Your Online/Mobile Banking User ID has been blocked. Please go to “Forgot Password” option to unblock it.")) 
        {
            Assert.fail("Unable to login to application");
        }
    } catch (Exception e) {
        System.out.println("DAashboard is displayed");
        // rest of code block
    }
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I tried the above code that you modified. For Successful login, the test case failed saying time out after 10 secs waiting for text "invalid User Name or Password') to be present in element found by By.xpath: //*[@class='small-9 small-pull-1 column content']". This is because user will be logged in and will not see this message – Aditya Jan 23 '18 at 09:40
  • My Intention in this test, is if the user logs in successful should continue to execute scripts and if the user fails with invalid username or password will see the message in Login screen – Aditya Jan 23 '18 at 09:41
  • That was my bad. Corrected the code. Let me know the status. – undetected Selenium Jan 23 '18 at 10:09
  • will try it and post you the status – Aditya Jan 23 '18 at 10:29
  • I have modified code . Here, I'm not able to exit if block and focus not shifting to else if block if the error message don't match . Kindly assist – Aditya Jan 24 '18 at 07:17
  • Can you raise a new question with your new requirement please? We will be glad to help you out. – undetected Selenium Jan 24 '18 at 07:18
0

Note that there are two elements that satisfy the properties you were looking for:

By.xpath("//*[@class='small-9 small-pull-1 column content']")

// The two elements are:
<div class="small-9 small-pull-1 column content">You have specified an invalid User Name or Password. Please check and try again</div>
<div class="small-9 small-pull-1 column content">No Password Found for </div>

You can either handle a list of them, using findElements:

List<WebElement> list = driver.findElements(By.xpath("//*[@class='small-9 small-pull-1 column content']"))

Or just call each of them specifically (mind xpath's 1-based indexing):

By.xpath("(//*[@class='small-9 small-pull-1 column content'])[1]")
By.xpath("(//*[@class='small-9 small-pull-1 column content'])[2]")


As a side note: it's considered a bad habit to catch(Exception e), as it makes it impossible for you to determine what's wrong with your code. Try to specify the exact type of exceptions that might get thrown, or at least print the StackTrace (using e.printStackTrace() inside the catch) for further debugging.

GalAbra
  • 5,048
  • 4
  • 23
  • 42