Beardy
Beardy

Reputation: 172

How to get the value/string out of a ql-editor with selenium

I want to get the string out of the ql-editor.

This code below, works for other text fields like email/password text field and other too. But in the ql-editor it wont work. The variable checkText receives a Null.

    public static void SendKeysElement(IWebDriver webDriver, string statusMessage)
    {

        IWebElement Field = webDriver.FindElement(By.XPath("//div[@class='ql-editor']//p"));
        Field.SendKeys("Do this and this");

        Thread.Sleep(500);
        string checkText = Field.GetAttribute("value");

        if (maxTries < 0)
        {
            throw new Exception("String not found");
        }
        else if (checkText != "Do this and this")
        {
            Thread.Sleep(1000);
            maxTries--;
            SendKeysElement(webDriver, statusMessage);  
        }
        Console.WriteLine(statusMessage);
    }

And here is the inspect: enter image description here

Upvotes: 0

Views: 121

Answers (2)

JeffC
JeffC

Reputation: 25611

You are trying to retrieve the value of a P tag. value is used with INPUTs, etc. that do not actually contain the text inside the tag. In this case the tag is a P so use

string checkText = Field.Text;

Having said that, you are doing too much in your method. The name is SendKeysElement() but you've hard coded not only the locator but the text sent along with validations. You should break each major action out into a separate piece of code. Make SendKeysElement() generic enough that it can be used to put text into any element and add a wait.

public static void SendKeysElement(IWebDriver webDriver, By locator, string text)
{
    new WebDriverWait(webDriver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(locator)).SendKeys(text);
}

Create another method to get text from an element (and add a wait) and make it generic enough that it's reusable.

public static string GetText(IWebDriver webDriver, By locator)
{
    return new WebDriverWait(webDriver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(locator)).Text;
}

Now in your test, it should look like

string text = "Do this and this";
By qlEditorLocator = By.XPath("//div[@class='ql-editor']//p");
SendKeysElement(webDriver, qlEditorLocator, text); ;
Assert.AreEqual(text, GetText(webDriver, qlEditorLocator), "Verify ql Editor string");

Upvotes: 0

Villa_7
Villa_7

Reputation: 547

Your p element (which contains entered text) doesn't have value attribute, so you need to do like this:

string checkText = Field.Text;

Upvotes: 1

Related Questions