0

enter image description here

I have a scenario, where I want to Assert the UI is displaying 'Active'.

When debugging the assertion, I am trying different methods.

This is what I started with:

Assert.IsTrue(webdriver.Text.Contains("Active")) 

but it is throwing an exception;

Assert.IsTrue failed

Any recommendations for properly verifying, that the UI displays Active?

Picture attached for reference.

2
  • what is webdriver.text ?
    – PDHide
    Dec 30, 2021 at 5:00
  • 1
    Complementing PDHide comment, probably you would want to inspect with your debugger the content of webdriver.text object. You may not have a reference to the span which contains "Active" Dec 30, 2021 at 8:10

3 Answers 3

0

Your best option is to find the element containing the text - webdriver refers to the selenium driver object that you use to find your elements.

The code could be something like this (accuracy is not guaranteed since I'm coding on the fly):

// Find all the span elements
List<IWebElement> spans = webdriver.FindElements(By.Tag("span"));
// use Linq to query the list for the span you want
IWebElement span = spans.Where(x => x.Text.Contains("Active"));
// And now assert that span is not null - that is, we found an element with text = "Active"
Assert.IsTrue(span != null);

There are other ways to do this, including finding by class, finding a unique parent and using XPath to navigate the DOM to your target element, and so on.

I suggest you experiment a bit and possibly look up the WebDriver API for more information.

4
  • Thank you Kate, I am still learning so I will play around with this. This is the code I currently have but the assert is throwing an exception; var wait = new WebDriverWait(Common.Browser, new TimeSpan(0, 0, 20000)); var Account_Information_Status = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.XPath("//span[contains(@class, 'info__text')]"))); Assert.IsTrue(Account_Information_Status.Text.Contains("Active"));
    – MJB27
    Dec 30, 2021 at 15:32
  • How many elements on the page have the class info_text? If there are more than one, there's no guarantee you'll get the element you want.
    – Kate Paulk
    Dec 30, 2021 at 15:48
  • I scanned the page and it only contains the one class "info__text" --thankfully
    – MJB27
    Dec 30, 2021 at 15:53
  • I screwed up; it does have multiple instances of "info__text" (user search error). I did make an edit to the assert and it is now working; var wait = new WebDriverWait(Common.Browser, new TimeSpan(0, 0, 20000)); var Account_Information_Status = Common.Browser.FindElement(By.CssSelector(".user-profile__data--status > .user-profile__info > .info__group:first-child > .info__text")); Assert.AreEqual(Account_Information_Status.Text, "Active");
    – MJB27
    Dec 30, 2021 at 16:26
0

You could use a one-liner that asserts a span with the text "Active" is present.

Assert.NotNull(driver.findElement(By.XPath("//span[text()='Active']")));

0

Shared screenshot is a common representation of objects containing structure & content of UI application. While working in a security testing services company we have automated this in multiple ways and one of the ways is using Sibling concept of Selenium WebDriver.

In this feature if a Parent object is known then web element can be easily found in your DOM (Data Object Model) or if unique sibling element is available, then you can navigate back to parent node and can find required element easily. For example - in this case, 'STATUS' looks a unique object in your entire web page and from DOM, 'Active' is sibling of 'Status', so you can try the following code -

Assert.assertTrue(driver.findElement(By.xpath("//span[text() = 'Status']//..//span[text() = 'Active']")).isDisplayed());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.