gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:53:25 25 4
gpt4 key购买 nike

我正在检查网页上的文件夹层次结构,具体取决于用户类型。 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 代码,因为 child 的xpath会随着用户的变化而变化。
我可以使用“if else”语句来根据用户获取 xpath,但想知道是否还有其他方法可以解决这个问题?

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

提前致谢。

最佳答案

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

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)
}
}
}

我真的无法对其进行测试,因此它可能无法按原样工作,但这只是为了让您了解我将如何解决它。

关于java - 使用 selenium webdriver 测试文件夹层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31480755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com