java - Testing folder hierarchy using selenium webdriver -


i checking folder hierarchy on webpage, depending on type of user. user1 has set of permissions enable him see folder structure :

main folder     - first child         -first grandchild         -second grandchild     - second child     - third child 

html code :

<div id = 0>     <div id = 1>     <table id = 1>     <tbody>     <td id="content1"      <a id="label1"      <span id="treeview_treenode1_name"           main folder     </span></a></td></tbody></table>              <div id = 2>             <table id = 2>             <tbody>             <td id="content2"              <a id="label2"              <span id="treeview_treenode2_name"                   first child              </span></a></td></tbody></table>                      <div id = 5>                     <table id = 5>                     <tbody>                     <td id="content5"                      <a id="label5"                      <span id="treeview_treenode5_name"                           first grandchild                      </span></a></td></tbody></table>                     </div>                      <div id = 6>                     <table id = 6>                     <tbody>                     <td id="content6"                      <a id="label6"                      <span id="treeview_treenode6_name"                           second grandchild                      </span></a></td></tbody></table>                     </div>             </div>               <div id = 3>             <table id = 3>             <tbody>             <td id="content3"              <a id="label3"              <span id="treeview_treenode3_name"                   second child              </span></a></td></tbody></table>             </div>               <div id = 4>             <table id = 4>             <tbody>             <td id="content4"              <a id="label4"              <span id="treeview_treenode4_name"                   third child              </span></a></td></tbody></table>             </div>      </div> /*end of division 1 */ </div> /* end of division 0 */ 

user2 has different set of permissions, enable him see folder structure :

main folder     - first child         -first grandchild         -second grandchild 

html code :

<div id = 0>     <div id = 1>     <table id = 1>     <tbody>     <td id="content1"      <a id="label1"      <span id="treeview_treenode1_name"           main folder     </span></a></td></tbody></table>              <div id = 2>             <table id = 2>             <tbody>             <td id="content2"              <a id="label2"              <span id="treeview_treenode2_name"                   first child              </span></a></td></tbody></table>                      <div id = 3>                     <table id = 3>                     <tbody>                     <td id="content3"                      <a id="label3"                      <span id="treeview_treenode3_name"                           first grandchild                      </span></a></td></tbody></table>                     </div>                      <div id = 4>                     <table id = 4>                     <tbody>                     <td id="content4"                      <a id="label4"                      <span id="treeview_treenode4_name"                           second grandchild                      </span></a></td></tbody></table>                     </div>             </div>     </div> /*end of division 1 */ </div> /*end of division 1 */ 

i using java cucumber tests run scenarios.

given user'user1' when log in should see "main folder" , should see "first child" , should see "first grandchild" , should see "second grandchild" , should see "second child" , should see "third child"   given user 'user2' when log in should see "main folder" , should see "first child" , should see "first grandchild" , should see "second grandchild" , should not see "second child" , should not see "third child" 

1) many of steps overlap, cannot use same selenium code xpath of children changes user changes. use "if else" statement take xpath based on user, wondering if there way go ?

2) how can check see folder not present ? check text in pagesource , assert not present, wondering if there better way.

thanks in advance.

so create wrapper handle structure, wrote top of head, did not test it:

public class folderhelper {      final webelement folder;      public folderhelper(webelement element) {         folder = element;         isloaded()     }      public void isloaded() {         new webdriverwait(getdriverfromsomeplace(), 30)                 .ignoring(nosuchelementexception.class)                 .ignoring(staleelementreferenceexception.class)                 .until(expectedconditions.visibilityof(folder));     }      public list<webelement> getdecendants(){         return getdecendants(folder);     }      public list<webelement> getdecendants(webelement element){         // afaik "/div" should return first level, if not appropriate xpath          return element.findelements(by.xpath("/div"));     }      public string getelementtext(webelement element) {         return element.findelement(by.tagname("span")).gettext();     } } 

and can use code using like:

public void yourmethod(){     // main element:     webelement main = driver.findelement(by.id("1"))     folderhelper helper;     try{         helper = new folderhelper(main);     }catch(timeoutexception e){ // or whatever want use determine if main element exists:         // hope abandon, main not found, want stop here      }      // first level decendants:     list<webelement> firstleveldecendants = helper.getdecendants();     // assert here, la:     assert.areequal(firstleveldecendants.size(), 3)      // children assertions:     for(webelement e : firstleveldecendants){         if(helper.getelementtext(e).equals("first child ")){             // ok, 1 should have decendants:             list<webelement> grandchildren = helper.getdecendants(e);             assert.areequal(grandchildren.size(), 2)         }     } } 

i unable test may not work as-is, it's give idea how solve it.


Comments