1

I have following source-code:

main.container-fluid:nth-child(5)<main class="container-fluid ng-scope" ng-view=""><div class="row ng-scope">
<section class="col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1">
    <h3 ng-show="searchQuery" class="page-header page-header-sm ng-hide"><span translate="TITLE_SEARCH_RESULTS" class="ng-scope">Search Results</span> <span class="label label-default ng-binding" ng-bind-html="searchQuery"></span></h3>
    <h3 ng-show="!searchQuery" class="page-header page-header-sm ng-scope" translate="TITLE_ALL_PRODUCTS">All Products</h3>

    <div class="alert-info ng-hide" ng-show="confirmation">
        <p class="ng-binding"></p>
    </div>
</section>

(this is just a part of the source code here is the full Source code)

Now I want to verify if the source code contains All Products.

Notice: I got, that text content on multi places same page so its important I select this one (for XPath).

This is my code I wrote so far:

 IList<IWebElement> links = Driver.Instance.FindElements(By.XPath("//h3[@class='page-header page-header-sm ng-scope']"));
     
            IList<IWebElement> listOflinks = new List<IWebElement>();

            for (int i = 0; i < links.Count; i++)
            {
                links = Driver.Instance.FindElements(By.XPath("//h3[@class='page-header page-header-sm ng-scope']"));

                if (!string.IsNullOrEmpty(links[i].Text))
                    //links[i].Click();
                    Console.WriteLine(links[i].ToString());
            }

Then I return in the console:

Element (id = c25b9ecd-b828-4353-a9d8-9f8a53b9cee9)

but the goal is instead to write in the console All Products + verify its there.

maybe could be done with Assert?

Could someone help me to correct the code?

2 Answers 2

1

An assert is a function that verifies if something happens or has happened. If you are trying to use the script for exploratory purposes, you should extract the text of the element:

links[i].Text

Note: It can return an empty string, which may not be nice for exploratory testing, since your console will show nothing.

Maybe you should create a function to print this result propely for this case:

return (string.IsNullOrEmpty(links[i].Text)) ? "_NULL-Result" : links[i].Text;  
2
  • I try to use ur return (string.IsNullOrEmpty(links[i].Text)) ? "_NULL-Result" : links[i].Text; though It says cannot convert string to 'bool'.
    – XsiSec
    Jan 9, 2018 at 18:41
  • Does the function you created return a bool or a string? Notice the (string.IsNullOrEmpty(links[i].Text)) is just a conditional - the possible return values are strings ( "_NULL-Result" or links[i].Text). Since C# is strongly typed, returning a bool would cause this error. Jan 11, 2018 at 10:33
0

I solved it by use

try
            {
                driver.find_element(By.XPATH, '//button[text()="Some text"]')
                <br>console.WriteLine("Found All Products")


                return true;
            }
            catch (NoSuchElementException)
            {
                element = null;
                <br>console.WriteLine("Did not found Webelement")

                return false;
            }

Your Answer

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

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