使用selenium webdriver测试文件夹层次结构

时间:2015-07-17 16:43:51

标签: java selenium xpath selenium-webdriver cucumber

我正在检查网页上的文件夹层次结构,具体取决于用户的类型。 User1具有一组权限,使他能够看到如下文件夹结构:

Main Folder
    - First Child
        -First Grandchild
        -Second Grandchild
    - Second Child
    - Third Child

Html代码:

<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拥有一组不同的权限,这使他能够看到如下文件夹结构:

Main Folder
    - First Child
        -First Grandchild
        -Second Grandchild

Html代码:

<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 */

我正在使用Java Cucumber测试来运行场景。

Given I am user'User1'
When I log in
Then I should see "Main Folder"
And I should see "First Child"
And I should see "First Grandchild"
And I should see "Second Grandchild"
And I should see "Second Child"
And I should see "Third Child"


Given I am user 'User2'
When I log in
Then I should see "Main Folder"
And I should see "First Child"
And I should see "First Grandchild"
And I should see "Second Grandchild"
And I should NOT see "Second Child"
And I should NOT see "Third Child"

1)许多步骤都会重叠,但我不能使用相同的selenium代码,因为子代的xpath会随着用户的变化而变化。 我可以使用&#34; if else&#34;声明根据用户获取xpath,但是想知道是否还有其他方法可以解决这个问题?

2)如何检查文件夹是否存在?我可以检查我的pagesource中的文本并断言它不存在,但是想知道是否有更好的方法。

提前致谢。

1 个答案:

答案 0 :(得分:0)

所以你可以创建一个包装来处理这个结构,我从头顶写了一些东西,没有测试它:

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 only first level, if not then look into appropriate xpath 
        return element.findElements(By.xpath("/div"));
    }

    public String getElementText(WebElement element) {
        return element.findElement(By.tagName("span")).getText();
    }
}

然后您可以使用以下内容从代码中使用它:

public void yourMethod(){
    // get your main element:
    WebElement main = driver.findElement(By.id("1"))
    FolderHelper helper;
    try{
        helper = new FolderHelper(main);
    }catch(TimeOutException e){ // or whatever you want to use to determine if main element exists:
        // all hope abandon, main was not found, you probably want to stop here 
    }

    // get first level decendants:
    List<WebElement> firstLevelDecendants = helper.getDecendants();
    // assert here, a la:
    Assert.AreEqual(firstLevelDecendants.size(), 3)

    // do with children your assertions:
    for(WebElement e : firstLevelDecendants){
        if(helper.getElementText(e).equals("First Child ")){
            // ok, this one should have decendants:
            List<WebElement> grandChildren = helper.getDecendants(e);
            Assert.AreEqual(grandChildren.size(), 2)
        }
    }
}

我真的无法测试它,所以它可能无法正常工作,但它只是让你知道我将如何解决它。