-1

I have a method below where I retrieve a HTML tag:

    public void CheckEmailDisplayed()
    {
        var email = _driver.FindElement(ConfirmationResponsiveElements.ViewEmail);

    }

ViewEmail is below:

public static By ViewEmail => By.ClassName("confirmation-banner__text");

The HTML it corresponds to is:

<div class="confirmation-banner__text firefinder-match">
<p>
 We've sent an email to
<strong>[email protected]</strong>
</p>
<p>
</div>

What I want to do is be able to use the variable email to check that the text contains an @. This is to help determine an email address is displayed. How can this be achieved?

Thanks

0

3 Answers 3

1

Option 1: Check for the @ symbol

        string email = "[email protected]";
        if (email.Contains("@"))
        {
            // code here
        }

Option 2: Validate Email Address

public static bool IsEmail(string emailToValidate)
{
    if (string.IsNullOrEmpty(emailToValidate)) return true;

    return Regex.IsMatch(emailToValidate, @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}

How to use option 2:

        string email = "[email protected]";            
        if (IsEmail(email))
        {
            // valid email address
        }
        else
        {
            // not a valid email address
        }
Sign up to request clarification or add additional context in comments.

Comments

0

You can add another definition

public static By ViewEmailAddress => By.TagName("strong");

Then use

var emailAddress = emai.FindElement(ConfirmationResponsiveElements.ViewEmailAddress);
var emailAddressText = emailAddress.Text;

And then you can do different operations that you want on emailAddressText. Like validating it having @ or doing more complex validations like a email pattern check

Comments

0

You can use IndexOf method

bool found = Value1.IndexOf("abc", 0, 7) != -1;

OR

You can also use regular expressions (less readable though)

string regex = "^.{0,7}abc";

System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex);
string Value1 = "sssddabcgghh";

Console.WriteLine(reg.Match(Value1).Success);

Source :- How to determine if string contains specific substring within the first X characters

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.