我需要对正面和负面的情况进行测试。 场景:通过搜索组名(并选择复选框)删除一个组。 在搜索是否找不到组时,我们得到一个元素(“ noGroupFoundText”)
如果没有“ noGroupFoundText”元素(即如果不存在特定的组),那么我编写的代码可以正常工作。但是,如果搜索后存在组,则代码不起作用。给出一个异常,说“ noGroupFoundText”不可见。
我应该如何处理这两种情况
// Method that will delete the existing group
public String deleteGroup(String groupName) throws Exception {
Dashboard dashboardInstance = PageFactory.initElements(driver, Dashboard.class);
try {
dashboardInstance.navigatePeopleGroups();
searchGroups.sendKeys(groupName);
Thread.sleep(2000);
try {
if (noGroupFoundText.isDisplayed())
throw new Exception("No groups found ! Aborting deleting groups");
} catch (Exception a) {
dashboardInstance.signout();
FrameworkBase.logger.info(a.getMessage());
return "Fail";
}
selectedGroupCheckBox.click();
deleteGroupButton.click();
confirmDeleteButton.click();
Thread.sleep(2000);
if (deletePopup.getText().equals("Successfully deleted group")) {
FrameworkBase.logger.info("Successfully deleted group");
return "Pass";
}
throw new Exception("Cannot delete group !");
} catch (Exception e) {
dashboardInstance.signout();
FrameworkBase.logger.info(e.getMessage());
return "Fail";
}
答案 0 :(得分:0)
在页面类中将noGroupFoundText
更改为List<WebElement> noGroupFoundText
并使用以下代码:
public String deleteGroup(String groupName){
Dashboard dashboardInstance = PageFactory.initElements(driver, Dashboard.class);
dashboardInstance.navigatePeopleGroups();
searchGroups.sendKeys(groupName);
Thread.sleep(2000);
// condition to check whether nogrouptext present if not then delete the group
if(noGroupFoundText.isEmpty()){
System.out.println("Group is there...")
selectedGroupCheckBox.click();
deleteGroupButton.click();
confirmDeleteButton.click();
Thread.sleep(2000);
if (deletePopup.getText().equals("Successfully deleted group")) {
FrameworkBase.logger.info("Successfully deleted group");
dashboardInstance.signout();
return "Pass";
}else{
dashboardInstance.signout();
return "Fail"
}
}else{
System.out.println("Group is not there..")
dashboardInstance.signout();
return "Fail";
}
}