0

There is a functionality, when i enter invalid login credentials, an error message appears on right side of page showing error message"Please enter valid username and password" but it appears only for few seconds (we call it toast message). I have to validate that error text message in selenium by java. Please suggest me what are the ways to achieve this. Thanks in advance

Pooja thakur
  • 1
  • 1
  • 1
  • 2
  • https://stackoverflow.com/questions/3500197/how-to-display-toast-in-android – SNS Mar 26 '18 at 04:47
  • 2
    Unfortunately your question boils down to *write code for me*, while many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input, expected output, and the output you actually get (console output, tracebacks, etc.). Check the [FAQ](https://stackoverflow.com/tour) and [How to Ask](https://stackoverflow.com/questions/how-to-ask). – Aniket Sahrawat Mar 26 '18 at 04:51
  • 1
    *Thanks in advance*, yeah you are welcome. – Aniket Sahrawat Mar 26 '18 at 04:52

4 Answers4

0

You need to open the page's source (F12) and find this "toast" element - it should be some <div> with either id or class which you can select with selenium. I dont know how this toast is implemented but I'm pretty sure it's not re-created every time message is shown - so you can rely on your selector.

Then you can just driver.findElement(By.id("toast_id")) or driver.findElement(By.className("toast-class-name")) in java and check it's text value

davidluckystar
  • 928
  • 5
  • 15
  • Please enter valid email and password

    this is the HTML code where either i dont have ID value but have class value and i tried with that it didnt work out
    – Pooja thakur Mar 26 '18 at 21:58
0

Instead of using text() try

elem.getAttribute("innerHTML");
Rajarajan
  • 21
  • 2
0

This should work:

    String loginStatus = new WebDriverWait(webDriver, 10).until(ExpectedConditions.elementToBeClickable(By.id("toast-message"))).getText();
    if (loginStatus.contains("enter valid")) {
        System.out.println("Login Failed: " + loginStatus);
    } else {
        System.out.println("Login Success:" + loginStatus);
    }
Chathuz
  • 1
  • 2
0

from selenium import webdriver

driver = webdriver.Firefox(executable_path ='C:\\....\gekodriver')
driver.get("http://www.example.org")
toast_text = driver.find_element_by_xpath("Copy the xpath of toast and paste here...")
assert toast_text =="Text which is shown in toast paste here"
print("Test case passed")
GHULAM NABI
  • 498
  • 5
  • 15