0

Need to retrieve all elements information present in the UI screen. For more clarification i have attached the generic screen enter image description here

From above image i need to retrieve label-name with mandatory symbol followed by the respective Text-field detail also.

Here i have attached my sample snippet :

//<!--Initializing object-->
def driver = DF.getWebDriver()
//<---Reteriving the all labels  name present in the UI screen-->
List<String> alltag = driver.findElements(By.xpath("//*[string-length(normalize-space(text())) > 0]"));
System.out.println("Links count is: "+alltag.size());
int flag=0;
for(WebElement singleTag : alltag)
{
    System.out.println(singleTag.getText());
    String s= singleTag.getText();
    if (s=="*")
    {
        System.out.println ("Mandatory Symbol Occured");
        flag=1;
    }
    else
    {
        if(flag==1)
        {
            System.out.println("Here I will call common function for mandatorytextfieldvalidation " + singleTag.getText());
            flag=0;
        }
    }
}

enter image description here

  • 2
    What is the problem? You can just locate elements one by one and get all the data you need. – Alexey R. Apr 26 '18 at 12:15
  • I need to dynamically verify the mandatory symbol which is belongs for particular text field so that i can start necessary mandatory field validations for that particular input text field – Vel Guru Apr 26 '18 at 12:39
  • 2
    So have you tried anything so far? Could you share your trials? This might make your question more clear for us. – Alexey R. Apr 26 '18 at 12:55
  • I have attached the sample code, here i am trying to get textfield present after the mandatory symbol . – Vel Guru Apr 27 '18 at 9:46
  • 1
    Well, first of all the strings in Java should be compared in other way. You should use '"".equals(s)' instead of 's==""'. The second thing is that your question is still not clear? As far as I understand you need to have the list of WebElements which are labeled as "mandatory" (but not the labels themselves). If I am right you should provide the HTML code of the form as well since this is the question of the valid xPath rather than a valid Java code. – Alexey R. Apr 27 '18 at 9:59
0

Well, assume that you have the following structure (better do not put screenshots of your source, rather put the code snippet - this will make us easier to reproduce your issue).

<div>
    <div>
        <span>
            "Name"
            <sup class="mandatory">
                <font color="red">*</font>
            </sup>
        </span>
        <div>
            <input id="name"/>
        </div>
    </div>
    <div>
        <span>
            "Email"
            <sup class="mandatory">
                <font color="red">*</font>
            </sup>
        </span>
        <div>
            <input id="email"/>
        </div>
    </div>
    <div>
        <span>
            "Age"
            <sup class="mandatory">
                <font color="red">*</font>
            </sup>
        </span>
        <div>
            <input id="age"/>
        </div>
    </div>
</div>

You should use the approach as it is shown below:

List<WebElement> requiredCompositeElements = driver
        .findElements(By.xpath("//div[span/sup[@class='mandatory']]")); // Get divs where we have mandatory elements

for(WebElement requiredCompositeElement: requiredCompositeElements){
    // Here we iterate by each such div
    String label = requiredCompositeElement.findElement(By.xpath("./span")).getText();
    WebElement input = requiredCompositeElement.findElement(By.xpath("./div/input"));
    // Here you do what you need with your elements
}
  • First you define all the elements which contain tags with mandatory class
  • Then you iterate through those elements (each of which is composed by both label and the input) and break them down by span and input.
  • Now you have span and can get the text that will return you both the label and the asterix
  • You also have the input and can process it according to your logic.
| improve this answer | |
0

I would recommend specifying exactly which fields you expect to be mandatory and then check each individually. In this way your test will have a clear expected outcome.

Let's assume that there is a functional requirement that the username field must be mandatory, than your current test is not properly covering that. There could be some bug which causes the 'fontmandatory' class to be missing from the element. Your current test would then not recognize it as mandatory field and thus not perform the specific mandatory field tests and simply pass.

Also, if for some reason somebody chooses to use a new and possibly incorrect implementation for field validation and marks affected elements with a different class for that, your test would miss it.

It would thus be better to write a specific test that runs the mandatory field validation test code explicitly on the username field (and then of course for any other field that is required to be mandatory).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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