1

I am writing an automation test script for our web application using Robot framework and Selenium2Library. One of my test case involves the following scenario.

I have to run a particular test case only if web page contains more than one div tag with a similar id !

Is there any specific keyword in robot framework to set the value of a variable as a count of html tags having same id's ?

Here is a similar html code am dealing with :

<div id="dashboard">
    <span>........</span>
</div>
<div id="dashboard">
    <span>........</span>
</div>

I have to store the total number of div tag's with id=dashboard in a variable ${Count} .

Along with that , can we make use of Comparison/Relational operators directly in Robotframework test scipt ? i.e , can I write a test case similar to this :

Run Keyword If  ${Count} > 1  Found More Elements

Can you please help me to resolve this issue ?

2

Selenium2Library has a keyword named Get Matching XPath Count which can return the number of items that match an xpath. The documentation even includes an example of counting items based on an id.

It would look something like this:

${Count}=    Get matching xpath count    //div[@id='dashboard']

Along with that , can we make use of Comparison/Relational operators directly in Robotframework test scipt ?

Yes you can, exactly like you showed in your question (assuming Found More Elements is a keyword):

Run Keyword If  ${Count} > 1  Found More Elements
0

I see that you are using Selenium. You can use that to get a list of web elements with same attribute and then use it in your check.

List<WebElement> allelementwithsameid = driver.findElements(By.id("dashboard"));

Here you're using the findElements( ) method to get all the elements, which have the id "dashboard".

Next, check if the length of the list is greater than zero.

if((allelemtwithsameid.size()> 0) {
  System.out.println("Size greater than zero. can use the element");
  count=allelementwithsameid;
 }

 else {
  //use some exception handling code here.
 }

Now, I don't have much knowledge of Robot plus Selenium, but I think you can use the "count" variable in your Robot framework's code.

My Java coding skills are not the best. But I think you would get the idea.

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.