【发布时间】:2017-07-28 15:32:44
【问题描述】:
目标:弹出警报。无论是否显示,我都希望它继续。如果显示,必须选中复选框,然后点击继续。如果没有,请忽略。
拦截器:如果警报显示,它将处理该操作并关闭对话框。但是如果不是,selenium 在未显示时会挂在那里而不处理条件。
背景:我之前用过 UFT,可能我的逻辑有问题。 弹出的是应用程序警报(不是系统),因此假设“切换到(),接受()/解雇()将不起作用。我将在登录后立即添加句柄警报,并在下面的登录方法中。
Selenium 框架背景。 : 我们使用 selenium maven 框架,serenity BDD。对象设置在页面的开头。和 serenity.properties 处理超时默认值等等。
弹出对象(如果出现):
@FindBy(xpath = "//input[@id='notification-ack']")
private WebElement PcoNoticeChbx; //this is a check box, needs to be checked
@FindBy(xpath = "//button[contains(.,'Continue')]")
private WebElement PcoNoticeContinueBtn;//button to close the alert
*登录方式*
public void loginIntoExtApplication(String baseURL, String loginURL, String uId, String pwd, String extAppEnv)throws Exception {
gotoExtLoginPage(baseURL, loginURL);
enterLoginCredential(uId, pwd);
openAt("https://" + extAppEnv + ".programportaltest.hrsa.gov/sdms-
extranet/index.xhtml");
我的方法:
//1.
if (PcoNoticeChbx!=null) {
PcoNoticeChbx.click();
PcoNoticeContinueBtn.click();
} else {
System.out.println("Dialog didn't display, no need any action");
}
//2.登录操作后挂在这里。
if(!getDriver().findElements(By.xpath("//*[@id='submit']")).isEmpty()){
PcoNoticeChbx.click();
PcoNoticeContinueBtn.click();
}
else {
System.out.println("Dialog didn't display, no need any action");
}
//3. 添加到手表不起作用,它显示待处理,下面的代码也失败了。我在 Junit 中以调试模式运行我的 Maven。它曾经工作正常。但手表元素总是显示(待处理)..
boolean isPresent = getDriver().findElements(By.id("noticebox")).size() >0
System.out.println("the diaolog exist= " + isPresent);
//4.甚至尝试了try-catch方法。
try{
PcoNoticeChbx.click();
PcoNoticeContinueBtn.click();
}catch (Exception e){
// Printing logs for my report
Log.error("Report Category button element is not found.");
// After doing my work, now i want to stop my test case
throw(e);
}
return;
}
//5.尝试列出 webelemets:
列表 temp = webdriver.findElements(org.openqa.selenium.By.id("noticebox"));
if (temp.Count > 0 && temp[0].Displayed) {
// script to execute if element is found
} else {
// continue the test
}
//6.及以下
if (!WebDriver.findElements(By.xpath("//*[@id='submit']")).isEmpty()==true);
{
//handle the dialog
}
else{
//continue
}
// 7.tried 使用布尔值,但它也停留在第一步
boolean Nbox = PcoNoticeChbx.isDisplayed(); {
if (Nbox==false)
{
System.out.println("Dialog didn't display, no need any action");
}
else if (Nbox==true) {
PcoNoticeChbx.click() ;
PcoNoticeContinueBtn.click();
}
【问题讨论】:
-
当使用try catch代码时,它停止为:元素不可点击。
-
public void checkAlert() { try { WebDriverWait wait = new WebDriverWait(getDriver(), 2); System.out.println("尝试步骤"); wait.until(ExpectedConditions.alertIsPresent()); org.openqa.selenium.Alert alert = getDriver().switchTo().alert();警报.accept(); // } catch (Exception e) { //异常处理 System.out.println("catch exception steps"); } } }
标签: java maven selenium serenity-bdd findby