Lees2460
Lees2460

Reputation: 11

Convert webelement to string Selenium

Hi I am currently trying to run selenium testing for a website, however I am unable to convert the webelement to string. The ErrorMessage 1 = validation which appears, therefore I want ErrorMessage1 = ErrorMessage so I know the test is successful.

public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\lees2\\Downloads\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://psage.public.cs.qub.ac.uk/ForgotPassword.aspx");
    driver.manage().window().maximize();
    driver.findElement(By.id("MainContent_Email")).sendKeys("[email protected]");
    driver.findElement(By.name("ctl00$MainContent$BtnSend")).click();
    driver.findElement(By.className("validation-summary-errors"));
    WebElement ErrorMessage1 = driver.findElement(By.className("validation-summary-errors"));
    String ErrorMessage = "\r\n" + "                    Unrecognised email address\r\n" + "                ";
    driver.close();
    if (((String) ErrorMessage1).equalsIgnoreCase(ErrorMessage)) {
        System.out.println("Test successful");
    } else {
        System.out.println("Test failure");
    }

}

Upvotes: 0

Views: 4404

Answers (4)

Abhishek Dhoundiyal
Abhishek Dhoundiyal

Reputation: 1417

It is not wise to convert WebElement to String, as it is not going to yield a meaningful result.

Also you cannot compare WebElement with the string value.

To get a string value, you need to use getText() after your implementation.

getText( ) is a Selenium WebDrivers predefined method which is used for retrieving the specified elements Text.

Return type - String.

***************** Modified Code *****************************

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\lees2\\Downloads\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://psage.public.cs.qub.ac.uk/ForgotPassword.aspx");
    driver.manage().window().maximize();
    driver.findElement(By.id("MainContent_Email")).sendKeys("[email protected]");
    driver.findElement(By.name("ctl00$MainContent$BtnSend")).click();
    driver.findElement(By.className("validation-summary-errors"));
    String ErrorMessage1 = driver.findElement(By.className("validation-summary-errors")).getText();
    String ErrorMessage = "\r\n" + "                    Unrecognised email address\r\n" + "                ";
    driver.close();
    if (((String) ErrorMessage1).equalsIgnoreCase(ErrorMessage)) {
        System.out.println("Test successful");
    } else {
        System.out.println("Test failure");
    }

}

Also instead of using this:

 if (((String) ErrorMessage1).equalsIgnoreCase(ErrorMessage)) {
        System.out.println("Test successful");
    } else {
        System.out.println("Test failure");
    }

Use assert:

import org.testng.Assert;
Assert.assertEquals(ErrorMessage1, ErrorMessage);

Upvotes: 0

Rmahajan
Rmahajan

Reputation: 1361

try ErrorMessage1.getText() to compare the values.

Updated:

WebElement cannot be used to compare string values. It exposes getText() method which can be used to compare string values.

Upvotes: 1

Sameer Arora
Sameer Arora

Reputation: 4507

The error message that the website is displaying on entering an invalid email id is displayed as a text in the html structure. So you can simply use getText() method to fetch the error message from the element.

For your element, you can get the text like:

WebElement ErrorMessage1 = driver.findElement(By.className("validation-summary-errors"));   
String invalidEmailErrorMessage = ErrorMessage1.getText();

And then you can directly match this invalidEmailErrorMessage string with the message you want to validate with like:

if (invalidEmailErrorMessage.equalsIgnoreCase(ErrorMessage))

Upvotes: 0

KunduK
KunduK

Reputation: 33384

Yes you can't compare an WebElement with string value.You have to get text value of this webelement which is string and then compare with your expected string value.

if (ErrorMessage1.getText().equalsIgnoreCase(ErrorMessage)) {
            System.out.println("Test successful");
        } else {
            System.out.println("Test failure");
        }

driver.close(); should put at the end.Hope this helps.

Upvotes: 1

Related Questions